event/Call, ...: use wait() with predicate

This commit is contained in:
Max Kellermann 2019-05-07 20:01:45 +02:00
parent 72fc117393
commit 973c87b351
12 changed files with 20 additions and 35 deletions

View File

@ -53,8 +53,7 @@ public:
{ {
std::unique_lock<Mutex> lock(mutex); std::unique_lock<Mutex> lock(mutex);
while (!done) cond.wait(lock, [this]{ return done; });
cond.wait(lock);
} }
if (exception) if (exception)

View File

@ -136,8 +136,8 @@ AsyncInputStream::Seek(std::unique_lock<Mutex> &lock,
CondInputStreamHandler cond_handler; CondInputStreamHandler cond_handler;
const ScopeExchangeInputStreamHandler h(*this, &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(); Check();
} }

View File

@ -84,8 +84,7 @@ BufferedInputStream::Seek(std::unique_lock<Mutex> &lock,
seek = true; seek = true;
wake_cond.notify_one(); wake_cond.notify_one();
while (seek) client_cond.wait(lock, [this]{ return !seek; });
client_cond.wait(lock);
if (seek_error) if (seek_error)
std::rethrow_exception(std::exchange(seek_error, {})); std::rethrow_exception(std::exchange(seek_error, {}));

View File

@ -59,13 +59,10 @@ InputStream::OpenReady(const char *uri, Mutex &mutex)
{ {
std::unique_lock<Mutex> lock(mutex); std::unique_lock<Mutex> lock(mutex);
while (true) { handler.cond.wait(lock, [&is]{
is->Update(); is->Update();
if (is->IsReady()) return is->IsReady();
break; });
handler.cond.wait(lock);
}
is->Check(); is->Check();
} }

View File

@ -92,8 +92,7 @@ void
ProxyInputStream::Seek(std::unique_lock<Mutex> &lock, ProxyInputStream::Seek(std::unique_lock<Mutex> &lock,
offset_type new_offset) offset_type new_offset)
{ {
while (!input) set_input_cond.wait(lock, [this]{ return !!input; });
set_input_cond.wait(lock);
input->Seek(lock, new_offset); input->Seek(lock, new_offset);
CopyAttributes(); CopyAttributes();
@ -124,8 +123,7 @@ size_t
ProxyInputStream::Read(std::unique_lock<Mutex> &lock, ProxyInputStream::Read(std::unique_lock<Mutex> &lock,
void *ptr, size_t read_size) void *ptr, size_t read_size)
{ {
while (!input) set_input_cond.wait(lock, [this]{ return !!input; });
set_input_cond.wait(lock);
size_t nbytes = input->Read(lock, ptr, read_size); size_t nbytes = input->Read(lock, ptr, read_size);
CopyAttributes(); CopyAttributes();

View File

@ -114,8 +114,7 @@ AudioOutputControl::LockToggleEnabled() noexcept
void void
AudioOutputControl::WaitForCommand(std::unique_lock<Mutex> &lock) noexcept AudioOutputControl::WaitForCommand(std::unique_lock<Mutex> &lock) noexcept
{ {
while (!IsCommandFinished()) client_cond.wait(lock, [this]{ return IsCommandFinished(); });
client_cond.wait(lock);
} }
void void

View File

@ -805,8 +805,7 @@ AlsaOutput::Drain()
Activate(); Activate();
while (drain && active) cond.wait(lock, [this]{ return !drain || !active; });
cond.wait(lock);
if (error) if (error)
std::rethrow_exception(error); std::rethrow_exception(error);

View File

@ -278,8 +278,7 @@ HttpdOutput::BroadcastFromEncoder()
/* synchronize with the IOThread */ /* synchronize with the IOThread */
{ {
std::unique_lock<Mutex> lock(mutex); std::unique_lock<Mutex> lock(mutex);
while (!pages.empty()) cond.wait(lock, [this]{ return pages.empty(); });
cond.wait(lock);
} }
bool empty = true; bool empty = true;

View File

@ -321,10 +321,10 @@ SlesOutput::Play(const void *chunk, size_t size)
assert(filled < BUFFER_SIZE); assert(filled < BUFFER_SIZE);
while (n_queued == N_BUFFERS) { cond.wait(lock, [this]{
assert(filled == 0); assert(filled == 0);
cond.wait(lock); return n_queued != N_BUFFERS;
} });
size_t nbytes = std::min(BUFFER_SIZE - filled, size); size_t nbytes = std::min(BUFFER_SIZE - filled, size);
memcpy(buffers[next] + filled, chunk, nbytes); memcpy(buffers[next] + filled, chunk, nbytes);
@ -350,8 +350,7 @@ SlesOutput::Drain()
assert(filled < BUFFER_SIZE); assert(filled < BUFFER_SIZE);
while (n_queued > 0) cond.wait(lock, [this]{ return n_queued == 0; });
cond.wait(lock);
} }
void void

View File

@ -125,8 +125,7 @@ public:
void Wait() { void Wait() {
std::unique_lock<Mutex> lock(mutex); std::unique_lock<Mutex> lock(mutex);
while (!done) cond.wait(lock, [this]{ return done; });
cond.wait(lock);
if (postponed_error) if (postponed_error)
std::rethrow_exception(postponed_error); std::rethrow_exception(postponed_error);

View File

@ -209,8 +209,7 @@ UdisksStorage::MountWait()
defer_mount.Schedule(); defer_mount.Schedule();
} }
while (want_mount) cond.wait(lock, [this]{ return !want_mount; });
cond.wait(lock);
if (mount_error) if (mount_error)
std::rethrow_exception(mount_error); std::rethrow_exception(mount_error);
@ -280,8 +279,7 @@ UdisksStorage::UnmountWait()
defer_unmount.Schedule(); defer_unmount.Schedule();
while (mounted_storage) cond.wait(lock, [this]{ return !mounted_storage; });
cond.wait(lock);
if (mount_error) if (mount_error)
std::rethrow_exception(mount_error); std::rethrow_exception(mount_error);

View File

@ -176,8 +176,7 @@ class DumpRemoteTagHandler final : public RemoteTagHandler {
public: public:
Tag Wait() { Tag Wait() {
std::unique_lock<Mutex> lock(mutex); std::unique_lock<Mutex> lock(mutex);
while (!done) cond.wait(lock, [this]{ return done; });
cond.wait(lock);
if (error) if (error)
std::rethrow_exception(error); std::rethrow_exception(error);