24 #include <core/threading/barrier.h>
25 #include <core/exception.h>
31 #if defined(_POSIX_BARRIERS) && (_POSIX_BARRIERS - 200112L) >= 0
32 # define USE_POSIX_BARRIERS
34 # undef USE_POSIX_BARRIERS
35 # include <core/threading/mutex.h>
36 # include <core/threading/wait_condition.h>
46 #ifdef USE_POSIX_BARRIERS
47 pthread_barrier_t barrier;
49 BarrierData() : threads_left(0), mutex(), waitcond(&mutex) {}
51 unsigned int threads_left;
53 WaitCondition waitcond;
119 throw Exception(
"Barrier count must be at least 1");
121 barrier_data =
new BarrierData();
122 #ifdef USE_POSIX_BARRIERS
123 pthread_barrier_init( &(barrier_data->barrier), NULL,
_count );
125 barrier_data->threads_left =
_count;
144 #ifdef USE_POSIX_BARRIERS
145 pthread_barrier_destroy( &(barrier_data->barrier) );
159 #ifdef USE_POSIX_BARRIERS
160 pthread_barrier_wait( &(barrier_data->barrier) );
162 barrier_data->mutex.lock();
164 if ( --barrier_data->threads_left == 0 ) {
165 barrier_data->threads_left =
_count;
166 barrier_data->mutex.unlock();
167 barrier_data->waitcond.wake_all();
169 barrier_data->waitcond.wait();
170 barrier_data->mutex.unlock();
unsigned int count()
Get number of threads this barrier will wait for.
virtual void wait()
Wait for other threads.
Barrier()
Protected Constructor.
Base class for exceptions in Fawkes.
unsigned int _count
Number of threads that are expected to wait for the barrier.
virtual ~Barrier()
Destructor.