1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 06:14:58 +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

@ -51,7 +51,7 @@ static void proxy_socket_through_notifier(ClientType& client, QSocketNotifier& n
notifier.setEnabled(true);
QObject::connect(&notifier, &QSocketNotifier::activated, [&client]() mutable {
client.socket().notifier()->on_ready_to_read();
client.socket().notifier()->on_activation();
});
client.set_deferred_invoker(make<DeferredInvokerQt>());

View file

@ -610,7 +610,7 @@ void WebContentView::create_client(WebView::EnableCallgrindProfiling enable_call
QObject::connect(&m_web_content_notifier, &QSocketNotifier::activated, [new_client = new_client.ptr()] {
if (auto notifier = new_client->socket().notifier())
notifier->on_ready_to_read();
notifier->on_activation();
});
struct DeferredInvokerQt final : IPC::DeferredInvoker {

View file

@ -69,8 +69,8 @@ FileOperationProgressWidget::FileOperationProgressWidget(FileOperation operation
VERIFY_NOT_REACHED();
}
m_notifier = Core::Notifier::construct(helper_pipe_fd, Core::Notifier::Read);
m_notifier->on_ready_to_read = [this] {
m_notifier = Core::Notifier::construct(helper_pipe_fd, Core::Notifier::Type::Read);
m_notifier->on_activation = [this] {
auto line_buffer_or_error = ByteBuffer::create_zeroed(1 * KiB);
if (line_buffer_or_error.is_error()) {
did_error("Failed to allocate ByteBuffer for reading data."sv);
@ -211,7 +211,7 @@ void FileOperationProgressWidget::close_pipe()
m_helper_pipe = nullptr;
if (m_notifier) {
m_notifier->set_enabled(false);
m_notifier->on_ready_to_read = nullptr;
m_notifier->on_activation = nullptr;
}
m_notifier = nullptr;
}

View file

@ -15,7 +15,7 @@ namespace Chess::UCI {
Endpoint::Endpoint(NonnullRefPtr<Core::IODevice> in, NonnullRefPtr<Core::IODevice> out)
: m_in(in)
, m_out(out)
, m_in_notifier(Core::Notifier::construct(in->fd(), Core::Notifier::Read))
, m_in_notifier(Core::Notifier::construct(in->fd(), Core::Notifier::Type::Read))
{
set_in_notifier();
}
@ -70,8 +70,8 @@ void Endpoint::custom_event(Core::CustomEvent& custom_event)
void Endpoint::set_in_notifier()
{
m_in_notifier = Core::Notifier::construct(m_in->fd(), Core::Notifier::Read);
m_in_notifier->on_ready_to_read = [this] {
m_in_notifier = Core::Notifier::construct(m_in->fd(), Core::Notifier::Type::Read);
m_in_notifier->on_activation = [this] {
if (!m_in->can_read_line()) {
Core::EventLoop::current().post_event(*this, make<Core::CustomEvent>(EndpointEventType::UnexpectedEof));
m_in_notifier->set_enabled(false);

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

View file

@ -747,9 +747,9 @@ auto Editor::get_line(DeprecatedString const& prompt) -> Result<DeprecatedString
Core::EventLoop loop;
m_notifier = Core::Notifier::construct(STDIN_FILENO, Core::Notifier::Read);
m_notifier = Core::Notifier::construct(STDIN_FILENO, Core::Notifier::Type::Read);
m_notifier->on_ready_to_read = [&] {
m_notifier->on_activation = [&] {
if (try_update_once().is_error())
loop.quit(Exit);
};

View file

@ -25,7 +25,7 @@ void Request::stream_into(Stream& stream)
VERIFY(!m_internal_stream_data);
m_internal_stream_data = make<InternalStreamData>(MUST(Core::File::adopt_fd(fd(), Core::File::OpenMode::Read)));
m_internal_stream_data->read_notifier = Core::Notifier::construct(fd(), Core::Notifier::Read);
m_internal_stream_data->read_notifier = Core::Notifier::construct(fd(), Core::Notifier::Type::Read);
auto user_on_finish = move(on_finish);
on_finish = [this](auto success, auto total_size) {
@ -41,7 +41,7 @@ void Request::stream_into(Stream& stream)
user_on_finish(m_internal_stream_data->success, m_internal_stream_data->total_size);
}
};
m_internal_stream_data->read_notifier->on_ready_to_read = [this, &stream] {
m_internal_stream_data->read_notifier->on_activation = [this, &stream] {
constexpr size_t buffer_size = 256 * KiB;
static char buf[buffer_size];
do {

View file

@ -47,8 +47,8 @@ void TerminalWidget::set_pty_master_fd(int fd)
m_notifier = nullptr;
return;
}
m_notifier = Core::Notifier::construct(m_ptm_fd, Core::Notifier::Read);
m_notifier->on_ready_to_read = [this] {
m_notifier = Core::Notifier::construct(m_ptm_fd, Core::Notifier::Type::Read);
m_notifier->on_activation = [this] {
u8 buffer[BUFSIZ];
ssize_t nread = read(m_ptm_fd, buffer, sizeof(buffer));
if (nread < 0) {

View file

@ -19,8 +19,8 @@ SpiceAgent::SpiceAgent(int fd, ConnectionToClipboardServer& connection)
: m_fd(fd)
, m_clipboard_connection(connection)
{
m_notifier = Core::Notifier::construct(fd, Core::Notifier::Read);
m_notifier->on_ready_to_read = [this] {
m_notifier = Core::Notifier::construct(fd, Core::Notifier::Type::Read);
m_notifier->on_activation = [this] {
on_message_received();
};
m_clipboard_connection.on_data_changed = [this] {

View file

@ -82,8 +82,8 @@ void Service::setup_notifier()
VERIFY(m_sockets.size() == 1);
VERIFY(!m_socket_notifier);
m_socket_notifier = Core::Notifier::construct(m_sockets[0].fd, Core::Notifier::Event::Read, this);
m_socket_notifier->on_ready_to_read = [this] {
m_socket_notifier = Core::Notifier::construct(m_sockets[0].fd, Core::Notifier::Type::Read, this);
m_socket_notifier->on_activation = [this] {
if (auto result = handle_socket_connection(); result.is_error())
dbgln("{}", result.release_error());
};

View file

@ -21,7 +21,7 @@ Client::Client(int id, NonnullOwnPtr<Core::TCPSocket> socket, int ptm_fd)
: m_id(id)
, m_socket(move(socket))
, m_ptm_fd(ptm_fd)
, m_ptm_notifier(Core::Notifier::construct(ptm_fd, Core::Notifier::Read))
, m_ptm_notifier(Core::Notifier::construct(ptm_fd, Core::Notifier::Type::Read))
{
m_socket->on_ready_to_read = [this] {
auto result = drain_socket();
@ -31,7 +31,7 @@ Client::Client(int id, NonnullOwnPtr<Core::TCPSocket> socket, int ptm_fd)
}
};
m_ptm_notifier->on_ready_to_read = [this] {
m_ptm_notifier->on_activation = [this] {
auto result = drain_pty();
if (result.is_error()) {
dbgln("Failed to drain the PTY: {}", result.error());

View file

@ -27,15 +27,15 @@ EventLoop::EventLoop()
m_wm_server = MUST(IPC::MultiServer<WMConnectionFromClient>::try_create("/tmp/portal/wm"));
if (m_keyboard_fd >= 0) {
m_keyboard_notifier = Core::Notifier::construct(m_keyboard_fd, Core::Notifier::Read);
m_keyboard_notifier->on_ready_to_read = [this] { drain_keyboard(); };
m_keyboard_notifier = Core::Notifier::construct(m_keyboard_fd, Core::Notifier::Type::Read);
m_keyboard_notifier->on_activation = [this] { drain_keyboard(); };
} else {
dbgln("Couldn't open /dev/input/keyboard/0");
}
if (m_mouse_fd >= 0) {
m_mouse_notifier = Core::Notifier::construct(m_mouse_fd, Core::Notifier::Read);
m_mouse_notifier->on_ready_to_read = [this] { drain_mouse(); };
m_mouse_notifier = Core::Notifier::construct(m_mouse_fd, Core::Notifier::Type::Read);
m_mouse_notifier->on_activation = [this] { drain_mouse(); };
} else {
dbgln("Couldn't open /dev/input/mouse/0");
}

View file

@ -1742,7 +1742,7 @@ ErrorOr<void> Execute::for_each_entry(RefPtr<Shell> shell, Function<ErrorOr<Iter
Core::EventLoop loop;
auto notifier = Core::Notifier::construct(pipefd[0], Core::Notifier::Read);
auto notifier = Core::Notifier::construct(pipefd[0], Core::Notifier::Type::Read);
AllocatingMemoryStream stream;
enum CheckResult {
@ -1788,18 +1788,18 @@ ErrorOr<void> Execute::for_each_entry(RefPtr<Shell> shell, Function<ErrorOr<Iter
return NothingLeft;
};
notifier->on_ready_to_read = [&]() -> void {
notifier->on_activation = [&]() -> void {
constexpr static auto buffer_size = 16;
u8 buffer[buffer_size];
size_t remaining_size = buffer_size;
for (;;) {
notifier->set_event_mask(Core::Notifier::None);
notifier->set_type(Core::Notifier::Type::None);
bool should_enable_notifier = false;
ScopeGuard notifier_enabler { [&] {
if (should_enable_notifier)
notifier->set_event_mask(Core::Notifier::Read);
notifier->set_type(Core::Notifier::Type::Read);
} };
if (check_and_call().release_value_but_fixme_should_propagate_errors() == Break) {
@ -1841,7 +1841,7 @@ ErrorOr<void> Execute::for_each_entry(RefPtr<Shell> shell, Function<ErrorOr<Iter
auto exit_reason = loop.exec();
notifier->on_ready_to_read = nullptr;
notifier->on_activation = nullptr;
if (close(pipefd[0]) < 0) {
dbgln("close() failed: {}", strerror(errno));