crimson/osd/object_context: add helper for acquiring locks
authorKefu Chai <kchai@redhat.com>
Sat, 19 Sep 2020 03:00:22 +0000 (11:00 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 19 Sep 2020 05:30:51 +0000 (13:30 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/crimson/osd/object_context.h

index a75863258afa6e0c7f31074abeb6bfe4c6cdb3e8..4815a28529b1c1bd4eee44f330913015b4df6126 100644 (file)
@@ -112,6 +112,32 @@ private:
   }
 
 public:
+  template<typename Func>
+  auto with_lock(RWState::State type, Func&& func) {
+    switch (type) {
+    case RWState::RWWRITE:
+      return seastar::with_lock(lock.for_write(), std::forward<Func>(func));
+    case RWState::RWREAD:
+      return seastar::with_lock(lock.for_read(), std::forward<Func>(func));
+    case RWState::RWEXCL:
+      return seastar::with_lock(lock.for_excl(), std::forward<Func>(func));
+    default:
+      assert(0 == "noop");
+    }
+  }
+  template<typename Func>
+  auto with_promoted_lock(RWState::State type, Func&& func) {
+    switch (type) {
+    case RWState::RWWRITE:
+      return seastar::with_lock(lock.excl_from_write(), std::forward<Func>(func));
+    case RWState::RWREAD:
+      return seastar::with_lock(lock.excl_from_read(), std::forward<Func>(func));
+    case RWState::RWEXCL:
+      return seastar::with_lock(lock.excl_from_excl(), std::forward<Func>(func));
+     default:
+      assert(0 == "noop");
+    }
+  }
   seastar::future<> get_lock_type(Operation *op, RWState::State type) {
     switch (type) {
     case RWState::RWWRITE: