From d7edfbfadcd1420fb756e3f002926ec49a5a009a Mon Sep 17 00:00:00 2001 From: Gammasoft Date: Sun, 3 Nov 2024 14:38:16 +0100 Subject: [PATCH] Fix thread_pool items --- .../include/xtd/threading/thread_pool.h | 40 ++++++++++++++----- .../xtd/threading/registered_wait_handle.cpp | 6 +-- .../src/xtd/threading/thread_pool.cpp | 8 ++-- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/xtd.core/include/xtd/threading/thread_pool.h b/src/xtd.core/include/xtd/threading/thread_pool.h index ef8ba7e2b457..45f296287fe4 100644 --- a/src/xtd.core/include/xtd/threading/thread_pool.h +++ b/src/xtd.core/include/xtd/threading/thread_pool.h @@ -8,6 +8,7 @@ #include "wait_callback.h" #include "wait_or_timer_callback.h" #include "../core_export.h" +#include "../new_ptr.h" #include "../static.h" #include "../time_span.h" #include "../types.h" @@ -59,21 +60,38 @@ namespace xtd { template struct thread_item : public object { thread_item() = default; - thread_item(const callback_t& callback) : callback(callback) {} - thread_item(const callback_t& callback, std::any state) : callback(callback), state(state) {} - thread_item(const callback_t& callback, std::any state, wait_handle& wait_object, int32 milliseconds_timeout_interval, bool execute_only_once) : callback(callback), state(state), wait_object(&wait_object), milliseconds_timeout_interval(milliseconds_timeout_interval), execute_only_once(execute_only_once) {} + thread_item(thread_item&&) = default; + thread_item(const thread_item&) = default; + thread_item& operator =(thread_item&&) = default; + thread_item& operator =(const thread_item&) = default; + thread_item(const callback_t& callback) : data {xtd::new_ptr(callback)} {} + thread_item(const callback_t& callback, std::any state) : data {xtd::new_ptr(callback, state)} {} + thread_item(const callback_t& callback, std::any state, wait_handle& wait_object, int32 milliseconds_timeout_interval, bool execute_only_once) : data {xtd::new_ptr(callback, state, &wait_object, milliseconds_timeout_interval, execute_only_once)} {} - callback_t callback; - std::any state; - wait_handle* wait_object = null; - int32 milliseconds_timeout_interval; - bool execute_only_once = true; - bool unregistered = false; + struct sdata { + sdata() = default; + sdata(sdata&&) = default; + sdata(const sdata&) = default; + sdata& operator =(sdata&&) = default; + sdata& operator =(const sdata&) = default; + sdata(const callback_t& callback) : callback {callback} {} + sdata(const callback_t& callback, std::any state) : callback {callback}, state {state} {} + sdata(const callback_t& callback, std::any state, wait_handle* wait_object, int32 milliseconds_timeout_interval, bool execute_only_once) : callback {callback}, state {state}, wait_object {wait_object}, milliseconds_timeout_interval {milliseconds_timeout_interval}, execute_only_once {execute_only_once} {} + + callback_t callback; + std::any state; + wait_handle* wait_object = null; + int32 milliseconds_timeout_interval; + bool execute_only_once = true; + bool unregistered = false; + }; + + ptr data = xtd::new_ptr(); void run() { do { - this->callback(state); - } while (!execute_only_once); + this->callback(data->state); + } while (!data->execute_only_once); } }; diff --git a/src/xtd.core/src/xtd/threading/registered_wait_handle.cpp b/src/xtd.core/src/xtd/threading/registered_wait_handle.cpp index f49b26196547..cd78ad9582ff 100644 --- a/src/xtd.core/src/xtd/threading/registered_wait_handle.cpp +++ b/src/xtd.core/src/xtd/threading/registered_wait_handle.cpp @@ -8,14 +8,14 @@ bool registered_wait_handle::unregister() { if (item_ == 0) return false; auto item = reinterpret_cast(item_); - item->unregistered = true; - return item->wait_object->signal(); + item->data->unregistered = true; + return item->data->wait_object->signal(); } bool registered_wait_handle::unregister(wait_handle& wait_object) { if (item_ == 0) return false; auto item = reinterpret_cast(item_); - item->unregistered = true; + item->data->unregistered = true; return wait_object.signal(); } diff --git a/src/xtd.core/src/xtd/threading/thread_pool.cpp b/src/xtd.core/src/xtd/threading/thread_pool.cpp index ba9a320bd80a..114ed48406c2 100644 --- a/src/xtd.core/src/xtd/threading/thread_pool.cpp +++ b/src/xtd.core/src/xtd/threading/thread_pool.cpp @@ -146,11 +146,11 @@ void thread_pool::asynchronous_io_run() { } do { - auto wait_result = wait_handle::wait_any({&static_data_.close_asynchronous_io_threads_manual_reset_event, item.wait_object}, item.milliseconds_timeout_interval); + auto wait_result = wait_handle::wait_any({&static_data_.close_asynchronous_io_threads_manual_reset_event, item.data->wait_object}, item.data->milliseconds_timeout_interval); if (wait_result == 0) break; auto timeout = wait_result == wait_handle::wait_timeout; - if (!item.unregistered) item.callback(item.state, timeout); - } while (!item.execute_only_once && !item.unregistered); + if (!item.data->unregistered) item.data->callback(item.data->state, timeout); + } while (!item.data->execute_only_once && !item.data->unregistered); } } @@ -222,6 +222,6 @@ void thread_pool::run() { item = static_data_.thread_pool_items.back(); static_data_.thread_pool_items.pop_back(); } - item.callback(item.state); + item.data->callback(item.data->state); } }