Userland: Use Threading::MutexLocker to lock/unlock mutexes

This commit is contained in:
Oleg Kosenkov 2022-10-30 02:34:38 -04:00 committed by Andreas Kling
parent 466000e05f
commit 0c27d95e76
Notes: sideshowbarker 2024-07-17 10:05:47 +09:00
3 changed files with 15 additions and 20 deletions

View file

@ -59,10 +59,8 @@ ErrorOr<void> ImageProcessor::enqueue_command(NonnullRefPtr<ImageProcessingComma
m_processor_thread->detach();
}
m_wakeup_mutex.lock();
Threading::MutexLocker const locker(m_wakeup_mutex);
m_wakeup_variable.signal();
m_wakeup_mutex.unlock();
return {};
}

View file

@ -27,9 +27,8 @@ ConnectionToServer::ConnectionToServer(NonnullOwnPtr<Core::Stream::LocalSocket>
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();
}

View file

@ -45,11 +45,10 @@ Mixer::Mixer(NonnullRefPtr<Core::ConfigFile> config)
NonnullRefPtr<ClientAudioStream> 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(); });