Skip to content

Commit

Permalink
Refactoring: deduplicate thread body boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterTh committed Nov 21, 2024
1 parent 746975c commit 4e228e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
32 changes: 20 additions & 12 deletions include/thread_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,31 @@
#include "utils.h"

#include <chrono>
#include <concepts>
#include <future>
#include <thread>
#include <type_traits>
#include <variant>

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:
Expand Down Expand Up @@ -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(); });
}
};

Expand Down
15 changes: 3 additions & 12 deletions src/live_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -949,19 +950,9 @@ void live_executor::submit(std::vector<const instruction*> instructions, std::ve
}

void live_executor::thread_main(std::unique_ptr<backend> 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
15 changes: 2 additions & 13 deletions src/scheduler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "log.h"
#include "named_threads.h"
#include "recorders.h"
#include "thread_queue.h"
#include "tracy.h"

#include <matchbox.hh>
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4e228e7

Please sign in to comment.