1 #ifndef UVW_LOOP_INCLUDE_H
2 #define UVW_LOOP_INCLUDE_H
12 #include <type_traits>
41 enum class UVLoopOption: std::underlying_type_t<uv_loop_option> {
42 BLOCK_SIGNAL = UV_LOOP_BLOCK_SIGNAL,
43 IDLE_TIME = UV_METRICS_IDLE_TIME
47 enum class UVRunMode: std::underlying_type_t<uv_run_mode> {
48 DEFAULT = UV_RUN_DEFAULT,
50 NOWAIT = UV_RUN_NOWAIT
65 class Loop final:
public Emitter<Loop>,
public std::enable_shared_from_this<Loop> {
66 using Deleter = void(*)(uv_loop_t *);
68 template<
typename,
typename>
71 template<
typename R,
typename... Args>
72 auto create_resource(
int, Args&&... args) -> decltype(std::declval<R>().init(), std::shared_ptr<R>{}) {
73 auto ptr = R::create(shared_from_this(), std::forward<Args>(args)...);
74 ptr = ptr->init() ? ptr :
nullptr;
78 template<
typename R,
typename... Args>
79 std::shared_ptr<R> create_resource(
char, Args&&... args) {
80 return R::create(shared_from_this(), std::forward<Args>(args)...);
83 Loop(std::unique_ptr<uv_loop_t, Deleter> ptr) noexcept;
86 using Time = std::chrono::duration<uint64_t, std::milli>;
87 using Configure = details::UVLoopOption;
88 using Mode = details::UVRunMode;
94 static std::shared_ptr<Loop>
create();
106 static std::shared_ptr<Loop>
create(uv_loop_t *loop);
124 Loop & operator=(
const Loop &) =
delete;
125 Loop & operator=(
Loop &&other) =
delete;
148 template<typename... Args>
150 auto option =
static_cast<std::underlying_type_t<Configure>
>(flag);
151 auto err = uv_loop_configure(loop.get(),
static_cast<uv_loop_option
>(option), std::forward<Args>(args)...);
165 template<
typename R,
typename... Args>
167 return create_resource<R>(0, std::forward<Args>(args)...);
198 template<Mode mode = Mode::DEFAULT>
234 std::pair<
bool, Time>
timeout() const noexcept;
255 Time
now() const noexcept;
275 template<typename Func>
278 uv_walk(loop.get(), [](uv_handle_t *handle,
void *func) {
280 auto &cb = *static_cast<Func *>(func);
282 switch(Utilities::guessHandle(HandleCategory{handle->type})) {
283 case HandleType::ASYNC:
284 cb(*static_cast<AsyncHandle *>(handle->data));
286 case HandleType::CHECK:
287 cb(*static_cast<CheckHandle *>(handle->data));
289 case HandleType::FS_EVENT:
290 cb(*static_cast<FsEventHandle *>(handle->data));
292 case HandleType::FS_POLL:
293 cb(*static_cast<FsPollHandle *>(handle->data));
295 case HandleType::IDLE:
296 cb(*static_cast<IdleHandle *>(handle->data));
298 case HandleType::PIPE:
299 cb(*static_cast<PipeHandle *>(handle->data));
301 case HandleType::POLL:
302 cb(*static_cast<PollHandle *>(handle->data));
304 case HandleType::PREPARE:
305 cb(*static_cast<PrepareHandle *>(handle->data));
307 case HandleType::PROCESS:
308 cb(*static_cast<ProcessHandle *>(handle->data));
310 case HandleType::SIGNAL:
311 cb(*static_cast<SignalHandle *>(handle->data));
313 case HandleType::TCP:
314 cb(*static_cast<TCPHandle *>(handle->data));
316 case HandleType::TIMER:
317 cb(*static_cast<TimerHandle *>(handle->data));
319 case HandleType::TTY:
320 cb(*static_cast<TTYHandle *>(handle->data));
322 case HandleType::UDP:
323 cb(*static_cast<UDPHandle *>(handle->data));
369 template<typename R =
void>
370 std::shared_ptr<R> data()
const {
371 return std::static_pointer_cast<R>(userData);
378 void data(std::shared_ptr<void> uData);
395 const uv_loop_t *
raw() const noexcept;
412 uv_loop_t * raw() noexcept;
415 std::unique_ptr<uv_loop_t, Deleter> loop;
416 std::shared_ptr<
void> userData{
nullptr};
422 extern template bool Loop::run<Loop::Mode::DEFAULT>() noexcept;
423 extern template
bool Loop::run<Loop::Mode::ONCE>() noexcept;
424 extern template
bool Loop::run<Loop::Mode::NOWAIT>() noexcept;
Event emitter base class.
void update() const noexcept
Updates the event loop’s concept of now.
void fork() noexcept
Reinitialize any kernel state necessary in the child process after a fork(2) system call.
static std::shared_ptr< Loop > getDefault()
Gets the initialized default loop.
std::shared_ptr< R > resource(Args &&... args)
Creates resources of any type.
static std::shared_ptr< Loop > create(uv_loop_t *loop)
Initializes a new Loop instance from an existing resource.
void walk(Func callback)
Walks the list of handles.
void data(std::shared_ptr< void > uData)
Sets arbitrary data. uvw won't use this field in any case.
void stop() noexcept
Stops the event loop.
std::pair< bool, Time > timeout() const noexcept
Gets the poll timeout.
bool run() noexcept
Runs the event loop.
Time idleTime() const noexcept
Returns the amount of time the event loop has been idle. The call is thread safe.
Time now() const noexcept
Returns the current timestamp in milliseconds.
const uv_loop_t * raw() const noexcept
Gets the underlying raw data structure.
int descriptor() const noexcept
Get backend file descriptor.
void configure(Configure flag, Args &&... args)
Sets additional loop options.
bool alive() const noexcept
Checks if there are active resources.
void close()
Releases all internal loop resources.
static std::shared_ptr< Loop > create()
Initializes a new Loop instance.
Common class for almost all the resources available in uvw.