rippled
ripple
core
impl
semaphore.h
1
//------------------------------------------------------------------------------
2
/*
3
This file is part of rippled: https://github.com/ripple/rippled
4
Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6
Permission to use, copy, modify, and/or distribute this software for any
7
purpose with or without fee is hereby granted, provided that the above
8
copyright notice and this permission notice appear in all copies.
9
10
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
//==============================================================================
19
20
#ifndef RIPPLE_CORE_SEMAPHORE_H_INCLUDED
21
#define RIPPLE_CORE_SEMAPHORE_H_INCLUDED
22
23
#include <
condition_variable
>
24
#include <
mutex
>
25
26
namespace
ripple
{
27
28
template
<
class
Mutex,
class
CondVar>
29
class
basic_semaphore
30
{
31
private
:
32
Mutex
m_mutex
;
33
CondVar
m_cond
;
34
std::size_t
m_count
;
35
36
public
:
37
using
size_type
=
std::size_t
;
38
42
explicit
basic_semaphore
(
size_type
count = 0) :
m_count
(count)
43
{
44
}
45
47
void
48
notify
()
49
{
50
std::lock_guard
lock{
m_mutex
};
51
++
m_count
;
52
m_cond
.notify_one();
53
}
54
56
void
57
wait
()
58
{
59
std::unique_lock
lock{
m_mutex
};
60
while
(
m_count
== 0)
61
m_cond
.wait(lock);
62
--
m_count
;
63
}
64
68
bool
69
try_wait
()
70
{
71
std::lock_guard
lock{
m_mutex
};
72
if
(
m_count
== 0)
73
return
false
;
74
--
m_count
;
75
return
true
;
76
}
77
};
78
79
using
semaphore
=
basic_semaphore<std::mutex, std::condition_variable>
;
80
81
}
// namespace ripple
82
83
#endif
std::lock_guard
STL class.
ripple::basic_semaphore::m_mutex
Mutex m_mutex
Definition:
semaphore.h:32
ripple::basic_semaphore::m_cond
CondVar m_cond
Definition:
semaphore.h:33
ripple::basic_semaphore
Definition:
semaphore.h:29
ripple::basic_semaphore::wait
void wait()
Block until notify is called.
Definition:
semaphore.h:57
std::unique_lock
STL class.
ripple::basic_semaphore::basic_semaphore
basic_semaphore(size_type count=0)
Create the semaphore, with an optional initial count.
Definition:
semaphore.h:42
ripple::basic_semaphore::notify
void notify()
Increment the count and unblock one waiting thread.
Definition:
semaphore.h:48
ripple::basic_semaphore::try_wait
bool try_wait()
Perform a non-blocking wait.
Definition:
semaphore.h:69
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
RCLCensorshipDetector.h:29
ripple::basic_semaphore::m_count
std::size_t m_count
Definition:
semaphore.h:34
condition_variable
mutex
std::size_t
Generated by
1.8.17