Commit 1a8b3841 authored by Dominik Charousset's avatar Dominik Charousset

Merge pull request #1368

parents ecf585a7 4f4c3d79
...@@ -647,6 +647,32 @@ public: ...@@ -647,6 +647,32 @@ public:
return run_scheduled(time_point_cast<duration_t>(when), make_action(what)); return run_scheduled(time_point_cast<duration_t>(when), make_action(what));
} }
/// Runs `what` asynchronously at some point after `when`, iff this actor is
/// alive.
/// @param when The local time until the actor waits before invoking the
/// action. Due to scheduling delays, there will always be some
/// additional wait time. Passing the current time or a past time
/// immediately schedules the action for execution.
/// @param what The action to invoke after waiting on the timeout.
/// @returns A @ref disposable that allows the actor to cancel the action.
template <class Duration, class F>
disposable run_scheduled_weak(
std::chrono::time_point<std::chrono::system_clock, Duration> when, F what) {
using std::chrono::time_point_cast;
return run_scheduled_weak(time_point_cast<timespan>(when),
make_action(what));
}
/// @copydoc run_scheduled_weak
template <class Duration, class F>
disposable run_scheduled_weak(
std::chrono::time_point<actor_clock::clock_type, Duration> when, F what) {
using std::chrono::time_point_cast;
using duration_t = actor_clock::duration_type;
return run_scheduled_weak(time_point_cast<duration_t>(when),
make_action(what));
}
/// Runs `what` asynchronously after the `delay`. /// Runs `what` asynchronously after the `delay`.
/// @param delay Minimum amount of time that actor waits before invoking the /// @param delay Minimum amount of time that actor waits before invoking the
/// action. Due to scheduling delays, there will always be some /// action. Due to scheduling delays, there will always be some
...@@ -659,6 +685,18 @@ public: ...@@ -659,6 +685,18 @@ public:
return run_delayed(duration_cast<timespan>(delay), make_action(what)); return run_delayed(duration_cast<timespan>(delay), make_action(what));
} }
/// Runs `what` asynchronously after the `delay`, iff this actor is alive.
/// @param delay Minimum amount of time that actor waits before invoking the
/// action. Due to scheduling delays, there will always be some
/// additional wait time.
/// @param what The action to invoke after the delay.
/// @returns A @ref disposable that allows the actor to cancel the action.
template <class Rep, class Period, class F>
disposable run_delayed_weak(std::chrono::duration<Rep, Period> delay, F what) {
using std::chrono::duration_cast;
return run_delayed_weak(duration_cast<timespan>(delay), make_action(what));
}
// -- properties ------------------------------------------------------------- // -- properties -------------------------------------------------------------
/// Returns `true` if the actor has a behavior, awaits responses, or /// Returns `true` if the actor has a behavior, awaits responses, or
...@@ -744,7 +782,10 @@ private: ...@@ -744,7 +782,10 @@ private:
disposable run_scheduled(timestamp when, action what); disposable run_scheduled(timestamp when, action what);
disposable run_scheduled(actor_clock::time_point when, action what); disposable run_scheduled(actor_clock::time_point when, action what);
disposable run_scheduled_weak(timestamp when, action what);
disposable run_scheduled_weak(actor_clock::time_point when, action what);
disposable run_delayed(timespan delay, action what); disposable run_delayed(timespan delay, action what);
disposable run_delayed_weak(timespan delay, action what);
// -- caf::flow bindings ----------------------------------------------------- // -- caf::flow bindings -----------------------------------------------------
......
...@@ -885,11 +885,28 @@ disposable scheduled_actor::run_scheduled(actor_clock::time_point when, ...@@ -885,11 +885,28 @@ disposable scheduled_actor::run_scheduled(actor_clock::time_point when,
return clock().schedule(when, std::move(what), strong_actor_ptr{ctrl()}); return clock().schedule(when, std::move(what), strong_actor_ptr{ctrl()});
} }
disposable scheduled_actor::run_delayed(timespan delay, action what) { disposable scheduled_actor::run_scheduled_weak(timestamp when, action what) {
CAF_ASSERT(what.ptr() != nullptr);
CAF_LOG_TRACE(CAF_ARG(when));
auto delay = when - make_timestamp();
return run_scheduled_weak(clock().now() + delay, std::move(what));
}
disposable scheduled_actor::run_scheduled_weak(actor_clock::time_point when,
action what) {
CAF_ASSERT(what.ptr() != nullptr); CAF_ASSERT(what.ptr() != nullptr);
CAF_LOG_TRACE(CAF_ARG(when));
return clock().schedule(when, std::move(what), weak_actor_ptr{ctrl()});
}
disposable scheduled_actor::run_delayed(timespan delay, action what) {
CAF_LOG_TRACE(CAF_ARG(delay));
return run_scheduled(clock().now() + delay, std::move(what));
}
disposable scheduled_actor::run_delayed_weak(timespan delay, action what) {
CAF_LOG_TRACE(CAF_ARG(delay)); CAF_LOG_TRACE(CAF_ARG(delay));
return clock().schedule(clock().now() + delay, std::move(what), return run_scheduled_weak(clock().now() + delay, std::move(what));
strong_actor_ptr{ctrl()});
} }
// -- caf::flow bindings ------------------------------------------------------- // -- caf::flow bindings -------------------------------------------------------
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment