uvw 3.1.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
50using metrics_type = uv_metrics_t;
60class loop final: public emitter<loop>, public std::enable_shared_from_this<loop> {
61 using deleter = void (*)(uv_loop_t *);
62
63 template<typename, typename, typename...>
64 friend class resource;
65
66 class uv_token {
67 friend class loop;
68 explicit uv_token(int) {}
69 };
70
71 loop(std::unique_ptr<uv_loop_t, deleter> ptr) noexcept;
72
73public:
74 using token = uv_token;
75 using time = std::chrono::duration<uint64_t, std::milli>;
76 using option = details::uvw_loop_option;
77 using run_mode = details::uvw_run_mode;
78
83 static std::shared_ptr<loop> create();
84
95 static std::shared_ptr<loop> create(uv_loop_t *res);
96
109 static std::shared_ptr<loop> get_default();
110
111 loop(const loop &) = delete;
112 loop(loop &&other) = delete;
113
114 loop &operator=(const loop &) = delete;
115 loop &operator=(loop &&other) = delete;
116
117 ~loop() noexcept;
118
138 template<typename... Args>
139 int configure(option flag, Args &&...args) {
140 return uv_loop_configure(uv_loop.get(), static_cast<uv_loop_option>(flag), std::forward<Args>(args)...);
141 }
142
153 template<typename R, typename... Args>
154 std::shared_ptr<R> resource(Args &&...args) {
155 auto ptr = uninitialized_resource<R>(std::forward<Args>(args)...);
156 ptr = (ptr->init() == 0) ? ptr : nullptr;
157 return ptr;
158 }
159
164 template<typename R, typename... Args>
165 std::shared_ptr<R> uninitialized_resource(Args &&...args) {
166 return std::make_shared<R>(token{0}, shared_from_this(), std::forward<Args>(args)...);
167 }
168
177 int close();
178
197 int run(run_mode mode = run_mode::DEFAULT) noexcept;
198
203 bool alive() const noexcept;
204
213 void stop() noexcept;
214
224 int descriptor() const noexcept;
225
232 std::pair<bool, time> timeout() const noexcept;
233
239 time idle_time() const noexcept;
240
245 metrics_type metrics() const noexcept;
246
259 time now() const noexcept;
260
270 void update() const noexcept;
271
279 template<typename Func>
280 void walk(Func callback) {
281 auto func = [](uv_handle_t *hndl, void *func) {
282 if(hndl->data) {
283 auto &cb = *static_cast<Func *>(func);
284
285 switch(utilities::guess_handle(handle_category{hndl->type})) {
286 case handle_type::ASYNC:
287 cb(*static_cast<async_handle *>(hndl->data));
288 break;
289 case handle_type::CHECK:
290 cb(*static_cast<check_handle *>(hndl->data));
291 break;
292 case handle_type::FS_EVENT:
293 cb(*static_cast<fs_event_handle *>(hndl->data));
294 break;
295 case handle_type::FS_POLL:
296 cb(*static_cast<fs_poll_handle *>(hndl->data));
297 break;
298 case handle_type::IDLE:
299 cb(*static_cast<idle_handle *>(hndl->data));
300 break;
301 case handle_type::PIPE:
302 cb(*static_cast<pipe_handle *>(hndl->data));
303 break;
304 case handle_type::POLL:
305 cb(*static_cast<poll_handle *>(hndl->data));
306 break;
307 case handle_type::PREPARE:
308 cb(*static_cast<prepare_handle *>(hndl->data));
309 break;
310 case handle_type::PROCESS:
311 cb(*static_cast<process_handle *>(hndl->data));
312 break;
313 case handle_type::SIGNAL:
314 cb(*static_cast<signal_handle *>(hndl->data));
315 break;
316 case handle_type::TCP:
317 cb(*static_cast<tcp_handle *>(hndl->data));
318 break;
319 case handle_type::TIMER:
320 cb(*static_cast<timer_handle *>(hndl->data));
321 break;
322 case handle_type::TTY:
323 cb(*static_cast<tty_handle *>(hndl->data));
324 break;
325 case handle_type::UDP:
326 cb(*static_cast<udp_handle *>(hndl->data));
327 break;
328 default:
329 // this handle isn't managed by uvw, let it be...
330 break;
331 }
332 }
333 };
334
335 uv_walk(uv_loop.get(), func, &callback);
336 }
337
368 int fork() noexcept;
369
374 template<typename R = void>
375 std::shared_ptr<R> data() const {
376 return std::static_pointer_cast<R>(user_data);
377 }
378
383 void data(std::shared_ptr<void> ud);
384
400 const uv_loop_t *raw() const noexcept;
401
417 uv_loop_t *raw() noexcept;
418
419private:
420 std::unique_ptr<uv_loop_t, deleter> uv_loop;
421 std::shared_ptr<void> user_data{nullptr};
422};
423
424} // namespace uvw
425
426#ifndef UVW_AS_LIB
427# include "loop.cpp"
428#endif
429
430#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:60
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:375
metrics_type metrics() const noexcept
Tracks various internal operations of the event loop.
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:154
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:280
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:139
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:165
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:84
uv_metrics_t metrics_type
Definition: loop.h:50
static handle_type guess_handle(handle_category category) noexcept
Gets the type of the handle given a category.