19 #ifndef LUNCHBOX_MONITOR_H
20 #define LUNCHBOX_MONITOR_H
22 #include <lunchbox/condition.h>
23 #include <lunchbox/scopedMutex.h>
31 #include <boost/bind.hpp>
47 void bool_true()
const {}
54 explicit Monitor(
const T& value ) : _value( value ) {}
84 {
set( value );
return *
this; }
88 {
set( from._value );
return *
this; }
109 void set(
const T& value )
126 return _waitPredicate( boost::bind( std::equal_to<T>(), value, _1 ));
136 return _waitPredicate( boost::bind( std::not_equal_to<T>(), value, _1));
144 const T
waitNE(
const T& v1,
const T& v2 )
const
146 if(
sizeof( T ) <= 8 )
148 const T current = _value;
149 if( current != v1 && current != v2 )
153 while( _value == v1 || _value == v2 )
165 return _waitPredicate( boost::bind( std::greater_equal<T>(), _1,
175 {
return _waitPredicate( boost::bind( std::less_equal<T>(), _1, value )); }
183 {
return _waitPredicate( boost::bind( std::greater<T>(), _1, value )); }
191 {
return _waitPredicate( boost::bind( std::less<T>(), _1, value )); }
204 return _timedWaitPredicate( boost::bind( std::equal_to<T>(), _1,
217 return _timedWaitPredicate( boost::bind( std::not_equal_to<T>(), _1,
230 return _timedWaitPredicate( boost::bind( std::greater_equal<T>(), _1,
243 return _timedWaitPredicate( boost::bind( std::less_equal<T>(), _1,
256 return _timedWaitPredicate( boost::bind( std::greater<T>(), _1,
269 return _timedWaitPredicate( boost::bind( std::less<T>(), _1, value ),
277 bool operator == (
const T& value )
const
280 return _value == value;
282 bool operator != (
const T& value )
const
285 return _value != value;
287 bool operator < (
const T& value )
const
290 return _value < value;
292 bool operator > (
const T& value )
const
295 return _value > value;
297 bool operator <= (
const T& value )
const
300 return _value <= value;
302 bool operator >= (
const T& value )
const
305 return _value >= value;
308 bool operator == (
const Monitor<T>& rhs )
const
311 return _value == rhs._value;
313 bool operator != (
const Monitor<T>& rhs )
const
316 return _value != rhs._value;
318 bool operator < ( const Monitor<T>& rhs )
const
321 return _value < rhs._value;
323 bool operator > (
const Monitor<T>& rhs )
const
326 return _value > rhs._value;
328 bool operator <= ( const Monitor<T>& rhs )
const
331 return _value <= rhs._value;
333 bool operator >= (
const Monitor<T>& rhs )
const
336 return _value >= rhs._value;
352 const T&
get()
const {
return _value; }
358 return _value + value;
365 return static_cast< T
>( _value | value );
372 return static_cast< T
>( _value & value );
380 template<
typename F >
381 const T _waitPredicate(
const F& predicate )
const
383 if(
sizeof( T ) <= 8 )
385 const T current = _value;
386 if( predicate( current ))
390 while( !predicate( _value ))
395 template<
typename F >
396 bool _timedWaitPredicate(
const F& predicate,
const uint32_t timeout )
const
398 if(
sizeof( T ) <= 8 )
400 const T current = _value;
401 if( predicate( current ))
405 while( !predicate( _value ))
420 template<
typename T >
424 os <<
"Monitor< " << monitor.
get() <<
" >";
459 #include <servus/uint128_t.h>
465 #endif //LUNCHBOX_MONITOR_H
const T waitGE(const T &value) const
Block until the monitor has a value greater or equal to the given value.
bool timedWaitLT(const T &value, const uint32_t timeout) const
Block until the monitor has a value less than the given value.
Basic type definitions not provided by the operating system.
A condition variable and associated lock.
ScopedMutex< Condition, WriteOp > ScopedCondition
A scoped mutex for a write operation on a condition.
const T & operator->() const
const T waitLT(const T &value) const
Block until the monitor has a value less than the given value.
Monitor< uint32_t > Monitoru
An unsigned 32bit integer monitor.
T operator|(const T &value) const
Monitor(const T &value)
Construct a new monitor with a given default value.
bool timedWaitGT(const T &value, const uint32_t timeout) const
Block until the monitor has a value greater than the given value.
const T waitLE(const T &value) const
Block until the monitor has a value less or equal to the given value.
bool timedWait(const uint32_t timeout)
Atomically unlock the mutex, wait for a signal and relock the mutex.
Monitor & operator--()
Decrement the monitored value, prefix only.
const T waitNE(const T &value) const
Block until the monitor has not the given value.
bool timedWaitGE(const T &value, const uint32_t timeout) const
Block until the monitor has a value greater or equal to the given value.
Monitor< bool > Monitorb
A boolean monitor variable.
const T waitNE(const T &v1, const T &v2) const
Block until the monitor has none of the given values.
void broadcast()
Broadcast the condition.
bool timedWaitNE(const T &value, const uint32_t timeout) const
Block until the monitor has not the given value.
Monitor(const Monitor< T > &from)
Ctor initializing with the given monitor value.
Monitor & operator|=(const T &value)
Perform an or operation on the value.
Monitor & operator=(const T &value)
Assign a new value.
Monitor & operator&=(const T &value)
Perform an and operation on the value.
T operator&(const T &value) const
Monitor()
Construct a new monitor with a default value of 0.
Abstraction layer and common utilities for multi-threaded programming.
bool timedWaitEQ(const T &value, const uint32_t timeout) const
Block until the monitor has the given value.
void set(const T &value)
Set a new value.
std::ostream & operator<<(std::ostream &os, const Array< T > &array)
Pretty-print all members of the array.
bool timedWaitLE(const T &value, const uint32_t timeout) const
Block until the monitor has a value less or equal to the given value.
void wait()
Atomically unlock the mutex, wait for a signal and relock the mutex.
const T waitGT(const T &value) const
Block until the monitor has a value greater than the given value.
Monitor & operator++()
Increment the monitored value, prefix only.
T operator+(const T &value) const
~Monitor()
Destructs the monitor.
const T waitEQ(const T &value) const
Block until the monitor has the given value.