25#ifndef JOIN_CORE_THREAD_HPP
26#define JOIN_CORE_THREAD_HPP
32#include <system_error>
39#include <sys/syscall.h>
67 template <
class Function,
class... Args>
68 explicit Invoker (
int core,
int prio,
size_t stackSize,
bool prefault,
Function&& func, Args&&... args)
72 , _prefault (prefault)
76 pthread_attr_init (&attr);
80 const int err = pthread_attr_setstacksize (&attr, stackSize);
83 pthread_attr_destroy (&attr);
84 throw std::system_error (err, std::system_category (),
"pthread_attr_setstacksize failed");
88 const int err = pthread_create (&_handle, &attr, _routine,
this);
89 pthread_attr_destroy (&attr);
92 throw std::system_error (err, std::system_category (),
"pthread_create failed");
133 static int prefault () noexcept;
140 static
void* _routine (
void* context);
149 std::function<
void ()> _func;
152 pthread_t _handle = {};
155 std::atomic_int _core{-1};
158 std::atomic_int _priority{0};
161 bool _prefault =
false;
164 static constexpr pid_t _invalidId = -1;
167 std::atomic<pid_t> _id{_invalidId};
170 std::atomic_bool _done;
196 template <class
Function, class... Args>
211 template <
class Function,
class... Args>
213 requires (!std::is_arithmetic_v<std::decay_t<Function>>)
228 template <
class Function,
class... Args>
229 explicit Thread (
int core,
int prio,
size_t stackSize,
bool prefault,
Function&& func, Args&&... args)
230 : _invoker (new
Invoker (core, prio, stackSize, prefault,
std::forward<
Function> (func),
231 std::forward<Args> (args)...))
318 bool running () const noexcept;
323 void join () noexcept;
348 static thread_local pid_t tid
__attribute__ ((tls_model (
"initial-exec"))) = 0;
351 static const int registered = ::pthread_atfork (
nullptr,
nullptr, [] () {
354 static_cast<void> (registered);
355 tid =
static_cast<pid_t
> (::syscall (SYS_gettid));
364 pid_t
id () const noexcept;
370 pthread_t
handle () const noexcept;
fixed-capacity move-only allocation-free alternative to std::function.
Definition function.hpp:44
thread invoker class.
Definition thread.hpp:49
Invoker()=delete
default constructor.
~Invoker()=default
destoyer.
Invoker(const Invoker &other)=delete
copy constructor.
Invoker & operator=(const Invoker &other)=delete
copy assignment.
Invoker(Invoker &&other)=delete
move constructor.
thread class.
Definition thread.hpp:180
int priority() const noexcept
get current thread priority.
Definition thread.cpp:265
int affinity() const noexcept
get current thread affinity.
Definition thread.cpp:202
~Thread()
destroys the thread object.
Definition thread.cpp:139
bool tryJoin() noexcept
performs a nonblocking join on the running thread.
Definition thread.cpp:305
Thread() noexcept=default
default constructor.
static constexpr pid_t invalidId
invalid thread id.
Definition thread.hpp:183
Thread(int core, int prio, size_t stackSize, bool prefault, Function &&func, Args &&... args)
creates a new thread object associated with a thread of execution.
Definition thread.hpp:229
static pid_t currentId() noexcept
get calling thread id.
Definition thread.hpp:346
Thread(const Thread &other)=delete
copy constructor.
void swap(Thread &other) noexcept
swap underlying handles of two thread objects.
Definition thread.cpp:332
void cancel() noexcept
cancel the running thread if any.
Definition thread.cpp:319
Thread & operator=(const Thread &other)=delete
copy assignment.
pid_t id() const noexcept
get thread id.
Definition thread.cpp:341
bool joinable() const noexcept
check if thread is joinable.
Definition thread.cpp:274
pthread_t handle() const noexcept
get the handle of the thread of execution.
Definition thread.cpp:350
bool running() const noexcept
check if the thread is running.
Definition thread.cpp:283
Thread(int core, int prio, Function &&func, Args &&... args)
creates a new thread object associated with a thread of execution.
Definition thread.hpp:212
Definition acceptor.hpp:32
__attribute__((cold, noinline)) void throwException(Args &&... args)
throw an exception out of the hot path.
Definition utils.hpp:75
#define JOIN_UNLIKELY(x)
Definition utils.hpp:51