15 #ifndef A_UTIL_UTIL_CONCURRENCY_DETAIL_SEMAPHORE_IMPL_HEADER_INCLUDED
16 #define A_UTIL_UTIL_CONCURRENCY_DETAIL_SEMAPHORE_IMPL_HEADER_INCLUDED
22 #include <condition_variable>
31 namespace concurrency {
33 template <
typename Mutex,
typename CondVar>
35 : _mutex(), _cv(), _lock_count(count)
39 template <
typename Mutex,
typename CondVar>
42 std::unique_lock<Mutex> lock(_mutex);
47 template <
typename Mutex,
typename CondVar>
50 std::unique_lock<Mutex> lock(_mutex);
52 while (_lock_count == 0) {
58 template <
typename Mutex,
typename CondVar>
61 std::unique_lock<Mutex> lock(_mutex);
62 bool is_success =
false;
63 if (_lock_count > 0) {
70 template <
typename Mutex,
typename CondVar>
73 std::unique_lock<Mutex> lock(_mutex);
77 template <
typename Mutex,
typename CondVar>
80 std::unique_lock<Mutex> lock(_mutex);
81 return _lock_count > 0;
84 template <
typename Mutex,
typename CondVar>
85 template <
typename Rep,
typename Period>
87 const std::chrono::duration<Rep, Period>& timeout)
89 if (timeout.count() < 0) {
90 throw std::invalid_argument(
"timeout.count() < 0");
96 std::chrono::duration_cast<std::chrono::microseconds>(timeout).count();
98 std::unique_lock<Mutex> lock(_mutex);
100 bool is_timeout =
false;
101 while (_lock_count == 0 && !is_timeout) {
109 if (_lock_count > 0) {
Semaphore implementation, combining a mutex and a condition variable to manage a counter.
void notify()
Increment the counter and notify any waiters.
bool is_set()
Check whether the counter is set.
void wait()
Decrement the counter, blocks until the count becomes non-zero (if neccessary)
void reset()
Reset the counter to 0.
bool try_wait()
Try decrementing the counter.
bool wait_for(const std::chrono::duration< Rep, Period > &timeout)
Wait for a specified duration of time and decrement the counter afterwards.
std::int64_t timestamp_t
Type of a timestamp value. If not otherwise stated, always in microseconds.
std::chrono::microseconds microseconds
Compatibility to C++11 std::chrono::microseconds
timestamp_t getCurrentMicroseconds()
Get the current microseconds passed since the first invocation (or -1 if not available)
Serves as the root component, with common functionality documented in core functionality.
Public API for basic_semaphore type.
Public API for system classes and functions.