WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Idle.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <memory>
8
9#include <uv.h>
10
11#include "wpi/net/uv/Handle.hpp"
12#include "wpi/util/Signal.h"
13
14namespace wpi::net::uv {
15
16class Loop;
17
18/**
19 * Idle handle.
20 *
21 * Idle handles will generate a signal once per loop iteration, right
22 * before the Prepare handles.
23 *
24 * The notable difference with Prepare handles is that when there are active
25 * idle handles, the loop will perform a zero timeout poll instead of blocking
26 * for I/O.
27 *
28 * @warning Despite the name, idle handles will signal every loop iteration,
29 * not when the loop is actually "idle". This also means they can easily become
30 * CPU hogs.
31 */
32class Idle final : public HandleImpl<Idle, uv_idle_t> {
33 struct private_init {};
34
35 public:
36 explicit Idle(const private_init&) {}
37 ~Idle() noexcept override = default;
38
39 /**
40 * Create an idle handle.
41 *
42 * @param loop Loop object where this handle runs.
43 */
44 static std::shared_ptr<Idle> Create(Loop& loop);
45
46 /**
47 * Create an idle handle.
48 *
49 * @param loop Loop object where this handle runs.
50 */
51 static std::shared_ptr<Idle> Create(const std::shared_ptr<Loop>& loop) {
52 return Create(*loop);
53 }
54
55 /**
56 * Start the handle.
57 */
58 void Start();
59
60 /**
61 * Stop the handle. The signal will no longer be generated.
62 */
63 void Stop() { Invoke(&uv_idle_stop, GetRaw()); }
64
65 /**
66 * Signal generated once per loop iteration prior to Prepare signals.
67 */
69};
70
71} // namespace wpi::net::uv
bool Invoke(F &&f, Args &&... args) const
Definition Handle.hpp:265
uv_idle_t * GetRaw() const noexcept
Definition Handle.hpp:303
HandleImpl()
Definition Handle.hpp:308
Idle(const private_init &)
Definition Idle.hpp:36
void Stop()
Stop the handle.
Definition Idle.hpp:63
~Idle() noexcept override=default
void Start()
Start the handle.
wpi::util::sig::Signal idle
Signal generated once per loop iteration prior to Prepare signals.
Definition Idle.hpp:68
static std::shared_ptr< Idle > Create(Loop &loop)
Create an idle handle.
Event loop.
Definition Loop.hpp:35
Definition StringMap.hpp:773
Definition Prepare.hpp:14
SignalBase< detail::NullMutex, T... > Signal
Specialization of SignalBase to be used in single threaded contexts.
Definition Signal.h:809
UV_EXTERN int uv_idle_stop(uv_idle_t *idle)