rippled
Loading...
Searching...
No Matches
semaphore.h
1
29#ifndef XRPL_CORE_SEMAPHORE_H_INCLUDED
30#define XRPL_CORE_SEMAPHORE_H_INCLUDED
31
32#include <condition_variable>
33#include <mutex>
34
35namespace ripple {
36
37template <class Mutex, class CondVar>
39{
40private:
41 Mutex m_mutex;
42 CondVar m_cond;
44
45public:
47
51 explicit basic_semaphore(size_type count = 0) : m_count(count)
52 {
53 }
54
56 void
58 {
60 ++m_count;
61 m_cond.notify_one();
62 }
63
65 void
67 {
69 while (m_count == 0)
70 m_cond.wait(lock);
71 --m_count;
72 }
73
77 bool
79 {
81 if (m_count == 0)
82 return false;
83 --m_count;
84 return true;
85 }
86};
87
89
90} // namespace ripple
91
92#endif
void wait()
Block until notify is called.
Definition semaphore.h:66
basic_semaphore(size_type count=0)
Create the semaphore, with an optional initial count.
Definition semaphore.h:51
bool try_wait()
Perform a non-blocking wait.
Definition semaphore.h:78
void notify()
Increment the count and unblock one waiting thread.
Definition semaphore.h:57
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6