1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 01:17:46 +00:00

LibCore: Simplify Core::Notifier by only allowing one event type

Not a single client of this API actually used the event mask feature to
listen for readability AND writability.

Let's simplify the API and have only one hook: on_activation.
This commit is contained in:
Andreas Kling 2023-04-23 20:59:32 +02:00
parent 1587caef84
commit 411d36719e
24 changed files with 80 additions and 99 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
@ -21,8 +21,7 @@ public:
Invalid = 0,
Quit,
Timer,
NotifierRead,
NotifierWrite,
NotifierActivation,
DeferredInvoke,
ChildAdded,
ChildRemoved,
@ -79,29 +78,14 @@ private:
int m_timer_id;
};
class NotifierReadEvent final : public Event {
class NotifierActivationEvent final : public Event {
public:
explicit NotifierReadEvent(int fd)
: Event(Event::NotifierRead)
explicit NotifierActivationEvent(int fd)
: Event(Event::NotifierActivation)
, m_fd(fd)
{
}
~NotifierReadEvent() = default;
int fd() const { return m_fd; }
private:
int m_fd;
};
class NotifierWriteEvent final : public Event {
public:
explicit NotifierWriteEvent(int fd)
: Event(Event::NotifierWrite)
, m_fd(fd)
{
}
~NotifierWriteEvent() = default;
~NotifierActivationEvent() = default;
int fd() const { return m_fd; }

View file

@ -651,11 +651,11 @@ retry:
max_fd = max(max_fd, max_fd_added);
for (auto& notifier : *s_notifiers) {
if (notifier->event_mask() & Notifier::Read)
if (notifier->type() == Notifier::Type::Read)
add_fd_to_set(notifier->fd(), rfds);
if (notifier->event_mask() & Notifier::Write)
if (notifier->type() == Notifier::Type::Write)
add_fd_to_set(notifier->fd(), wfds);
if (notifier->event_mask() & Notifier::Exceptional)
if (notifier->type() == Notifier::Type::Exceptional)
VERIFY_NOT_REACHED();
}
@ -757,13 +757,11 @@ try_select_again:
// Handle file system notifiers by making them normal events.
for (auto& notifier : *s_notifiers) {
if (FD_ISSET(notifier->fd(), &rfds)) {
if (notifier->event_mask() & Notifier::Event::Read)
post_event(*notifier, make<NotifierReadEvent>(notifier->fd()));
if (notifier->type() == Notifier::Type::Read && FD_ISSET(notifier->fd(), &rfds)) {
post_event(*notifier, make<NotifierActivationEvent>(notifier->fd()));
}
if (FD_ISSET(notifier->fd(), &wfds)) {
if (notifier->event_mask() & Notifier::Event::Write)
post_event(*notifier, make<NotifierWriteEvent>(notifier->fd()));
if (notifier->type() == Notifier::Type::Write && FD_ISSET(notifier->fd(), &wfds)) {
post_event(*notifier, make<NotifierActivationEvent>(notifier->fd()));
}
}
}

View file

@ -97,7 +97,7 @@ ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(FileWatcherFlags flags)
if (watcher_fd < 0)
return Error::from_errno(errno);
auto notifier = TRY(Notifier::try_create(watcher_fd, Notifier::Event::Read));
auto notifier = TRY(Notifier::try_create(watcher_fd, Notifier::Type::Read));
return adopt_nonnull_ref_or_enomem(new (nothrow) FileWatcher(watcher_fd, move(notifier)));
}
@ -105,7 +105,7 @@ FileWatcher::FileWatcher(int watcher_fd, NonnullRefPtr<Notifier> notifier)
: FileWatcherBase(watcher_fd)
, m_notifier(move(notifier))
{
m_notifier->on_ready_to_read = [this] {
m_notifier->on_activation = [this] {
auto maybe_event = get_event_from_fd(m_notifier->fd(), m_wd_to_path);
if (maybe_event.has_value()) {
auto event = maybe_event.value();

View file

@ -59,7 +59,7 @@ public:
// NOTE: This isn't actually used on macOS, but is needed for FileWatcherBase.
// Creating it with an FD of -1 will effectively disable the notifier.
auto notifier = TRY(Notifier::try_create(-1, Notifier::Event::None));
auto notifier = TRY(Notifier::try_create(-1, Notifier::Type::None));
return adopt_nonnull_ref_or_enomem(new (nothrow) FileWatcherMacOS(move(context), dispatch_queue, move(notifier)));
}

View file

@ -188,7 +188,7 @@ ErrorOr<NonnullRefPtr<FileWatcher>> FileWatcher::create(FileWatcherFlags flags)
if (watcher_fd < 0)
return Error::from_errno(errno);
auto notifier = Notifier::construct(watcher_fd, Notifier::Event::Read);
auto notifier = Notifier::construct(watcher_fd, Notifier::Type::Read);
return adopt_ref(*new FileWatcher(watcher_fd, move(notifier)));
}
@ -196,7 +196,7 @@ FileWatcher::FileWatcher(int watcher_fd, NonnullRefPtr<Notifier> notifier)
: FileWatcherBase(watcher_fd)
, m_notifier(move(notifier))
{
m_notifier->on_ready_to_read = [this] {
m_notifier->on_activation = [this] {
auto maybe_event = get_event_from_fd(m_notifier->fd(), m_wd_to_path);
if (maybe_event.has_value()) {
auto event = maybe_event.value();
@ -214,7 +214,7 @@ FileWatcher::FileWatcher(int watcher_fd, NonnullRefPtr<Notifier> notifier)
FileWatcher::~FileWatcher()
{
m_notifier->on_ready_to_read = nullptr;
m_notifier->on_activation = nullptr;
close(m_notifier->fd());
dbgln_if(FILE_WATCHER_DEBUG, "Stopped watcher at fd {}", m_notifier->fd());
}

View file

@ -49,8 +49,8 @@ ErrorOr<void> LocalServer::take_over_from_system_server(DeprecatedString const&
void LocalServer::setup_notifier()
{
m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
m_notifier->on_ready_to_read = [this] {
m_notifier = Notifier::construct(m_fd, Notifier::Type::Read, this);
m_notifier->on_activation = [this] {
if (on_accept) {
auto maybe_client_socket = accept();
if (maybe_client_socket.is_error()) {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,10 +10,10 @@
namespace Core {
Notifier::Notifier(int fd, unsigned event_mask, Object* parent)
Notifier::Notifier(int fd, Type type, Object* parent)
: Object(parent)
, m_fd(fd)
, m_event_mask(event_mask)
, m_type(type)
{
set_enabled(true);
}
@ -43,13 +43,12 @@ void Notifier::close()
void Notifier::event(Core::Event& event)
{
if (event.type() == Core::Event::NotifierRead && on_ready_to_read) {
on_ready_to_read();
} else if (event.type() == Core::Event::NotifierWrite && on_ready_to_write) {
on_ready_to_write();
} else {
Object::event(event);
if (event.type() == Core::Event::NotifierActivation) {
if (on_activation)
on_activation();
return;
}
Object::event(event);
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -11,36 +11,36 @@
namespace Core {
class Notifier : public Object {
C_OBJECT(Notifier)
class Notifier final : public Object {
C_OBJECT(Notifier);
public:
enum Event {
None = 0,
Read = 1,
Write = 2,
Exceptional = 4,
enum class Type {
None,
Read,
Write,
Exceptional,
};
virtual ~Notifier() override;
void set_enabled(bool);
Function<void()> on_ready_to_read;
Function<void()> on_ready_to_write;
Function<void()> on_activation;
void close();
int fd() const { return m_fd; }
unsigned event_mask() const { return m_event_mask; }
void set_event_mask(unsigned event_mask) { m_event_mask = event_mask; }
Type type() const { return m_type; }
void set_type(Type type) { m_type = type; }
void event(Core::Event&) override;
private:
Notifier(int fd, unsigned event_mask, Object* parent = nullptr);
Notifier(int fd, Type type, Object* parent = nullptr);
int m_fd { -1 };
unsigned m_event_mask { 0 };
Type m_type { Type::None };
};
}

View file

@ -186,7 +186,7 @@ ErrorOr<void> PosixSocketHelper::set_receive_timeout(Time timeout)
void PosixSocketHelper::setup_notifier()
{
if (!m_notifier)
m_notifier = Core::Notifier::construct(m_fd, Core::Notifier::Read);
m_notifier = Core::Notifier::construct(m_fd, Core::Notifier::Type::Read);
}
ErrorOr<NonnullOwnPtr<TCPSocket>> TCPSocket::connect(DeprecatedString const& host, u16 port)

View file

@ -204,7 +204,7 @@ private:
VERIFY(is_open());
m_helper.setup_notifier();
m_helper.notifier()->on_ready_to_read = [this] {
m_helper.notifier()->on_activation = [this] {
if (on_ready_to_read)
on_ready_to_read();
};
@ -278,7 +278,7 @@ private:
VERIFY(is_open());
m_helper.setup_notifier();
m_helper.notifier()->on_ready_to_read = [this] {
m_helper.notifier()->on_activation = [this] {
if (on_ready_to_read)
on_ready_to_read();
};
@ -352,7 +352,7 @@ private:
VERIFY(is_open());
m_helper.setup_notifier();
m_helper.notifier()->on_ready_to_read = [this] {
m_helper.notifier()->on_activation = [this] {
if (on_ready_to_read)
on_ready_to_read();
};

View file

@ -57,8 +57,8 @@ ErrorOr<void> TCPServer::listen(IPv4Address const& address, u16 port, AllowAddre
TRY(Core::System::listen(m_fd, 5));
m_listening = true;
m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
m_notifier->on_ready_to_read = [this] {
m_notifier = Notifier::construct(m_fd, Notifier::Type::Read, this);
m_notifier->on_activation = [this] {
if (on_ready_to_accept)
on_ready_to_accept();
};

View file

@ -55,8 +55,8 @@ bool UDPServer::bind(IPv4Address const& address, u16 port)
m_bound = true;
m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
m_notifier->on_ready_to_read = [this] {
m_notifier = Notifier::construct(m_fd, Notifier::Type::Read, this);
m_notifier->on_activation = [this] {
if (on_ready_to_receive)
on_ready_to_receive();
};