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;
102 bool join() 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;
195 void lock() noexcept;
215 explicit RWLock(ConstructorAccess ca, std::shared_ptr<Loop> ref) noexcept;
262 explicit Semaphore(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int value) noexcept;
269 void post() noexcept;
274 void wait() noexcept;
289 explicit Condition(ConstructorAccess ca, std::shared_ptr<Loop> ref) noexcept;
349 explicit Barrier(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int count) noexcept;
357 bool wait() noexcept;
365 #include "thread.cpp"
368 #endif // UVW_THREAD_INCLUDE_H