Skip to content

Commit

Permalink
LibCore: Add Notifier::close
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tomuta authored and awesomekling committed Sep 16, 2020
1 parent 0579a2d commit d67553e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Libraries/LibCore/Notifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions Libraries/LibCore/Notifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Notifier : public Object {
Function<void()> on_ready_to_read;
Function<void()> 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; }
Expand Down

0 comments on commit d67553e

Please sign in to comment.