15#include <system_error>
20#include "CompilerSupport.hpp"
71 std::promise<void> startPromise;
72 std::shared_future<void> startFuture;
73 void * runnable =
nullptr;
88 template<
Runnable R,
typename ... Args>
89 Thread(std::in_place_type_t<R>, std::string threadName, Args && ... args)
90 : _st {
std::make_shared<State>() }
92 _st->startFuture = _st->startPromise.get_future();
97 _thread = std::jthread([newName = std::move(threadName), ...args = std::forward<Args>(args), state = _st](std::stop_token stopToken)
mutable noexcept
102 DFX_UTILITIES_LOG_WARN(
"Failed to set name '{}' for thread ID {}: {}", newName, gettid(), ::strerror(ec.value()));
104 auto logger = DFX_UTILITIES_LOGGER();
107 R runnable(std::move(args)...);
108 if (DFX_UNLIKELY(stopToken.stop_requested()))
111 state->runnable =
static_cast<void *
>(&runnable);
112 state->startPromise.set_value();
114 runnable.exec(std::move(stopToken));
120 catch (std::exception
const & e)
121 {
if (logger !=
nullptr) SPDLOG_LOGGER_ERROR(logger,
"An error occured: {}", e.what()); }
123 {
if (logger !=
nullptr) SPDLOG_LOGGER_ERROR(logger,
"An unknown error occured"); }
137 {
return _st->startFuture.get(); }
142 void stop(
bool waitForFinished =
false) noexcept;
153 R &
getRunnable()
const {
return *
static_cast<R *
>(_st->runnable); }
161 template<
Runnable R,
typename ... Args>
164 return Thread(std::in_place_type<R>, std::move(threadName), std::forward<Args>(args)...);
175 static void setName(std::string_view name, std::error_code & ec)
noexcept;
192 static std::string
getName(std::error_code & ec)
noexcept;
203 std::jthread _thread;
204 std::shared_ptr<State> _st;
Convenience macros to explicitly control copy and move semantics.
#define DFX_ENABLE_DEFAULT_MOVE_DISABLE_COPY_NOEXCEPT(ClassName)
Enable default move and disable copy noexcept.
Definition CopyMoveControl.hpp:77
Exception utilities for dfx (source-location aware exceptions, nested stacks, and safe invocation hel...
Runtime error that captures a std::source_location.
Definition Exception.hpp:52
static void printToLogger(Exception const &e, spdlog::level::level_enum lvl=spdlog::level::err, std::string const &loggerName="", std::size_t initialIndentLevel=0, std::string const &logPattern=noFileLogPattern, std::string const ¤tLogPattern=defaultLogPattern)
Log an exception (including nested exceptions) using a logger name.
A managed execution wrapper that ensures a Runnable's full lifecycle occurs within the worker thread.
Definition Thread.hpp:67
std::jthread const & nativeThread() const noexcept
Access the underlying jthread instance.
Definition Thread.hpp:145
void stop(bool waitForFinished=false) noexcept
Signals the worker thread to stop.
void waitForStarted()
Blocks until the worker thread has reached the start of the Runnable lifecycle.
Definition Thread.hpp:136
static Thread create(std::string threadName, Args &&... args)
Factory method for creating a Thread.
Definition Thread.hpp:162
R & getRunnable() const
Access the constructed Runnable.
Definition Thread.hpp:153
static void setName(std::string_view name, std::error_code &ec) noexcept
Set the calling thread name.
Thread(std::in_place_type_t< R >, std::string threadName, Args &&... args)
Constructs the Thread and enters the worker loop.
Definition Thread.hpp:89
static void setName(std::string_view name)
Set the calling thread name.
static std::string getName(std::error_code &ec) noexcept
Get the calling thread name.
static std::string getName()
Get the calling thread name.
Concept for a task that can be executed with a stop token.
Definition Thread.hpp:34
Concept for a task that can be executed without a stop token.
Definition Thread.hpp:41
Concept defining a valid Thread worker.
Definition Thread.hpp:49
Definition SystemConfigCommandHandler.hpp:15