uvw 3.0.0
Loading...
Searching...
No Matches
loop.h
1#ifndef UVW_LOOP_INCLUDE_H
2#define UVW_LOOP_INCLUDE_H
3
4#ifdef _WIN32
5# include <ciso646>
6#endif
7
8#include <chrono>
9#include <functional>
10#include <memory>
11#include <type_traits>
12#include <utility>
13#include <uv.h>
14#include "config.h"
15#include "emitter.h"
16#include "util.h"
17
18namespace uvw {
19
20class async_handle;
21class check_handle;
22class fs_event_handle;
23class fs_poll_handle;
24class idle_handle;
25class pipe_handle;
26class poll_handle;
27class prepare_handle;
28class process_handle;
29class signal_handle;
30class tcp_handle;
31class timer_handle;
32class tty_handle;
33class udp_handle;
34
35namespace details {
36
37enum class uvw_loop_option : std::underlying_type_t<uv_loop_option> {
38 BLOCK_SIGNAL = UV_LOOP_BLOCK_SIGNAL,
39 IDLE_TIME = UV_METRICS_IDLE_TIME
40};
41
42enum class uvw_run_mode : std::underlying_type_t<uv_run_mode> {
43 DEFAULT = UV_RUN_DEFAULT,
44 ONCE = UV_RUN_ONCE,
45 NOWAIT = UV_RUN_NOWAIT
46};
47
48} // namespace details
49
58class loop final: public emitter<loop>, public std::enable_shared_from_this<loop> {
59 using deleter = void (*)(uv_loop_t *);
60
61 template<typename, typename, typename...>
62 friend class resource;
63
64 class uv_token {
65 friend class loop;
66 explicit uv_token(int) {}
67 };
68
69 loop(std::unique_ptr<uv_loop_t, deleter> ptr) noexcept;
70
71public:
72 using token = uv_token;
73 using time = std::chrono::duration<uint64_t, std::milli>;
74 using option = details::uvw_loop_option;
75 using run_mode = details::uvw_run_mode;
76
81 static std::shared_ptr<loop> create();
82
93 static std::shared_ptr<loop> create(uv_loop_t *res);
94
107 static std::shared_ptr<loop> get_default();
108
109 loop(const loop &) = delete;
110 loop(loop &&other) = delete;
111
112 loop &operator=(const loop &) = delete;
113 loop &operator=(loop &&other) = delete;
114
115 ~loop() noexcept;
116
136 template<typename... Args>
137 int configure(option flag, Args &&...args) {
138 return uv_loop_configure(uv_loop.get(), static_cast<uv_loop_option>(flag), std::forward<Args>(args)...);
139 }
140
151 template<typename R, typename... Args>
152 std::shared_ptr<R> resource(Args &&...args) {
153 auto ptr = uninitialized_resource<R>(std::forward<Args>(args)...);
154 ptr = (ptr->init() == 0) ? ptr : nullptr;
155 return ptr;
156 }
157
162 template<typename R, typename... Args>
163 std::shared_ptr<R> uninitialized_resource(Args &&...args) {
164 return std::make_shared<R>(token{0}, shared_from_this(), std::forward<Args>(args)...);
165 }
166
175 int close();
176
195 int run(run_mode mode = run_mode::DEFAULT) noexcept;
196
201 bool alive() const noexcept;
202
211 void stop() noexcept;
212
222 int descriptor() const noexcept;
223
230 std::pair<bool, time> timeout() const noexcept;
231
237 time idle_time() const noexcept;
238
251 time now() const noexcept;
252
262 void update() const noexcept;
263
271 template<typename Func>
272 void walk(Func callback) {
273 auto func = [](uv_handle_t *hndl, void *func) {
274 if(hndl->data) {
275 auto &cb = *static_cast<Func *>(func);
276
277 switch(utilities::guess_handle(handle_category{hndl->type})) {
278 case handle_type::ASYNC:
279 cb(*static_cast<async_handle *>(hndl->data));
280 break;
281 case handle_type::CHECK:
282 cb(*static_cast<check_handle *>(hndl->data));
283 break;
284 case handle_type::FS_EVENT:
285 cb(*static_cast<fs_event_handle *>(hndl->data));
286 break;
287 case handle_type::FS_POLL:
288 cb(*static_cast<fs_poll_handle *>(hndl->data));
289 break;
290 case handle_type::IDLE:
291 cb(*static_cast<idle_handle *>(hndl->data));
292 break;
293 case handle_type::PIPE:
294 cb(*static_cast<pipe_handle *>(hndl->data));
295 break;
296 case handle_type::POLL:
297 cb(*static_cast<poll_handle *>(hndl->data));
298 break;
299 case handle_type::PREPARE:
300 cb(*static_cast<prepare_handle *>(hndl->data));
301 break;
302 case handle_type::PROCESS:
303 cb(*static_cast<process_handle *>(hndl->data));
304 break;
305 case handle_type::SIGNAL:
306 cb(*static_cast<signal_handle *>(hndl->data));
307 break;
308 case handle_type::TCP:
309 cb(*static_cast<tcp_handle *>(hndl->data));
310 break;
311 case handle_type::TIMER:
312 cb(*static_cast<timer_handle *>(hndl->data));
313 break;
314 case handle_type::TTY:
315 cb(*static_cast<tty_handle *>(hndl->data));
316 break;
317 case handle_type::UDP:
318 cb(*static_cast<udp_handle *>(hndl->data));
319 break;
320 default:
321 // this handle isn't managed by uvw, let it be...
322 break;
323 }
324 }
325 };
326
327 uv_walk(uv_loop.get(), func, &callback);
328 }
329
360 int fork() noexcept;
361
366 template<typename R = void>
367 std::shared_ptr<R> data() const {
368 return std::static_pointer_cast<R>(user_data);
369 }
370
375 void data(std::shared_ptr<void> ud);
376
392 const uv_loop_t *raw() const noexcept;
393
409 uv_loop_t *raw() noexcept;
410
411private:
412 std::unique_ptr<uv_loop_t, deleter> uv_loop;
413 std::shared_ptr<void> user_data{nullptr};
414};
415
416} // namespace uvw
417
418#ifndef UVW_AS_LIB
419# include "loop.cpp"
420#endif
421
422#endif // UVW_LOOP_INCLUDE_H
The async handle.
Definition: async.h:21
The check handle.
Definition: check.h:21
Event emitter base class.
Definition: emitter.h:83
The fs event handle.
Definition: fs_event.h:67
The fs poll handle.
Definition: fs_poll.h:31
The idle handle.
Definition: idle.h:29
The loop class.
Definition: loop.h:58
int run(run_mode mode=run_mode::DEFAULT) noexcept
Runs the event loop.
std::shared_ptr< R > data() const
Gets user-defined data. uvw won't use this field in any case.
Definition: loop.h:367
int fork() noexcept
Reinitialize any kernel state necessary in the child process after a fork(2) system call.
std::shared_ptr< R > resource(Args &&...args)
Creates resources of any type.
Definition: loop.h:152
const uv_loop_t * raw() const noexcept
Gets the underlying raw data structure.
bool alive() const noexcept
Checks if there are active resources.
static std::shared_ptr< loop > get_default()
Gets the initialized default loop.
void update() const noexcept
Updates the event loop’s concept of now.
static std::shared_ptr< loop > create()
Initializes a new loop instance.
void data(std::shared_ptr< void > ud)
Sets arbitrary data. uvw won't use this field in any case.
std::pair< bool, time > timeout() const noexcept
Gets the poll timeout.
void walk(Func callback)
Walks the list of handles.
Definition: loop.h:272
void stop() noexcept
Stops the event loop.
time now() const noexcept
Returns the current timestamp in milliseconds.
int close()
Releases all internal loop resources.
int configure(option flag, Args &&...args)
Sets additional loop options.
Definition: loop.h:137
static std::shared_ptr< loop > create(uv_loop_t *res)
Initializes a new loop instance from an existing resource.
int descriptor() const noexcept
Get backend file descriptor.
time idle_time() const noexcept
Returns the amount of time the event loop has been idle. The call is thread safe.
std::shared_ptr< R > uninitialized_resource(Args &&...args)
Creates uninitialized resources of any type.
Definition: loop.h:163
The pipe handle.
Definition: pipe.h:38
The poll handle.
Definition: poll.h:59
The prepare handle.
Definition: prepare.h:21
The process handle.
Definition: process.h:58
Common class for almost all the resources available in uvw.
Definition: resource.hpp:18
The signal handle.
Definition: signal.h:31
The TCP handle.
Definition: tcp.h:43
The timer handle.
Definition: timer.h:22
The tty handle.
Definition: tty.h:50
The UDP handle.
Definition: udp.h:82
uvw default namespace.
Definition: async.h:8
details::uv_type_wrapper< uv_handle_type > handle_category
Definition: util.h:79
static handle_type guess_handle(handle_category category) noexcept
Gets the type of the handle given a category.