Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Wefts::Condition Class Reference
[Conditions]

Class implementing a condition variable. More...

#include <wefts_cond.h>

Inheritance diagram for Wefts::Condition:

Inheritance graph
[legend]
Collaboration diagram for Wefts::Condition:

Collaboration graph
[legend]
List of all members.

Public Methods

 Condition (Mutex *mtx=0)
 Initializes the internal data of the condition.

 ~Condition ()
virtual bool wait ()
 Waits for the condition to be active.

void setMutex (Mutex *mtx)
virtual bool wait (long seconds, long nanoseconds)
 Waits for the condition to be active up to a certain time.

int wait (double seconds)
 Waits for the condition to be active for a certain time.

virtual void signal ()
 Signals the condition.

void onStop (CleanupHandler *guard=0, int param=0)
 Set up cancelation handler.


Protected Attributes

OSCondition m_cond
 Internal condition variable.

Mutexm_my_mutex
 Internal mutex.

CleanupItem m_whenStopped

Detailed Description

Class implementing a condition variable.

This condition class is meant to be overloaded by classes using specific mutex types. To use it, you have to create your own class using a combination of this class plus a fast or a reentrant mutex. This is implemented in the wtFastCondition and wtRCondition classes.

Subclassess must set m_my_mutex to a valid Wefts::Mutex pointer (or any subclass of that).

All condition classes are implemented via inline calls to maximize execution speed; also, their metods are extremely small.


Constructor & Destructor Documentation

Wefts::Condition::Condition Mutex   mtx = 0 [inline]
 

Initializes the internal data of the condition.

Initialization of m_my_mutex member is left to subclasses.

Wefts::Condition::~Condition   [inline]
 


Member Function Documentation

void Wefts::Condition::onStop CleanupHandler   guard = 0,
int    param = 0
[inline]
 

Set up cancelation handler.

When a thread waiting on a condition is canceled the thread cleanup system is called with the condition in locked() status. It is necessary to unlock it before to proceed; so it is necessary to provide a CleanupHandler. If you have access to the calling thread you can use Thread::pushCleanup(), while if you have not access to it you should use this method (that is anyhow more efficient).

See also:
Condition cleanup

void Wefts::Condition::setMutex Mutex   mtx [inline]
 

virtual void Wefts::Condition::signal   [inline, virtual]
 

Signals the condition.

When a condition is signaled all the waiting threads are given a chance to go.

int Wefts::Condition::wait double    seconds [inline]
 

Waits for the condition to be active for a certain time.

Works as wait( long seconds, long nanoseconds), but the wait time can be expressed as a real number, 1.0 meaning a second.

Be sure to 1) don't stop a thread while waiting on a condition, 2) disable cancelation around a wait() or 3) set up a cancelation sequence (Condition cleanup).

ams seconds time to wait for (seconds and fraction of seconds expressed as decimal part)
Returns:
0 if condition is signaled, non zero (posix pthread error) otherwise.

virtual bool Wefts::Condition::wait long    seconds,
long    nanoseconds
[inline, virtual]
 

Waits for the condition to be active up to a certain time.

This method works as wait() but returns (with nonzero value) if a certain time has elapsed.

If you call this function, you have to specify the amount of seconds and second fractions (expressed in nanoseconds) you want to wait for a single event to be risen. Granularity of the system greatly depends on underlying OS and process priviledges.

Be sure to 1) don't stop a thread while waiting on a condition, 2) disable cancelation around a wait() or 3) set up a cancelation sequence (Condition cleanup).

Parameters:
seconds  integer count of seconds to wait for.
nanoseconds  integer count of nanoseconds to wait for (maximum is 999,999,999).
Returns:
true if condition is signaled, false on error.

virtual bool Wefts::Condition::wait   [inline, virtual]
 

Waits for the condition to be active.

This method waits forever or until stopped by an external event (signal).

Be sure to 1) don't stop a thread while waiting on a condition, 2) disable cancelation around a wait() or 3) set up a cancelation sequence (Condition cleanup).

Returns:
true if condition is signaled, false on error.


Member Data Documentation

OSCondition Wefts::Condition::m_cond [protected]
 

Internal condition variable.

Mutex* Wefts::Condition::m_my_mutex [protected]
 

Internal mutex.

Subclasses are invited to set this mutex to an initialized specific kind of mutex at creation.

CleanupItem Wefts::Condition::m_whenStopped [protected]
 


The documentation for this class was generated from the following file:
Generated on Mon Dec 22 04:12:36 2003 for Wefts by doxygen1.2.18