13 #ifndef __PROCESS_LOOP_HPP__ 14 #define __PROCESS_LOOP_HPP__ 117 template <
typename Iterate,
121 typename V =
typename CF::ValueType>
127 template <
typename Iterate,
131 typename V =
typename CF::ValueType>
137 return loop<Iterate, Body, T, CF, V>(
139 std::forward<Iterate>(iterate),
140 std::forward<Body>(body))
162 template <
typename T>
179 template <
typename U>
191 template <
typename U>
197 template <
typename U>
212 const T&
value() const & {
return t.get(); }
214 const T&&
value() const && {
return t.get(); }
227 template <
typename T>
236 template <
typename T>
252 template <
typename Iterate,
typename Body,
typename T,
typename R>
253 class Loop :
public std::enable_shared_from_this<Loop<Iterate, Body, T, R>>
256 template <
typename Iterate_,
typename Body_>
262 return std::shared_ptr<Loop>(
265 std::forward<Iterate_>(iterate),
266 std::forward<Body_>(body)));
272 return std::enable_shared_from_this<Loop>::shared_from_this();
277 return std::weak_ptr<Loop>(shared());
282 auto self = shared();
283 auto weak_self = weak();
301 promise.future().onDiscard([weak_self]() {
302 auto self = weak_self.lock();
309 std::function<void()>
f = []() {};
310 synchronized (
self->mutex) {
320 self->run(self->iterate());
335 auto self = shared();
341 synchronized (mutex) {
348 switch (flow->statement()) {
361 switch (flow->statement()) {
363 self->run(self->iterate());
367 self->promise.set(flow->value());
372 self->promise.fail(flow.
failure());
374 self->promise.discard();
381 flow.
onAny(continuation);
384 if (!
promise.future().hasDiscard()) {
385 synchronized (mutex) {
386 self->discard = [=]()
mutable { flow.
discard(); };
395 if (
promise.future().hasDiscard()) {
403 auto continuation = [
self](
const Future<T>& next) {
407 self->promise.fail(next.
failure());
409 self->promise.discard();
416 next.
onAny(continuation);
419 if (!
promise.future().hasDiscard()) {
420 synchronized (mutex) {
427 if (
promise.future().hasDiscard()) {
434 : pid(pid), iterate(iterate), body(body) {}
437 : pid(pid), iterate(
std::move(iterate)), body(
std::move(body)) {}
449 std::function<void()>
discard = []() {};
455 template <
typename Iterate,
typename Body,
typename T,
typename CF,
typename V>
466 std::forward<Iterate>(iterate),
467 std::forward<Body>(body));
469 return loop->start();
474 #endif // __PROCESS_LOOP_HPP__ ControlFlow(Statement s, Option< T > t)
Definition: loop.hpp:207
bool isReady() const
Definition: future.hpp:1215
Definition: nothing.hpp:16
Definition: option.hpp:29
F && f
Definition: defer.hpp:270
static std::shared_ptr< Loop > create(const Option< UPID > &pid, Iterate_ &&iterate, Body_ &&body)
Definition: loop.hpp:257
const T & get() const
Definition: future.hpp:1294
T type
Definition: future.hpp:939
ControlFlow< typename std::decay< T >::type >::Break Break(T &&t)
Definition: loop.hpp:237
Statement statement() const
Definition: loop.hpp:209
std::weak_ptr< Loop > weak()
Definition: loop.hpp:275
Definition: type_utils.hpp:619
Statement
Definition: loop.hpp:168
Definition: process.hpp:72
bool discard()
Definition: future.hpp:1157
T ValueType
Definition: loop.hpp:166
UPID spawn(ProcessBase *process, bool manage=false)
Spawn a new process.
void terminate(const UPID &pid, bool inject=true)
Sends a TerminateEvent to the given process.
ControlFlow< Nothing >::Break Break()
Definition: loop.hpp:244
void dispatch(const PID< T > &pid, void(T::*method)())
Definition: dispatch.hpp:174
bool isDiscarded() const
Definition: future.hpp:1222
An "untyped" PID, used to encapsulate the process ID for lower-layer abstractions (eg...
Definition: pid.hpp:39
const Future< T > & onAny(AnyCallback &&callback) const
Definition: future.hpp:1442
const T & value() const &
Definition: loop.hpp:212
Loop(const Option< UPID > &pid, Iterate &&iterate, Body &&body)
Definition: loop.hpp:436
Protocol< PromiseRequest, PromiseResponse > promise
void run(Future< T > next)
Definition: loop.hpp:333
Future< V > loop(const Option< UPID > &pid, Iterate &&iterate, Body &&body)
Definition: loop.hpp:456
void discard(const Futures &futures)
Definition: future.hpp:991
Future< R > run(R(*method)())
Definition: run.hpp:55
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Definition: attributes.hpp:24
Definition: executor.hpp:48
const T && value() const &&
Definition: loop.hpp:214
Try< uint32_t > type(const std::string &path)
T && value()&&
Definition: loop.hpp:213
Loop(const Option< UPID > &pid, const Iterate &iterate, const Body &body)
Definition: loop.hpp:433
Try< Nothing > create(const std::string &hierarchy, const std::string &cgroup, bool recursive=false)
std::shared_ptr< Loop > shared()
Definition: loop.hpp:269
const std::string & failure() const
Definition: future.hpp:1320
Break(T t)
Definition: loop.hpp:189
T & value()&
Definition: loop.hpp:211
Deferred< void()> defer(const PID< T > &pid, void(T::*method)())
Definition: defer.hpp:35
Future< R > start()
Definition: loop.hpp:280
bool isFailed() const
Definition: future.hpp:1229
Definition: future.hpp:58