From 4e228e7131e38f1c58a120b892b01931878832df Mon Sep 17 00:00:00 2001 From: Peter Thoman Date: Thu, 21 Nov 2024 23:27:41 +0100 Subject: [PATCH] Refactoring: deduplicate thread body boilerplate --- include/thread_queue.h | 32 ++++++++++++++++++++------------ src/live_executor.cc | 15 +++------------ src/scheduler.cc | 15 ++------------- 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/include/thread_queue.h b/include/thread_queue.h index 85e078b0..5fdc4f03 100644 --- a/include/thread_queue.h +++ b/include/thread_queue.h @@ -8,6 +8,7 @@ #include "utils.h" #include +#include #include #include #include @@ -15,6 +16,23 @@ namespace celerity::detail { +// A helper to implement the body of a worker thread while taking care of all boilerplate code +void thread_body(const named_threads::thread_type t_type, const std::invocable auto& fun) { + thread_pinning::name_and_pin_thread(t_type); + const auto name = named_threads::thread_type_to_string(t_type); + CELERITY_DETAIL_TRACY_SET_THREAD_NAME_AND_ORDER(tracy_detail::leak_name(name), tracy_detail::thread_order::thread_queue); + try { + fun(); + } + // LCOV_EXCL_START + catch(std::exception& e) { // + utils::panic("exception in {}: {}", name, e.what()); + } catch(...) { // + utils::panic("exception in {}", name); + } + // LCOV_EXCL_STOP +} + /// A single-thread job queue accepting functors and returning events that conditionally forward job results. class thread_queue { public: @@ -141,18 +159,8 @@ class thread_queue { } } - void thread_main(const named_threads::thread_type& t_type) { - thread_pinning::name_and_pin_thread(t_type); - const auto name = named_threads::thread_type_to_string(t_type); - CELERITY_DETAIL_TRACY_SET_THREAD_NAME_AND_ORDER(tracy_detail::leak_name(name), tracy_detail::thread_order::thread_queue); - - try { - loop(); - } catch(std::exception& e) { // - utils::panic("exception in {}: {}", name, e.what()); - } catch(...) { // - utils::panic("exception in {}", name); - } + void thread_main(const named_threads::thread_type t_type) { + thread_body(t_type, [this] { loop(); }); } }; diff --git a/src/live_executor.cc b/src/live_executor.cc index 796dfaeb..3ca5e925 100644 --- a/src/live_executor.cc +++ b/src/live_executor.cc @@ -10,6 +10,7 @@ #include "out_of_order_engine.h" #include "receive_arbiter.h" #include "system_info.h" +#include "thread_queue.h" #include "tracy.h" #include "types.h" #include "utils.h" @@ -949,19 +950,9 @@ void live_executor::submit(std::vector instructions, std::ve } void live_executor::thread_main(std::unique_ptr backend, executor::delegate* const dlg, const policy_set& policy) { - thread_pinning::name_and_pin_thread(named_threads::thread_type::executor); - const auto name = named_threads::thread_type_to_string(named_threads::thread_type::executor); - CELERITY_DETAIL_TRACY_SET_THREAD_NAME_AND_ORDER(tracy_detail::leak_name(name), tracy_detail::thread_order::executor); - - try { + thread_body(named_threads::thread_type::executor, [&] { // live_executor_detail::executor_impl(std::move(backend), m_root_comm.get(), m_submission_queue, dlg, policy).run(); - } - // LCOV_EXCL_START - catch(const std::exception& e) { - CELERITY_CRITICAL("[{}] {}", name, e.what()); - std::abort(); - } - // LCOV_EXCL_STOP + }); } } // namespace celerity::detail diff --git a/src/scheduler.cc b/src/scheduler.cc index 56b49b3b..ffb1ab73 100644 --- a/src/scheduler.cc +++ b/src/scheduler.cc @@ -6,6 +6,7 @@ #include "log.h" #include "named_threads.h" #include "recorders.h" +#include "thread_queue.h" #include "tracy.h" #include @@ -124,19 +125,7 @@ namespace detail { } void scheduler::thread_main() { - thread_pinning::name_and_pin_thread(named_threads::thread_type::scheduler); - const auto name = named_threads::thread_type_to_string(named_threads::thread_type::scheduler); - CELERITY_DETAIL_TRACY_SET_THREAD_NAME_AND_ORDER(tracy_detail::leak_name(name), tracy_detail::thread_order::scheduler); - - try { - schedule(); - } - // LCOV_EXCL_START - catch(const std::exception& e) { - CELERITY_CRITICAL("[{}] {}", name, e.what()); - std::abort(); - } - // LCOV_EXCL_STOP + thread_body(named_threads::thread_type::scheduler, [&] { schedule(); }); } } // namespace detail