From 9aa6d3398082e4e186ea2f82652d19b738f53548 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 13 Jul 2009 16:14:43 +0000 Subject: [PATCH] From David Fries, "Comparing the win32 barrier to the pthread barrier, win32 puts the while in an else clause. When if is true the phase changes and the while condition will always by false, so might as well put the while in the else to skip the check. There's also a benefit to having the code logic similar between platforms. " --- src/OpenThreads/pthreads/PThreadBarrier.c++ | 22 +++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/OpenThreads/pthreads/PThreadBarrier.c++ b/src/OpenThreads/pthreads/PThreadBarrier.c++ index 033ca8716..849bf019d 100644 --- a/src/OpenThreads/pthreads/PThreadBarrier.c++ +++ b/src/OpenThreads/pthreads/PThreadBarrier.c++ @@ -170,18 +170,20 @@ void Barrier::block(unsigned int numThreads) { ++pd->cnt; if (pd->cnt == pd->maxcnt) { // I am the last one - pd->cnt = 0; // reset for next use - pd->phase = 1 - my_phase; // toggle phase - pthread_cond_broadcast(&(pd->cond)); - } + pd->cnt = 0; // reset for next use + pd->phase = 1 - my_phase; // toggle phase + pthread_cond_broadcast(&(pd->cond)); + } + else + { + while (pd->phase == my_phase) + { + pthread_cleanup_push(barrier_cleanup_handler, &(pd->lock)); - while (pd->phase == my_phase) { + pthread_cond_wait(&(pd->cond), &(pd->lock)); - pthread_cleanup_push(barrier_cleanup_handler, &(pd->lock)); - - pthread_cond_wait(&(pd->cond), &(pd->lock)); - - pthread_cleanup_pop(0); + pthread_cleanup_pop(0); + } } }