forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncpoint.h
More file actions
258 lines (207 loc) · 7.48 KB
/
syncpoint.h
File metadata and controls
258 lines (207 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright Sebastian Jeckel 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef REACT_COMMON_SYNCPOINT_H_INCLUDED
#define REACT_COMMON_SYNCPOINT_H_INCLUDED
#pragma once
#include "react/detail/defs.h"
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <iterator>
#include <mutex>
#include <vector>
/*****************************************/ REACT_BEGIN /*****************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// A synchronization primitive that is essentially a cooperative semaphore, i.e. it blocks a
/// consumer until all producers are done.
/// Usage:
/// SyncPoint sp;
/// // Add a dependency the sync point should wait for. The depenency is released on destruction.
/// SyncPoint::Depenency dep(sp);
/// // Pass dependency to some operation. Dependencies can be copied as well.
/// SomeAsyncOperation(std::move(dep));
/// // Wait until all dependencies are released.
/// sp.Wait()
///////////////////////////////////////////////////////////////////////////////////////////////////
class SyncPoint
{
public:
class Dependency;
private:
class ISyncTarget
{
public:
virtual ~ISyncTarget() = default;
virtual void IncrementWaitCount() = 0;
virtual void DecrementWaitCount() = 0;
};
class SyncPointState : public ISyncTarget
{
public:
virtual void IncrementWaitCount() override
{// mutex_
std::lock_guard<std::mutex> scopedLock(mtx_);
++waitCount_;
}// ~mutex_
virtual void DecrementWaitCount() override
{// mutex_
std::lock_guard<std::mutex> scopedLock(mtx_);
--waitCount_;
if (waitCount_ == 0)
cv_.notify_all();
}// ~mutex_
void Wait()
{
std::unique_lock<std::mutex> lock(mtx_);
cv_.wait(lock, [this] { return waitCount_ == 0; });
}
template <typename TRep, typename TPeriod>
bool WaitFor(const std::chrono::duration<TRep, TPeriod>& relTime)
{
std::unique_lock<std::mutex> lock(mtx_);
return cv_.wait_for(lock, relTime, [this] { return waitCount_ == 0; });
}
template <typename TRep, typename TPeriod>
bool WaitUntil(const std::chrono::duration<TRep, TPeriod>& relTime)
{
std::unique_lock<std::mutex> lock(mtx_);
return cv_.wait_until(lock, relTime, [this] { return waitCount_ == 0; });
}
private:
std::mutex mtx_;
std::condition_variable cv_;
int waitCount_ = 0;
};
class SyncTargetCollection : public ISyncTarget
{
public:
virtual void IncrementWaitCount() override
{
for (const auto& e : targets)
e->IncrementWaitCount();
}
virtual void DecrementWaitCount() override
{
for (const auto& e : targets)
e->DecrementWaitCount();
}
std::vector<std::shared_ptr<ISyncTarget>> targets;
};
public:
/// Creates a sync point.
SyncPoint() :
state_( std::make_shared<SyncPointState>() )
{ }
SyncPoint(const SyncPoint&) = default;
SyncPoint& operator=(const SyncPoint&) = default;
SyncPoint(SyncPoint&&) = default;
SyncPoint& operator=(SyncPoint&&) = default;
/// Blocks the calling thread until all dependencies of this sync point are released.
void Wait()
{
state_->Wait();
}
/// Like Wait, but times out after relTime. Returns false if the timeout was hit.
template <typename TRep, typename TPeriod>
bool WaitFor(const std::chrono::duration<TRep, TPeriod>& relTime)
{
return state_->WaitFor(relTime);
}
/// Like Wait, but times out at relTime. Returns false if the timeout was hit.
template <typename TRep, typename TPeriod>
bool WaitUntil(const std::chrono::duration<TRep, TPeriod>& relTime)
{
return state_->WaitUntil(relTime);
}
/// A RAII-style token object that represents a dependency of a SyncPoint.
class Dependency
{
public:
/// Constructs an unbound dependency.
Dependency() = default;
/// Constructs a single dependency for a sync point.
explicit Dependency(const SyncPoint& sp) :
target_( sp.state_ )
{
target_->IncrementWaitCount();
}
/// Merges an input range of other dependencies into a single dependency.
/// This allows to create APIs that are agnostic of how many dependent operations they process.
template <typename TBegin, typename TEnd>
Dependency(TBegin first, TEnd last)
{
auto count = std::distance(first, last);
if (count == 1)
{
target_ = first->target_;
if (target_)
target_->IncrementWaitCount();
}
else if (count > 1)
{
auto collection = std::make_shared<SyncTargetCollection>();
collection->targets.reserve(count);
for (; !(first == last); ++first)
if (first->target_) // There's no point in propagating released/empty dependencies.
collection->targets.push_back(first->target_);
collection->IncrementWaitCount();
target_ = std::move(collection);
}
}
/// Copy constructor and assignment split a dependency.
/// The new dependency that has the same target(s) as other.
Dependency(const Dependency& other) :
target_( other.target_ )
{
if (target_)
target_->IncrementWaitCount();
}
Dependency& operator=(const Dependency& other)
{
if (other.target_)
other.target_->IncrementWaitCount();
if (target_)
target_->DecrementWaitCount();
target_ = other.target_;
return *this;
}
/// Move constructor and assignment transfer a dependency.
/// The moved from object is left unbound.
Dependency(Dependency&& other) :
target_( std::move(other.target_) )
{ }
Dependency& operator=(Dependency&& other)
{
if (target_)
target_->DecrementWaitCount();
target_ = std::move(other.target_);
return *this;
}
/// The destructor releases a dependency, if it's not unbound.
~Dependency()
{
if (target_)
target_->DecrementWaitCount();
}
/// Manually releases the dependency. Afterwards it is unbound.
void Release()
{
if (target_)
{
target_->DecrementWaitCount();
target_ = nullptr;
}
}
/// Returns if a dependency is released, i.e. if it is unbound.
bool IsReleased() const
{ return target_ == nullptr; }
private:
std::shared_ptr<ISyncTarget> target_;
};
private:
std::shared_ptr<SyncPointState> state_;
};
/******************************************/ REACT_END /******************************************/
#endif // REACT_COMMON_SYNCPOINT_H_INCLUDED