22#if KCENON_WITH_THREAD_SYSTEM
25#pragma clang diagnostic push
26#pragma clang diagnostic ignored "-Wdeprecated-declarations"
31#include <kcenon/thread/core/thread_worker.h>
35thread_system_pool_adapter::thread_system_pool_adapter(
36 std::shared_ptr<kcenon::thread::thread_pool> pool)
37 : pool_(std::move(pool)) {
39 throw std::invalid_argument(
"thread_system_pool_adapter: pool is null");
44thread_system_pool_adapter::~thread_system_pool_adapter() {
48std::future<void> thread_system_pool_adapter::submit(std::function<
void()> task) {
49 auto promise = std::make_shared<std::promise<void>>();
50 auto future = promise->get_future();
54 pool_->submit([task = std::move(task), promise]()
mutable {
59 promise->set_exception(std::current_exception());
62 }
catch (
const std::exception& e) {
63 promise->set_exception(std::make_exception_ptr(
65 std::string(
"thread_system_pool_adapter: submit failed: ") + e.what()
72std::future<void> thread_system_pool_adapter::submit_delayed(
73 std::function<
void()> task,
74 std::chrono::milliseconds delay
76#if defined(THREAD_HAS_COMMON_EXECUTOR)
79 return pool_->submit_delayed(std::move(task), delay);
83 auto promise = std::make_shared<std::promise<void>>();
84 auto future = promise->get_future();
86 if (!pool_->is_running()) {
87 promise->set_exception(std::make_exception_ptr(
88 std::runtime_error(
"thread_system_pool_adapter: pool is not running")));
94 pool_->submit([task = std::move(task), delay, promise]()
mutable {
96 std::this_thread::sleep_for(delay);
100 promise->set_exception(std::current_exception());
103 }
catch (
const std::exception& e) {
104 promise->set_exception(std::make_exception_ptr(
106 std::string(
"thread_system_pool_adapter: delayed submit failed: ") + e.what()
114size_t thread_system_pool_adapter::worker_count()
const {
115 return pool_->get_active_worker_count();
118bool thread_system_pool_adapter::is_running()
const {
119 return pool_->is_running();
122size_t thread_system_pool_adapter::pending_tasks()
const {
123 return pool_->get_pending_task_count();
126std::shared_ptr<thread_system_pool_adapter> thread_system_pool_adapter::create_default(
127 const std::string& pool_name
129 kcenon::thread::thread_context ctx;
130 auto pool = std::make_shared<kcenon::thread::thread_pool>(pool_name, ctx);
133 size_t num_threads = std::thread::hardware_concurrency();
134 if (num_threads == 0) {
138 for (
size_t i = 0; i < num_threads; ++i) {
139 pool->enqueue(std::make_unique<kcenon::thread::thread_worker>());
143 return std::make_shared<thread_system_pool_adapter>(std::move(pool));
146std::shared_ptr<thread_system_pool_adapter> thread_system_pool_adapter::from_service_or_default(
147 const std::string& pool_name
150 auto& sc = kcenon::thread::service_container::global();
151 if (
auto existing = sc.resolve<kcenon::thread::thread_pool>()) {
152 return std::make_shared<thread_system_pool_adapter>(std::move(existing));
157 return create_default(pool_name);
160bool bind_thread_system_pool_into_manager(
const std::string& pool_name) {
162 auto adapter = thread_system_pool_adapter::from_service_or_default(pool_name);
163 if (!adapter)
return false;
164 thread_integration_manager::instance().set_thread_pool(adapter);
173#pragma clang diagnostic pop
Feature flags for network_system.
Adapter that bridges thread_system::thread_pool to thread_pool_interface.