#include <Barrier.h>
Inheritance diagram for ACE_Barrier:
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_Barrier * | sub_barrier_ [2] |
Private Member Functions | |
void | operator= (const ACE_Barrier &) |
ACE_Barrier (const ACE_Barrier &) |
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.
|
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 } |
|
Default dtor.
Definition at line 13 of file Barrier.inl.
00014 { 00015 } |
|
|
|
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 } |
|
|
|
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.
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 } |
|
Block the caller until all
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 } |
|
Declare the dynamic allocation hooks.
Reimplemented in ACE_Thread_Barrier. |
|
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(). |
|
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(). |
|
Serialize access to the barrier state.
Definition at line 131 of file Barrier.h. Referenced by dump(). |
|
Definition at line 150 of file Barrier.h. Referenced by ACE_Barrier(), shutdown(), and wait(). |
|
We keep two Definition at line 148 of file Barrier.h. Referenced by ACE_Barrier(), and dump(). |
|
Definition at line 149 of file Barrier.h. Referenced by ACE_Barrier(), and dump(). |