Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 08:33:50

0001 //
0002 // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_MYSQL_IMPL_INTERNAL_CONNECTION_POOL_WAIT_GROUP_HPP
0009 #define BOOST_MYSQL_IMPL_INTERNAL_CONNECTION_POOL_WAIT_GROUP_HPP
0010 
0011 #include <boost/mysql/error_code.hpp>
0012 
0013 #include <boost/asio/any_io_executor.hpp>
0014 #include <boost/asio/bind_executor.hpp>
0015 #include <boost/asio/steady_timer.hpp>
0016 
0017 #include <chrono>
0018 #include <cstddef>
0019 
0020 namespace boost {
0021 namespace mysql {
0022 namespace detail {
0023 
0024 class wait_group
0025 {
0026     std::size_t running_tasks_{};
0027     asio::steady_timer finished_;
0028 
0029 public:
0030     wait_group(asio::any_io_executor ex)
0031         : finished_(std::move(ex), (std::chrono::steady_clock::time_point::max)())
0032     {
0033     }
0034 
0035     asio::any_io_executor get_executor() { return finished_.get_executor(); }
0036 
0037     void on_task_start() noexcept { ++running_tasks_; }
0038 
0039     void on_task_finish() noexcept
0040     {
0041         if (--running_tasks_ == 0u)
0042             finished_.cancel();  // If this happens to fail, terminate() is the best option
0043     }
0044 
0045     // Note: this operation always completes with a cancelled error code
0046     // (for simplicity).
0047     template <class CompletionToken>
0048     void async_wait(CompletionToken&& token)
0049     {
0050         return finished_.async_wait(std::forward<CompletionToken>(token));
0051     }
0052 
0053     // Runs op calling the adequate group member functions when op is started and finished.
0054     // The operation is run within this->get_executor()
0055     template <class Op>
0056     void run_task(Op&& op)
0057     {
0058         on_task_start();
0059         std::forward<Op>(op)(asio::bind_executor(get_executor(), [this](error_code) { on_task_finish(); }));
0060     }
0061 };
0062 
0063 }  // namespace detail
0064 }  // namespace mysql
0065 }  // namespace boost
0066 
0067 #endif