34template <
typename R,
typename... T>
36 :
public HandleImpl<AsyncFunction<R(T...)>, uv_async_t> {
37 struct private_init {};
45 if (
auto loop = m_loop.lock()) {
61 static std::shared_ptr<AsyncFunction>
Create(
64 return Create(
loop.shared_from_this(), std::move(func));
76 static std::shared_ptr<AsyncFunction>
Create(
77 const std::shared_ptr<Loop>&
loop,
79 if (
loop->IsClosing()) {
83 std::make_shared<AsyncFunction>(
loop, std::move(func), private_init{});
86 auto& h = *static_cast<AsyncFunction*>(handle->data);
87 std::unique_lock lock(h.m_mutex);
89 if (!h.m_params.empty()) {
93 for (auto&& v : h.m_params) {
94 auto p = h.m_promises.CreatePromise(v.first);
97 std::tuple_cat(std::make_tuple(std::move(p)),
98 std::move(v.second)));
104 h.m_promises.Notify();
125 template <
typename... U>
128 uint64_t req = m_promises.CreateRequest();
130 auto loop = m_loop.lock();
131 if (
loop->IsClosing()) {
132 if constexpr (std::same_as<R, void>) {
133 return m_promises.MakeReadyFuture();
135 return m_promises.MakeReadyFuture({});
138 if (
loop &&
loop->GetThreadId() == std::this_thread::get_id()) {
140 wakeup(m_promises.CreatePromise(req), std::forward<U>(u)...);
141 return m_promises.CreateFuture(req);
146 std::scoped_lock lock(m_mutex);
147 m_params.emplace_back(std::piecewise_construct,
148 std::forward_as_tuple(req),
149 std::forward_as_tuple(std::forward<U>(u)...));
158 return m_promises.CreateFuture(req);
161 template <
typename... U>
163 return Call(std::forward<U>(u)...);
173 std::vector<std::pair<uint64_t, std::tuple<T...>>> m_params;
175 std::weak_ptr<Loop> m_loop;
wpi::util::future< R > operator()(U &&... u)
Definition AsyncFunction.hpp:162
std::function< void(wpi::util::promise< R >, T...)> wakeup
Function called (on event loop thread) when the async is called.
Definition AsyncFunction.hpp:169
static std::shared_ptr< AsyncFunction > Create(const std::shared_ptr< Loop > &loop, std::function< void(wpi::util::promise< R >, T...)> func=nullptr)
Create an async handle.
Definition AsyncFunction.hpp:76
AsyncFunction(const std::shared_ptr< Loop > &loop, std::function< void(wpi::util::promise< R >, T...)> func, const private_init &)
Definition AsyncFunction.hpp:40
static std::shared_ptr< AsyncFunction > Create(Loop &loop, std::function< void(wpi::util::promise< R >, T...)> func=nullptr)
Create an async handle.
Definition AsyncFunction.hpp:61
~AsyncFunction() noexcept override
Definition AsyncFunction.hpp:44
wpi::util::future< R > Call(U &&... u)
Wakeup the event loop, call the async function, and return a future for the result.
Definition AsyncFunction.hpp:126
Definition AsyncFunction.hpp:27
void ForceClosed() noexcept
Definition Handle.hpp:259
bool Invoke(F &&f, Args &&... args) const
Definition Handle.hpp:265
void Close() noexcept
Request handle to be closed.
uv_async_t * GetRaw() const noexcept
Definition Handle.hpp:303
HandleImpl()
Definition Handle.hpp:308
Event loop.
Definition Loop.hpp:35
A promise factory for lightweight futures.
Definition future.hpp:122
A lightweight version of std::future.
Definition future.hpp:270
A lightweight version of std::promise.
Definition future.hpp:527
Definition StringMap.hpp:773
Definition Errors.hpp:112
Definition Prepare.hpp:14
::std::mutex mutex
Definition mutex.hpp:17
struct uv_async_s uv_async_t
Definition uv.h:228
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)