xrpld
Loading...
Searching...
No Matches
semaphore.h
1
28
29#pragma once
30
31#include <condition_variable>
32#include <cstddef>
33#include <mutex>
34
35namespace xrpl {
36
37template <class Mutex, class CondVar>
39{
40private:
41 Mutex mutex_;
42 CondVar cond_;
44
45public:
47
52 explicit BasicSemaphore(size_type count = 0) : count_(count)
53 {
54 }
55
59 void
61 {
62 std::scoped_lock const lock{mutex_};
63 ++count_;
64 cond_.notify_one();
65 }
66
70 void
72 {
74 while (count_ == 0)
75 cond_.wait(lock);
76 --count_;
77 }
78
83 bool
85 {
87 if (count_ == 0)
88 return false;
89 --count_;
90 return true;
91 }
92};
93
95
96} // namespace xrpl
std::size_t size_type
Definition semaphore.h:46
bool tryWait()
Perform a non-blocking wait.
Definition semaphore.h:84
BasicSemaphore(size_type count=0)
Create the semaphore, with an optional initial count.
Definition semaphore.h:52
void notify()
Increment the count and unblock one waiting thread.
Definition semaphore.h:60
void wait()
Block until notify is called.
Definition semaphore.h:71
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:94