]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
ceph/async: `io_context_pool` constructor/start takes init function
authorAdam C. Emerson <aemerson@redhat.com>
Fri, 14 Apr 2023 19:43:36 +0000 (15:43 -0400)
committerAdam Emerson <aemerson@redhat.com>
Wed, 24 Jan 2024 20:51:46 +0000 (15:51 -0500)
Add overloads of the constructor and `start` that take an init
function, for setting up the thread before dropping into
`io_context::run`.

Signed-off-by: Adam C. Emerson <aemerson@redhat.com>
src/common/async/context_pool.h

index 0fee5dd7fcacd323502ef73871ab61f9b78f141b..91a7090ade76675dcdb1e61bd64bbe195db0963e 100644 (file)
@@ -16,6 +16,7 @@
 #ifndef CEPH_COMMON_ASYNC_CONTEXT_POOL_H
 #define CEPH_COMMON_ASYNC_CONTEXT_POOL_H
 
+#include <concepts>
 #include <cstddef>
 #include <cstdint>
 #include <mutex>
@@ -46,9 +47,14 @@ class io_context_pool {
   }
 public:
   io_context_pool() noexcept {}
+
   io_context_pool(std::int64_t threadcnt) noexcept {
     start(threadcnt);
   }
+  template<std::invocable<> Init>
+  io_context_pool(std::int64_t threadcnt, Init&& init) noexcept {
+    start(threadcnt, std::move(init));
+  }
   ~io_context_pool() {
     stop();
   }
@@ -59,7 +65,22 @@ public:
       ioctx.restart();
       for (std::int16_t i = 0; i < threadcnt; ++i) {
        threadvec.emplace_back(make_named_thread("io_context_pool",
-                                                [this]() {
+                                                [this] {
+                                                  ioctx.run();
+                                                }));
+      }
+    }
+  }
+  template<std::invocable<> Init>
+  void start(std::int16_t threadcnt, Init&& init) noexcept {
+    auto l = std::scoped_lock(m);
+    if (threadvec.empty()) {
+      guard.emplace(boost::asio::make_work_guard(ioctx));
+      ioctx.restart();
+      for (std::int16_t i = 0; i < threadcnt; ++i) {
+       threadvec.emplace_back(make_named_thread("io_context_pool",
+                                                [this, init=std::move(init)] {
+                                                  std::move(init)();
                                                   ioctx.run();
                                                 }));
       }