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
26class ThreadLocalStorage;
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;
90 bool run(
Flags<Options> opts, std::
size_t stack = {})
noexcept;
99 std::shared_ptr<
void> data;
112 explicit ThreadLocalStorage(ConstructorAccess ca, std::shared_ptr<Loop> ref)
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);
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);
179 explicit Mutex(ConstructorAccess ca, std::shared_ptr<Loop> ref,
bool recursive =
false)
noexcept;
192 bool tryLock() noexcept;
197 void unlock() noexcept;
205 explicit RWLock(ConstructorAccess ca, std::shared_ptr<Loop> ref)
noexcept;
218 bool tryRdLock() noexcept;
223 void rdUnlock() noexcept;
228 void wrLock() noexcept;
234 bool tryWrLock() noexcept;
239 void wrUnlock() noexcept;
251 explicit Semaphore(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int value)
noexcept;
263 void wait() noexcept;
269 bool tryWait() noexcept;
277 explicit Condition(ConstructorAccess ca, std::shared_ptr<Loop> ref)
noexcept;
294 void broadcast() noexcept;
322 bool timedWait(
Mutex &mutex, uint64_t timeout) noexcept;
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 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.