mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 20:45:14 +00:00
CNotifier: Turn into a CObject and Use the event queue to deliver events
This way, CNotifier can mutate state to its little heart's content without destroying the world when the global CNotifier hash changes during delivery.
This commit is contained in:
parent
a714fc661d
commit
d8387f1506
Notes:
sideshowbarker
2024-07-19 13:14:15 +09:00
Author: https://github.com/rburchell Commit: https://github.com/SerenityOS/serenity/commit/d8387f1506c Pull-request: https://github.com/SerenityOS/serenity/pull/321 Reviewed-by: https://github.com/awesomekling ✅
4 changed files with 50 additions and 3 deletions
|
@ -13,6 +13,8 @@ public:
|
|||
Invalid = 0,
|
||||
Quit,
|
||||
Timer,
|
||||
NotifierRead,
|
||||
NotifierWrite,
|
||||
DeferredDestroy,
|
||||
DeferredInvoke,
|
||||
ChildAdded,
|
||||
|
@ -62,6 +64,36 @@ private:
|
|||
int m_timer_id;
|
||||
};
|
||||
|
||||
class CNotifierReadEvent final : public CEvent {
|
||||
public:
|
||||
explicit CNotifierReadEvent(int fd)
|
||||
: CEvent(CEvent::NotifierRead)
|
||||
, m_fd(fd)
|
||||
{
|
||||
}
|
||||
~CNotifierReadEvent() {}
|
||||
|
||||
int fd() const { return m_fd; }
|
||||
|
||||
private:
|
||||
int m_fd;
|
||||
};
|
||||
|
||||
class CNotifierWriteEvent final : public CEvent {
|
||||
public:
|
||||
explicit CNotifierWriteEvent(int fd)
|
||||
: CEvent(CEvent::NotifierWrite)
|
||||
, m_fd(fd)
|
||||
{
|
||||
}
|
||||
~CNotifierWriteEvent() {}
|
||||
|
||||
int fd() const { return m_fd; }
|
||||
|
||||
private:
|
||||
int m_fd;
|
||||
};
|
||||
|
||||
class CChildEvent final : public CEvent {
|
||||
public:
|
||||
CChildEvent(Type, CObject& child);
|
||||
|
|
|
@ -245,11 +245,11 @@ void CEventLoop::wait_for_event(WaitMode mode)
|
|||
for (auto& notifier : *s_notifiers) {
|
||||
if (FD_ISSET(notifier->fd(), &rfds)) {
|
||||
if (notifier->on_ready_to_read)
|
||||
notifier->on_ready_to_read();
|
||||
post_event(*notifier, make<CNotifierReadEvent>(notifier->fd()));
|
||||
}
|
||||
if (FD_ISSET(notifier->fd(), &wfds)) {
|
||||
if (notifier->on_ready_to_write)
|
||||
notifier->on_ready_to_write();
|
||||
post_event(*notifier, make<CNotifierWriteEvent>(notifier->fd()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,3 +21,14 @@ void CNotifier::set_enabled(bool enabled)
|
|||
else
|
||||
CEventLoop::unregister_notifier({}, *this);
|
||||
}
|
||||
|
||||
void CNotifier::event(CEvent& event)
|
||||
{
|
||||
if (event.type() == CEvent::NotifierRead && on_ready_to_read) {
|
||||
on_ready_to_read();
|
||||
} else if (event.type() == CEvent::NotifierWrite && on_ready_to_write) {
|
||||
on_ready_to_write();
|
||||
} else {
|
||||
CObject::event(event);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include "CObject.h"
|
||||
|
||||
class CNotifier {
|
||||
class CNotifier : public CObject {
|
||||
public:
|
||||
enum Event {
|
||||
None = 0,
|
||||
|
@ -22,6 +23,9 @@ public:
|
|||
unsigned event_mask() const { return m_event_mask; }
|
||||
void set_event_mask(unsigned event_mask) { m_event_mask = event_mask; }
|
||||
|
||||
const char* class_name() const override { return "CNotifier"; }
|
||||
void event(CEvent& event) override;
|
||||
|
||||
private:
|
||||
int m_fd { -1 };
|
||||
unsigned m_event_mask { 0 };
|
||||
|
|
Loading…
Add table
Reference in a new issue