summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Just <sam.just@inktank.com>2013-07-19 15:55:08 -0700
committerSamuel Just <sam.just@inktank.com>2013-07-22 10:31:02 -0700
commit8536ff9a430532d0a48cc9da0023bfe059eec6c3 (patch)
tree6394f283c019fa1b7606b0aa71544dfb169c4bb6
parenteabf2f6ae1ec244092315b79efe5b886c2b18578 (diff)
downloadceph-8536ff9a430532d0a48cc9da0023bfe059eec6c3.tar.gz
common/Cond.h: add a simpler C_SaferCond Context
Signed-off-by: Samuel Just <sam.just@inktank.com> Reviewed-by: Sage Weil <sage@inktank.com>
-rw-r--r--src/common/Cond.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/common/Cond.h b/src/common/Cond.h
index ee95a65b5b6..e6a13ae48bb 100644
--- a/src/common/Cond.h
+++ b/src/common/Cond.h
@@ -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