1 #ifndef UVW_THREAD_INCLUDE_H
2 #define UVW_THREAD_INCLUDE_H
12 #include "underlying_type.hpp"
21 enum class UVThreadCreateFlags: std::underlying_type_t<uv_thread_create_flags> {
22 THREAD_NO_FLAGS = UV_THREAD_NO_FLAGS,
23 THREAD_HAS_STACK_SIZE = UV_THREAD_HAS_STACK_SIZE
31 class ThreadLocalStorage;
50 using InternalTask = std::function<void(std::shared_ptr<void>)>;
52 static void createCallback(
void *arg);
55 using Options = details::UVThreadCreateFlags;
56 using Task = InternalTask;
57 using Type = uv_thread_t;
59 explicit Thread(ConstructorAccess ca, std::shared_ptr<Loop> ref, Task t, std::shared_ptr<void> d =
nullptr) noexcept;
65 static Type
self() noexcept;
96 bool run(
Flags<Options> opts, std::
size_t stack = {}) noexcept;
105 std::shared_ptr<
void> data;
119 explicit ThreadLocalStorage(ConstructorAccess ca, std::shared_ptr<Loop> ref) noexcept;
130 return static_cast<T*
>(uv_key_get(UnderlyingType::get()));
139 void set(T *value) noexcept {
140 return uv_key_set(UnderlyingType::get(), value);
152 static uv_once_t* guard() noexcept;
155 using UnderlyingType::UnderlyingType;
167 static void once(F &&f) noexcept {
168 using CallbackType = void(*)(void);
169 static_assert(std::is_convertible_v<F, CallbackType>);
171 uv_once(guard(), cb);
188 explicit Mutex(ConstructorAccess ca, std::shared_ptr<Loop> ref,
bool recursive =
false) noexcept;
201 bool tryLock() noexcept;
206 void unlock() noexcept;
215 explicit RWLock(ConstructorAccess ca, std::shared_ptr<Loop> ref) noexcept;
228 bool tryRdLock() noexcept;
233 void rdUnlock() noexcept;
238 void wrLock() noexcept;
244 bool tryWrLock() noexcept;
249 void wrUnlock() noexcept;
262 explicit Semaphore(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int value) noexcept;
274 void wait() noexcept;
280 bool tryWait() noexcept;
289 explicit Condition(ConstructorAccess ca, std::shared_ptr<Loop> ref) noexcept;
306 void broadcast() noexcept;
334 bool timedWait(
Mutex &mutex, uint64_t timeout) noexcept;
349 explicit Barrier(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int count) noexcept;
365 #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.
Wrapper class for underlying types.