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
28class thread_local_storage;
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;
98 bool run(create_flags opts, std::
size_t stack = {})
noexcept;
107 std::shared_ptr<
void> data;
140 void set(T *value)
noexcept {
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);
187 explicit mutex(loop::token token, std::shared_ptr<loop> ref,
bool recursive =
false)
noexcept;
200 bool try_lock() noexcept;
205 void unlock() noexcept;
213 explicit rwlock(loop::token token, std::shared_ptr<loop> ref)
noexcept;
226 bool try_rdlock() noexcept;
231 void rdunlock() noexcept;
236 void wrlock() noexcept;
242 bool try_wrlock() noexcept;
247 void wrunlock() noexcept;
259 explicit semaphore(loop::token token, std::shared_ptr<loop> ref,
unsigned int value)
noexcept;
271 void wait() noexcept;
277 bool try_wait() noexcept;
285 explicit condition(loop::token token, std::shared_ptr<loop> ref)
noexcept;
302 void broadcast() noexcept;
330 bool timed_wait(
mutex &mtx, uint64_t timeout) noexcept;
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 lock() noexcept
Locks the mutex.
static void run(F &&f) noexcept
Runs a function once and only once.
void rdlock() noexcept
Locks a read-write lock object for reading.
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.
Wrapper class for underlying types.
const U * raw() const noexcept
Gets the underlying raw data structure.