From 0045ea67d380ab20c5b7bda20d3b0a7d746503bd Mon Sep 17 00:00:00 2001
From: Pedro Gonnet <gonnet@google.com>
Date: Sun, 30 Jul 2017 22:01:13 +0200
Subject: [PATCH] grossly simplify the engine_barrier function using
 pthread_barriers.

---
 src/engine.c | 33 +++++----------------------------
 1 file changed, 5 insertions(+), 28 deletions(-)

diff --git a/src/engine.c b/src/engine.c
index ee48d6565d..dc66eab769 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -2943,35 +2943,12 @@ void engine_prepare(struct engine *e) {
  */
 void engine_barrier(struct engine *e, int tid) {
 
-  /* First, get the barrier mutex. */
-  if (pthread_mutex_lock(&e->barrier_mutex) != 0)
-    error("Failed to get barrier mutex.");
-
-  /* This thread is no longer running. */
-  e->barrier_running -= 1;
-
-  /* If all threads are in, send a signal... */
-  if (e->barrier_running == 0)
-    if (pthread_cond_broadcast(&e->barrier_cond) != 0)
-      error("Failed to broadcast barrier full condition.");
-
-  /* Wait for the barrier to open. */
-  while (e->barrier_launch == 0 || tid >= e->barrier_launchcount)
-    if (pthread_cond_wait(&e->barrier_cond, &e->barrier_mutex) != 0)
-      error("Error waiting for barrier to close.");
-
-  /* This thread has been launched. */
-  e->barrier_running += 1;
-  e->barrier_launch -= 1;
-
-  /* If I'm the last one out, signal the condition again. */
-  if (e->barrier_launch == 0)
-    if (pthread_cond_broadcast(&e->barrier_cond) != 0)
-      error("Failed to broadcast empty barrier condition.");
+  /* Wait at the wait barrier. */
+  pthread_barrier_wait(&e->wait_barrier);
+  
+  /* Wait at the run barrier. */
+  pthread_barrier_wait(&e->run_barrier);
 
-  /* Last but not least, release the mutex. */
-  if (pthread_mutex_unlock(&e->barrier_mutex) != 0)
-    error("Failed to get unlock the barrier mutex.");
 }
 
 /**
-- 
GitLab