5#ifndef WPINET_WORKERTHREAD_H_
6#define WPINET_WORKERTHREAD_H_
31 async->wakeup.connect(
37 if (
auto async =
m_async.lock()) {
43 std::weak_ptr<uv::Async<AfterWorkFunction, R>>
m_async;
59 if (
auto async =
m_async.lock()) {
65 std::weak_ptr<uv::Async<AfterWorkFunction>>
m_async;
68template <
typename R,
typename... T>
75 std::tuple<T...> params_)
77 work(
std::move(work_)),
80 std::tuple<T...> params_)
82 work(
std::move(work_)),
92template <
typename R,
typename... T>
104template <
typename R,
typename... T>
107 R result = std::apply(req.
work, std::move(req.
params));
109 if (
auto async = thr.
m_async.m_async.lock()) {
110 async->Send(std::move(req.
afterWork), std::move(result));
117template <
typename... T>
122 if (
auto async = thr.
m_async.m_async.lock()) {
130template <
typename R,
typename... T>
132 std::vector<Request> requests;
134 std::unique_lock lock(m_mutex);
135 m_cond.wait(lock, [&] {
return !m_active || !m_requests.empty(); });
141 requests.swap(m_requests);
144 for (
auto&& req : requests) {
160template <
typename R,
typename... T>
179 if (
auto thr = m_owner.GetThread()) {
180 thr->m_async.SetLoop(loop);
198 if (
auto thr = m_owner.GetThread()) {
199 thr->m_async.UnsetLoop();
211 if (
auto thr = m_owner.GetThread()) {
212 return thr->m_async.m_async.lock();
231 template <
typename... U>
233 if (
auto thr = m_owner.GetThread()) {
235 uint64_t req = thr->m_promises.CreateRequest();
238 thr->m_requests.emplace_back(
239 req, std::move(work), std::forward_as_tuple(std::forward<U>(u)...));
242 thr->m_cond.notify_one();
245 return thr->m_promises.CreateFuture(req);
267 template <
typename... U>
269 if (
auto thr = m_owner.GetThread()) {
271 thr->m_requests.emplace_back(
272 std::move(work), std::move(afterWork),
273 std::forward_as_tuple(std::forward<U>(u)...));
276 thr->m_cond.notify_one();
A promise factory for lightweight futures.
Definition future.h:123
void SetValue(uint64_t request, const T &value)
Sets a value directly for a future without creating a promise object.
Definition future.h:729
Definition SafeThread.h:33
Definition SafeThread.h:124
std::function< R(T...)> WorkFunction
Definition WorkerThread.h:165
future< R > QueueWork(WorkFunction work, U &&... u)
Wakeup the worker thread, call the work function, and return a future for the result.
Definition WorkerThread.h:232
typename detail::WorkerThreadAsync< R >::AfterWorkFunction AfterWorkFunction
Definition WorkerThread.h:166
void SetLoop(uv::Loop &loop)
Set the loop.
Definition WorkerThread.h:178
void SetLoop(std::shared_ptr< uv::Loop > loop)
Set the loop.
Definition WorkerThread.h:191
void QueueWorkThen(WorkFunction work, AfterWorkFunction afterWork, U &&... u)
Wakeup the worker thread, call the work function, and call the afterWork function with the result on ...
Definition WorkerThread.h:268
std::shared_ptr< uv::Handle > GetHandle() const
Get the handle used by QueueWorkThen() to run afterWork.
Definition WorkerThread.h:210
WorkerThread()
Definition WorkerThread.h:169
void UnsetLoop()
Unset the loop.
Definition WorkerThread.h:197
Definition WorkerThread.h:158
Definition WorkerThread.h:93
void Main() override
Definition WorkerThread.h:131
PromiseFactory< R > m_promises
Definition WorkerThread.h:100
detail::WorkerThreadAsync< R > m_async
Definition WorkerThread.h:101
std::vector< Request > m_requests
Definition WorkerThread.h:99
A lightweight version of std::future.
Definition future.h:271
static std::shared_ptr< Async > Create(Loop &loop)
Create an async handle.
Definition Async.h:55
Event loop.
Definition Loop.h:37
detail namespace with internal helper functions
Definition input_adapters.h:32
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
void RunWorkerThreadRequest(WorkerThreadThread< R, T... > &thr, WorkerThreadRequest< R, T... > &req)
Definition WorkerThread.h:105
Foonathan namespace.
Definition ntcore_cpp.h:26
std::weak_ptr< uv::Async< AfterWorkFunction > > m_async
Definition WorkerThread.h:65
void RemoveLoop()
Definition WorkerThread.h:58
void SetLoop(uv::Loop &loop)
Definition WorkerThread.h:52
std::function< void()> AfterWorkFunction
Definition WorkerThread.h:48
~WorkerThreadAsync()
Definition WorkerThread.h:50
Definition WorkerThread.h:24
void SetLoop(uv::Loop &loop)
Definition WorkerThread.h:29
void UnsetLoop()
Definition WorkerThread.h:36
std::weak_ptr< uv::Async< AfterWorkFunction, R > > m_async
Definition WorkerThread.h:43
std::function< void(R)> AfterWorkFunction
Definition WorkerThread.h:25
~WorkerThreadAsync()
Definition WorkerThread.h:27
Definition WorkerThread.h:69
std::function< R(T...)> WorkFunction
Definition WorkerThread.h:70
WorkFunction work
Definition WorkerThread.h:87
std::tuple< T... > params
Definition WorkerThread.h:89
AfterWorkFunction afterWork
Definition WorkerThread.h:88
WorkerThreadRequest()=default
typename WorkerThreadAsync< R >::AfterWorkFunction AfterWorkFunction
Definition WorkerThread.h:71
uint64_t promiseId
Definition WorkerThread.h:86
WorkerThreadRequest(WorkFunction work_, AfterWorkFunction afterWork_, std::tuple< T... > params_)
Definition WorkerThread.h:79
WorkerThreadRequest(uint64_t promiseId_, WorkFunction work_, std::tuple< T... > params_)
Definition WorkerThread.h:74