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:
Robin Burchell 2019-07-16 20:31:14 +02:00 committed by Andreas Kling
parent a714fc661d
commit d8387f1506
Notes: sideshowbarker 2024-07-19 13:14:15 +09:00
4 changed files with 50 additions and 3 deletions

View File

@ -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);

View File

@ -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()));
}
}

View File

@ -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);
}
}

View File

@ -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 };