ACE_Condition< MUTEX > Class Template Reference

ACE_Condition variable wrapper, which allows threads to block until shared data changes state. More...

#include <Condition_T.h>

Inheritance diagram for ACE_Condition< MUTEX >:

Inheritance graph
[legend]
Collaboration diagram for ACE_Condition< MUTEX >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Condition (MUTEX &m, int type=USYNC_THREAD, const ACE_TCHAR *name=0, void *arg=0)
 Initialize the condition variable.

 ~ACE_Condition (void)
 Implicitly destroy the condition variable.

int wait (const ACE_Time_Value *abstime)
int wait (void)
 Block on condition.

int wait (MUTEX &mutex, const ACE_Time_Value *abstime=0)
int signal (void)
 Signal one waiting thread.

int broadcast (void)
 Signal *all* waiting threads.

int remove (void)
 Explicitly destroy the condition variable.

MUTEX & mutex (void)
 Returns a reference to the underlying mutex_;.

void dump (void) const
 Dump the state of an object.


Protected Attributes

ACE_cond_t cond_
 Condition variable.

MUTEX & mutex_
 Reference to mutex lock.


Private Member Functions

void operator= (const ACE_Condition< MUTEX > &)
 ACE_Condition (const ACE_Condition< MUTEX > &)

Detailed Description

template<class MUTEX>
class ACE_Condition< MUTEX >

ACE_Condition variable wrapper, which allows threads to block until shared data changes state.

A condition variable enables threads to atomically block and test the condition under the protection of a mutual exclu- sion lock (mutex) until the condition is satisfied. That is, the mutex must have been held by the thread before calling wait or signal on the condition. If the condition is false, a thread blocks on a condition variable and atomically releases the mutex that is waiting for the condition to change. If another thread changes the condition, it may wake up waiting threads by signaling the associated condition variable. The waiting threads, upon awakening, reacquire the mutex and re-evaluate the condition. Note, you can only parameterize with ACE_Thread_Mutex, ACE_Recursive_Thread_Mutex, or ACE_Null_Mutex.

Definition at line 54 of file Condition_T.h.


Constructor & Destructor Documentation

template<class MUTEX>
ACE_Condition< MUTEX >::ACE_Condition MUTEX &  m,
int  type = USYNC_THREAD,
const ACE_TCHAR name = 0,
void *  arg = 0
 

Initialize the condition variable.

Definition at line 56 of file Condition_T.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TCHAR, ACE_OS::cond_init(), and LM_ERROR.

00060   :
00061     mutex_ (m)
00062 {
00063   // ACE_TRACE ("ACE_Condition<MUTEX>::ACE_Condition");
00064 
00065   if (ACE_OS::cond_init (&this->cond_,
00066                          (short) type,
00067                          name,
00068                          arg) != 0)
00069     ACE_ERROR ((LM_ERROR,
00070                 ACE_LIB_TEXT ("%p\n"),
00071                 ACE_LIB_TEXT ("ACE_Condition::ACE_Condition")));
00072 }

template<class MUTEX>
ACE_Condition< MUTEX >::~ACE_Condition void   ) 
 

Implicitly destroy the condition variable.

Definition at line 75 of file Condition_T.cpp.

References ACE_ERROR, ACE_LIB_TEXT, LM_ERROR, and ACE_Condition< MUTEX >::remove().

00076 {
00077   // ACE_TRACE ("ACE_Condition<MUTEX>::~ACE_Condition");
00078 
00079   if (this->remove () == -1)
00080     ACE_ERROR ((LM_ERROR,
00081                 ACE_LIB_TEXT ("%p\n"),
00082                 ACE_LIB_TEXT ("ACE_Condition::~ACE_Condition")));
00083 }

template<class MUTEX>
ACE_Condition< MUTEX >::ACE_Condition const ACE_Condition< MUTEX > &   )  [private]
 


Member Function Documentation

template<class MUTEX>
ACE_INLINE int ACE_Condition< MUTEX >::broadcast void   ) 
 

Signal *all* waiting threads.

Definition at line 45 of file Condition_T.inl.

References ACE_OS::cond_broadcast().

00046 {
00047 // ACE_TRACE ("ACE_Condition<MUTEX>::broadcast");
00048   return ACE_OS::cond_broadcast (&this->cond_);
00049 }

template<class MUTEX>
ACE_BEGIN_VERSIONED_NAMESPACE_DECL void ACE_Condition< MUTEX >::dump void   )  const
 

Dump the state of an object.

Reimplemented in ACE_Thread_Condition< MUTEX >, and ACE_Thread_Condition< ACE_Thread_Mutex >.

Definition at line 25 of file Condition_T.cpp.

References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, ACE_LIB_TEXT, and LM_DEBUG.

Referenced by ACE_Thread_Condition< MUTEX >::dump().

00026 {
00027 #if defined (ACE_HAS_DUMP)
00028 // ACE_TRACE ("ACE_Condition<MUTEX>::dump");
00029 
00030   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00031   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\n")));
00032   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00033 #endif /* ACE_HAS_DUMP */
00034 }

template<class MUTEX>
ACE_INLINE MUTEX & ACE_Condition< MUTEX >::mutex void   ) 
 

Returns a reference to the underlying mutex_;.

Definition at line 31 of file Condition_T.inl.

00032 {
00033   // ACE_TRACE ("ACE_Condition<MUTEX>::mutex");
00034   return this->mutex_;
00035 }

template<class MUTEX>
void ACE_Condition< MUTEX >::operator= const ACE_Condition< MUTEX > &   )  [private]
 

template<class MUTEX>
ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE int ACE_Condition< MUTEX >::remove void   ) 
 

Explicitly destroy the condition variable.

Definition at line 8 of file Condition_T.inl.

References ACE_OS::cond_broadcast(), ACE_OS::cond_destroy(), EBUSY, and ACE_OS::thr_yield().

Referenced by ACE_Condition< MUTEX >::~ACE_Condition(), and ACE_Condition< ACE_Recursive_Thread_Mutex >::~ACE_Condition().

00009 {
00010   // ACE_TRACE ("ACE_Condition<MUTEX>::remove");
00011 
00012   // cond_destroy() is called in a loop if the condition variable is
00013   // BUSY.  This avoids a condition where a condition is signaled and
00014   // because of some timing problem, the thread that is to be signaled
00015   // has called the cond_wait routine after the signal call.  Since
00016   // the condition signal is not queued in any way, deadlock occurs.
00017 
00018   int result = 0;
00019 
00020   while ((result = ACE_OS::cond_destroy (&this->cond_)) == -1
00021           && errno == EBUSY)
00022     {
00023       ACE_OS::cond_broadcast (&this->cond_);
00024       ACE_OS::thr_yield ();
00025     }
00026 
00027   return result;
00028 }

template<class MUTEX>
ACE_INLINE int ACE_Condition< MUTEX >::signal void   ) 
 

Signal one waiting thread.

Definition at line 38 of file Condition_T.inl.

References ACE_OS::cond_signal().

00039 {
00040 // ACE_TRACE ("ACE_Condition<MUTEX>::signal");
00041   return ACE_OS::cond_signal (&this->cond_);
00042 }

template<class MUTEX>
int ACE_Condition< MUTEX >::wait MUTEX &  mutex,
const ACE_Time_Value abstime = 0
 

Block on condition or until absolute time-of-day has passed. If abstime == 0 use "blocking" wait() semantics on the passed as a parameter (this is useful if you need to store the in shared memory). Else, if != 0 and the call times out before the condition is signaled returns -1 and sets errno to ETIME.

Definition at line 94 of file Condition_T.cpp.

References ACE_OS::cond_timedwait(), and ACE_Condition< MUTEX >::wait().

00096 {
00097 // ACE_TRACE ("ACE_Condition<MUTEX>::wait");
00098   if (abstime == 0)
00099     return this->wait ();
00100   else
00101     {
00102       return ACE_OS::cond_timedwait (&this->cond_,
00103                                      &mutex.lock_,
00104                                      (ACE_Time_Value *) abstime);
00105     }
00106 }

template<class MUTEX>
int ACE_Condition< MUTEX >::wait void   ) 
 

Block on condition.

Definition at line 86 of file Condition_T.cpp.

References ACE_OS::cond_wait().

Referenced by ACE_Condition< MUTEX >::wait(), and ACE_Condition< ACE_Recursive_Thread_Mutex >::wait().

00087 {
00088   // ACE_TRACE ("ACE_Condition<MUTEX>::wait");
00089   return ACE_OS::cond_wait (&this->cond_,
00090                             &this->mutex_.lock_);
00091 }

template<class MUTEX>
int ACE_Condition< MUTEX >::wait const ACE_Time_Value abstime  ) 
 

Block on condition, or until absolute time-of-day has passed. If abstime == 0 use "blocking" semantics. Else, if != 0 and the call times out before the condition is signaled returns -1 and sets errno to ETIME.

Definition at line 113 of file Condition_T.cpp.

References ACE_Condition< MUTEX >::wait().

00114 {
00115 // ACE_TRACE ("ACE_Condition<MUTEX>::wait");
00116   return this->wait (this->mutex_, abstime);
00117 }


Member Data Documentation

template<class MUTEX>
ACE_cond_t ACE_Condition< MUTEX >::cond_ [protected]
 

Condition variable.

Definition at line 108 of file Condition_T.h.

template<class MUTEX>
MUTEX& ACE_Condition< MUTEX >::mutex_ [protected]
 

Reference to mutex lock.

Definition at line 111 of file Condition_T.h.


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:20:44 2006 for ACE by doxygen 1.3.6