LogCabin
|
Similar to std::condition_variable but with improvements for testing, support for monotonic clocks, and less buggy. More...
#include <ConditionVariable.h>
Public Member Functions | |
ConditionVariable () | |
~ConditionVariable () | |
void | notify_one () |
void | notify_all () |
void | wait (std::unique_lock< std::mutex > &lockGuard) |
void | wait (std::unique_lock< Core::Mutex > &lockGuard) |
void | wait_until (std::unique_lock< std::mutex > &lockGuard, const Core::Time::SteadyClock::time_point &abs_time) |
template<typename Clock , typename Duration > | |
void | wait_until (std::unique_lock< std::mutex > &lockGuard, const std::chrono::time_point< Clock, Duration > &abs_time) |
template<typename Clock , typename Duration > | |
void | wait_until (std::unique_lock< Core::Mutex > &lockGuard, const std::chrono::time_point< Clock, Duration > &abs_time) |
Public Attributes | |
std::atomic< uint64_t > | notificationCount |
The number of times this condition variable has been notified. | |
Core::Time::SteadyClock::time_point | lastWaitUntil |
In the last call to wait_until, the timeout that the caller provided (in terms of SteadyClock). | |
Private Attributes | |
pthread_cond_t | cv |
Underlying condition variable. | |
std::function< void()> | callback |
This function will be called with the lock released during every invocation of wait/wait_until. |
Similar to std::condition_variable but with improvements for testing, support for monotonic clocks, and less buggy.
For testing, you can set a callback to be called when the condition variable is waited on; instead of waiting, this callback will be called. This callback can, for example, change some shared state so that the calling thread's condition is satisfied. It also counts how many times the condition variable has been notified.
The interface to this class is a subset of std::condition_variable:
This class also goes through some trouble so that it can be used with std::unique_lock<Core::Mutex>, since normal condition variables may only be used with std::unique_lock<std::mutex>.
All waiting on this class is done using a monotonic clock internally, so it will not be affected by time jumps from, e.g., NTP. This implies that, if you're actually waiting for a specific system time to come around, you might end up with surprising behavior. If you're wondering why, Linux/POSIX condition variables use a single clock for all waiters, whereas C++11 uses an impossible-to-implement interface where different waiters can use different clocks.
Definition at line 59 of file ConditionVariable.h.
Definition at line 25 of file ConditionVariable.cc.
Definition at line 50 of file ConditionVariable.cc.
Definition at line 58 of file ConditionVariable.cc.
Definition at line 67 of file ConditionVariable.cc.
void LogCabin::Core::ConditionVariable::wait | ( | std::unique_lock< std::mutex > & | lockGuard | ) |
Definition at line 76 of file ConditionVariable.cc.
void LogCabin::Core::ConditionVariable::wait | ( | std::unique_lock< Core::Mutex > & | lockGuard | ) |
Definition at line 91 of file ConditionVariable.cc.
void LogCabin::Core::ConditionVariable::wait_until | ( | std::unique_lock< std::mutex > & | lockGuard, |
const Core::Time::SteadyClock::time_point & | abs_time | ||
) |
Definition at line 109 of file ConditionVariable.cc.
void LogCabin::Core::ConditionVariable::wait_until | ( | std::unique_lock< std::mutex > & | lockGuard, |
const std::chrono::time_point< Clock, Duration > & | abs_time | ||
) | [inline] |
Definition at line 76 of file ConditionVariable.h.
void LogCabin::Core::ConditionVariable::wait_until | ( | std::unique_lock< Core::Mutex > & | lockGuard, |
const std::chrono::time_point< Clock, Duration > & | abs_time | ||
) | [inline] |
Definition at line 96 of file ConditionVariable.h.
pthread_cond_t LogCabin::Core::ConditionVariable::cv [private] |
Underlying condition variable.
Definition at line 115 of file ConditionVariable.h.
std::function<void()> LogCabin::Core::ConditionVariable::callback [private] |
This function will be called with the lock released during every invocation of wait/wait_until.
No wait will actually occur; this is only used for unit testing.
Definition at line 121 of file ConditionVariable.h.
std::atomic<uint64_t> LogCabin::Core::ConditionVariable::notificationCount |
The number of times this condition variable has been notified.
Definition at line 126 of file ConditionVariable.h.
In the last call to wait_until, the timeout that the caller provided (in terms of SteadyClock).
This is used in some unit tests to check that timeouts are set correctly.
Definition at line 133 of file ConditionVariable.h.