5#ifndef WPINET_UV_ASYNCFUNCTION_H_
6#define WPINET_UV_ASYNCFUNCTION_H_
34template <
typename R,
typename... T>
36 :
public HandleImpl<AsyncFunction<R(T...)>, uv_async_t> {
37 struct private_init {};
41 std::function<
void(
promise<R>, T...)> func,
const private_init&)
42 : wakeup{
std::move(func)}, m_loop{loop} {}
44 if (
auto loop = m_loop.lock()) {
60 static std::shared_ptr<AsyncFunction>
Create(
61 Loop& loop, std::function<
void(
promise<R>, T...)> func =
nullptr) {
62 return Create(loop.shared_from_this(), std::move(func));
74 static std::shared_ptr<AsyncFunction>
Create(
75 const std::shared_ptr<Loop>& loop,
76 std::function<
void(
promise<R>, T...)> func =
nullptr) {
77 if (loop->IsClosing()) {
81 std::make_shared<AsyncFunction>(loop, std::move(func), private_init{});
84 auto& h = *static_cast<AsyncFunction*>(handle->data);
85 std::unique_lock lock(h.m_mutex);
87 if (!h.m_params.empty()) {
91 for (auto&& v : h.m_params) {
92 auto p = h.m_promises.CreatePromise(v.first);
95 std::tuple_cat(std::make_tuple(std::move(p)),
96 std::move(v.second)));
102 h.m_promises.Notify();
106 loop->ReportError(err);
123 template <
typename... U>
126 uint64_t req = m_promises.CreateRequest();
128 auto loop = m_loop.lock();
129 if (loop->IsClosing()) {
130 if constexpr (std::is_same_v<R, void>) {
131 return m_promises.MakeReadyFuture();
133 return m_promises.MakeReadyFuture({});
136 if (loop && loop->GetThreadId() == std::this_thread::get_id()) {
138 wakeup(m_promises.CreatePromise(req), std::forward<U>(u)...);
139 return m_promises.CreateFuture(req);
144 std::scoped_lock lock(m_mutex);
145 m_params.emplace_back(std::piecewise_construct,
146 std::forward_as_tuple(req),
147 std::forward_as_tuple(std::forward<U>(u)...));
156 return m_promises.CreateFuture(req);
159 template <
typename... U>
161 return Call(std::forward<U>(u)...);
171 std::vector<std::pair<
uint64_t, std::tuple<T...>>> m_params;
173 std::weak_ptr<Loop> m_loop;
A lightweight version of std::future.
Definition: future.h:271
A lightweight version of std::promise.
Definition: future.h:528
future< R > operator()(U &&... u)
Definition: AsyncFunction.h:160
static std::shared_ptr< AsyncFunction > Create(const std::shared_ptr< Loop > &loop, std::function< void(promise< R >, T...)> func=nullptr)
Create an async handle.
Definition: AsyncFunction.h:74
static std::shared_ptr< AsyncFunction > Create(Loop &loop, std::function< void(promise< R >, T...)> func=nullptr)
Create an async handle.
Definition: AsyncFunction.h:60
std::function< void(promise< R >, T...)> wakeup
Function called (on event loop thread) when the async is called.
Definition: AsyncFunction.h:167
AsyncFunction(const std::shared_ptr< Loop > &loop, std::function< void(promise< R >, T...)> func, const private_init &)
Definition: AsyncFunction.h:40
~AsyncFunction() noexcept override
Definition: AsyncFunction.h:43
future< R > Call(U &&... u)
Wakeup the event loop, call the async function, and return a future for the result.
Definition: AsyncFunction.h:124
Definition: AsyncFunction.h:27
Handle.
Definition: Handle.h:273
Event loop.
Definition: Loop.h:37
std::vector< uint8_t > GetRaw(NT_Handle subentry, std::span< const uint8_t > defaultValue)
Get the last published value.
::uint64_t uint64_t
Definition: Meta.h:58
Definition: StdDeque.h:50
static constexpr const unit_t< compound_unit< energy::joules, inverse< temperature::kelvin >, inverse< substance::moles > > > R(8.3144598)
Gas constant.
static constexpr const unit_t< compound_unit< energy::joule, time::seconds > > h(6.626070040e-34)
Planck constant.
Definition: ParallelTcpConnector.h:22
::std::mutex mutex
Definition: mutex.h:17
UV_EXTERN int uv_async_init(uv_loop_t *, uv_async_t *async, uv_async_cb async_cb)
UV_EXTERN int uv_async_send(uv_async_t *async)