2222/* ****************************************/ REACT_BEGIN /* ****************************************/
2323
2424// /////////////////////////////////////////////////////////////////////////////////////////////////
25- // / SyncPoint
25+ // / A synchronization primitive that is essentially a cooperative semaphore, i.e. it blocks a
26+ // / consumer until all producers are done.
27+ // / Usage:
28+ // / SyncPoint sp;
29+ // / // Add a dependency the sync point should wait for. The depenency is released on destruction.
30+ // / SyncPoint::Depenency dep(sp);
31+ // / // Pass dependency to some operation. Dependencies can be copied as well.
32+ // / SomeAsyncOperation(std::move(dep));
33+ // / // Wait until all dependencies are released.
34+ // / sp.Wait()
2635// /////////////////////////////////////////////////////////////////////////////////////////////////
2736class SyncPoint
2837{
@@ -104,6 +113,7 @@ class SyncPoint
104113 };
105114
106115public:
116+ // / Creates a sync point.
107117 SyncPoint () :
108118 state_ ( std::make_shared<SyncPointState>() )
109119 { }
@@ -116,36 +126,42 @@ class SyncPoint
116126
117127 SyncPoint& operator =(SyncPoint&&) = default ;
118128
129+ // / Blocks the calling thread until all dependencies of this sync point are released.
119130 void Wait ()
120131 {
121132 state_->Wait ();
122133 }
123134
135+ // / Like Wait, but times out after relTime. Returns false if the timeout was hit.
124136 template <typename TRep, typename TPeriod>
125137 bool WaitFor (const std::chrono::duration<TRep, TPeriod>& relTime)
126138 {
127139 return state_->WaitFor (relTime);
128140 }
129141
142+ // / Like Wait, but times out at relTime. Returns false if the timeout was hit.
130143 template <typename TRep, typename TPeriod>
131144 bool WaitUntil (const std::chrono::duration<TRep, TPeriod>& relTime)
132145 {
133146 return state_->WaitUntil (relTime);
134147 }
135148
149+ // / A RAII-style token object that represents a dependency of a SyncPoint.
136150 class Dependency
137151 {
138152 public:
153+ // / Constructs an unbound dependency.
139154 Dependency () = default ;
140155
141- // Construct from single sync point.
156+ // / Constructs a single dependency for a sync point.
142157 explicit Dependency (const SyncPoint& sp) :
143158 target_( sp.state_ )
144159 {
145160 target_->IncrementWaitCount ();
146161 }
147162
148- // Construct from vector of other dependencies.
163+ // / Merges an input range of other dependencies into a single dependency.
164+ // / This allows to create APIs that are agnostic of how many dependent operations they process.
149165 template <typename TBegin, typename TEnd>
150166 Dependency (TBegin first, TEnd last)
151167 {
@@ -172,6 +188,8 @@ class SyncPoint
172188 }
173189 }
174190
191+ // / Copy constructor and assignment split a dependency.
192+ // / The new dependency that has the same target(s) as other.
175193 Dependency (const Dependency& other) :
176194 target_ ( other.target_ )
177195 {
@@ -191,6 +209,8 @@ class SyncPoint
191209 return *this ;
192210 }
193211
212+ // / Move constructor and assignment transfer a dependency.
213+ // / The moved from object is left unbound.
194214 Dependency (Dependency&& other) :
195215 target_ ( std::move(other.target_) )
196216 { }
@@ -204,12 +224,14 @@ class SyncPoint
204224 return *this ;
205225 }
206226
227+ // / The destructor releases a dependency, if it's not unbound.
207228 ~Dependency ()
208229 {
209230 if (target_)
210231 target_->DecrementWaitCount ();
211232 }
212233
234+ // / Manually releases the dependency. Afterwards it is unbound.
213235 void Release ()
214236 {
215237 if (target_)
@@ -219,6 +241,7 @@ class SyncPoint
219241 }
220242 }
221243
244+ // / Returns if a dependency is released, i.e. if it is unbound.
222245 bool IsReleased () const
223246 { return target_ == nullptr ; }
224247
0 commit comments