WPILibC++  2018.4.1-1228-gb9fa3a4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Async.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_ASYNC_H_
9 #define WPIUTIL_WPI_UV_ASYNC_H_
10 
11 #include <uv.h>
12 
13 #include <memory>
14 #include <tuple>
15 #include <utility>
16 #include <vector>
17 
18 #include "wpi/STLExtras.h"
19 #include "wpi/Signal.h"
20 #include "wpi/mutex.h"
21 #include "wpi/uv/Handle.h"
22 #include "wpi/uv/Loop.h"
23 
24 namespace wpi {
25 namespace uv {
26 
38 template <typename... T>
39 class Async final : public HandleImpl<Async<T...>, uv_async_t> {
40  struct private_init {};
41 
42  public:
43  explicit Async(const private_init&) {}
44  ~Async() noexcept override = default;
45 
51  static std::shared_ptr<Async> Create(Loop& loop) {
52  auto h = std::make_shared<Async>(private_init{});
53  int err = uv_async_init(loop.GetRaw(), h->GetRaw(), [](uv_async_t* handle) {
54  auto& h = *static_cast<Async*>(handle->data);
55  std::lock_guard<wpi::mutex> lock(h.m_mutex);
56  for (auto&& v : h.m_data) apply_tuple(h.wakeup, v);
57  h.m_data.clear();
58  });
59  if (err < 0) {
60  loop.ReportError(err);
61  return nullptr;
62  }
63  h->Keep();
64  return h;
65  }
66 
72  static std::shared_ptr<Async> Create(const std::shared_ptr<Loop>& loop) {
73  return Create(*loop);
74  }
75 
82  template <typename... U>
83  void Send(U&&... u) {
84  {
85  std::lock_guard<wpi::mutex> lock(m_mutex);
86  m_data.emplace_back(std::forward_as_tuple(std::forward<U>(u)...));
87  }
88  this->Invoke(&uv_async_send, this->GetRaw());
89  }
90 
95 
96  private:
97  wpi::mutex m_mutex;
98  std::vector<std::tuple<T...>> m_data;
99 };
100 
105 template <>
106 class Async<> final : public HandleImpl<Async<>, uv_async_t> {
107  struct private_init {};
108 
109  public:
110  explicit Async(const private_init&) {}
111  ~Async() noexcept override = default;
112 
118  static std::shared_ptr<Async> Create(Loop& loop);
119 
125  static std::shared_ptr<Async> Create(const std::shared_ptr<Loop>& loop) {
126  return Create(*loop);
127  }
128 
135  void Send() { Invoke(&uv_async_send, GetRaw()); }
136 
141 };
142 
143 } // namespace uv
144 } // namespace wpi
145 
146 #endif // WPIUTIL_WPI_UV_ASYNC_H_
uv_async_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:261
sig::Signal wakeup
Signal generated (on event loop thread) when the async event occurs.
Definition: Async.h:140
Handle.
Definition: Handle.h:246
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
static std::shared_ptr< Async > Create(Loop &loop)
Create an async handle.
Definition: Async.h:51
void Send()
Wakeup the event loop and emit the event.
Definition: Async.h:135
auto apply_tuple(F &&f, Tuple &&t) -> decltype(detail::apply_tuple_impl(std::forward< F >(f), std::forward< Tuple >(t), build_index_impl< std::tuple_size< typename std::decay< Tuple >::type >::value >
Given an input tuple (a1, a2, ..., an), pass the arguments of the tuple variadically to f as if by ca...
Definition: STLExtras.h:1209
sig::Signal< T...> wakeup
Signal generated (on event loop thread) when the async event occurs.
Definition: Async.h:94
static std::shared_ptr< Async > Create(const std::shared_ptr< Loop > &loop)
Create an async handle.
Definition: Async.h:125
static std::shared_ptr< Async > Create(const std::shared_ptr< Loop > &loop)
Create an async handle.
Definition: Async.h:72
Definition: uv.h:768
Event loop.
Definition: Loop.h:39
void Send(U &&...u)
Wakeup the event loop and emit the event.
Definition: Async.h:83
Async handle.
Definition: Async.h:39
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495