From 7d546f80f9b1724bf0e240a896cb0bc8175b78af Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 26 Apr 2018 16:07:15 +0200 Subject: [PATCH] output/alsa: merge Activate() and UnlockActivate() Prepare to make the "active" attribute thread-safe. --- src/output/plugins/AlsaOutputPlugin.cxx | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/output/plugins/AlsaOutputPlugin.cxx b/src/output/plugins/AlsaOutputPlugin.cxx index 2f51ef8d5..5eae7928f 100644 --- a/src/output/plugins/AlsaOutputPlugin.cxx +++ b/src/output/plugins/AlsaOutputPlugin.cxx @@ -233,28 +233,20 @@ private: * #EventLoop. Before calling this, filling the ring buffer * has no effect; nothing will be played, and no code will be * run on #EventLoop's thread. - */ - void Activate() noexcept { - if (active) - return; - - active = true; - defer_invalidate_sockets.Schedule(); - } - - /** - * Wrapper for Activate() which unlocks our mutex. Call this - * if you're holding the mutex. + * + * Caller must hold the mutex. * * @return true if Activate() was called, false if the mutex * was never unlocked */ - bool UnlockActivate() noexcept { + bool Activate() noexcept { if (active) return false; + active = true; + const ScopeUnlock unlock(mutex); - Activate(); + defer_invalidate_sockets.Schedule(); return true; } @@ -767,7 +759,7 @@ AlsaOutput::Drain() drain = true; - UnlockActivate(); + Activate(); while (drain) { if (error) @@ -853,7 +845,7 @@ AlsaOutput::Play(const void *chunk, size_t size) /* now that the ring_buffer is full, we can activate the socket handlers to trigger the first snd_pcm_writei() */ - if (UnlockActivate()) + if (Activate()) /* since everything may have changed while the mutex was unlocked, we need to skip the cond.wait() call below and check the new