ACE_Barrier Class Reference

Implements "barrier synchronization". More...

#include <Barrier.h>

Inheritance diagram for ACE_Barrier:

Inheritance graph
[legend]
Collaboration diagram for ACE_Barrier:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACE_Barrier (unsigned int count, const ACE_TCHAR *name=0, void *arg=0)
 Initialize the barrier to synchronize count threads.

 ~ACE_Barrier (void)
 Default dtor.

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


Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.


Protected Attributes

ACE_Thread_Mutex lock_
 Serialize access to the barrier state.

int current_generation_
int count_
 Total number of threads that can be waiting at any one time.

ACE_Sub_Barrier sub_barrier_1_
ACE_Sub_Barrier sub_barrier_2_
ACE_Sub_Barriersub_barrier_ [2]

Private Member Functions

void operator= (const ACE_Barrier &)
 ACE_Barrier (const ACE_Barrier &)

Detailed Description

Implements "barrier synchronization".

This class allows number of threads to synchronize their completion of (one round of) a task, which is known as "barrier synchronization". After all the threads call <wait()> on the barrier they are all atomically released and can begin a new round.

This implementation uses a "sub-barrier generation numbering" scheme to avoid overhead and to ensure that all threads wait to leave the barrier correct. This code is based on an article from SunOpsis Vol. 4, No. 1 by Richard Marejka (Richard.Marejka@canada.sun.com).

Definition at line 96 of file Barrier.h.


Constructor & Destructor Documentation

ACE_Barrier::ACE_Barrier unsigned int  count,
const ACE_TCHAR name = 0,
void *  arg = 0
 

Initialize the barrier to synchronize count threads.

Definition at line 68 of file Barrier.cpp.

References ACE_TCHAR, ACE_TRACE, sub_barrier_, sub_barrier_1_, and sub_barrier_2_.

00071   : lock_ (name, (ACE_mutexattr_t *) arg),
00072     current_generation_ (0),
00073     count_ (count),
00074     sub_barrier_1_ (count, lock_, name, arg),
00075     sub_barrier_2_ (count, lock_, name, arg)
00076 {
00077   ACE_TRACE ("ACE_Barrier::ACE_Barrier");
00078   this->sub_barrier_[0] = &this->sub_barrier_1_;
00079   this->sub_barrier_[1] = &this->sub_barrier_2_;
00080 }

ACE_INLINE ACE_Barrier::~ACE_Barrier void   ) 
 

Default dtor.

Definition at line 13 of file Barrier.inl.

00014 {
00015 }

ACE_Barrier::ACE_Barrier const ACE_Barrier  )  [private]
 


Member Function Documentation

void ACE_Barrier::dump void   )  const
 

Dump the state of an object.

Reimplemented in ACE_Thread_Barrier.

Definition at line 53 of file Barrier.cpp.

References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, ACE_LIB_TEXT, ACE_Sub_Barrier::dump(), ACE_Thread_Mutex::dump(), LM_DEBUG, lock_, sub_barrier_1_, and sub_barrier_2_.

Referenced by ACE_Thread_Barrier::dump().

00054 {
00055 #if defined (ACE_HAS_DUMP)
00056 // ACE_TRACE ("ACE_Barrier::dump");
00057 
00058   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00059   this->lock_.dump ();
00060   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("current_generation_ = %d"), this->current_generation_));
00061   ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ncount_ = %d"), this->count_));
00062   this->sub_barrier_1_.dump ();
00063   this->sub_barrier_2_.dump ();
00064   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00065 #endif /* ACE_HAS_DUMP */
00066 }

void ACE_Barrier::operator= const ACE_Barrier  )  [private]
 

int ACE_Barrier::shutdown void   ) 
 

Shut the barrier down, aborting the wait of all waiting threads. Any threads waiting on the barrier when it is shut down will return with value -1, errno ESHUTDOWN.

Return values:
0 for success, -1 if already shut down.
Since:
ACE beta 5.4.9.

Definition at line 132 of file Barrier.cpp.

References ACE_GUARD_RETURN, ACE_TRACE, ACE_Sub_Barrier::barrier_finished_, ACE_Condition_Thread_Mutex::broadcast(), count_, current_generation_, ESHUTDOWN, ACE_Sub_Barrier::running_threads_, and sub_barrier_.

00133 {
00134   ACE_TRACE ("ACE_Barrier::shutdown");
00135   ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1);
00136 
00137   ACE_Sub_Barrier *sbp =
00138     this->sub_barrier_[this->current_generation_];
00139 
00140   // Check for shutdown...
00141   if (sbp == 0)
00142     {
00143       errno = ESHUTDOWN;
00144       return -1;
00145     }
00146 
00147   // Flag the shutdown
00148   this->sub_barrier_[0] = 0;
00149   this->sub_barrier_[1] = 0;
00150   // Tell all the threads waiting on the barrier to continue on their way.
00151   sbp->running_threads_ = this->count_;
00152   sbp->barrier_finished_.broadcast ();
00153 
00154   return 0;
00155 }

int ACE_Barrier::wait void   ) 
 

Block the caller until all count threads have called wait and then allow all the caller threads to continue in parallel.

Return values:
0 after successfully waiting for all threads to wait. -1 if an error occurs or the barrier is shut down (
See also:
shutdown ()).

Definition at line 83 of file Barrier.cpp.

References ACE_GUARD_RETURN, ACE_TRACE, ACE_Sub_Barrier::barrier_finished_, ACE_Condition_Thread_Mutex::broadcast(), count_, current_generation_, ESHUTDOWN, ACE_Sub_Barrier::running_threads_, sub_barrier_, and ACE_Condition_Thread_Mutex::wait().

00084 {
00085   ACE_TRACE ("ACE_Barrier::wait");
00086   ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1);
00087 
00088   ACE_Sub_Barrier *sbp =
00089     this->sub_barrier_[this->current_generation_];
00090 
00091   // Check for shutdown...
00092   if (sbp == 0)
00093     {
00094       errno = ESHUTDOWN;
00095       return -1;
00096     }
00097 
00098   int retval = 0;
00099 
00100   if (sbp->running_threads_ == 1)
00101     {
00102       // We're the last running thread, so swap generations and tell
00103       // all the threads waiting on the barrier to continue on their
00104       // way.
00105       sbp->running_threads_ = this->count_;
00106       // Swap generations.
00107       this->current_generation_ = 1 - this->current_generation_;
00108       sbp->barrier_finished_.broadcast ();
00109     }
00110   else
00111     {
00112       --sbp->running_threads_;
00113 
00114       // Block until all the other threads wait().
00115       while (sbp->running_threads_ != this->count_)
00116         sbp->barrier_finished_.wait ();
00117 
00118       // We're awake and the count has completed. See if it completed
00119       // because all threads hit the barrier, or because the barrier
00120       // was shut down.
00121       if (this->sub_barrier_[this->current_generation_] == 0)
00122         {
00123           errno = ESHUTDOWN;
00124           retval = -1;
00125         }
00126     }
00127 
00128   return retval;
00129 }


Member Data Documentation

ACE_Barrier::ACE_ALLOC_HOOK_DECLARE
 

Declare the dynamic allocation hooks.

Reimplemented in ACE_Thread_Barrier.

Definition at line 127 of file Barrier.h.

int ACE_Barrier::count_ [protected]
 

Total number of threads that can be waiting at any one time.

Definition at line 138 of file Barrier.h.

Referenced by shutdown(), and wait().

int ACE_Barrier::current_generation_ [protected]
 

Either 0 or 1, depending on whether we are the first generation of waiters or the next generation of waiters.

Definition at line 135 of file Barrier.h.

Referenced by shutdown(), and wait().

ACE_Thread_Mutex ACE_Barrier::lock_ [protected]
 

Serialize access to the barrier state.

Definition at line 131 of file Barrier.h.

Referenced by dump().

ACE_Sub_Barrier* ACE_Barrier::sub_barrier_[2] [protected]
 

Definition at line 150 of file Barrier.h.

Referenced by ACE_Barrier(), shutdown(), and wait().

ACE_Sub_Barrier ACE_Barrier::sub_barrier_1_ [protected]
 

We keep two sub_barriers, one for the first "generation" of waiters, and one for the next "generation" of waiters. This efficiently solves the problem of what to do if all the first generation waiters don't leave the barrier before one of the threads calls wait() again (i.e., starts up the next generation barrier).

Definition at line 148 of file Barrier.h.

Referenced by ACE_Barrier(), and dump().

ACE_Sub_Barrier ACE_Barrier::sub_barrier_2_ [protected]
 

Definition at line 149 of file Barrier.h.

Referenced by ACE_Barrier(), and dump().


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