diff options
author | Andreas Kling <kling@serenityos.org> | 2023-04-23 20:59:32 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-04-25 14:48:40 +0200 |
commit | 411d36719e8ee562bede1a6520891df4e43992f2 (patch) | |
tree | d4c0d4927bc2b4d1212756858fa3239ce80be5e0 /Userland/Libraries/LibCore | |
parent | 1587caef842c66ed382fc75c8f116d09ea7acc06 (diff) | |
download | serenity-411d36719e8ee562bede1a6520891df4e43992f2.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r-- | Userland/Libraries/LibCore/Event.h | 28 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/EventLoop.cpp | 16 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/FileWatcherLinux.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/FileWatcherMacOS.mm | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/FileWatcherSerenity.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/LocalServer.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Notifier.cpp | 17 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Notifier.h | 28 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Socket.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Socket.h | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/TCPServer.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/UDPServer.cpp | 4 |
12 files changed, 51 insertions, 70 deletions
diff --git a/Userland/Libraries/LibCore/Event.h b/Userland/Libraries/LibCore/Event.h index f3da4419e4..a539ee5943 100644 --- a/Userland/Libraries/LibCore/Event.h +++ b/Userland/Libraries/LibCore/Event.h @@ -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; } diff --git a/Userland/Libraries/LibCore/EventLoop.cpp b/Userland/Libraries/LibCore/EventLoop.cpp index 979e551047..c8f48c6ebf 100644 --- a/Userland/Libraries/LibCore/EventLoop.cpp +++ b/Userland/Libraries/LibCore/EventLoop.cpp @@ -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())); } } } diff --git a/Userland/Libraries/LibCore/FileWatcherLinux.cpp b/Userland/Libraries/LibCore/FileWatcherLinux.cpp index 963e809e73..4c0ccdc969 100644 --- a/Userland/Libraries/LibCore/FileWatcherLinux.cpp +++ b/Userland/Libraries/LibCore/FileWatcherLinux.cpp @@ -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(); diff --git a/Userland/Libraries/LibCore/FileWatcherMacOS.mm b/Userland/Libraries/LibCore/FileWatcherMacOS.mm index 760fe6dc51..c546698273 100644 --- a/Userland/Libraries/LibCore/FileWatcherMacOS.mm +++ b/Userland/Libraries/LibCore/FileWatcherMacOS.mm @@ -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))); } diff --git a/Userland/Libraries/LibCore/FileWatcherSerenity.cpp b/Userland/Libraries/LibCore/FileWatcherSerenity.cpp index fbf3344729..946826981e 100644 --- a/Userland/Libraries/LibCore/FileWatcherSerenity.cpp +++ b/Userland/Libraries/LibCore/FileWatcherSerenity.cpp @@ -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()); } diff --git a/Userland/Libraries/LibCore/LocalServer.cpp b/Userland/Libraries/LibCore/LocalServer.cpp index 88a614fc8b..49f78c37fa 100644 --- a/Userland/Libraries/LibCore/LocalServer.cpp +++ b/Userland/Libraries/LibCore/LocalServer.cpp @@ -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()) { diff --git a/Userland/Libraries/LibCore/Notifier.cpp b/Userland/Libraries/LibCore/Notifier.cpp index 17d55cfa7b..04e620f1be 100644 --- a/Userland/Libraries/LibCore/Notifier.cpp +++ b/Userland/Libraries/LibCore/Notifier.cpp @@ -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); } } diff --git a/Userland/Libraries/LibCore/Notifier.h b/Userland/Libraries/LibCore/Notifier.h index ff7d0eb8c8..1815ad0b2c 100644 --- a/Userland/Libraries/LibCore/Notifier.h +++ b/Userland/Libraries/LibCore/Notifier.h @@ -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 }; }; } diff --git a/Userland/Libraries/LibCore/Socket.cpp b/Userland/Libraries/LibCore/Socket.cpp index dfc400322f..639837ae0b 100644 --- a/Userland/Libraries/LibCore/Socket.cpp +++ b/Userland/Libraries/LibCore/Socket.cpp @@ -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) diff --git a/Userland/Libraries/LibCore/Socket.h b/Userland/Libraries/LibCore/Socket.h index 0906b02116..f005fa2074 100644 --- a/Userland/Libraries/LibCore/Socket.h +++ b/Userland/Libraries/LibCore/Socket.h @@ -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(); }; diff --git a/Userland/Libraries/LibCore/TCPServer.cpp b/Userland/Libraries/LibCore/TCPServer.cpp index d5ab99f241..0a5a42963e 100644 --- a/Userland/Libraries/LibCore/TCPServer.cpp +++ b/Userland/Libraries/LibCore/TCPServer.cpp @@ -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(); }; diff --git a/Userland/Libraries/LibCore/UDPServer.cpp b/Userland/Libraries/LibCore/UDPServer.cpp index da46081c19..70249b623a 100644 --- a/Userland/Libraries/LibCore/UDPServer.cpp +++ b/Userland/Libraries/LibCore/UDPServer.cpp @@ -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(); }; |