From d67553e128200d4a8adf85a680f34425a70c42ef Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 16 Sep 2020 09:42:59 -0600 Subject: [PATCH] LibCore: Add Notifier::close If a file descriptor is being closed, we need to permanently disable any Notifier and remove it from the event loop. This method removes the notifier and disables it so that the EventLoop does not use a invalid file descriptor. --- Libraries/LibCore/Notifier.cpp | 10 ++++++++++ Libraries/LibCore/Notifier.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/Libraries/LibCore/Notifier.cpp b/Libraries/LibCore/Notifier.cpp index 7d39ae0e890..655a73ed1bb 100644 --- a/Libraries/LibCore/Notifier.cpp +++ b/Libraries/LibCore/Notifier.cpp @@ -46,12 +46,22 @@ Notifier::~Notifier() void Notifier::set_enabled(bool enabled) { + if (m_fd < 0) + return; if (enabled) Core::EventLoop::register_notifier({}, *this); else Core::EventLoop::unregister_notifier({}, *this); } +void Notifier::close() +{ + if (m_fd < 0) + return; + set_enabled(false); + m_fd = -1; +} + void Notifier::event(Core::Event& event) { if (event.type() == Core::Event::NotifierRead && on_ready_to_read) { diff --git a/Libraries/LibCore/Notifier.h b/Libraries/LibCore/Notifier.h index fd40053284a..b197b552046 100644 --- a/Libraries/LibCore/Notifier.h +++ b/Libraries/LibCore/Notifier.h @@ -48,6 +48,8 @@ public: Function on_ready_to_read; Function on_ready_to_write; + 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; }