]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/RWLock: add lock context
authorYehuda Sadeh <yehuda@inktank.com>
Sat, 3 May 2014 15:33:33 +0000 (08:33 -0700)
committerJohn Spray <john.spray@redhat.com>
Mon, 25 Aug 2014 00:33:39 +0000 (01:33 +0100)
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/common/RWLock.h

index 6f3ffe315b6d66001d004a1bbacd7c334db6962e..bd60c417688de5866a2b2309e62037ba07c05c5e 100644 (file)
@@ -154,6 +154,65 @@ public:
       }
     }
   };
+
+  class Context {
+    RWLock& lock;
+
+  public:
+    enum LockState {
+      Untaken = 0,
+      TakenForRead = 1,
+      TakenForWrite = 2,
+    };
+
+  private:
+    LockState state;
+
+  public:
+    Context(RWLock& l) : lock(l) {}
+    Context(RWLock& l, LockState s) : lock(l), state(s) {}
+
+    void get_write() {
+      assert(state == Untaken);
+
+      lock.get_write();
+      state = TakenForWrite;
+    }
+
+    void get_read() {
+      assert(state == Untaken);
+
+      lock.get_read();
+      state = TakenForRead;
+    }
+
+    void unlock() {
+      assert(state != Untaken);
+      lock.unlock();
+      state = Untaken;
+    }
+
+    void promote() {
+      assert(state == TakenForRead);
+      unlock();
+      get_write();
+    }
+
+    LockState get_state() { return state; }
+    void set_state(LockState s) {
+      state = s;
+    }
+
+    bool is_locked() {
+      return (state != Untaken);
+    }
+
+    bool is_rlocked() {
+      return (state == TakenForRead);
+    }
+
+    bool is_wlocked() {
+      return (state == TakenForWrite);
     }
   };
 };