xrpld
Loading...
Searching...
No Matches
semaphore.h
1
28
29#pragma once
30
31#include <condition_variable>
32#include <mutex>
33
34namespace xrpl {
35
36template <class Mutex, class CondVar>
38{
39private:
40 Mutex mutex_;
41 CondVar cond_;
43
44public:
46
50 explicit BasicSemaphore(size_type count = 0) : count_(count)
51 {
52 }
53
55 void
57 {
58 std::scoped_lock const lock{mutex_};
59 ++count_;
60 cond_.notify_one();
61 }
62
64 void
66 {
68 while (count_ == 0)
69 cond_.wait(lock);
70 --count_;
71 }
72
76 bool
78 {
80 if (count_ == 0)
81 return false;
82 --count_;
83 return true;
84 }
85};
86
88
89} // namespace xrpl
std::size_t size_type
Definition semaphore.h:45
bool tryWait()
Perform a non-blocking wait.
Definition semaphore.h:77
BasicSemaphore(size_type count=0)
Create the semaphore, with an optional initial count.
Definition semaphore.h:50
void notify()
Increment the count and unblock one waiting thread.
Definition semaphore.h:56
void wait()
Block until notify is called.
Definition semaphore.h:65
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BasicSemaphore< std::mutex, std::condition_variable > semaphore
Definition semaphore.h:87