+#ifdef HAVE_FUTEX_H
+ static void futex_wait(int *uaddr, int val)
+ {
+ DEBUG1("Waiting on futex %d", *uaddr);
+ syscall(SYS_futex, uaddr, FUTEX_WAIT_PRIVATE, val, NULL, NULL, 0);
+ }
+
+ static void futex_wake(int *uaddr, int val)
+ {
+ DEBUG1("Waking futex %d", *uaddr);
+ syscall(SYS_futex, uaddr, FUTEX_WAKE_PRIVATE, val, NULL, NULL, 0);
+ }
+#endif
+
+/* Futex based implementation of the barrier */
+void xbt_barrier_init(xbt_barrier_t barrier, unsigned int threads_to_wait)
+{
+ barrier->threads_to_wait = threads_to_wait;
+ barrier->thread_count = 0;
+}
+
+#ifdef HAVE_FUTEX_H
+ void xbt_barrier_wait(xbt_barrier_t barrier)
+ {
+ int myflag = 0;
+ unsigned int mycount = 0;
+
+ myflag = barrier->futex;
+ mycount = __sync_add_and_fetch(&barrier->thread_count, 1);
+ if(mycount < barrier->threads_to_wait){
+ futex_wait(&barrier->futex, myflag);
+ }else{
+ barrier->futex = __sync_add_and_fetch(&barrier->futex, 1);
+ barrier->thread_count = 0;
+ futex_wake(&barrier->futex, barrier->threads_to_wait);
+ }
+ }
+#else
+ void xbt_barrier_wait(xbt_barrier_t barrier)
+ {
+ int myflag = 0;
+ unsigned int mycount = 0;
+
+ xbt_os_mutex_acquire(barrier->mutex);
+
+ barrier->thread_count++;
+ if(barrier->thread_count < barrier->threads_to_wait){
+ xbt_os_cond_wait(barrier->cond,barrier->mutex);
+ }else{
+ barrier->thread_count = 0;
+ xbt_os_cond_broadcast(barrier->cond);
+ }
+ xbt_os_mutex_release(barrier->mutex);
+ }
+#endif
+