QuantumLibrary
quantum_condition_variable.h
1 /*
2 ** Copyright 2018 Bloomberg Finance L.P.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 #ifndef BLOOMBERG_QUANTUM_CONDITION_VARIABLE_H
17 #define BLOOMBERG_QUANTUM_CONDITION_VARIABLE_H
18 
19 #include <list>
20 #include <atomic>
21 #include <quantum/quantum_mutex.h>
22 #include <quantum/quantum_yielding_thread.h>
23 #include <quantum/interface/quantum_icontext.h>
24 #include <quantum/quantum_traits.h>
25 
26 namespace Bloomberg {
27 namespace quantum {
28 
29 //==============================================================================================
30 // class ConditionVariable
31 //==============================================================================================
37 {
38 public:
41 
43  ConditionVariable(const ConditionVariable& other) = delete;
44 
46  ConditionVariable(ConditionVariable&& other) = delete;
47 
49  ConditionVariable& operator=(const ConditionVariable& other) = delete;
50 
53 
59 
61  void notifyOne();
62 
64  void notifyAll();
65 
70  void wait(Mutex& mutex);
71 
77  void wait(ICoroSync::Ptr sync,
78  Mutex& mutex);
79 
94  template <class PREDICATE = bool()>
95  void wait(Mutex& mutex,
96  PREDICATE predicate);
97 
114  template <class PREDICATE = bool()>
115  void wait(ICoroSync::Ptr sync,
116  Mutex& mutex,
117  PREDICATE predicate);
118 
128  template <class REP, class PERIOD>
129  bool waitFor(Mutex& mutex,
130  const std::chrono::duration<REP, PERIOD>& time);
131 
142  template <class REP, class PERIOD>
143  bool waitFor(ICoroSync::Ptr sync,
144  Mutex& mutex,
145  const std::chrono::duration<REP, PERIOD>& time);
146 
166  template <class REP, class PERIOD, class PREDICATE = bool()>
167  bool waitFor(Mutex& mutex,
168  const std::chrono::duration<REP, PERIOD>& time,
169  PREDICATE predicate);
170 
191  template <class REP, class PERIOD, class PREDICATE = bool()>
192  bool waitFor(ICoroSync::Ptr sync,
193  Mutex& mutex,
194  const std::chrono::duration<REP, PERIOD>& time,
195  PREDICATE predicate);
196 
197 private:
198  template <class YIELDING>
199  void waitImpl(YIELDING&& yield,
200  Mutex& mutex,
201  std::atomic_int& signal);
202 
203  template <class YIELDING, class PREDICATE = bool()>
204  void waitImpl(YIELDING&& yield,
205  Mutex& mutex,
206  PREDICATE predicate,
207  std::atomic_int& signal);
208 
209  template <class YIELDING, class REP, class PERIOD>
210  bool waitForImpl(YIELDING&& yield,
211  Mutex& mutex,
212  std::chrono::duration<REP, PERIOD>& time,
213  std::atomic_int& signal);
214 
215  template <class YIELDING, class REP, class PERIOD, class PREDICATE = bool()>
216  bool waitForImpl(YIELDING&& yield,
217  Mutex& mutex,
218  const std::chrono::duration<REP, PERIOD>& time,
219  PREDICATE predicate,
220  std::atomic_int& signal);
221 
222  //MEMBERS
223  Mutex _thisLock; //sync access to this object
224  std::list<std::atomic_int*> _waiters;
225  std::atomic_bool _destroyed;
226 };
227 
228 }}
229 
230 #include <quantum/impl/quantum_condition_variable_impl.h>
231 
232 #endif //BLOOMBERG_QUANTUM_CONDITION_VARIABLE_H
void notifyOne()
Notify one waiting thread or coroutine.
Definition: quantum_condition_variable_impl.h:40
Definition: quantum_buffer_impl.h:22
void notifyAll()
Notify all waiting threads and coroutines.
Definition: quantum_condition_variable_impl.h:53
std::shared_ptr< ICoroSync > Ptr
Definition: quantum_icoro_sync.h:36
~ConditionVariable()
Destructor.
Definition: quantum_condition_variable_impl.h:33
This class represents a coroutine-compatible implementation of the std::condition_variable....
Definition: quantum_condition_variable.h:36
ConditionVariable()
Default constructor.
Definition: quantum_condition_variable_impl.h:28
bool waitFor(Mutex &mutex, const std::chrono::duration< REP, PERIOD > &time)
Block the current thread until the condition is signalled via notifyOne() or notifyAll() or until 'ti...
Definition: quantum_condition_variable_impl.h:92
ConditionVariable & operator=(const ConditionVariable &other)=delete
void wait(Mutex &mutex)
Block the current thread until the condition is signalled via notifyOne() or notifyAll().
Definition: quantum_condition_variable_impl.h:65