Skip to content

Commit

Permalink
Added bool-cast defaults for Wait and WaitFor in WaitableAtomic.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorolev committed Apr 20, 2024
1 parent a16dc24 commit 65915d6
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions bricks/sync/waitable_atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ class WaitableAtomic {
f();
}

bool Wait(std::function<bool(const data_t&)> predicate) const {
bool Wait(std::function<bool(const data_t&)> pred = [](const data_t& e) { return static_cast<bool>(e); }) const {
std::unique_lock<std::mutex> lock(data_mutex_);
if (!predicate(data_)) {
if (!pred(data_)) {
const data_t& data = data_;
data_condition_variable_.wait(lock, [&predicate, &data] { return predicate(data); });
data_condition_variable_.wait(lock, [&pred, &data] { return pred(data); });
}
return true;
}
Expand Down Expand Up @@ -237,6 +237,16 @@ class WaitableAtomic {
return true;
}

template <typename T>
bool WaitFor(T duration) const {
std::unique_lock<std::mutex> lock(data_mutex_);
if (!static_cast<bool>(data_)) {
const data_t& data = data_;
return data_condition_variable_.wait_for(lock, duration, [&data] { return static_cast<bool>(data); });
}
return true;
}

#ifndef CURRENT_FOR_CPP14

// NOTE(dkorolev): Deliberately not bothering with C++14 for these three- and four-argument `WaitFor()`-s.
Expand Down Expand Up @@ -337,8 +347,10 @@ class WaitableAtomic {
struct WaitableAtomicSubscriberRemoverImpl final : WaitableAtomicSubscriberRemover {
WaitableAtomic& self_;
const size_t id_;
WaitableAtomicSubscriberRemoverImpl(WaitableAtomic& self, size_t id) : self_(self), id_(id) {}
void Remove() override {
// TODO(dkorolev): Use `DISALLOW_COPY_AND_ASSIGN`?!
WaitableAtomicSubscriberRemoverImpl(WaitableAtomicSubscriberRemoverImpl const&) = delete;
WaitableAtomicSubscriberRemoverImpl& operator=(WaitableAtomicSubscriberRemoverImpl const&) = delete;
WaitableAtomicSubscriberRemoverImpl(WaitableAtomic& self, size_t id) : self_(self), id_(id) {} void Remove() override {
// Okay to only lock the subscribers map, but not the data.
std::lock_guard lock(self_.subscribers_mutex_);
self_.subscribers_.erase(id_);
Expand Down

0 comments on commit 65915d6

Please sign in to comment.