From 0c27d95e760d2a30b287f913a4d238c7af63c724 Mon Sep 17 00:00:00 2001 From: Oleg Kosenkov Date: Sun, 30 Oct 2022 02:34:38 -0400 Subject: [PATCH] Userland: Use Threading::MutexLocker to lock/unlock mutexes --- .../PixelPaint/ImageProcessor.cpp | 4 +--- .../Libraries/LibAudio/ConnectionToServer.cpp | 7 ++---- Userland/Services/AudioServer/Mixer.cpp | 24 +++++++++---------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Userland/Applications/PixelPaint/ImageProcessor.cpp b/Userland/Applications/PixelPaint/ImageProcessor.cpp index ab16a604ddd..7e5c31eef84 100644 --- a/Userland/Applications/PixelPaint/ImageProcessor.cpp +++ b/Userland/Applications/PixelPaint/ImageProcessor.cpp @@ -59,10 +59,8 @@ ErrorOr ImageProcessor::enqueue_command(NonnullRefPtrdetach(); } - m_wakeup_mutex.lock(); + Threading::MutexLocker const locker(m_wakeup_mutex); m_wakeup_variable.signal(); - m_wakeup_mutex.unlock(); - return {}; } diff --git a/Userland/Libraries/LibAudio/ConnectionToServer.cpp b/Userland/Libraries/LibAudio/ConnectionToServer.cpp index 7f063f8502d..5d61fa19e0b 100644 --- a/Userland/Libraries/LibAudio/ConnectionToServer.cpp +++ b/Userland/Libraries/LibAudio/ConnectionToServer.cpp @@ -27,9 +27,8 @@ ConnectionToServer::ConnectionToServer(NonnullOwnPtr Core::EventLoop enqueuer_loop; m_enqueuer_loop = &enqueuer_loop; enqueuer_loop.exec(); - m_enqueuer_loop_destruction.lock(); + Threading::MutexLocker const locker(m_enqueuer_loop_destruction); m_enqueuer_loop = nullptr; - m_enqueuer_loop_destruction.unlock(); return (intptr_t) nullptr; })) { @@ -45,12 +44,10 @@ ConnectionToServer::~ConnectionToServer() void ConnectionToServer::die() { // We're sometimes getting here after the other thread has already exited and its event loop does no longer exist. - m_enqueuer_loop_destruction.lock(); - if (m_enqueuer_loop != nullptr) { + if (Threading::MutexLocker const locker(m_enqueuer_loop_destruction); m_enqueuer_loop != nullptr) { m_enqueuer_loop->wake(); m_enqueuer_loop->quit(0); } - m_enqueuer_loop_destruction.unlock(); (void)m_background_audio_enqueuer->join(); } diff --git a/Userland/Services/AudioServer/Mixer.cpp b/Userland/Services/AudioServer/Mixer.cpp index 9e8cf14d8b1..4b5cdc41bee 100644 --- a/Userland/Services/AudioServer/Mixer.cpp +++ b/Userland/Services/AudioServer/Mixer.cpp @@ -45,11 +45,10 @@ Mixer::Mixer(NonnullRefPtr config) NonnullRefPtr Mixer::create_queue(ConnectionFromClient& client) { auto queue = adopt_ref(*new ClientAudioStream(client)); - m_pending_mutex.lock(); - - m_pending_mixing.append(*queue); - - m_pending_mutex.unlock(); + { + Threading::MutexLocker const locker(m_pending_mutex); + m_pending_mixing.append(*queue); + } // Signal the mixer thread to start back up, in case nobody was connected before. m_mixing_necessary.signal(); @@ -61,14 +60,15 @@ void Mixer::mix() decltype(m_pending_mixing) active_mix_queues; for (;;) { - m_pending_mutex.lock(); - // While we have nothing to mix, wait on the condition. - m_mixing_necessary.wait_while([this, &active_mix_queues]() { return m_pending_mixing.is_empty() && active_mix_queues.is_empty(); }); - if (!m_pending_mixing.is_empty()) { - active_mix_queues.extend(move(m_pending_mixing)); - m_pending_mixing.clear(); + { + Threading::MutexLocker const locker(m_pending_mutex); + // While we have nothing to mix, wait on the condition. + m_mixing_necessary.wait_while([this, &active_mix_queues]() { return m_pending_mixing.is_empty() && active_mix_queues.is_empty(); }); + if (!m_pending_mixing.is_empty()) { + active_mix_queues.extend(move(m_pending_mixing)); + m_pending_mixing.clear(); + } } - m_pending_mutex.unlock(); active_mix_queues.remove_all_matching([&](auto& entry) { return !entry->client(); });