From c2ec0c0eb6922b5aed12198d23e103eda631b125 Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Wed, 14 Feb 2024 21:18:13 -0500 Subject: [PATCH] common/async: SharedMutex uses async_initiate Signed-off-by: Casey Bodley --- src/common/async/detail/shared_mutex.h | 86 +++++++++++++------------- 1 file changed, 42 insertions(+), 44 deletions(-) diff --git a/src/common/async/detail/shared_mutex.h b/src/common/async/detail/shared_mutex.h index 5bfd0d58b1cc3..80a5c3bd7fd4e 100644 --- a/src/common/async/detail/shared_mutex.h +++ b/src/common/async/detail/shared_mutex.h @@ -123,29 +123,28 @@ auto SharedMutexImpl::async_lock(Mutex& mtx, CompletionToken&& token) { using Request = AsyncRequest; using Signature = typename Request::Signature; - boost::asio::async_completion init(token); - auto& handler = init.completion_handler; - auto ex1 = mtx.get_executor(); - { - std::lock_guard lock{mutex}; - - boost::system::error_code ec; - if (state == Unlocked) { - state = Exclusive; - - // post a successful completion - auto ex2 = boost::asio::get_associated_executor(handler, ex1); - auto h = boost::asio::bind_executor(ex2, std::move(handler)); - boost::asio::post(bind_handler(std::move(h), ec, - std::unique_lock{mtx, std::adopt_lock})); - } else { - // create a request and add it to the exclusive list - using LockCompletion = typename Request::LockCompletion; - auto request = LockCompletion::create(ex1, std::move(handler), mtx); - exclusive_queue.push_back(*request.release()); - } - } - return init.result.get(); + return boost::asio::async_initiate( + [this] (auto handler, Mutex& mtx) { + auto ex1 = mtx.get_executor(); + + std::lock_guard lock{mutex}; + + boost::system::error_code ec; + if (state == Unlocked) { + state = Exclusive; + + // post a successful completion + auto ex2 = boost::asio::get_associated_executor(handler, ex1); + auto h = boost::asio::bind_executor(ex2, std::move(handler)); + boost::asio::post(bind_handler(std::move(h), ec, + std::unique_lock{mtx, std::adopt_lock})); + } else { + // create a request and add it to the exclusive list + using LockCompletion = typename Request::LockCompletion; + auto request = LockCompletion::create(ex1, std::move(handler), mtx); + exclusive_queue.push_back(*request.release()); + } + }, token, mtx); } inline void SharedMutexImpl::lock() @@ -215,27 +214,26 @@ auto SharedMutexImpl::async_lock_shared(Mutex& mtx, CompletionToken&& token) { using Request = AsyncRequest; using Signature = typename Request::Signature; - boost::asio::async_completion init(token); - auto& handler = init.completion_handler; - auto ex1 = mtx.get_executor(); - { - std::lock_guard lock{mutex}; - - boost::system::error_code ec; - if (exclusive_queue.empty() && state < MaxShared) { - state++; - - auto ex2 = boost::asio::get_associated_executor(handler, ex1); - auto h = boost::asio::bind_executor(ex2, std::move(handler)); - boost::asio::post(bind_handler(std::move(h), ec, - std::shared_lock{mtx, std::adopt_lock})); - } else { - using LockCompletion = typename Request::LockCompletion; - auto request = LockCompletion::create(ex1, std::move(handler), mtx); - shared_queue.push_back(*request.release()); - } - } - return init.result.get(); + return boost::asio::async_initiate( + [this] (auto handler, Mutex& mtx) { + auto ex1 = mtx.get_executor(); + + std::lock_guard lock{mutex}; + + boost::system::error_code ec; + if (exclusive_queue.empty() && state < MaxShared) { + state++; + + auto ex2 = boost::asio::get_associated_executor(handler, ex1); + auto h = boost::asio::bind_executor(ex2, std::move(handler)); + boost::asio::post(bind_handler(std::move(h), ec, + std::shared_lock{mtx, std::adopt_lock})); + } else { + using LockCompletion = typename Request::LockCompletion; + auto request = LockCompletion::create(ex1, std::move(handler), mtx); + shared_queue.push_back(*request.release()); + } + }, token, mtx); } inline void SharedMutexImpl::lock_shared() -- 2.39.5