QuantumLibrary
Bloomberg::quantum::ConditionVariable Class Reference

This class represents a coroutine-compatible implementation of the std::condition_variable. Most methods of the latter have been recreated with the same behavior. This object will yield instead of blocking if called from a coroutine. More...

#include <quantum_condition_variable.h>

Public Member Functions

 ConditionVariable ()
 Default constructor. More...
 
 ConditionVariable (const ConditionVariable &other)=delete
 
 ConditionVariable (ConditionVariable &&other)=delete
 
ConditionVariableoperator= (const ConditionVariable &other)=delete
 
ConditionVariableoperator= (ConditionVariable &&other)=delete
 
 ~ConditionVariable ()
 Destructor. More...
 
void notifyOne ()
 Notify one waiting thread or coroutine. More...
 
void notifyAll ()
 Notify all waiting threads and coroutines. More...
 
void wait (Mutex &mutex)
 Block the current thread until the condition is signalled via notifyOne() or notifyAll(). More...
 
void wait (ICoroSync::Ptr sync, Mutex &mutex)
 Yield the current coroutine until the condition is signalled via notifyOne() or notifyAll(). More...
 
template<class PREDICATE = bool()>
void wait (Mutex &mutex, PREDICATE predicate)
 Block the current thread until the condition is signalled via notifyOne() or notifyAll(). More...
 
template<class PREDICATE = bool()>
void wait (ICoroSync::Ptr sync, Mutex &mutex, PREDICATE predicate)
 Yield the current coroutine until the condition is signalled via notifyOne() or notifyAll(). When this function returns, the mutex is guaranteed to be locked. More...
 
template<class REP , class PERIOD >
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 'time' duration expires. More...
 
template<class REP , class PERIOD >
bool waitFor (ICoroSync::Ptr sync, Mutex &mutex, const std::chrono::duration< REP, PERIOD > &time)
 Block the current thread until the condition is signalled via notifyOne() or notifyAll() or until 'time' duration expires. More...
 
template<class REP , class PERIOD , class PREDICATE = bool()>
bool waitFor (Mutex &mutex, const std::chrono::duration< REP, PERIOD > &time, PREDICATE predicate)
 Block the current thread until the condition is signalled via notifyOne() or notifyAll() or until 'time' duration expires. More...
 
template<class REP , class PERIOD , class PREDICATE = bool()>
bool waitFor (ICoroSync::Ptr sync, Mutex &mutex, const std::chrono::duration< REP, PERIOD > &time, PREDICATE predicate)
 Block the current thread until the condition is signalled via notifyOne() or notifyAll() or until 'time' duration expires. More...
 

Detailed Description

This class represents a coroutine-compatible implementation of the std::condition_variable. Most methods of the latter have been recreated with the same behavior. This object will yield instead of blocking if called from a coroutine.

Constructor & Destructor Documentation

◆ ConditionVariable() [1/3]

Bloomberg::quantum::ConditionVariable::ConditionVariable ( )
inline

Default constructor.

◆ ConditionVariable() [2/3]

Bloomberg::quantum::ConditionVariable::ConditionVariable ( const ConditionVariable other)
delete
Warning
Copy constructor is explicitly deleted.

◆ ConditionVariable() [3/3]

Bloomberg::quantum::ConditionVariable::ConditionVariable ( ConditionVariable &&  other)
delete
Warning
Move constructor is explicitly deleted.

◆ ~ConditionVariable()

Bloomberg::quantum::ConditionVariable::~ConditionVariable ( )
inline

Destructor.

Warning
Deleting this object while there are still waiting threads results in undefined behavior. Ensure that all threads have been notified before doing so. Once the destructor is called, no other wait attempts should be made on this object.

Member Function Documentation

◆ notifyAll()

void Bloomberg::quantum::ConditionVariable::notifyAll ( )
inline

Notify all waiting threads and coroutines.

◆ notifyOne()

void Bloomberg::quantum::ConditionVariable::notifyOne ( )
inline

Notify one waiting thread or coroutine.

◆ operator=() [1/2]

ConditionVariable& Bloomberg::quantum::ConditionVariable::operator= ( const ConditionVariable other)
delete
Warning
Assignment operator is explicitly deleted.

◆ operator=() [2/2]

ConditionVariable& Bloomberg::quantum::ConditionVariable::operator= ( ConditionVariable &&  other)
delete
Warning
Move assignment operator is explicitly deleted.

◆ wait() [1/4]

void Bloomberg::quantum::ConditionVariable::wait ( Mutex mutex)
inline

Block the current thread until the condition is signalled via notifyOne() or notifyAll().

When this function returns, the mutex is guaranteed to be locked.

Parameters
[in]mutexMutex object which is locked by the current thread.
Note
This function should be called from a regular thread not from a coroutine.

◆ wait() [2/4]

void Bloomberg::quantum::ConditionVariable::wait ( ICoroSync::Ptr  sync,
Mutex mutex 
)
inline

Yield the current coroutine until the condition is signalled via notifyOne() or notifyAll().

When this function returns, the mutex is guaranteed to be locked.

Parameters
[in]syncPointer to a coroutine synchronization object.
[in]mutexMutex object which is locked by the current coroutine.
Note
This function should be called from a coroutine.

◆ wait() [3/4]

template<class PREDICATE >
void Bloomberg::quantum::ConditionVariable::wait ( Mutex mutex,
PREDICATE  predicate 
)

Block the current thread until the condition is signalled via notifyOne() or notifyAll().

When this function returns, the mutex is guaranteed to be locked. This function calls wait() in a loop until the predicate returns true. This ensures that the condition has not changed after notifyOne() or notifyAll() have been called. The internal logic is equivalent to:

while(!predicate())
{
wait(mutex);
}
Template Parameters
PREDICATECallable function type having the following signature 'bool f()'.
Parameters
[in]mutexMutex object which is locked by the current coroutine.
[in]predicateFunction or functor to be tested as exit condition of the endless while loop.
Note
This function should be called from a regular thread not from a coroutine.

◆ wait() [4/4]

template<class PREDICATE >
void Bloomberg::quantum::ConditionVariable::wait ( ICoroSync::Ptr  sync,
Mutex mutex,
PREDICATE  predicate 
)

Yield the current coroutine until the condition is signalled via notifyOne() or notifyAll(). When this function returns, the mutex is guaranteed to be locked.

When this function returns, the mutex is guaranteed to be locked. This function calls wait() in a loop until the predicate returns true. This ensures that the condition has not changed after notifyOne() or notifyAll() have been called. The internal logic is equivalent to:

while(!predicate())
{
wait(mutex);
}
Template Parameters
PREDICATECallable function type having the following signature 'bool f()'.
Parameters
[in]syncPointer to a coroutine synchronization object.
[in]mutexMutex object which is locked by the current coroutine.
[in]predicateFunction or functor to be tested as exit condition of the endless while loop.
Note
This function should be called from a coroutine.

◆ waitFor() [1/4]

template<class REP , class PERIOD >
bool Bloomberg::quantum::ConditionVariable::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 'time' duration expires.

When this function returns, the mutex is guaranteed to be locked.

Template Parameters
REPAn arithmetic type such as int or double representing the number of ticks.
PERIODA std::ratio representing the tick period such as ticks per second.
Parameters
[in]mutexMutex object which is locked by the current coroutine.
[in]timeMaximum duration for which to wait on this condition.
Returns
True if the mutex was acquired before 'time' expired or false otherwise.
Note
This function should be called from a regular thread not from a coroutine.

◆ waitFor() [2/4]

template<class REP , class PERIOD >
bool Bloomberg::quantum::ConditionVariable::waitFor ( ICoroSync::Ptr  sync,
Mutex mutex,
const std::chrono::duration< REP, PERIOD > &  time 
)

Block the current thread until the condition is signalled via notifyOne() or notifyAll() or until 'time' duration expires.

When this function returns, the mutex is guaranteed to be locked.

Template Parameters
REPAn arithmetic type such as int or double representing the number of ticks.
PERIODA std::ratio representing the tick period such as ticks per second.
Parameters
[in]syncPointer to a coroutine synchronization object.
[in]mutexMutex object which is locked by the current coroutine.
[in]timeMaximum duration for which to wait on this condition.
Returns
True if the mutex was acquired before 'time' expired or false otherwise.
Note
This function should be called from a coroutine.

◆ waitFor() [3/4]

template<class REP , class PERIOD , class PREDICATE >
bool Bloomberg::quantum::ConditionVariable::waitFor ( Mutex mutex,
const std::chrono::duration< REP, PERIOD > &  time,
PREDICATE  predicate 
)

Block the current thread until the condition is signalled via notifyOne() or notifyAll() or until 'time' duration expires.

When this function returns, the mutex is guaranteed to be locked. This function calls wait() in a loop until the predicate returns true or 'time' expires. This ensures that the condition has not changed after notifyOne() or notifyAll() have been called. The internal logic is equivalent to:

while(!predicate())
{
waitFor(mutex, time);
}
Template Parameters
REPAn arithmetic type such as int or double representing the number of ticks.
PERIODA std::ratio representing the tick period such as ticks per second.
PREDICATECallable function type having the following signature 'bool f()'.
Parameters
[in]mutexMutex object which is locked by the current coroutine.
[in]timeMaximum duration for which to wait on this condition.
[in]predicateFunction or functor to be tested as exit condition of the endless while loop.
Returns
True if the mutex was acquired before 'time' expired, otherwise the predicate result after timeout.
Note
This function should be called from a regular thread not from a coroutine.

◆ waitFor() [4/4]

template<class REP , class PERIOD , class PREDICATE >
bool Bloomberg::quantum::ConditionVariable::waitFor ( ICoroSync::Ptr  sync,
Mutex mutex,
const std::chrono::duration< REP, PERIOD > &  time,
PREDICATE  predicate 
)

Block the current thread until the condition is signalled via notifyOne() or notifyAll() or until 'time' duration expires.

When this function returns, the mutex is guaranteed to be locked. This function calls wait() in a loop until the predicate returns true or 'time' expires. This ensures that the condition has not changed after notifyOne() or notifyAll() have been called. The internal logic is equivalent to:

while(!predicate())
{
waitFor(mutex, time);
}
Template Parameters
REPAn arithmetic type such as int or double representing the number of ticks.
PERIODA std::ratio representing the tick period such as ticks per second.
PREDICATECallable function type having the following signature 'bool f()'.
Parameters
[in]syncPointer to a coroutine synchronization object.
[in]mutexMutex object which is locked by the current coroutine.
[in]timeMaximum duration for which to wait on this condition.
[in]predicateFunction or functor to be tested as exit condition of the endless while loop.
Returns
True if the mutex was acquired before 'time' expired, otherwise the predicate result after timeout.
Note
This function should be called from a coroutine.