]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: add C_Cond
authorSage Weil <sage@inktank.com>
Sat, 5 May 2012 03:23:07 +0000 (20:23 -0700)
committerSage Weil <sage@newdream.net>
Sat, 5 May 2012 23:32:21 +0000 (16:32 -0700)
Similar to C_SafeCond, but assume finisher already holds the relevant lock.

Signed-off-by: Sage Weil <sage@newdream.net>
src/common/Cond.h

index deb94a66cf75553abd2dabebfadd5acabdad06df..70441f87c2ecb4c6d0bc55d3d7a713062edcf9cd 100644 (file)
@@ -90,18 +90,47 @@ class Cond {
   }
 };
 
+/**
+ * context to signal a cond
+ *
+ * Generic context to signal a cond and store the return value.  We
+ * assume the caller is holding the appropriate lock.
+ */
+class C_Cond : public Context {
+  Cond *cond;   ///< Cond to signal
+  bool *done;   ///< true if finish() has been called
+  int *rval;    ///< return value
+public:
+  C_Cond(Cond *c, bool *d, int *r) : cond(c), done(d), rval(r) {
+    *done = false;
+  }
+  void finish(int r) {
+    *done = true;
+    *rval = r;
+    cond->Signal();
+  }
+};
+
+/**
+ * context to signal a cond, protected by a lock
+ *
+ * Generic context to signal a cond under a specific lock. We take the
+ * lock in the finish() callback, so the finish() caller must not
+ * already hold it.
+ */
 class C_SafeCond : public Context {
-  Mutex *lock;
-  Cond *cond;
-  bool *done;
-  int *rval;
+  Mutex *lock;    ///< Mutex to take
+  Cond *cond;     ///< Cond to signal
+  bool *done;     ///< true after finish() has been called
+  int *rval;      ///< return value (optional)
 public:
   C_SafeCond(Mutex *l, Cond *c, bool *d, int *r=0) : lock(l), cond(c), done(d), rval(r) {
     *done = false;
   }
   void finish(int r) {
     lock->Lock();
-    if (rval) *rval = r;
+    if (rval)
+      *rval = r;
     *done = true;
     cond->Signal();
     lock->Unlock();