8 #ifndef WPIUTIL_WPI_UV_LOOP_H_
9 #define WPIUTIL_WPI_UV_LOOP_H_
20 #include "wpi/Signal.h"
21 #include "wpi/uv/Error.h"
39 class Loop final :
public std::enable_shared_from_this<Loop> {
40 struct private_init {};
43 using Time = std::chrono::duration<uint64_t, std::milli>;
46 kDefault = UV_RUN_DEFAULT,
48 kNoWait = UV_RUN_NOWAIT
51 explicit Loop(
const private_init&) noexcept;
54 Loop& operator=(
const Loop&) =
delete;
56 Loop& operator=(
Loop&& oth) =
delete;
65 static std::shared_ptr<Loop>
Create();
99 bool Run(Mode mode = kDefault) {
100 m_tid = std::this_thread::get_id();
101 int rv = uv_run(m_loop, static_cast<uv_run_mode>(static_cast<int>(mode)));
102 m_tid = std::thread::id{};
111 bool IsAlive() const noexcept {
return uv_loop_alive(m_loop) != 0; }
121 void Stop() noexcept { uv_stop(m_loop); }
142 auto to = uv_backend_timeout(m_loop);
143 return std::make_pair(to == -1, Time{to});
158 Time
Now() const noexcept {
return Time{uv_now(m_loop)}; }
178 void Walk(std::function<
void(
Handle&)> callback);
219 template <
typename T =
void>
221 return std::static_pointer_cast<T>(m_data);
228 void SetData(std::shared_ptr<void> data) { m_data = std::move(data); }
248 std::shared_ptr<void> m_data;
251 std::atomic<std::thread::id> m_tid;
257 #endif // WPIUTIL_WPI_UV_LOOP_H_
static std::shared_ptr< Loop > GetDefault()
Create the default event loop.
void Stop() noexcept
Stop the event loop.
Definition: Loop.h:121
int GetDescriptor() const noexcept
Get backend file descriptor.
Definition: Loop.h:132
std::thread::id GetThreadId() const
Get the thread id of the loop thread.
Definition: Loop.h:234
void ReportError(int err)
Reports error.
Definition: Loop.h:245
uv_loop_t * GetRaw() const noexcept
Get the underlying event loop data structure.
Definition: Loop.h:213
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
std::shared_ptr< T > GetData() const
Gets user-defined data.
Definition: Loop.h:220
bool Run(Mode mode=kDefault)
Run the event loop.
Definition: Loop.h:99
bool IsAlive() const noexcept
Check if there are active resources.
Definition: Loop.h:111
sig::Signal< Error > error
Error signal.
Definition: Loop.h:239
Event loop.
Definition: Loop.h:39
std::pair< bool, Time > GetTimeout() const noexcept
Get the poll timeout.
Definition: Loop.h:141
static std::shared_ptr< Loop > Create()
Create a new event loop.
void Close()
Release all internal loop resources.
Time Now() const noexcept
Return the current timestamp in milliseconds.
Definition: Loop.h:158
Error code.
Definition: Error.h:19
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
void SetData(std::shared_ptr< void > data)
Sets user-defined data.
Definition: Loop.h:228
Handle.
Definition: Handle.h:32
void Walk(std::function< void(Handle &)> callback)
Walk the list of handles.
void Fork()
Reinitialize any kernel state necessary in the child process after a fork(2) system call...
void UpdateTime() noexcept
Update the event loop’s concept of now.
Definition: Loop.h:169