Update libprocess Gate to use synchronized. Review: https://reviews.apache.org/r/35094
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/eb33a57f Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/eb33a57f Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/eb33a57f Branch: refs/heads/master Commit: eb33a57ff32297d885d444336da86762dc98d793 Parents: 149f42f Author: Joris Van Remoortere <[email protected]> Authored: Sat Jun 13 06:51:59 2015 -0700 Committer: Benjamin Hindman <[email protected]> Committed: Sun Jun 14 02:43:00 2015 -0700 ---------------------------------------------------------------------- 3rdparty/libprocess/src/gate.hpp | 38 ++++++++++++++--------------------- 1 file changed, 15 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/eb33a57f/3rdparty/libprocess/src/gate.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/src/gate.hpp b/3rdparty/libprocess/src/gate.hpp index 69c906b..e5c9379 100644 --- a/3rdparty/libprocess/src/gate.hpp +++ b/3rdparty/libprocess/src/gate.hpp @@ -3,6 +3,8 @@ // TODO(benh): Build implementation directly on-top-of futex's for Linux. +#include <stout/synchronized.hpp> + class Gate { public: @@ -31,21 +33,21 @@ public: // all (if 'all' is true) of the threads waiting on it. void open(bool all = true) { - pthread_mutex_lock(&mutex); - { + synchronized (mutex) { state++; - if (all) pthread_cond_broadcast(&cond); - else pthread_cond_signal(&cond); + if (all) { + pthread_cond_broadcast(&cond); + } else { + pthread_cond_signal(&cond); + } } - pthread_mutex_unlock(&mutex); } // Blocks the current thread until the gate's state changes from // the current state. void wait() { - pthread_mutex_lock(&mutex); - { + synchronized (mutex) { waiters++; state_t old = state; while (old == state) { @@ -53,7 +55,6 @@ public: } waiters--; } - pthread_mutex_unlock(&mutex); } // Gets the current state of the gate and notifies the gate about @@ -61,14 +62,10 @@ public: // Call 'leave()' if no longer interested in the state change. state_t approach() { - state_t old; - pthread_mutex_lock(&mutex); - { + synchronized (mutex) { waiters++; - old = state; + return state; } - pthread_mutex_unlock(&mutex); - return old; } // Blocks the current thread until the gate's state changes from @@ -76,25 +73,22 @@ public: // calling 'approach()'. void arrive(state_t old) { - pthread_mutex_lock(&mutex); - { + synchronized (mutex) { while (old == state) { pthread_cond_wait(&cond, &mutex); } + waiters--; } - pthread_mutex_unlock(&mutex); } // Notifies the gate that a waiter (the current thread) is no // longer waiting for the gate's state change. void leave() { - pthread_mutex_lock(&mutex); - { + synchronized (mutex) { waiters--; } - pthread_mutex_unlock(&mutex); } // Returns true if there is no one waiting on the gate's state @@ -102,11 +96,9 @@ public: bool empty() { bool occupied = true; - pthread_mutex_lock(&mutex); - { + synchronized (mutex) { occupied = waiters > 0 ? true : false; } - pthread_mutex_unlock(&mutex); return !occupied; } };
