stlab 2.3.0
Modern, modular C++ algorithms, data structures, and concurrency primitives
Loading...
Searching...
No Matches
executor_base.hpp
Go to the documentation of this file.
1/*
2 Copyright 2015 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6
7/**************************************************************************************************/
8
9#ifndef STLAB_CONCURRENCY_EXECUTOR_BASE_HPP
10#define STLAB_CONCURRENCY_EXECUTOR_BASE_HPP
11
21
22#include <stlab/config.hpp>
23
24#include <chrono>
25#include <functional>
26
29
30/**************************************************************************************************/
31
32namespace stlab {
33STLAB_VERSION_NAMESPACE_BEGIN()
34
35
40
41/**************************************************************************************************/
42
43
44using executor_t = std::function<void(stlab::task<void() noexcept>)>;
45
47template <typename Rep, typename Per = std::ratio<1>>
48auto execute_at(std::chrono::duration<Rep, Per> duration, executor_t executor) -> executor_t {
49 return [_duration = std::move(duration), _executor = std::move(executor)](auto f) mutable {
50 if (_duration != std::chrono::duration<Rep, Per>{})
51 system_timer(_duration,
52 [_f = std::move(f), _executor = std::move(_executor)]() mutable noexcept {
53 _executor(std::move(_f));
54 });
55 else
56 _executor(std::move(f));
57 };
58}
59
61[[deprecated("Use chrono::duration as parameter instead")]] inline auto execute_at(
62 std::chrono::steady_clock::time_point when, executor_t executor) -> executor_t {
63 using namespace std::chrono;
64 return execute_at(duration_cast<nanoseconds>(when - steady_clock::now()), std::move(executor));
65}
66
68template <typename E, typename Rep, typename Per = std::ratio<1>>
69auto execute_delayed(std::chrono::duration<Rep, Per> duration, E executor) {
70 return execute_at(duration, std::move(executor));
71}
72
74struct executor {
75 executor_t _executor;
76};
77
79template <typename F>
81 executor_t _executor;
82 F _f;
83};
84
85template <typename F>
86auto operator&(executor e, F&& f) -> executor_task_pair<F> {
87 return executor_task_pair<F>{std::move(e._executor), std::forward<F>(f)};
88}
89
90template <typename F>
91auto operator&(F&& f, executor e) -> executor_task_pair<F> {
92 return executor_task_pair<F>{std::move(e._executor), std::forward<F>(f)};
93}
94
95/**************************************************************************************************/
96
98
99STLAB_VERSION_NAMESPACE_END()
100} // namespace stlab
101
102/**************************************************************************************************/
103
104#endif
105
106/**************************************************************************************************/
std::function< void(stlab::task< void() noexcept >)> executor_t
Type-erased executor: accepts a void() noexcept task.
Definition executor_base.hpp:44
auto execute_delayed(std::chrono::duration< Rep, Per > duration, E executor)
Returns an executor that delays each submitted task by duration before forwarding to executor.
Definition executor_base.hpp:69
auto execute_at(std::chrono::duration< Rep, Per > duration, executor_t executor) -> executor_t
Returns an executor that posts tasks to executor after duration (immediate if zero).
Definition executor_base.hpp:48
constexpr auto system_timer
Schedules void() noexcept tasks on a system timer / run loop (platform-dependent).
Definition system_timer.hpp:341
typename noexcept_deducer< task_, F >::type task
task_ with noexcept deduced from the function type F (e.g. void() vs void() noexcept).
Definition task.hpp:324
Definition reverse.hpp:28
Executor plus callable, produced by executor & f (used by futures and channels).
Definition executor_base.hpp:80
Wraps an executor_t for use with operator&.
Definition executor_base.hpp:74
System timer / delayed execution (platform run loop or portable thread).
Move-only callable wrapper for executor scheduling (task<Signature>).