From 973c87b351b31961a51b6ea9afc4bea0872565ad Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 7 May 2019 20:01:45 +0200 Subject: [PATCH] event/Call, ...: use wait() with predicate --- src/event/Call.cxx | 3 +-- src/input/AsyncInputStream.cxx | 4 ++-- src/input/BufferedInputStream.cxx | 3 +-- src/input/Open.cxx | 9 +++------ src/input/ProxyInputStream.cxx | 6 ++---- src/output/Control.cxx | 3 +-- src/output/plugins/AlsaOutputPlugin.cxx | 3 +-- src/output/plugins/httpd/HttpdOutputPlugin.cxx | 3 +-- src/output/plugins/sles/SlesOutputPlugin.cxx | 9 ++++----- src/storage/plugins/CurlStorage.cxx | 3 +-- src/storage/plugins/UdisksStorage.cxx | 6 ++---- test/run_input.cxx | 3 +-- 12 files changed, 20 insertions(+), 35 deletions(-) diff --git a/src/event/Call.cxx b/src/event/Call.cxx index 2711c1190..f244e9cac 100644 --- a/src/event/Call.cxx +++ b/src/event/Call.cxx @@ -53,8 +53,7 @@ public: { std::unique_lock lock(mutex); - while (!done) - cond.wait(lock); + cond.wait(lock, [this]{ return done; }); } if (exception) diff --git a/src/input/AsyncInputStream.cxx b/src/input/AsyncInputStream.cxx index 3ccd41353..817922ecb 100644 --- a/src/input/AsyncInputStream.cxx +++ b/src/input/AsyncInputStream.cxx @@ -136,8 +136,8 @@ AsyncInputStream::Seek(std::unique_lock &lock, CondInputStreamHandler cond_handler; const ScopeExchangeInputStreamHandler h(*this, &cond_handler); - while (seek_state != SeekState::NONE) - cond_handler.cond.wait(lock); + cond_handler.cond.wait(lock, + [this]{ return seek_state == SeekState::NONE; }); Check(); } diff --git a/src/input/BufferedInputStream.cxx b/src/input/BufferedInputStream.cxx index 69a557a79..974262cc0 100644 --- a/src/input/BufferedInputStream.cxx +++ b/src/input/BufferedInputStream.cxx @@ -84,8 +84,7 @@ BufferedInputStream::Seek(std::unique_lock &lock, seek = true; wake_cond.notify_one(); - while (seek) - client_cond.wait(lock); + client_cond.wait(lock, [this]{ return !seek; }); if (seek_error) std::rethrow_exception(std::exchange(seek_error, {})); diff --git a/src/input/Open.cxx b/src/input/Open.cxx index 8ac6d868e..8cff75bb7 100644 --- a/src/input/Open.cxx +++ b/src/input/Open.cxx @@ -59,13 +59,10 @@ InputStream::OpenReady(const char *uri, Mutex &mutex) { std::unique_lock lock(mutex); - while (true) { + handler.cond.wait(lock, [&is]{ is->Update(); - if (is->IsReady()) - break; - - handler.cond.wait(lock); - } + return is->IsReady(); + }); is->Check(); } diff --git a/src/input/ProxyInputStream.cxx b/src/input/ProxyInputStream.cxx index 345e6f319..746612fbd 100644 --- a/src/input/ProxyInputStream.cxx +++ b/src/input/ProxyInputStream.cxx @@ -92,8 +92,7 @@ void ProxyInputStream::Seek(std::unique_lock &lock, offset_type new_offset) { - while (!input) - set_input_cond.wait(lock); + set_input_cond.wait(lock, [this]{ return !!input; }); input->Seek(lock, new_offset); CopyAttributes(); @@ -124,8 +123,7 @@ size_t ProxyInputStream::Read(std::unique_lock &lock, void *ptr, size_t read_size) { - while (!input) - set_input_cond.wait(lock); + set_input_cond.wait(lock, [this]{ return !!input; }); size_t nbytes = input->Read(lock, ptr, read_size); CopyAttributes(); diff --git a/src/output/Control.cxx b/src/output/Control.cxx index d49dbd3dc..4d751926e 100644 --- a/src/output/Control.cxx +++ b/src/output/Control.cxx @@ -114,8 +114,7 @@ AudioOutputControl::LockToggleEnabled() noexcept void AudioOutputControl::WaitForCommand(std::unique_lock &lock) noexcept { - while (!IsCommandFinished()) - client_cond.wait(lock); + client_cond.wait(lock, [this]{ return IsCommandFinished(); }); } void diff --git a/src/output/plugins/AlsaOutputPlugin.cxx b/src/output/plugins/AlsaOutputPlugin.cxx index 68bdafb4e..a967a3865 100644 --- a/src/output/plugins/AlsaOutputPlugin.cxx +++ b/src/output/plugins/AlsaOutputPlugin.cxx @@ -805,8 +805,7 @@ AlsaOutput::Drain() Activate(); - while (drain && active) - cond.wait(lock); + cond.wait(lock, [this]{ return !drain || !active; }); if (error) std::rethrow_exception(error); diff --git a/src/output/plugins/httpd/HttpdOutputPlugin.cxx b/src/output/plugins/httpd/HttpdOutputPlugin.cxx index 403712ff3..098278df2 100644 --- a/src/output/plugins/httpd/HttpdOutputPlugin.cxx +++ b/src/output/plugins/httpd/HttpdOutputPlugin.cxx @@ -278,8 +278,7 @@ HttpdOutput::BroadcastFromEncoder() /* synchronize with the IOThread */ { std::unique_lock lock(mutex); - while (!pages.empty()) - cond.wait(lock); + cond.wait(lock, [this]{ return pages.empty(); }); } bool empty = true; diff --git a/src/output/plugins/sles/SlesOutputPlugin.cxx b/src/output/plugins/sles/SlesOutputPlugin.cxx index f2b309655..168dadc72 100644 --- a/src/output/plugins/sles/SlesOutputPlugin.cxx +++ b/src/output/plugins/sles/SlesOutputPlugin.cxx @@ -321,10 +321,10 @@ SlesOutput::Play(const void *chunk, size_t size) assert(filled < BUFFER_SIZE); - while (n_queued == N_BUFFERS) { + cond.wait(lock, [this]{ assert(filled == 0); - cond.wait(lock); - } + return n_queued != N_BUFFERS; + }); size_t nbytes = std::min(BUFFER_SIZE - filled, size); memcpy(buffers[next] + filled, chunk, nbytes); @@ -350,8 +350,7 @@ SlesOutput::Drain() assert(filled < BUFFER_SIZE); - while (n_queued > 0) - cond.wait(lock); + cond.wait(lock, [this]{ return n_queued == 0; }); } void diff --git a/src/storage/plugins/CurlStorage.cxx b/src/storage/plugins/CurlStorage.cxx index eb61f1b52..75d8dae81 100644 --- a/src/storage/plugins/CurlStorage.cxx +++ b/src/storage/plugins/CurlStorage.cxx @@ -125,8 +125,7 @@ public: void Wait() { std::unique_lock lock(mutex); - while (!done) - cond.wait(lock); + cond.wait(lock, [this]{ return done; }); if (postponed_error) std::rethrow_exception(postponed_error); diff --git a/src/storage/plugins/UdisksStorage.cxx b/src/storage/plugins/UdisksStorage.cxx index cf1854112..ca3374a5f 100644 --- a/src/storage/plugins/UdisksStorage.cxx +++ b/src/storage/plugins/UdisksStorage.cxx @@ -209,8 +209,7 @@ UdisksStorage::MountWait() defer_mount.Schedule(); } - while (want_mount) - cond.wait(lock); + cond.wait(lock, [this]{ return !want_mount; }); if (mount_error) std::rethrow_exception(mount_error); @@ -280,8 +279,7 @@ UdisksStorage::UnmountWait() defer_unmount.Schedule(); - while (mounted_storage) - cond.wait(lock); + cond.wait(lock, [this]{ return !mounted_storage; }); if (mount_error) std::rethrow_exception(mount_error); diff --git a/test/run_input.cxx b/test/run_input.cxx index 3b47daf4d..e11a2df19 100644 --- a/test/run_input.cxx +++ b/test/run_input.cxx @@ -176,8 +176,7 @@ class DumpRemoteTagHandler final : public RemoteTagHandler { public: Tag Wait() { std::unique_lock lock(mutex); - while (!done) - cond.wait(lock); + cond.wait(lock, [this]{ return done; }); if (error) std::rethrow_exception(error);