]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/Cond.h: add a simpler C_SaferCond Context
authorSamuel Just <sam.just@inktank.com>
Fri, 19 Jul 2013 22:55:08 +0000 (15:55 -0700)
committerSamuel Just <sam.just@inktank.com>
Mon, 22 Jul 2013 17:31:02 +0000 (10:31 -0700)
Signed-off-by: Samuel Just <sam.just@inktank.com>
Reviewed-by: Sage Weil <sage@inktank.com>
src/common/Cond.h

index ee95a65b5b6608fe03eaef1e57767df83b33745d..e6a13ae48bb72a20709ce9ffc67338d90158e24e 100644 (file)
@@ -156,4 +156,36 @@ public:
   }
 };
 
+/**
+ * Context providing a simple wait() mechanism to wait for completion
+ *
+ * The context will not be deleted as part of complete and must live
+ * until wait() returns.
+ */
+class C_SaferCond : public Context {
+  Mutex lock;    ///< Mutex to take
+  Cond cond;     ///< Cond to signal
+  bool done;     ///< true after finish() has been called
+  int rval;      ///< return value
+public:
+  C_SaferCond() : lock("C_SaferCond"), done(false), rval(0) {}
+  void finish(int r) { complete(r); }
+
+  /// We overload complete in order to not delete the context
+  void complete(int r) {
+    Mutex::Locker l(lock);
+    done = true;
+    rval = r;
+    cond.Signal();
+  }
+
+  /// Returns rval once the Context is called
+  int wait() {
+    Mutex::Locker l(lock);
+    while (!done)
+      cond.Wait(lock);
+    return rval;
+  }
+};
+
 #endif