}
};
+/**
+ * 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();