8 #ifndef WPIUTIL_WPI_UV_ASYNC_H_
9 #define WPIUTIL_WPI_UV_ASYNC_H_
19 #include "wpi/STLExtras.h"
20 #include "wpi/Signal.h"
21 #include "wpi/mutex.h"
22 #include "wpi/uv/Handle.h"
23 #include "wpi/uv/Loop.h"
39 template <
typename... T>
41 struct private_init {};
44 Async(
const std::shared_ptr<Loop>& loop,
const private_init&)
46 ~
Async() noexcept
override {
47 if (
auto loop = m_loop.lock())
59 return Create(loop.shared_from_this());
67 static std::shared_ptr<Async>
Create(
const std::shared_ptr<Loop>& loop) {
68 auto h = std::make_shared<Async>(loop, private_init{});
70 uv_async_init(loop->GetRaw(), h->GetRaw(), [](
uv_async_t* handle) {
71 auto& h = *
static_cast<Async*
>(handle->data);
72 std::lock_guard<wpi::mutex> lock(h.m_mutex);
77 loop->ReportError(err);
90 template <
typename... U>
92 auto loop = m_loop.lock();
93 if (loop && loop->GetThreadId() == std::this_thread::get_id()) {
95 wakeup(std::forward<U>(u)...);
100 std::lock_guard<wpi::mutex> lock(m_mutex);
101 m_data.emplace_back(std::forward_as_tuple(std::forward<U>(u)...));
103 if (loop) this->Invoke(&uv_async_send, this->
GetRaw());
113 std::vector<std::tuple<T...>> m_data;
114 std::weak_ptr<Loop> m_loop;
123 struct private_init {};
126 Async(
const std::shared_ptr<Loop>& loop,
const private_init&)
128 ~
Async() noexcept
override;
136 return Create(loop.shared_from_this());
144 static std::shared_ptr<Async>
Create(
const std::shared_ptr<Loop>& loop);
153 if (
auto loop = m_loop.lock()) Invoke(&uv_async_send,
GetRaw());
162 std::weak_ptr<Loop> m_loop;
168 #endif // WPIUTIL_WPI_UV_ASYNC_H_
static std::shared_ptr< Async > Create(Loop &loop)
Create an async handle.
Definition: Async.h:135
uv_async_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:264
sig::Signal wakeup
Signal generated (on event loop thread) when the async event occurs.
Definition: Async.h:159
Handle.
Definition: Handle.h:249
void Close() noexcept
Request handle to be closed.
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
static std::shared_ptr< Async > Create(Loop &loop)
Create an async handle.
Definition: Async.h:58
void Send()
Wakeup the event loop and emit the event.
Definition: Async.h:152
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:109
static std::shared_ptr< Async > Create(const std::shared_ptr< Loop > &loop)
Create an async handle.
Definition: Async.h:67
Event loop.
Definition: Loop.h:39
void Send(U &&...u)
Wakeup the event loop and emit the event.
Definition: Async.h:91
Async handle.
Definition: Async.h:40
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495