From d629f18088da6a371079ec8e22329b535f5c5e13 Mon Sep 17 00:00:00 2001 From: Xuehan Xu Date: Wed, 2 Dec 2020 10:30:07 +0800 Subject: [PATCH] crimson/osd: add _with_lock method to obc this is modeling the seastar::with_lock() method with the exception that obc is also captured in the finally block. Signed-off-by: Xuehan Xu --- src/crimson/osd/object_context.h | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/crimson/osd/object_context.h b/src/crimson/osd/object_context.h index 7e14ac3e16f..335d26a21e9 100644 --- a/src/crimson/osd/object_context.h +++ b/src/crimson/osd/object_context.h @@ -111,18 +111,28 @@ private: make_blocking_future(std::forward(lockf))); } + template + auto _with_lock(Lock&& lock, Func&& func) { + Ref obc = this; + return lock.lock().then([&lock, func = std::forward(func), obc]() mutable { + return seastar::futurize_invoke(func).finally([&lock, obc] { + lock.unlock(); + }); + }); + } + public: template auto with_lock(Func&& func) { switch (Type) { case RWState::RWWRITE: - return seastar::with_lock(lock.for_write(), std::forward(func)); + return _with_lock(lock.for_write(), std::forward(func)); case RWState::RWREAD: - return seastar::with_lock(lock.for_read(), std::forward(func)); + return _with_lock(lock.for_read(), std::forward(func)); case RWState::RWEXCL: - return seastar::with_lock(lock.for_excl(), std::forward(func)); + return _with_lock(lock.for_excl(), std::forward(func)); case RWState::RWNONE: - return seastar::futurize_invoke(func); + return seastar::futurize_invoke(std::forward(func)); default: assert(0 == "noop"); } @@ -131,11 +141,11 @@ public: auto with_promoted_lock(Func&& func) { switch (Type) { case RWState::RWWRITE: - return seastar::with_lock(lock.excl_from_write(), std::forward(func)); + return _with_lock(lock.excl_from_write(), std::forward(func)); case RWState::RWREAD: - return seastar::with_lock(lock.excl_from_read(), std::forward(func)); + return _with_lock(lock.excl_from_read(), std::forward(func)); case RWState::RWEXCL: - return seastar::with_lock(lock.excl_from_excl(), std::forward(func)); + return _with_lock(lock.excl_from_excl(), std::forward(func)); default: assert(0 == "noop"); } -- 2.39.5