1#ifndef UVW_THREAD_INCLUDE_H
2#define UVW_THREAD_INCLUDE_H
20enum class uvw_thread_create_flags : std::underlying_type_t<uv_thread_create_flags> {
21 THREAD_NO_FLAGS = UV_THREAD_NO_FLAGS,
22 THREAD_HAS_STACK_SIZE = UV_THREAD_HAS_STACK_SIZE
45class thread final:
public uv_type<uv_thread_t> {
46 using internal_task = std::function<void(std::shared_ptr<void>)>;
48 static void create_callback(
void *arg);
51 using create_flags = details::uvw_thread_create_flags;
52 using task = internal_task;
53 using type = uv_thread_t;
55 explicit thread(loop::token token, std::shared_ptr<loop> ref, task t, std::shared_ptr<void> d =
nullptr)
noexcept;
61 static type
self() noexcept;
75 static
bool equal(const thread &tl, const thread &tr) noexcept;
98 bool run(create_flags opts, std::
size_t stack = {})
noexcept;
107 std::shared_ptr<
void> data;
118class thread_local_storage final: public uv_type<uv_key_t> {
120 explicit thread_local_storage(loop::token token, std::shared_ptr<loop> ref)
noexcept;
122 ~thread_local_storage()
noexcept;
140 void set(T *value)
noexcept {
151class once final:
public uv_type<uv_once_t> {
152 static uv_once_t *guard()
noexcept;
155 using uv_type::uv_type;
167 static void run(F &&f)
noexcept {
168 using callback_type = void (*)(void);
169 static_assert(std::is_convertible_v<F, callback_type>);
170 callback_type cb = f;
171 uv_once(guard(), cb);
183class mutex final:
public uv_type<uv_mutex_t> {
184 friend class condition;
187 explicit mutex(loop::token token, std::shared_ptr<loop> ref,
bool recursive =
false)
noexcept;
211class rwlock final: public uv_type<uv_rwlock_t> {
213 explicit rwlock(loop::token token, std::shared_ptr<loop> ref)
noexcept;
257class semaphore final: public uv_type<uv_sem_t> {
259 explicit semaphore(loop::token token, std::shared_ptr<loop> ref,
unsigned int value)
noexcept;
261 ~semaphore()
noexcept;
283class condition final: public uv_type<uv_cond_t> {
285 explicit condition(loop::token token, std::shared_ptr<loop> ref)
noexcept;
287 ~condition()
noexcept;
342class barrier final: public uv_type<uv_barrier_t> {
344 explicit barrier(loop::token token, std::shared_ptr<loop> ref,
unsigned int count)
noexcept;
358# include "thread.cpp"
bool wait() noexcept
Synchronizes at a barrier.
void signal() noexcept
Signals a condition.
void wait(mutex &mtx) noexcept
Waits on a condition.
void broadcast() noexcept
Broadcasts a condition.
bool timed_wait(mutex &mtx, uint64_t timeout) noexcept
Waits on a condition.
void unlock() noexcept
Unlocks the mutex.
bool try_lock() noexcept
Tries to lock the mutex.
void lock() noexcept
Locks the mutex.
static void run(F &&f) noexcept
Runs a function once and only once.
void wrunlock() noexcept
Unlocks a read-write lock object previously locked for writing.
void rdlock() noexcept
Locks a read-write lock object for reading.
bool try_rdlock() noexcept
Tries to lock a read-write lock object for reading.
bool try_wrlock() noexcept
Tries to lock a read-write lock object for writing.
void wrlock() noexcept
Locks a read-write lock object for writing.
void rdunlock() noexcept
Unlocks a read-write lock object previously locked for reading.
bool try_wait() noexcept
Tries to lock a semaphore.
void wait() noexcept
Locks a semaphore.
void post() noexcept
Unlocks a semaphore.
The thread local storage wrapper.
T * get() noexcept
Gets the value of a given variable.
void set(T *value) noexcept
Sets the value of a given variable.
static type self() noexcept
Obtains the identifier of the calling thread.
static int getcpu() noexcept
Gets the CPU number on which the calling thread is running.
static bool equal(const thread &tl, const thread &tr) noexcept
Compares thread by means of their identifiers.
bool join() noexcept
Joins with a terminated thread.
bool run() noexcept
Creates a new thread.
const U * raw() const noexcept
Gets the underlying raw data structure.