uvw 2.12.1
Loading...
Searching...
No Matches
thread.h
1#ifndef UVW_THREAD_INCLUDE_H
2#define UVW_THREAD_INCLUDE_H
3
4#include <cstddef>
5#include <memory>
6#include <string>
7#include <type_traits>
8#include <utility>
9#include <uv.h>
10#include "loop.h"
11#include "underlying_type.hpp"
12
13namespace uvw {
14
15namespace details {
16
17enum class UVThreadCreateFlags : std::underlying_type_t<uv_thread_create_flags> {
18 THREAD_NO_FLAGS = UV_THREAD_NO_FLAGS,
19 THREAD_HAS_STACK_SIZE = UV_THREAD_HAS_STACK_SIZE
20};
21
22}
23
24class Thread;
25class ThreadLocalStorage;
26class Once;
27class Mutex;
28class RWLock;
29class Semaphore;
30class Condition;
31class Barrier;
32
42class Thread final: public UnderlyingType<Thread, uv_thread_t> {
43 using InternalTask = std::function<void(std::shared_ptr<void>)>;
44
45 static void createCallback(void *arg);
46
47public:
48 using Options = details::UVThreadCreateFlags;
49 using Task = InternalTask;
50 using Type = uv_thread_t;
51
52 explicit Thread(ConstructorAccess ca, std::shared_ptr<Loop> ref, Task t, std::shared_ptr<void> d = nullptr) noexcept;
53
58 static Type self() noexcept;
59
66 static bool equal(const Thread &tl, const Thread &tr) noexcept;
67
68 ~Thread() noexcept;
69
74 bool run() noexcept;
75
89 bool run(Flags<Options> opts, std::size_t stack = {}) noexcept;
90
95 bool join() noexcept;
96
97private:
98 std::shared_ptr<void> data;
99 Task task;
100};
101
110public:
111 explicit ThreadLocalStorage(ConstructorAccess ca, std::shared_ptr<Loop> ref) noexcept;
112
113 ~ThreadLocalStorage() noexcept;
114
120 template<typename T>
121 T *get() noexcept {
122 return static_cast<T *>(uv_key_get(UnderlyingType::get()));
123 }
124
130 template<typename T>
131 void set(T *value) noexcept {
132 return uv_key_set(UnderlyingType::get(), value);
133 }
134};
135
142class Once final: public UnderlyingType<Once, uv_once_t> {
143 static uv_once_t *guard() noexcept;
144
145public:
146 using UnderlyingType::UnderlyingType;
147
157 template<typename F>
158 static void once(F &&f) noexcept {
159 using CallbackType = void (*)(void);
160 static_assert(std::is_convertible_v<F, CallbackType>);
161 CallbackType cb = f;
162 uv_once(guard(), cb);
163 }
164};
165
174class Mutex final: public UnderlyingType<Mutex, uv_mutex_t> {
175 friend class Condition;
176
177public:
178 explicit Mutex(ConstructorAccess ca, std::shared_ptr<Loop> ref, bool recursive = false) noexcept;
179
180 ~Mutex() noexcept;
181
185 void lock() noexcept;
186
191 bool tryLock() noexcept;
192
196 void unlock() noexcept;
197};
198
202class RWLock final: public UnderlyingType<RWLock, uv_rwlock_t> {
203public:
204 explicit RWLock(ConstructorAccess ca, std::shared_ptr<Loop> ref) noexcept;
205
206 ~RWLock() noexcept;
207
211 void rdLock() noexcept;
212
217 bool tryRdLock() noexcept;
218
222 void rdUnlock() noexcept;
223
227 void wrLock() noexcept;
228
233 bool tryWrLock() noexcept;
234
238 void wrUnlock() noexcept;
239};
240
248class Semaphore final: public UnderlyingType<Semaphore, uv_sem_t> {
249public:
250 explicit Semaphore(ConstructorAccess ca, std::shared_ptr<Loop> ref, unsigned int value) noexcept;
251
252 ~Semaphore() noexcept;
253
257 void post() noexcept;
258
262 void wait() noexcept;
263
268 bool tryWait() noexcept;
269};
270
274class Condition final: public UnderlyingType<Condition, uv_cond_t> {
275public:
276 explicit Condition(ConstructorAccess ca, std::shared_ptr<Loop> ref) noexcept;
277
278 ~Condition() noexcept;
279
286 void signal() noexcept;
287
293 void broadcast() noexcept;
294
304 void wait(Mutex &mutex) noexcept;
305
321 bool timedWait(Mutex &mutex, uint64_t timeout) noexcept;
322};
323
333class Barrier final: public UnderlyingType<Barrier, uv_barrier_t> {
334public:
335 explicit Barrier(ConstructorAccess ca, std::shared_ptr<Loop> ref, unsigned int count) noexcept;
336
337 ~Barrier() noexcept;
338
343 bool wait() noexcept;
344};
345
346} // namespace uvw
347
348#ifndef UVW_AS_LIB
349# include "thread.cpp"
350#endif
351
352#endif // UVW_THREAD_INCLUDE_H
The Barrier wrapper.
Definition: thread.h:333
bool wait() noexcept
Synchronizes at a barrier.
The Condition wrapper.
Definition: thread.h:274
void signal() noexcept
Signals a condition.
Utility class to handle flags.
Definition: util.h:79
The Mutex wrapper.
Definition: thread.h:174
void lock() noexcept
Locks the mutex.
The Once wrapper.
Definition: thread.h:142
static void once(F &&f) noexcept
Runs a function once and only once.
Definition: thread.h:158
The RWLock wrapper.
Definition: thread.h:202
void rdLock() noexcept
Locks a read-write lock object for reading.
The Semaphore wrapper.
Definition: thread.h:248
void post() noexcept
Unlocks a semaphore.
The ThreadLocalStorage wrapper.
Definition: thread.h:109
void set(T *value) noexcept
Sets the value of a given variable.
Definition: thread.h:131
T * get() noexcept
Gets the value of a given variable.
Definition: thread.h:121
The Thread wrapper.
Definition: thread.h:42
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.
uvw default namespace.
Definition: async.h:8