diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-10-04 15:12:33 +0330 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-04 15:31:26 +0200 |
commit | 29acb7fcf8993d96d7c776d6e60dbb7464d8ebad (patch) | |
tree | c3d5a849f1450b7df80e5c227c9575ce6fb30c90 | |
parent | 830b0e8f2dbaccdd2e0b0b953c6f75f6d1659a5f (diff) | |
download | serenity-29acb7fcf8993d96d7c776d6e60dbb7464d8ebad.zip |
LibCore: Add a Socket::set_idle() API that turns the notifiers on/off
When a socket's user doesn't need it to be active, but wants to keep it
open, the socket's notifiers should not be enabled to avoid hogging the
CPU with effectively useless notifications.
This API can be used to disable said notifiers until the user needs the
notifications.
-rw-r--r-- | Userland/Libraries/LibCore/Socket.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Socket.h | 1 |
2 files changed, 9 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/Socket.cpp b/Userland/Libraries/LibCore/Socket.cpp index b21c487041..6d263de7ee 100644 --- a/Userland/Libraries/LibCore/Socket.cpp +++ b/Userland/Libraries/LibCore/Socket.cpp @@ -218,6 +218,14 @@ bool Socket::close() return IODevice::close(); } +void Socket::set_idle(bool idle) +{ + if (m_read_notifier) + m_read_notifier->set_enabled(!idle); + if (m_notifier) + m_notifier->set_enabled(!idle); +} + void Socket::ensure_read_notifier() { VERIFY(m_connected); diff --git a/Userland/Libraries/LibCore/Socket.h b/Userland/Libraries/LibCore/Socket.h index 3ccbae20e2..85c1c442db 100644 --- a/Userland/Libraries/LibCore/Socket.h +++ b/Userland/Libraries/LibCore/Socket.h @@ -43,6 +43,7 @@ public: int destination_port() const { return m_destination_port; } virtual bool close() override; + virtual void set_idle(bool); Function<void()> on_connected; Function<void()> on_error; |