From 9601b516b81c70fb109ded5f0c9a038301c4f80e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 23 Apr 2023 18:20:38 +0200 Subject: [PATCH] LibCore: Remove awkward EventLoop::wake_once() API This was used in exactly one place, to avoid sending multiple CustomEvents to the enqueuer thread in Audio::ConnectionToServer. Instead of this, we now just send a CustomEvent and wake the enqueuer thread. If it wakes up and has multiple CustomEvents, they get delivered and ignored in no time anyway. Since they only get ignored if there's no work to be done, this seems harmless. --- .../Libraries/LibAudio/ConnectionToServer.cpp | 3 ++- Userland/Libraries/LibCore/EventLoop.cpp | 23 ++----------------- Userland/Libraries/LibCore/EventLoop.h | 12 +++------- 3 files changed, 7 insertions(+), 31 deletions(-) diff --git a/Userland/Libraries/LibAudio/ConnectionToServer.cpp b/Userland/Libraries/LibAudio/ConnectionToServer.cpp index ab46dd8a20..2402de36e7 100644 --- a/Userland/Libraries/LibAudio/ConnectionToServer.cpp +++ b/Userland/Libraries/LibAudio/ConnectionToServer.cpp @@ -69,7 +69,8 @@ ErrorOr ConnectionToServer::async_enqueue(FixedArray&& samples) update_good_sleep_time(); m_user_queue->append(move(samples)); // Wake the background thread to make sure it starts enqueuing audio. - m_enqueuer_loop->wake_once(*this, 0); + m_enqueuer_loop->post_event(*this, make(0)); + m_enqueuer_loop->wake(); async_start_playback(); return {}; diff --git a/Userland/Libraries/LibCore/EventLoop.cpp b/Userland/Libraries/LibCore/EventLoop.cpp index e4e921cced..a1784964fd 100644 --- a/Userland/Libraries/LibCore/EventLoop.cpp +++ b/Userland/Libraries/LibCore/EventLoop.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2023, Andreas Kling * Copyright (c) 2022, kleines Filmröllchen * Copyright (c) 2022, the SerenityOS developers. * @@ -518,30 +518,11 @@ size_t EventLoop::pump(WaitMode mode) return processed_events; } -void EventLoop::post_event(Object& receiver, NonnullOwnPtr&& event, ShouldWake should_wake) +void EventLoop::post_event(Object& receiver, NonnullOwnPtr&& event) { Threading::MutexLocker lock(m_private->lock); dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop::post_event: ({}) << receiver={}, event={}", m_queued_events.size(), receiver, event); m_queued_events.empend(receiver, move(event)); - if (should_wake == ShouldWake::Yes) - wake(); -} - -void EventLoop::wake_once(Object& receiver, int custom_event_type) -{ - Threading::MutexLocker lock(m_private->lock); - dbgln_if(EVENTLOOP_DEBUG, "Core::EventLoop::wake_once: event type {}", custom_event_type); - auto identical_events = m_queued_events.find_if([&](auto& queued_event) { - if (queued_event.receiver.is_null()) - return false; - auto const& event = queued_event.event; - auto is_receiver_identical = queued_event.receiver.ptr() == &receiver; - auto event_id_matches = event->type() == Event::Type::Custom && static_cast(event.ptr())->custom_type() == custom_event_type; - return is_receiver_identical && event_id_matches; - }); - // Event is not in the queue yet, so we want to wake. - if (identical_events.is_end()) - post_event(receiver, make(custom_event_type), ShouldWake::Yes); } void EventLoop::add_job(NonnullRefPtr>> job_promise) diff --git a/Userland/Libraries/LibCore/EventLoop.h b/Userland/Libraries/LibCore/EventLoop.h index a8519507b4..7b49eb8e8f 100644 --- a/Userland/Libraries/LibCore/EventLoop.h +++ b/Userland/Libraries/LibCore/EventLoop.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2023, Andreas Kling * Copyright (c) 2022, kleines Filmröllchen * Copyright (c) 2022, the SerenityOS developers. * @@ -55,11 +55,6 @@ public: Yes, }; - enum class ShouldWake { - No, - Yes - }; - enum class WaitMode { WaitForEvents, PollForEvents, @@ -82,9 +77,8 @@ public: // Pump the event loop until some condition is met. void spin_until(Function); - // Post an event to this event loop and possibly wake the loop. - void post_event(Object& receiver, NonnullOwnPtr&&, ShouldWake = ShouldWake::No); - void wake_once(Object& receiver, int custom_event_type); + // Post an event to this event loop. + void post_event(Object& receiver, NonnullOwnPtr&&); void add_job(NonnullRefPtr>> job_promise);