uvw 3.1.0
Loading...
Searching...
No Matches
thread.h
1#ifndef UVW_THREAD_INCLUDE_H
2#define UVW_THREAD_INCLUDE_H
3
4#include <cstddef>
5#include <cstdint>
6#include <memory>
7#include <string>
8#include <type_traits>
9#include <utility>
10#include <uv.h>
11#include "config.h"
12#include "enum.hpp"
13#include "loop.h"
14#include "uv_type.hpp"
15
16namespace uvw {
17
18namespace details {
19
20enum class uvw_thread_create_flags : 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
23};
24
25}
26
27class thread;
28class thread_local_storage;
29class once;
30class mutex;
31class rwlock;
32class semaphore;
33class condition;
34class barrier;
35
45class thread final: public uv_type<uv_thread_t> {
46 using internal_task = std::function<void(std::shared_ptr<void>)>;
47
48 static void create_callback(void *arg);
49
50public:
51 using create_flags = details::uvw_thread_create_flags;
52 using task = internal_task;
53 using type = uv_thread_t;
54
55 explicit thread(loop::token token, std::shared_ptr<loop> ref, task t, std::shared_ptr<void> d = nullptr) noexcept;
56
61 static type self() noexcept;
62
67 static int getcpu() noexcept;
68
75 static bool equal(const thread &tl, const thread &tr) noexcept;
76
77 ~thread() noexcept;
78
83 bool run() noexcept;
84
98 bool run(create_flags opts, std::size_t stack = {}) noexcept;
99
104 bool join() noexcept;
105
106private:
107 std::shared_ptr<void> data;
108 task func;
109};
110
118class thread_local_storage final: public uv_type<uv_key_t> {
119public:
120 explicit thread_local_storage(loop::token token, std::shared_ptr<loop> ref) noexcept;
121
122 ~thread_local_storage() noexcept;
123
129 template<typename T>
130 T *get() noexcept {
131 return static_cast<T *>(uv_key_get(uv_type::raw()));
132 }
133
139 template<typename T>
140 void set(T *value) noexcept {
141 return uv_key_set(uv_type::raw(), value);
142 }
143};
144
151class once final: public uv_type<uv_once_t> {
152 static uv_once_t *guard() noexcept;
153
154public:
155 using uv_type::uv_type;
156
166 template<typename F>
167 static void run(F &&f) noexcept {
168 using callback_type = void (*)(void);
169 static_assert(std::is_convertible_v<F, callback_type>);
170 callback_type cb = f;
171 uv_once(guard(), cb);
172 }
173};
174
183class mutex final: public uv_type<uv_mutex_t> {
184 friend class condition;
185
186public:
187 explicit mutex(loop::token token, std::shared_ptr<loop> ref, bool recursive = false) noexcept;
188
189 ~mutex() noexcept;
190
194 void lock() noexcept;
195
200 bool try_lock() noexcept;
201
205 void unlock() noexcept;
206};
207
211class rwlock final: public uv_type<uv_rwlock_t> {
212public:
213 explicit rwlock(loop::token token, std::shared_ptr<loop> ref) noexcept;
214
215 ~rwlock() noexcept;
216
220 void rdlock() noexcept;
221
226 bool try_rdlock() noexcept;
227
231 void rdunlock() noexcept;
232
236 void wrlock() noexcept;
237
242 bool try_wrlock() noexcept;
243
247 void wrunlock() noexcept;
248};
249
257class semaphore final: public uv_type<uv_sem_t> {
258public:
259 explicit semaphore(loop::token token, std::shared_ptr<loop> ref, unsigned int value) noexcept;
260
261 ~semaphore() noexcept;
262
266 void post() noexcept;
267
271 void wait() noexcept;
272
277 bool try_wait() noexcept;
278};
279
283class condition final: public uv_type<uv_cond_t> {
284public:
285 explicit condition(loop::token token, std::shared_ptr<loop> ref) noexcept;
286
287 ~condition() noexcept;
288
295 void signal() noexcept;
296
302 void broadcast() noexcept;
303
313 void wait(mutex &mtx) noexcept;
314
330 bool timed_wait(mutex &mtx, uint64_t timeout) noexcept;
331};
332
342class barrier final: public uv_type<uv_barrier_t> {
343public:
344 explicit barrier(loop::token token, std::shared_ptr<loop> ref, unsigned int count) noexcept;
345
346 ~barrier() noexcept;
347
352 bool wait() noexcept;
353};
354
355} // namespace uvw
356
357#ifndef UVW_AS_LIB
358# include "thread.cpp"
359#endif
360
361#endif // UVW_THREAD_INCLUDE_H
The barrier wrapper.
Definition: thread.h:342
bool wait() noexcept
Synchronizes at a barrier.
The condition wrapper.
Definition: thread.h:283
void signal() noexcept
Signals a condition.
The mutex wrapper.
Definition: thread.h:183
void lock() noexcept
Locks the mutex.
The once wrapper.
Definition: thread.h:151
static void run(F &&f) noexcept
Runs a function once and only once.
Definition: thread.h:167
The rwlock wrapper.
Definition: thread.h:211
void rdlock() noexcept
Locks a read-write lock object for reading.
The semaphore wrapper.
Definition: thread.h:257
void post() noexcept
Unlocks a semaphore.
The thread local storage wrapper.
Definition: thread.h:118
T * get() noexcept
Gets the value of a given variable.
Definition: thread.h:130
void set(T *value) noexcept
Sets the value of a given variable.
Definition: thread.h:140
The thread wrapper.
Definition: thread.h:45
static type self() noexcept
Obtains the identifier of the calling thread.
static int getcpu() noexcept
Gets the CPU number on which the calling thread is running.
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.
uvw default namespace.
Definition: async.h:8
Wrapper class for underlying types.
Definition: uv_type.hpp:18
const U * raw() const noexcept
Gets the underlying raw data structure.
Definition: uv_type.hpp:59