xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
core
detail
semaphore.h
1
28
29
#pragma once
30
31
#include <
condition_variable
>
32
#include <
cstddef
>
33
#include <
mutex
>
34
35
namespace
xrpl
{
36
37
template
<
class
Mutex,
class
CondVar>
38
class
BasicSemaphore
39
{
40
private
:
41
Mutex
mutex_
;
42
CondVar
cond_
;
43
std::size_t
count_
;
44
45
public
:
46
using
size_type
=
std::size_t
;
47
52
explicit
BasicSemaphore
(
size_type
count = 0) :
count_
(count)
53
{
54
}
55
59
void
60
notify
()
61
{
62
std::scoped_lock
const
lock{
mutex_
};
63
++
count_
;
64
cond_
.notify_one();
65
}
66
70
void
71
wait
()
72
{
73
std::unique_lock
lock{
mutex_
};
74
while
(
count_
== 0)
75
cond_
.wait(lock);
76
--
count_
;
77
}
78
83
bool
84
tryWait
()
85
{
86
std::scoped_lock
lock{
mutex_
};
87
if
(
count_
== 0)
88
return
false
;
89
--
count_
;
90
return
true
;
91
}
92
};
93
94
using
Semaphore
=
BasicSemaphore<std::mutex, std::condition_variable>
;
95
96
}
// namespace xrpl
xrpl::BasicSemaphore
Definition
semaphore.h:39
xrpl::BasicSemaphore::size_type
std::size_t size_type
Definition
semaphore.h:46
xrpl::BasicSemaphore::tryWait
bool tryWait()
Perform a non-blocking wait.
Definition
semaphore.h:84
xrpl::BasicSemaphore< std::mutex, std::condition_variable >::cond_
std::condition_variable cond_
Definition
semaphore.h:42
xrpl::BasicSemaphore::BasicSemaphore
BasicSemaphore(size_type count=0)
Create the semaphore, with an optional initial count.
Definition
semaphore.h:52
xrpl::BasicSemaphore::notify
void notify()
Increment the count and unblock one waiting thread.
Definition
semaphore.h:60
xrpl::BasicSemaphore< std::mutex, std::condition_variable >::mutex_
std::mutex mutex_
Definition
semaphore.h:41
xrpl::BasicSemaphore< std::mutex, std::condition_variable >::count_
std::size_t count_
Definition
semaphore.h:43
xrpl::BasicSemaphore::wait
void wait()
Block until notify is called.
Definition
semaphore.h:71
condition_variable
cstddef
mutex
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::Semaphore
BasicSemaphore< std::mutex, std::condition_variable > Semaphore
Definition
semaphore.h:94
std::scoped_lock
std::size_t
std::unique_lock
Generated by
1.17.0