CppCommon
1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
include
threads
wait_ring.inl
Go to the documentation of this file.
1
9
namespace
CppCommon
{
10
11
template
<
typename
T>
12
inline
WaitRing<T>::WaitRing
(
size_t
capacity) : _closed(false), _capacity(capacity - 1), _mask(capacity - 1), _head(0), _tail(0), _ring(capacity)
13
{
14
assert((
capacity
> 1) &&
"Ring capacity must be greater than one!"
);
15
assert(((
capacity
& (
capacity
- 1)) == 0) &&
"Ring capacity must be a power of two!"
);
16
}
17
18
template
<
typename
T>
19
inline
WaitRing<T>::~WaitRing
()
20
{
21
Close();
22
}
23
24
template
<
typename
T>
25
inline
bool
WaitRing<T>::closed
()
const
26
{
27
Locker<CriticalSection>
locker(_cs);
28
return
_closed;
29
}
30
31
template
<
typename
T>
32
inline
size_t
WaitRing<T>::size
()
const
33
{
34
Locker<CriticalSection>
locker(_cs);
35
return
_head - _tail;
36
}
37
38
template
<
typename
T>
39
inline
bool
WaitRing<T>::Enqueue
(
const
T& item)
40
{
41
T temp = item;
42
return
Enqueue(std::forward<T>(temp));
43
}
44
45
template
<
typename
T>
46
inline
bool
WaitRing<T>::Enqueue
(T&& item)
47
{
48
Locker<CriticalSection>
locker(_cs);
49
50
if
(_closed)
51
return
false
;
52
53
do
54
{
55
if
(((_head - _tail + 1) & _mask) != 0)
56
{
57
_ring[_head++ & _mask] = std::move(item);
58
_cv1.NotifyOne();
59
return
true
;
60
}
61
62
_cv2.Wait(_cs, [
this
]() {
return
(_closed || (((_head - _tail + 1) & _mask) != 0)); });
63
64
}
while
(!_closed);
65
66
return
false
;
67
}
68
69
template
<
typename
T>
70
inline
bool
WaitRing<T>::Dequeue
(T& item)
71
{
72
Locker<CriticalSection>
locker(_cs);
73
74
if
(_closed && (((_head - _tail) & _mask) == 0))
75
return
false
;
76
77
do
78
{
79
if
(((_head - _tail) & _mask) != 0)
80
{
81
item = std::move(_ring[_tail++ & _mask]);
82
_cv2.NotifyOne();
83
return
true
;
84
}
85
86
_cv1.Wait(_cs, [
this
]() {
return
(_closed || (((_head - _tail) & _mask) != 0)); });
87
88
}
while
(!_closed || (((_head - _tail) & _mask) != 0));
89
90
return
false
;
91
}
92
93
template
<
typename
T>
94
inline
void
WaitRing<T>::Close
()
95
{
96
Locker<CriticalSection>
locker(_cs);
97
_closed =
true
;
98
_cv1.NotifyAll();
99
_cv2.NotifyAll();
100
}
101
102
}
// namespace CppCommon
CppCommon::Locker
Locker synchronization primitive.
Definition
locker.h:23
CppCommon::WaitRing::closed
bool closed() const
Is wait ring closed?
Definition
wait_ring.inl:25
CppCommon::WaitRing::capacity
size_t capacity() const
Get wait ring capacity.
Definition
wait_ring.h:54
CppCommon::WaitRing::WaitRing
WaitRing(size_t capacity)
Default class constructor.
Definition
wait_ring.inl:12
CppCommon::WaitRing::Enqueue
bool Enqueue(const T &item)
Enqueue an item into the wait ring.
Definition
wait_ring.inl:39
CppCommon::WaitRing::Dequeue
bool Dequeue(T &item)
Dequeue an item from the wait ring.
Definition
wait_ring.inl:70
CppCommon::WaitRing::~WaitRing
~WaitRing()
Definition
wait_ring.inl:19
CppCommon::WaitRing::size
size_t size() const
Get wait ring size.
Definition
wait_ring.inl:32
CppCommon::WaitRing::Close
void Close()
Close the wait ring.
Definition
wait_ring.inl:94
CppCommon
C++ Common project definitions.
Definition
token_bucket.h:15
Generated by
1.9.8