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 ) {}
115 void set(
const T& value )
132 return _waitPredicate(
133 boost::bind( std::equal_to< T >(), value, _1 ));
143 return _waitPredicate(
144 boost::bind( std::not_equal_to< T >(), value, _1 ));
152 const T
waitNE(
const T& v1,
const T& v2 )
const
154 if(
sizeof( T ) <= 8 )
156 const T current = _value;
157 if( current != v1 && current != v2 )
161 while( _value == v1 || _value == v2 )
173 return _waitPredicate(
174 boost::bind( std::greater_equal< T >(), _1, value ));
183 return _waitPredicate(
184 boost::bind( std::less_equal< T >(), _1, value ));
194 return _waitPredicate(
195 boost::bind( std::greater< T >(), _1, value ));
204 return _waitPredicate(
205 boost::bind( std::less< T >(), _1, value ));
219 return _timedWaitPredicate(
220 boost::bind( std::equal_to< T >(), _1, value ), timeout);
232 return _timedWaitPredicate(
233 boost::bind( std::not_equal_to< T >(), _1, value ), timeout);
245 return _timedWaitPredicate(
246 boost::bind( std::greater_equal< T >(), _1, value ), timeout);
258 return _timedWaitPredicate(
259 boost::bind( std::less_equal< T >(), _1, value ), timeout);
271 return _timedWaitPredicate(
272 boost::bind( std::greater< T >(), _1, value ), timeout);
284 return _timedWaitPredicate(
285 boost::bind( std::less< T >(), _1, value ), timeout);
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;
307 bool operator > (
const T& value )
const
310 return _value > value;
312 bool operator <= (
const T& value )
const
315 return _value <= value;
317 bool operator >= (
const T& value )
const
320 return _value >= 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;
338 bool operator > (
const Monitor<T>& rhs )
const
341 return _value > rhs._value;
343 bool operator <= ( const Monitor<T>& rhs )
const
346 return _value <= rhs._value;
348 bool operator >= (
const Monitor<T>& rhs )
const
351 return _value >= rhs._value;
367 const T&
get()
const {
return _value; }
373 return _value + value;
380 return static_cast< T
>( _value | value );
387 return static_cast< T
>( _value & value );
395 template<
typename F >
396 const T _waitPredicate(
const F& predicate )
const
398 if(
sizeof( T ) <= 8 )
400 const T current = _value;
401 if( predicate( current ))
405 while( !predicate( _value ))
410 template<
typename F >
411 bool _timedWaitPredicate(
const F& predicate,
const uint32_t timeout )
const
413 if(
sizeof( T ) <= 8 )
415 const T current = _value;
416 if( predicate( current ))
420 while( !predicate( _value ))
435 template<
typename T >
439 os <<
"Monitor< " << monitor.
get() <<
" >";
474 #include <servus/uint128_t.h>
480 #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.
LUNCHBOX_API bool timedWait(const uint32_t timeout)
Atomically unlock the mutex, wait for a signal and relock the mutex.
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.
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.
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.
LUNCHBOX_API void broadcast()
Broadcast the condition.
LUNCHBOX_API void wait()
Atomically unlock the mutex, wait for a signal and relock the mutex.
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.
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.