You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2015/02/08 16:51:46 UTC

[19/20] mesos git commit: Refactored Once abstraction to not use Promise.

Refactored Once abstraction to not use Promise.

Review: https://reviews.apache.org/r/29569


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/9f9afca4
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/9f9afca4
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/9f9afca4

Branch: refs/heads/master
Commit: 9f9afca4e8556653880559e9d1dac18a0321c5e8
Parents: 4065fa6
Author: Benjamin Hindman <be...@gmail.com>
Authored: Sat Jan 3 09:37:39 2015 -0800
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Sat Feb 7 14:46:44 2015 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/include/process/once.hpp | 45 +++++++++++++++++++----
 1 file changed, 37 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/9f9afca4/3rdparty/libprocess/include/process/once.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/once.hpp b/3rdparty/libprocess/include/process/once.hpp
index e85b382..256ed07 100644
--- a/3rdparty/libprocess/include/process/once.hpp
+++ b/3rdparty/libprocess/include/process/once.hpp
@@ -12,7 +12,17 @@ namespace process {
 class Once
 {
 public:
-  Once() {}
+  Once() : started(false), finished(false)
+  {
+    pthread_mutex_init(&mutex, NULL);
+    pthread_cond_init(&cond, NULL);
+  }
+
+  ~Once()
+  {
+    pthread_cond_destroy(&cond);
+    pthread_mutex_destroy(&mutex);
+  }
 
   // Returns true if this Once instance has already transitioned to a
   // 'done' state (i.e., the action you wanted to perform "once" has
@@ -20,18 +30,35 @@ public:
   // called.
   bool once()
   {
-    if (!outer.set(&inner)) {
-      inner.future().await();
-      return true;
+    bool result = false;
+
+    pthread_mutex_lock(&mutex);
+    {
+      if (started) {
+        while (!finished) {
+          pthread_cond_wait(&cond, &mutex);
+        }
+        result = true;
+      } else {
+        started = true;
+      }
     }
+    pthread_mutex_unlock(&mutex);
 
-    return false;
+    return result;
   }
 
   // Transitions this Once instance to a 'done' state.
   void done()
   {
-    inner.set(Nothing());
+    pthread_mutex_lock(&mutex);
+    {
+      if (started && !finished) {
+        finished = true;
+        pthread_cond_broadcast(&cond);
+      }
+    }
+    pthread_mutex_unlock(&mutex);
   }
 
 private:
@@ -39,8 +66,10 @@ private:
   Once(const Once& that);
   Once& operator = (const Once& that);
 
-  Promise<Nothing> inner;
-  Promise<Promise<Nothing>*> outer;
+  pthread_mutex_t mutex;
+  pthread_cond_t cond;
+  bool started;
+  bool finished;
 };
 
 }  // namespace process {