1#ifndef UVW_THREAD_INCLUDE_H
2#define UVW_THREAD_INCLUDE_H
12#include "underlying_type.hpp"
18enum class UVThreadCreateFlags : std::underlying_type_t<uv_thread_create_flags> {
19 THREAD_NO_FLAGS = UV_THREAD_NO_FLAGS,
20 THREAD_HAS_STACK_SIZE = UV_THREAD_HAS_STACK_SIZE
43class Thread final:
public UnderlyingType<Thread, uv_thread_t> {
44 using InternalTask = std::function<void(std::shared_ptr<void>)>;
46 static void createCallback(
void *arg);
49 using Options = details::UVThreadCreateFlags;
50 using Task = InternalTask;
51 using Type = uv_thread_t;
53 explicit Thread(ConstructorAccess ca, std::shared_ptr<Loop> ref, Task t, std::shared_ptr<void> d =
nullptr)
noexcept;
59 static Type
self() noexcept;
67 static
bool equal(const Thread &tl, const Thread &tr) noexcept;
90 bool run(
Flags<Options> opts, std::
size_t stack = {})
noexcept;
99 std::shared_ptr<
void> data;
110class ThreadLocalStorage final: public UnderlyingType<ThreadLocalStorage, uv_key_t> {
112 explicit ThreadLocalStorage(ConstructorAccess ca, std::shared_ptr<Loop> ref)
noexcept;
114 ~ThreadLocalStorage()
noexcept;
123 return static_cast<T *
>(uv_key_get(UnderlyingType::get()));
132 void set(T *value)
noexcept {
133 return uv_key_set(UnderlyingType::get(), value);
143class Once final:
public UnderlyingType<Once, uv_once_t> {
144 static uv_once_t *guard()
noexcept;
147 using UnderlyingType::UnderlyingType;
159 static void once(F &&f)
noexcept {
160 using CallbackType = void (*)(void);
161 static_assert(std::is_convertible_v<F, CallbackType>);
163 uv_once(guard(), cb);
175class Mutex final:
public UnderlyingType<Mutex, uv_mutex_t> {
176 friend class Condition;
179 explicit Mutex(ConstructorAccess ca, std::shared_ptr<Loop> ref,
bool recursive =
false)
noexcept;
203class RWLock final: public UnderlyingType<RWLock, uv_rwlock_t> {
205 explicit RWLock(ConstructorAccess ca, std::shared_ptr<Loop> ref)
noexcept;
249class Semaphore final: public UnderlyingType<Semaphore, uv_sem_t> {
251 explicit Semaphore(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int value)
noexcept;
253 ~Semaphore()
noexcept;
275class Condition final: public UnderlyingType<Condition, uv_cond_t> {
277 explicit Condition(ConstructorAccess ca, std::shared_ptr<Loop> ref)
noexcept;
279 ~Condition()
noexcept;
334class Barrier final: public UnderlyingType<Barrier, uv_barrier_t> {
336 explicit Barrier(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int count)
noexcept;
350# include "thread.cpp"
bool wait() noexcept
Synchronizes at a barrier.
void broadcast() noexcept
Broadcasts a condition.
void wait(Mutex &mutex) noexcept
Waits on a condition.
void signal() noexcept
Signals a condition.
bool timedWait(Mutex &mutex, uint64_t timeout) noexcept
Waits on a condition.
Utility class to handle flags.
void lock() noexcept
Locks the mutex.
void unlock() noexcept
Unlocks the mutex.
bool tryLock() noexcept
Tries to lock the mutex.
static void once(F &&f) noexcept
Runs a function once and only once.
bool tryWrLock() noexcept
Tries to lock a read-write lock object for writing.
bool tryRdLock() noexcept
Tries to lock a read-write lock object for reading.
void wrUnlock() noexcept
Unlocks a read-write lock object previously locked 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.
void rdLock() noexcept
Locks a read-write lock object for reading.
void post() noexcept
Unlocks a semaphore.
void wait() noexcept
Locks a semaphore.
bool tryWait() noexcept
Tries to lock 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.