1#ifndef UVW_THREAD_INCLUDE_H
2#define UVW_THREAD_INCLUDE_H
11#include "underlying_type.hpp"
17enum class UVThreadCreateFlags : std::underlying_type_t<uv_thread_create_flags> {
18 THREAD_NO_FLAGS = UV_THREAD_NO_FLAGS,
19 THREAD_HAS_STACK_SIZE = UV_THREAD_HAS_STACK_SIZE
25class ThreadLocalStorage;
43 using InternalTask = std::function<void(std::shared_ptr<void>)>;
45 static void createCallback(
void *arg);
48 using Options = details::UVThreadCreateFlags;
49 using Task = InternalTask;
50 using Type = uv_thread_t;
52 explicit Thread(ConstructorAccess ca, std::shared_ptr<Loop> ref, Task t, std::shared_ptr<void> d =
nullptr)
noexcept;
58 static Type
self() noexcept;
89 bool run(
Flags<Options> opts, std::
size_t stack = {})
noexcept;
98 std::shared_ptr<
void> data;
111 explicit ThreadLocalStorage(ConstructorAccess ca, std::shared_ptr<Loop> ref)
noexcept;
122 return static_cast<T *
>(uv_key_get(UnderlyingType::get()));
131 void set(T *value)
noexcept {
132 return uv_key_set(UnderlyingType::get(), value);
143 static uv_once_t *guard()
noexcept;
146 using UnderlyingType::UnderlyingType;
158 static void once(F &&f)
noexcept {
159 using CallbackType = void (*)(void);
160 static_assert(std::is_convertible_v<F, CallbackType>);
162 uv_once(guard(), cb);
178 explicit Mutex(ConstructorAccess ca, std::shared_ptr<Loop> ref,
bool recursive =
false)
noexcept;
191 bool tryLock() noexcept;
196 void unlock() noexcept;
204 explicit RWLock(ConstructorAccess ca, std::shared_ptr<Loop> ref)
noexcept;
217 bool tryRdLock() noexcept;
222 void rdUnlock() noexcept;
227 void wrLock() noexcept;
233 bool tryWrLock() noexcept;
238 void wrUnlock() noexcept;
250 explicit Semaphore(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int value)
noexcept;
262 void wait() noexcept;
268 bool tryWait() noexcept;
276 explicit Condition(ConstructorAccess ca, std::shared_ptr<Loop> ref)
noexcept;
293 void broadcast() noexcept;
321 bool timedWait(
Mutex &mutex, uint64_t timeout) noexcept;
335 explicit Barrier(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int count)
noexcept;
349# include "thread.cpp"
bool wait() noexcept
Synchronizes at a barrier.
void signal() noexcept
Signals a condition.
Utility class to handle flags.
void lock() noexcept
Locks the mutex.
static void once(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 ThreadLocalStorage wrapper.
void set(T *value) noexcept
Sets the value of a given variable.
T * get() noexcept
Gets the value of a given variable.
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.
static Type self() noexcept
Obtains the identifier of the calling thread.
Wrapper class for underlying types.