20 #if defined(ION_PLATFORM_WINDOWS)
45 #if defined(ION_PLATFORM_WINDOWS)
68 : thread_count_(thread_count),
72 is_valid_(thread_count_ > 0) {
74 turnstile1_ = CreateSemaphore(NULL, 0, thread_count_, NULL);
75 turnstile2_ = CreateSemaphore(NULL, 0, thread_count_, NULL);
85 CloseHandle(turnstile2_);
86 CloseHandle(turnstile1_);
90 void Barrier::Wait() {
91 if (IsValid() && thread_count_ > 1) {
93 WaitInternal(1, thread_count_, turnstile1_);
97 WaitInternal(-1, 0, turnstile2_);
101 void Barrier::WaitInternal(int32 increment, int32 limit, HANDLE turnstile) {
102 if ((wait_count_ += increment) == limit) {
104 BOOL status = ReleaseSemaphore(turnstile, thread_count_ - 1, NULL);
108 DWORD status = WaitForSingleObject(turnstile, INFINITE);
110 assert(status == WAIT_OBJECT_0);
114 #elif defined(ION_PLATFORM_LINUX) || defined(ION_PLATFORM_QNX)
123 Barrier::Barrier(uint32 thread_count)
125 is_valid_(!pthread_barrier_init(&barrier_, NULL, thread_count)) {}
127 Barrier::~Barrier() {
131 while (waiting_count_ != 0) {}
132 pthread_barrier_destroy(&barrier_);
136 void Barrier::Wait() {
139 pthread_barrier_wait(&barrier_);
156 Barrier::Barrier(uint32 thread_count)
157 : thread_count_(thread_count),
160 is_valid_(thread_count_ > 0) {
162 pthread_cond_init(&condition_, NULL);
163 pthread_cond_init(&exit_condition_, NULL);
164 pthread_mutex_init(&mutex_, NULL);
170 pthread_mutex_lock(&mutex_);
171 if (thread_count_ > 1 && --exit_count_) {
173 pthread_cond_wait(&exit_condition_, &mutex_);
175 pthread_mutex_unlock(&mutex_);
176 pthread_cond_destroy(&condition_);
177 pthread_cond_destroy(&exit_condition_);
178 pthread_mutex_destroy(&mutex_);
184 pthread_mutex_lock(&mutex_);
186 if (++wait_count_ == thread_count_) {
188 exit_count_ = thread_count_ + 1;
189 pthread_cond_broadcast(&condition_);
192 pthread_cond_wait(&condition_, &mutex_);
199 if (--exit_count_ == 0)
200 pthread_cond_broadcast(&exit_condition_);
201 pthread_mutex_unlock(&mutex_);
void Wait()
Causes the current thread to wait at the barrier.
Barrier(uint32 thread_count)
Constructs an instance that will wait for thread_count threads.
bool IsValid() const
Returns true if a valid barrier was created by the constructor.