WPILibC++  2019.1.1-beta-4-6-g6593f43
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Loop.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_UV_LOOP_H_
9 #define WPIUTIL_WPI_UV_LOOP_H_
10 
11 #include <uv.h>
12 
13 #include <atomic>
14 #include <chrono>
15 #include <functional>
16 #include <memory>
17 #include <thread>
18 #include <utility>
19 
20 #include "wpi/Signal.h"
21 #include "wpi/uv/Error.h"
22 
23 namespace wpi {
24 namespace uv {
25 
26 class Handle;
27 
39 class Loop final : public std::enable_shared_from_this<Loop> {
40  struct private_init {};
41 
42  public:
43  using Time = std::chrono::duration<uint64_t, std::milli>;
44 
45  enum Mode {
46  kDefault = UV_RUN_DEFAULT,
47  kOnce = UV_RUN_ONCE,
48  kNoWait = UV_RUN_NOWAIT
49  };
50 
51  explicit Loop(const private_init&) noexcept;
52 
53  Loop(const Loop&) = delete;
54  Loop& operator=(const Loop&) = delete;
55  Loop(Loop&& oth) = delete;
56  Loop& operator=(Loop&& oth) = delete;
57 
58  ~Loop() noexcept;
59 
65  static std::shared_ptr<Loop> Create();
66 
73  static std::shared_ptr<Loop> GetDefault();
74 
83  void Close();
84 
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{};
103  return rv == 0;
104  }
105 
111  bool IsAlive() const noexcept { return uv_loop_alive(m_loop) != 0; }
112 
121  void Stop() noexcept { uv_stop(m_loop); }
122 
132  int GetDescriptor() const noexcept { return uv_backend_fd(m_loop); }
133 
141  std::pair<bool, Time> GetTimeout() const noexcept {
142  auto to = uv_backend_timeout(m_loop);
143  return std::make_pair(to == -1, Time{to});
144  }
145 
158  Time Now() const noexcept { return Time{uv_now(m_loop)}; }
159 
169  void UpdateTime() noexcept { uv_update_time(m_loop); }
170 
178  void Walk(std::function<void(Handle&)> callback);
179 
206  void Fork();
207 
213  uv_loop_t* GetRaw() const noexcept { return m_loop; }
214 
219  template <typename T = void>
220  std::shared_ptr<T> GetData() const {
221  return std::static_pointer_cast<T>(m_data);
222  }
223 
228  void SetData(std::shared_ptr<void> data) { m_data = std::move(data); }
229 
234  std::thread::id GetThreadId() const { return m_tid; }
235 
240 
245  void ReportError(int err) { error(Error(err)); }
246 
247  private:
248  std::shared_ptr<void> m_data;
249  uv_loop_t* m_loop;
250  uv_loop_t m_loopStruct;
251  std::atomic<std::thread::id> m_tid;
252 };
253 
254 } // namespace uv
255 } // namespace wpi
256 
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
Definition: uv.h:1542
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