]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- | |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | ||
16 | #ifndef CEPH_COND_H | |
17 | #define CEPH_COND_H | |
18 | ||
19 | #include "include/Context.h" | |
20 | #include "CondVar.h" | |
21 | ||
22 | /** | |
23 | * context to signal a cond | |
24 | * | |
25 | * Generic context to signal a cond and store the return value. We | |
26 | * assume the caller is holding the appropriate lock. | |
27 | */ | |
28 | class C_Cond : public Context { | |
29 | Cond *cond; ///< Cond to signal | |
30 | bool *done; ///< true if finish() has been called | |
31 | int *rval; ///< return value | |
32 | public: | |
33 | C_Cond(Cond *c, bool *d, int *r) : cond(c), done(d), rval(r) { | |
34 | *done = false; | |
35 | } | |
36 | void finish(int r) override { | |
37 | *done = true; | |
38 | *rval = r; | |
39 | cond->Signal(); | |
40 | } | |
41 | }; | |
42 | ||
43 | /** | |
44 | * context to signal a cond, protected by a lock | |
45 | * | |
46 | * Generic context to signal a cond under a specific lock. We take the | |
47 | * lock in the finish() callback, so the finish() caller must not | |
48 | * already hold it. | |
49 | */ | |
50 | class C_SafeCond : public Context { | |
51 | Mutex *lock; ///< Mutex to take | |
52 | Cond *cond; ///< Cond to signal | |
53 | bool *done; ///< true after finish() has been called | |
54 | int *rval; ///< return value (optional) | |
55 | public: | |
56 | C_SafeCond(Mutex *l, Cond *c, bool *d, int *r=0) : lock(l), cond(c), done(d), rval(r) { | |
57 | *done = false; | |
58 | } | |
59 | void finish(int r) override { | |
60 | lock->Lock(); | |
61 | if (rval) | |
62 | *rval = r; | |
63 | *done = true; | |
64 | cond->Signal(); | |
65 | lock->Unlock(); | |
66 | } | |
67 | }; | |
68 | ||
69 | /** | |
70 | * Context providing a simple wait() mechanism to wait for completion | |
71 | * | |
72 | * The context will not be deleted as part of complete and must live | |
73 | * until wait() returns. | |
74 | */ | |
75 | class C_SaferCond : public Context { | |
76 | Mutex lock; ///< Mutex to take | |
77 | Cond cond; ///< Cond to signal | |
78 | bool done; ///< true after finish() has been called | |
79 | int rval; ///< return value | |
80 | public: | |
81 | C_SaferCond() : lock("C_SaferCond"), done(false), rval(0) {} | |
82 | void finish(int r) override { complete(r); } | |
83 | ||
84 | /// We overload complete in order to not delete the context | |
85 | void complete(int r) override { | |
86 | Mutex::Locker l(lock); | |
87 | done = true; | |
88 | rval = r; | |
89 | cond.Signal(); | |
90 | } | |
91 | ||
92 | /// Returns rval once the Context is called | |
93 | int wait() { | |
94 | Mutex::Locker l(lock); | |
95 | while (!done) | |
96 | cond.Wait(lock); | |
97 | return rval; | |
98 | } | |
99 | }; | |
100 | ||
101 | #endif |