24 #include <core/exception.h>
25 #include <core/threading/barrier.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>
45 #ifdef USE_POSIX_BARRIERS
46 pthread_barrier_t barrier;
48 BarrierData() : threads_left(0), mutex(), waitcond(&mutex)
52 unsigned int threads_left;
54 WaitCondition waitcond;
118 throw Exception(
"Barrier count must be at least 1");
120 barrier_data =
new BarrierData();
121 #ifdef USE_POSIX_BARRIERS
122 pthread_barrier_init(&(barrier_data->barrier), NULL,
_count);
124 barrier_data->threads_left =
_count;
141 #ifdef USE_POSIX_BARRIERS
142 pthread_barrier_destroy(&(barrier_data->barrier));
155 #ifdef USE_POSIX_BARRIERS
156 pthread_barrier_wait(&(barrier_data->barrier));
158 barrier_data->mutex.lock();
160 if (--barrier_data->threads_left == 0) {
161 barrier_data->threads_left =
_count;
162 barrier_data->mutex.unlock();
163 barrier_data->waitcond.wake_all();
165 barrier_data->waitcond.wait();
166 barrier_data->mutex.unlock();
virtual ~Barrier()
Destructor.
unsigned int _count
Number of threads that are expected to wait for the barrier.
virtual void wait()
Wait for other threads.
unsigned int count()
Get number of threads this barrier will wait for.
Barrier()
Protected Constructor.
Base class for exceptions in Fawkes.
Fawkes library namespace.