From f28dd32275c1feba4854abad30ff5e21a7b39440 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Mon, 22 Mar 2021 21:00:48 -0300 Subject: common/thread_worker: Add wait for requests method --- src/common/thread_worker.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/common/thread_worker.h') diff --git a/src/common/thread_worker.h b/src/common/thread_worker.h index f1859971f..7a6756eb5 100644 --- a/src/common/thread_worker.h +++ b/src/common/thread_worker.h @@ -18,12 +18,14 @@ public: explicit ThreadWorker(std::size_t num_workers, const std::string& name); ~ThreadWorker(); void QueueWork(std::function&& work); + void WaitForRequests(); private: std::vector threads; std::queue> requests; std::mutex queue_mutex; std::condition_variable condition; + std::condition_variable wait_condition; std::atomic_bool stop{}; }; -- cgit v1.2.3 From bf5b5c1bf43946039d91f78253599c9996f86057 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Thu, 1 Apr 2021 01:05:45 -0300 Subject: common/thread_worker: Use unique function --- src/common/thread_worker.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/common/thread_worker.h') diff --git a/src/common/thread_worker.h b/src/common/thread_worker.h index 7a6756eb5..7e2b04a07 100644 --- a/src/common/thread_worker.h +++ b/src/common/thread_worker.h @@ -11,18 +11,20 @@ #include #include +#include "common/unique_function.h" + namespace Common { class ThreadWorker final { public: explicit ThreadWorker(std::size_t num_workers, const std::string& name); ~ThreadWorker(); - void QueueWork(std::function&& work); + void QueueWork(UniqueFunction work); void WaitForRequests(); private: std::vector threads; - std::queue> requests; + std::queue> requests; std::mutex queue_mutex; std::condition_variable condition; std::condition_variable wait_condition; -- cgit v1.2.3 From a10e112e6436b30c9eb5ca2a82c94f83205bbc34 Mon Sep 17 00:00:00 2001 From: FernandoS27 Date: Tue, 6 Apr 2021 04:23:02 +0200 Subject: common/thread_worker: Fix data race --- src/common/thread_worker.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/common/thread_worker.h') diff --git a/src/common/thread_worker.h b/src/common/thread_worker.h index 7e2b04a07..12bbf5fef 100644 --- a/src/common/thread_worker.h +++ b/src/common/thread_worker.h @@ -11,6 +11,7 @@ #include #include +#include "common/common_types.h" #include "common/unique_function.h" namespace Common { @@ -29,6 +30,10 @@ private: std::condition_variable condition; std::condition_variable wait_condition; std::atomic_bool stop{}; + std::atomic work_scheduled{}; + std::atomic work_done{}; + std::atomic workers_stopped{}; + std::atomic workers_queued{}; }; } // namespace Common -- cgit v1.2.3 From da34d3704405665b68d3d992f37a7eeb541238af Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 25 May 2021 20:37:06 -0300 Subject: common/thread_worker: Add support for stateful threads --- src/common/thread_worker.h | 97 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 11 deletions(-) (limited to 'src/common/thread_worker.h') diff --git a/src/common/thread_worker.h b/src/common/thread_worker.h index 12bbf5fef..16aa673bd 100644 --- a/src/common/thread_worker.h +++ b/src/common/thread_worker.h @@ -8,32 +8,107 @@ #include #include #include +#include #include #include -#include "common/common_types.h" +#include "common/thread.h" #include "common/unique_function.h" namespace Common { -class ThreadWorker final { +template +class StatefulThreadWorker { + static constexpr bool with_state = !std::is_same_v; + + struct DummyCallable { + int operator()() const noexcept { + return 0; + } + }; + + using Task = + std::conditional_t, UniqueFunction>; + using StateMaker = std::conditional_t, DummyCallable>; + public: - explicit ThreadWorker(std::size_t num_workers, const std::string& name); - ~ThreadWorker(); - void QueueWork(UniqueFunction work); - void WaitForRequests(); + explicit StatefulThreadWorker(size_t num_workers, std::string name, StateMaker func = {}) + : workers_queued{num_workers}, thread_name{std::move(name)} { + const auto lambda = [this, func] { + Common::SetCurrentThreadName(thread_name.c_str()); + { + std::conditional_t state{func()}; + while (!stop) { + Task task; + { + std::unique_lock lock{queue_mutex}; + if (requests.empty()) { + wait_condition.notify_all(); + } + condition.wait(lock, [this] { return stop || !requests.empty(); }); + if (stop) { + break; + } + task = std::move(requests.front()); + requests.pop(); + } + if constexpr (with_state) { + task(&state); + } else { + task(); + } + ++work_done; + } + } + ++workers_stopped; + wait_condition.notify_all(); + }; + for (size_t i = 0; i < num_workers; ++i) { + threads.emplace_back(lambda); + } + } + + ~StatefulThreadWorker() { + { + std::unique_lock lock{queue_mutex}; + stop = true; + } + condition.notify_all(); + for (std::thread& thread : threads) { + thread.join(); + } + } + + void QueueWork(Task work) { + { + std::unique_lock lock{queue_mutex}; + requests.emplace(std::move(work)); + ++work_scheduled; + } + condition.notify_one(); + } + + void WaitForRequests() { + std::unique_lock lock{queue_mutex}; + wait_condition.wait(lock, [this] { + return workers_stopped >= workers_queued || work_done >= work_scheduled; + }); + } private: std::vector threads; - std::queue> requests; + std::queue requests; std::mutex queue_mutex; std::condition_variable condition; std::condition_variable wait_condition; std::atomic_bool stop{}; - std::atomic work_scheduled{}; - std::atomic work_done{}; - std::atomic workers_stopped{}; - std::atomic workers_queued{}; + std::atomic work_scheduled{}; + std::atomic work_done{}; + std::atomic workers_stopped{}; + std::atomic workers_queued{}; + std::string thread_name; }; +using ThreadWorker = StatefulThreadWorker<>; + } // namespace Common -- cgit v1.2.3 From 0ddbbb64e514ea9bba6a4f8bd6908d654e7f114c Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 29 Jun 2021 01:47:13 -0300 Subject: common/thread_worker: Stop workers on stop_token when waiting --- src/common/thread_worker.h | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'src/common/thread_worker.h') diff --git a/src/common/thread_worker.h b/src/common/thread_worker.h index 16aa673bd..8272985ff 100644 --- a/src/common/thread_worker.h +++ b/src/common/thread_worker.h @@ -7,7 +7,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -34,19 +36,19 @@ class StatefulThreadWorker { public: explicit StatefulThreadWorker(size_t num_workers, std::string name, StateMaker func = {}) : workers_queued{num_workers}, thread_name{std::move(name)} { - const auto lambda = [this, func] { + const auto lambda = [this, func](std::stop_token stop_token) { Common::SetCurrentThreadName(thread_name.c_str()); { std::conditional_t state{func()}; - while (!stop) { + while (!stop_token.stop_requested()) { Task task; { std::unique_lock lock{queue_mutex}; if (requests.empty()) { wait_condition.notify_all(); } - condition.wait(lock, [this] { return stop || !requests.empty(); }); - if (stop) { + condition.wait(lock, stop_token, [this] { return !requests.empty(); }); + if (stop_token.stop_requested()) { break; } task = std::move(requests.front()); @@ -63,21 +65,17 @@ public: ++workers_stopped; wait_condition.notify_all(); }; + threads.reserve(num_workers); for (size_t i = 0; i < num_workers; ++i) { threads.emplace_back(lambda); } } - ~StatefulThreadWorker() { - { - std::unique_lock lock{queue_mutex}; - stop = true; - } - condition.notify_all(); - for (std::thread& thread : threads) { - thread.join(); - } - } + StatefulThreadWorker& operator=(const StatefulThreadWorker&) = delete; + StatefulThreadWorker(const StatefulThreadWorker&) = delete; + + StatefulThreadWorker& operator=(StatefulThreadWorker&&) = delete; + StatefulThreadWorker(StatefulThreadWorker&&) = delete; void QueueWork(Task work) { { @@ -88,7 +86,12 @@ public: condition.notify_one(); } - void WaitForRequests() { + void WaitForRequests(std::stop_token stop_token = {}) { + std::stop_callback callback(stop_token, [this] { + for (auto& thread : threads) { + thread.request_stop(); + } + }); std::unique_lock lock{queue_mutex}; wait_condition.wait(lock, [this] { return workers_stopped >= workers_queued || work_done >= work_scheduled; @@ -96,17 +99,16 @@ public: } private: - std::vector threads; std::queue requests; std::mutex queue_mutex; - std::condition_variable condition; + std::condition_variable_any condition; std::condition_variable wait_condition; - std::atomic_bool stop{}; std::atomic work_scheduled{}; std::atomic work_done{}; std::atomic workers_stopped{}; std::atomic workers_queued{}; std::string thread_name; + std::vector threads; }; using ThreadWorker = StatefulThreadWorker<>; -- cgit v1.2.3