11 #include "underlying_type.hpp" 20 enum class UVThreadCreateFlags: std::underlying_type_t<uv_thread_create_flags> {
21 THREAD_NO_FLAGS = UV_THREAD_NO_FLAGS,
22 THREAD_HAS_STACK_SIZE = UV_THREAD_HAS_STACK_SIZE
30 class ThreadLocalStorage;
49 using InternalTask = std::function<void(std::shared_ptr<void>)>;
51 static void createCallback(
void *arg) {
53 thread.task(thread.data);
57 using Options = details::UVThreadCreateFlags;
58 using Task = InternalTask;
59 using Type = uv_thread_t;
61 explicit Thread(ConstructorAccess ca, std::shared_ptr<Loop> ref, Task t, std::shared_ptr<void> d =
nullptr) noexcept
62 :
UnderlyingType{ca, std::move(ref)}, data{std::move(d)}, task{std::move(t)}
69 static Type
self() noexcept {
70 return uv_thread_self();
80 return !(0 == uv_thread_equal(tl.get(), tr.get()));
92 return (0 == uv_thread_create(
get(), &createCallback,
this));
109 uv_thread_options_t params{opts, stack};
110 return (0 == uv_thread_create_ex(
get(), ¶ms, &createCallback,
this));
118 return (0 == uv_thread_join(
get()));
122 std::shared_ptr<void> data;
139 uv_key_create(UnderlyingType::get());
143 uv_key_delete(UnderlyingType::get());
153 return static_cast<T*
>(uv_key_get(UnderlyingType::get()));
162 void set(T *value) noexcept {
163 return uv_key_set(UnderlyingType::get(), value);
175 static uv_once_t* guard() noexcept {
176 static uv_once_t once = UV_ONCE_INIT;
181 using UnderlyingType::UnderlyingType;
193 static void once(F &&f) noexcept {
194 using CallbackType = void(*)(void);
195 static_assert(std::is_convertible_v<F, CallbackType>);
197 uv_once(guard(), cb);
214 explicit Mutex(ConstructorAccess ca, std::shared_ptr<Loop> ref,
bool recursive =
false) noexcept
218 uv_mutex_init_recursive(
get());
220 uv_mutex_init(
get());
225 uv_mutex_destroy(
get());
232 uv_mutex_lock(
get());
240 return (0 == uv_mutex_trylock(
get()));
247 uv_mutex_unlock(
get());
257 explicit RWLock(ConstructorAccess ca, std::shared_ptr<Loop> ref) noexcept
260 uv_rwlock_init(
get());
264 uv_rwlock_destroy(
get());
271 uv_rwlock_rdlock(
get());
279 return (0 == uv_rwlock_tryrdlock(
get()));
286 uv_rwlock_rdunlock(
get());
293 uv_rwlock_wrlock(
get());
301 return (0 == uv_rwlock_trywrlock(
get()));
308 uv_rwlock_wrunlock(
get());
322 explicit Semaphore(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int value) noexcept
325 uv_sem_init(
get(), value);
329 uv_sem_destroy(
get());
351 return (0 == uv_sem_trywait(
get()));
361 explicit Condition(ConstructorAccess ca, std::shared_ptr<Loop> ref) noexcept
368 uv_cond_destroy(
get());
378 uv_cond_signal(
get());
387 uv_cond_broadcast(
get());
400 uv_cond_wait(
get(), mutex.get());
419 return (0 == uv_cond_timedwait(
get(), mutex.get(), timeout));
435 explicit Barrier(ConstructorAccess ca, std::shared_ptr<Loop> ref,
unsigned int count) noexcept
438 uv_barrier_init(
get(), count);
442 uv_barrier_destroy(
get());
450 return (0 == uv_barrier_wait(
get()));
bool wait() noexcept
Synchronizes at a barrier.
void wait(Mutex &mutex) noexcept
Waits on a condition.
void wait() noexcept
Locks a semaphore.
void signal() noexcept
Signals a condition.
Wrapper class for underlying types.
void unlock() noexcept
Unlocks the mutex.
bool tryWrLock() noexcept
Tries to lock a read-write lock object for writing.
void wrLock() noexcept
Locks a read-write lock object for writing.
Utility class to handle flags.
bool timedWait(Mutex &mutex, uint64_t timeout) noexcept
Waits on a condition.
bool tryLock() noexcept
Tries to lock the mutex.
static bool equal(const Thread &tl, const Thread &tr) noexcept
Compares thread by means of their identifiers.
void rdLock() noexcept
Locks a read-write lock object for reading.
bool run() noexcept
Creates a new thread.
bool run(Flags< Options > opts, std::size_t stack={}) noexcept
Creates a new thread.
bool join() noexcept
Joins with a terminated thread.
void broadcast() noexcept
Broadcasts a condition.
static void once(F &&f) noexcept
Runs a function once and only once.
bool tryRdLock() noexcept
Tries to lock a read-write lock object for reading.
void lock() noexcept
Locks the mutex.
void rdUnlock() noexcept
Unlocks a read-write lock object previously locked for reading.
bool tryWait() noexcept
Tries to lock a semaphore.
void post() noexcept
Unlocks a semaphore.
void wrUnlock() noexcept
Unlocks a read-write lock object previously locked for writing.
The ThreadLocalStorage wrapper.