WPILibC++ 2023.4.3-108-ge5452e3
Idle.h
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#ifndef WPINET_UV_IDLE_H_
6#define WPINET_UV_IDLE_H_
7
8#include <uv.h>
9
10#include <memory>
11
12#include <wpi/Signal.h>
13
14#include "wpinet/uv/Handle.h"
15
16namespace wpi::uv {
17
18class Loop;
19
20/**
21 * Idle handle.
22 *
23 * Idle handles will generate a signal once per loop iteration, right
24 * before the Prepare handles.
25 *
26 * The notable difference with Prepare handles is that when there are active
27 * idle handles, the loop will perform a zero timeout poll instead of blocking
28 * for I/O.
29 *
30 * @warning Despite the name, idle handles will signal every loop iteration,
31 * not when the loop is actually "idle". This also means they can easily become
32 * CPU hogs.
33 */
34class Idle final : public HandleImpl<Idle, uv_idle_t> {
35 struct private_init {};
36
37 public:
38 explicit Idle(const private_init&) {}
39 ~Idle() noexcept override = default;
40
41 /**
42 * Create an idle handle.
43 *
44 * @param loop Loop object where this handle runs.
45 */
46 static std::shared_ptr<Idle> Create(Loop& loop);
47
48 /**
49 * Create an idle handle.
50 *
51 * @param loop Loop object where this handle runs.
52 */
53 static std::shared_ptr<Idle> Create(const std::shared_ptr<Loop>& loop) {
54 return Create(*loop);
55 }
56
57 /**
58 * Start the handle.
59 */
60 void Start();
61
62 /**
63 * Stop the handle. The signal will no longer be generated.
64 */
65 void Stop() { Invoke(&uv_idle_stop, GetRaw()); }
66
67 /**
68 * Signal generated once per loop iteration prior to Prepare signals.
69 */
71};
72
73} // namespace wpi::uv
74
75#endif // WPINET_UV_IDLE_H_
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
bool Invoke(F &&f, Args &&... args) const
Definition: Handle.h:251
Handle.
Definition: Handle.h:273
uv_idle_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:288
Idle handle.
Definition: Idle.h:34
void Stop()
Stop the handle.
Definition: Idle.h:65
void Start()
Start the handle.
~Idle() noexcept override=default
sig::Signal idle
Signal generated once per loop iteration prior to Prepare signals.
Definition: Idle.h:70
Idle(const private_init &)
Definition: Idle.h:38
static std::shared_ptr< Idle > Create(Loop &loop)
Create an idle handle.
Event loop.
Definition: Loop.h:37
Definition: BFloat16.h:88
Definition: Buffer.h:18
UV_EXTERN int uv_idle_stop(uv_idle_t *idle)