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_)
80 std::tuple<T...> params_)
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();
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the work(an example is provided in the Appendix below). "Derivative Works" shall mean any work
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
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
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
typename detail::WorkerThreadAsync< R >::AfterWorkFunction AfterWorkFunction
Definition: WorkerThread.h:167
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: xchar.h:20
static constexpr const unit_t< compound_unit< energy::joules, inverse< temperature::kelvin >, inverse< substance::moles > > > R(8.3144598)
Gas constant.
void RunWorkerThreadRequest(WorkerThreadThread< R, T... > &thr, WorkerThreadRequest< R, T... > &req)
Definition: WorkerThread.h:105
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