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 T
set(
const T& value )
112 const T old = _value;
128 return _waitPredicate( boost::bind( std::equal_to<T>(), value, _1 ));
138 return _waitPredicate( boost::bind( std::not_equal_to<T>(), value, _1));
146 const T
waitNE(
const T& v1,
const T& v2 )
const 148 if(
sizeof( T ) <= 8 )
150 const T current = _value;
151 if( current != v1 && current != v2 )
155 while( _value == v1 || _value == v2 )
167 return _waitPredicate( boost::bind( std::greater_equal<T>(), _1,
177 {
return _waitPredicate( boost::bind( std::less_equal<T>(), _1, value )); }
185 {
return _waitPredicate( boost::bind( std::greater<T>(), _1, value )); }
193 {
return _waitPredicate( boost::bind( std::less<T>(), _1, value )); }
206 return _timedWaitPredicate( boost::bind( std::equal_to<T>(), _1,
219 return _timedWaitPredicate( boost::bind( std::not_equal_to<T>(), _1,
232 return _timedWaitPredicate( boost::bind( std::greater_equal<T>(), _1,
245 return _timedWaitPredicate( boost::bind( std::less_equal<T>(), _1,
258 return _timedWaitPredicate( boost::bind( std::greater<T>(), _1,
271 return _timedWaitPredicate( boost::bind( std::less<T>(), _1, value ),
279 bool operator == (
const T& value )
const 282 return _value == value;
284 bool operator != (
const T& value )
const 287 return _value != value;
289 bool operator < (
const T& value )
const 292 return _value < value;
294 bool operator > (
const T& value )
const 297 return _value > value;
299 bool operator <= (
const T& value )
const 302 return _value <= value;
304 bool operator >= (
const T& value )
const 307 return _value >= value;
310 bool operator == (
const Monitor<T>& rhs )
const 313 return _value == rhs._value;
315 bool operator != (
const Monitor<T>& rhs )
const 318 return _value != rhs._value;
320 bool operator < ( const Monitor<T>& rhs )
const 323 return _value < rhs._value;
325 bool operator > (
const Monitor<T>& rhs )
const 328 return _value > rhs._value;
330 bool operator <= ( const Monitor<T>& rhs )
const 333 return _value <= rhs._value;
335 bool operator >= (
const Monitor<T>& rhs )
const 338 return _value >= rhs._value;
354 const T&
get()
const {
return _value; }
360 return _value + value;
367 return static_cast< T
>( _value | value );
374 return static_cast< T
>( _value & value );
382 template<
typename F >
383 const T _waitPredicate(
const F& predicate )
const 385 if(
sizeof( T ) <= 8 )
387 const T current = _value;
388 if( predicate( current ))
392 while( !predicate( _value ))
397 template<
typename F >
398 bool _timedWaitPredicate(
const F& predicate,
const uint32_t timeout )
const 400 if(
sizeof( T ) <= 8 )
402 const T current = _value;
403 if( predicate( current ))
407 while( !predicate( _value ))
422 template<
typename T >
426 os <<
"Monitor< " << monitor.
get() <<
" >";
461 #include <servus/uint128_t.h> 467 #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.
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.
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.