diff options
author | Samuel Just <sam.just@inktank.com> | 2013-07-19 15:55:08 -0700 |
---|---|---|
committer | Samuel Just <sam.just@inktank.com> | 2013-07-19 15:55:08 -0700 |
commit | 50d63e5dea9b4d6314ca2d39009caa786cdcf5eb (patch) | |
tree | b79397806ec4688f7cc75087f95141e62c91a9a0 | |
parent | d3902e2e31574f4e91e2932c6ed7b06e6a6c7014 (diff) | |
download | ceph-50d63e5dea9b4d6314ca2d39009caa786cdcf5eb.tar.gz |
common/Cond.h: add a simpler C_SaferCond Context
Signed-off-by: Samuel Just <sam.just@inktank.com>
-rw-r--r-- | src/common/Cond.h | 32 |
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 |