Lunchbox  1.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lunchbox::Monitor< T > Class Template Reference

A monitor primitive. More...

#include <monitor.h>

+ Collaboration diagram for lunchbox::Monitor< T >:

Public Member Functions

 Monitor ()
 Construct a new monitor with a default value of 0. More...
 
 Monitor (const T &value)
 Construct a new monitor with a given default value. More...
 
 Monitor (const Monitor< T > &from)
 Ctor initializing with the given monitor value. More...
 
 ~Monitor ()
 Destructs the monitor. More...
 
template<>
Monitor< bool > & operator++ ()
 
template<>
Monitor< bool > & operator-- ()
 
template<>
Monitor< bool > & operator|= (const bool &value)
 
Changing the monitored value.
Monitoroperator++ ()
 Increment the monitored value, prefix only. More...
 
Monitoroperator-- ()
 Decrement the monitored value, prefix only. More...
 
Monitoroperator= (const T &value)
 Assign a new value. More...
 
const Monitoroperator= (const Monitor< T > &from)
 Assign a new value. More...
 
Monitoroperator|= (const T &value)
 Perform an or operation on the value. More...
 
Monitoroperator&= (const T &value)
 Perform an and operation on the value. More...
 
void set (const T &value)
 Set a new value. More...
 
Monitor the value.
const T & waitEQ (const T &value) const
 Block until the monitor has the given value. More...
 
const T waitNE (const T &value) const
 Block until the monitor has not the given value. More...
 
const T waitNE (const T &v1, const T &v2) const
 Block until the monitor has none of the given values. More...
 
const T waitGE (const T &value) const
 Block until the monitor has a value greater or equal to the given value. More...
 
const T waitLE (const T &value) const
 Block until the monitor has a value less or equal to the given value. More...
 
Monitor the value with a timeout.
bool timedWaitEQ (const T &value, const uint32_t timeout) const
 Block until the monitor has the given value. More...
 
bool timedWaitGE (const T &value, const uint32_t timeout) const
 Block until the monitor has a value greater or equal to the given value. More...
 
bool timedWaitNE (const T &value, const uint32_t timeout) const
 Block until the monitor has not the given value. More...
 
Comparison Operators. @version 1.0
bool operator== (const T &value) const
 
bool operator!= (const T &value) const
 
bool operator< (const T &value) const
 
bool operator> (const T &value) const
 
bool operator<= (const T &value) const
 
bool operator>= (const T &value) const
 
bool operator== (const Monitor< T > &rhs) const
 
bool operator!= (const Monitor< T > &rhs) const
 
bool operator< (const Monitor< T > &rhs) const
 
bool operator> (const Monitor< T > &rhs) const
 
bool operator<= (const Monitor< T > &rhs) const
 
bool operator>= (const Monitor< T > &rhs) const
 
 operator bool_t ()
 
Data Access.
const T & operator-> () const
 
const T & get () const
 
operator+ (const T &value) const
 
operator| (const T &value) const
 
operator& (const T &value) const
 

Detailed Description

template<class T>
class lunchbox::Monitor< T >

A monitor primitive.

A monitor has a value, which can be monitored to reach a certain state. The caller is blocked until the condition is fulfilled. The concept is similar to a pthread condition, with more usage convenience.

Example:

/* Copyright (c) 2010-2012, Stefan Eilemann <eile@equalizergraphics.com>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define TEST_RUNTIME 300 // seconds
#include "test.h"
#include <lunchbox/clock.h>
#include <lunchbox/monitor.h>
#include <lunchbox/thread.h>
#include <iostream>
#define NLOOPS 200000
class Thread : public lunchbox::Thread
{
public:
virtual ~Thread() {}
virtual void run()
{
int64_t nOps = NLOOPS;
while( nOps-- )
{
monitor.waitEQ( nOps );
monitor = -nOps;
}
const float time = clock.getTimef();
std::cout << 2*NLOOPS/time << " ops/ms" << std::endl;
}
};
int main( int, char** )
{
TEST( !boolMonitor );
boolMonitor = true;
TEST( boolMonitor );
int64_t nOps = NLOOPS;
Thread waiter;
TEST( waiter.start( ));
while( nOps-- )
{
monitor = nOps;
monitor.waitEQ( -nOps );
}
const float time = clock.getTimef();
TEST( waiter.join( ));
std::cout << 2*NLOOPS/time << " ops/ms" << std::endl;
return EXIT_SUCCESS;
}

Definition at line 42 of file monitor.h.

Constructor & Destructor Documentation

template<class T>
lunchbox::Monitor< T >::Monitor ( )
inline

Construct a new monitor with a default value of 0.

Version
1.0

Definition at line 49 of file monitor.h.

template<class T>
lunchbox::Monitor< T >::Monitor ( const T &  value)
inlineexplicit

Construct a new monitor with a given default value.

Version
1.0

Definition at line 52 of file monitor.h.

template<class T>
lunchbox::Monitor< T >::Monitor ( const Monitor< T > &  from)
inline

Ctor initializing with the given monitor value.

Version
1.1.5

Definition at line 55 of file monitor.h.

template<class T>
lunchbox::Monitor< T >::~Monitor ( )
inline

Destructs the monitor.

Version
1.0

Definition at line 58 of file monitor.h.

Member Function Documentation

template<class T>
const T& lunchbox::Monitor< T >::get ( ) const
inline
Returns
the current value.
Version
1.0

Definition at line 362 of file monitor.h.

Referenced by lunchbox::operator<<().

+ Here is the caller graph for this function:

template<class T>
lunchbox::Monitor< T >::operator bool_t ( )
inline
Returns
a bool conversion of the result.
Version
1.9.1

Definition at line 349 of file monitor.h.

template<class T>
T lunchbox::Monitor< T >::operator& ( const T &  value) const
inline
Returns
the current and the given value.
Version
1.0

Definition at line 379 of file monitor.h.

template<class T>
Monitor& lunchbox::Monitor< T >::operator&= ( const T &  value)
inline

Perform an and operation on the value.

Version
1.7

Definition at line 104 of file monitor.h.

References lunchbox::Condition::broadcast().

+ Here is the call graph for this function:

template<class T>
T lunchbox::Monitor< T >::operator+ ( const T &  value) const
inline
Returns
the current plus the given value.
Version
1.0

Definition at line 365 of file monitor.h.

template<class T>
Monitor& lunchbox::Monitor< T >::operator++ ( void  )
inline

Increment the monitored value, prefix only.

Version
1.0

Definition at line 63 of file monitor.h.

References lunchbox::Condition::broadcast().

+ Here is the call graph for this function:

template<class T>
Monitor& lunchbox::Monitor< T >::operator-- ( void  )
inline

Decrement the monitored value, prefix only.

Version
1.0

Definition at line 72 of file monitor.h.

References lunchbox::Condition::broadcast().

+ Here is the call graph for this function:

template<class T>
const T& lunchbox::Monitor< T >::operator-> ( ) const
inline
Returns
the current value.
Version
1.0

Definition at line 359 of file monitor.h.

template<class T>
Monitor& lunchbox::Monitor< T >::operator= ( const T &  value)
inline

Assign a new value.

Version
1.0

Definition at line 81 of file monitor.h.

References lunchbox::Monitor< T >::set().

+ Here is the call graph for this function:

template<class T>
const Monitor& lunchbox::Monitor< T >::operator= ( const Monitor< T > &  from)
inline

Assign a new value.

Version
1.1.5

Definition at line 88 of file monitor.h.

References lunchbox::Monitor< T >::set().

+ Here is the call graph for this function:

template<class T>
T lunchbox::Monitor< T >::operator| ( const T &  value) const
inline
Returns
the current or'ed with the given value.
Version
1.0

Definition at line 372 of file monitor.h.

template<class T>
Monitor& lunchbox::Monitor< T >::operator|= ( const T &  value)
inline

Perform an or operation on the value.

Version
1.0

Definition at line 95 of file monitor.h.

References lunchbox::Condition::broadcast().

+ Here is the call graph for this function:

template<class T>
void lunchbox::Monitor< T >::set ( const T &  value)
inline

Set a new value.

Version
1.0

Definition at line 113 of file monitor.h.

References lunchbox::Condition::broadcast().

Referenced by lunchbox::Monitor< T >::operator=().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class T>
bool lunchbox::Monitor< T >::timedWaitEQ ( const T &  value,
const uint32_t  timeout 
) const
inline

Block until the monitor has the given value.

Parameters
valuethe exact value to monitor.
timeoutthe timeout in milliseconds to wait for the value.
Returns
true on success, false on timeout.
Version
1.1

Definition at line 222 of file monitor.h.

References lunchbox::Condition::timedWait().

+ Here is the call graph for this function:

template<class T>
bool lunchbox::Monitor< T >::timedWaitGE ( const T &  value,
const uint32_t  timeout 
) const
inline

Block until the monitor has a value greater or equal to the given value.

Parameters
valuethe exact value to monitor.
timeoutthe timeout in milliseconds to wait for the value.
Returns
true on success, false on timeout.
Version
1.1

Definition at line 245 of file monitor.h.

References lunchbox::Condition::timedWait().

+ Here is the call graph for this function:

template<class T>
bool lunchbox::Monitor< T >::timedWaitNE ( const T &  value,
const uint32_t  timeout 
) const
inline

Block until the monitor has not the given value.

Parameters
valuethe exact value to monitor.
timeoutthe timeout in milliseconds to wait for not the value.
Returns
true on success, false on timeout.
Version
1.3.3

Definition at line 268 of file monitor.h.

References lunchbox::Condition::timedWait().

+ Here is the call graph for this function:

template<class T>
const T& lunchbox::Monitor< T >::waitEQ ( const T &  value) const
inline

Block until the monitor has the given value.

Returns
the value when reaching the condition.
Version
1.0

Definition at line 128 of file monitor.h.

References lunchbox::Condition::wait().

+ Here is the call graph for this function:

template<class T>
const T lunchbox::Monitor< T >::waitGE ( const T &  value) const
inline

Block until the monitor has a value greater or equal to the given value.

Returns
the value when reaching the condition.
Version
1.0

Definition at line 181 of file monitor.h.

References lunchbox::Condition::wait().

+ Here is the call graph for this function:

template<class T>
const T lunchbox::Monitor< T >::waitLE ( const T &  value) const
inline

Block until the monitor has a value less or equal to the given value.

Returns
the value when reaching the condition.
Version
1.0

Definition at line 199 of file monitor.h.

References lunchbox::Condition::wait().

+ Here is the call graph for this function:

template<class T>
const T lunchbox::Monitor< T >::waitNE ( const T &  value) const
inline

Block until the monitor has not the given value.

Returns
the value when reaching the condition.
Version
1.0

Definition at line 143 of file monitor.h.

References lunchbox::Condition::wait().

+ Here is the call graph for this function:

template<class T>
const T lunchbox::Monitor< T >::waitNE ( const T &  v1,
const T &  v2 
) const
inline

Block until the monitor has none of the given values.

Returns
the value when reaching the condition.
Version
1.0

Definition at line 162 of file monitor.h.

References lunchbox::Condition::wait().

+ Here is the call graph for this function:


The documentation for this class was generated from the following file: