Flow 2.0.0
Flow project: Full implementation reference.
sched_task_fwd.hpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
22#include "flow/log/log_fwd.hpp"
23#include <boost/shared_ptr.hpp>
24
25namespace flow::util
26{
27
28// Types.
29
30// Find doc headers near the bodies of these compound types.
31
32struct Scheduled_task_handle_state;
33
34/**
35 * Black-box type that represents a handle to a scheduled task as scheduled by
36 * schedule_task_at() or schedule_task_from_now() or similar, which can be (optionally) used to control the
37 * scheduled task after it has been thus scheduled. Special value `Scheduled_task_handle{}` represents an invalid task
38 * and can be used as a sentinel, as with a null pointer.
39 *
40 * Values of this type are to be passed around by value, not reference. They are light-weight.
41 * Officially, passing it by reference results in undefined
42 * behavior. Unofficially, it will probably work (just haven't thought super-hard about it to make sure) but will
43 * bring no performance benefit.
44 */
45using Scheduled_task_handle = boost::shared_ptr<Scheduled_task_handle_state>;
46
47/**
48 * Equivalent to `Scheduled_task_handle` but refers to immutable version of a task.
49 * One can safely construct a `Scheduled_task_const_handle` directly from `Scheduled_task_handle` (but not vice versa;
50 * nor would that compile without some kind of frowned-upon `const_cast` or equivalent).
51 */
52using Scheduled_task_const_handle = boost::shared_ptr<const Scheduled_task_handle_state>;
53
54/**
55 * Short-hand for tasks that can be scheduled/fired by schedule_task_from_now() and similar. `short_fire` shall be
56 * set to `false` if the task executed based on its scheduled time; `true` if it is fired early due to
57 * scheduled_task_short_fire().
58 */
59using Scheduled_task = Function<void (bool short_fire)>;
60
61// Free functions.
62
63/**
64 * Schedule the given function to execute in a certain amount of time: A handy wrapper around #Timer (asio's timer
65 * facility). Compared to using #Timer, this has far simplified semantics at the cost of certain
66 * less-used features of #Timer. Recommend using this facility when sufficient; otherwise use #Timer directly.
67 * The trade-offs are explained below, but first:
68 *
69 * ### Semantics ###
70 * Conceptually this is similar to JavaScript's ubiquitous (albeit single-threaded) `setTimeout()` feature.
71 * The given function shall execute as if `post()`ed onto the given `Task_engine`; unless successfully canceled by
72 * `scheduled_task_cancel(X)`, where X is the (optionally used and entirely ignorable) returned handle. Barring
73 * unrelated crashes/etc. there are exactly three mutually exclusive outcomes of this function executing:
74 * - It runs at the scheduled time, with the `bool short_fire` arg to it set to `false`.
75 * - It runs before the scheduled time, with `short_fire == true`. To trigger this, use scheduled_task_short_fire()
76 * before the scheduled time.
77 * - If this loses the race with normal firing, the short-fire function will indicate that via its return value.
78 * - It never runs. To trigger this, use scheduled_task_cancel() before the scheduled time.
79 * - If this loses the race with normal firing, the cancel function will indicate that via its return value.
80 *
81 * All related functions are thread-safe w/r/t a given returned `Scheduled_task_handle`, if `single_threaded == false`.
82 * In addition, for extra performance (internally, by omitting certain locking),
83 * set `single_threaded = true` only if you can guarantee the following:
84 * - `*task_engine` is `run()`ning in no more than one thread throughout all work with the returned
85 * `Scheduled_task_handle` including the present function.
86 * - Any calls w/r/t the returned `Scheduled_task_handle` are also -- if at all -- called from that one thread.
87 *
88 * The minimum is step 1: Call schedule_task_at() or schedule_task_from_now(). Optionally, if one saves the return
89 * value X from either, one can do step 2: `scheduled_task_short_fire(X)` or `scheduled_task_cancel(X)`, which
90 * will succeed (return `true`) if called sufficiently early. There is no step 3; any subsequent calls on X
91 * will fail (return `false`).
92 *
93 * ### Simplifications over #Timer ###
94 * - There is no need to, separately, create a #Timer object; set the firing time; and kick off the
95 * asynchronous wait. One call does everything, and there is no object to maintain. Sole exception to the latter:
96 * *if* you want to be able to cancel or short-circuit the task later on, you can optionally save the return value
97 * and later call a `scheduled_task_*()` function on it.)
98 * - You need not worry about internal errors -- no need to worry about any `Error_code` passed in to your task
99 * function.
100 * - Cancellation semantics are straightforward and intuitive, lacking corner cases of vanilla #Timer. To wit:
101 * - A successful scheduled_task_cancel() call means the task will *not* execute. (`Timer::cancel()` means it
102 * will still execute but with `operation_aborted` code.)
103 * - `Timer::cancel()` can result in ~3 behaviors: it already executed, so it does nothing; it has not yet executed
104 * but was JUST about to execute, so it will still execute with non-`operation_aborted` code; it has not yet
105 * executed, so now it will execute but with `operation_aborted`. With this facility, it's simpler:
106 * it either succeeds (so acts per previous bullet -- does not execute); or it fails, hence it will have executed
107 * (and there is no `Error_code` to decipher).
108 * - Short-firing semantics are arguably more intuitive. With #Timer, `cancel()` actually means short-firing
109 * (firing ASAP) but with a special `Error_code`. One can also change the expiration time to the past
110 * to short-fire in a different way. With this facility, cancellation means it won't run; and
111 * scheduled_task_short_fire() means it will fire early; that's it.
112 *
113 * ### Features lost vs. #Timer ###
114 * - A #Timer can be reused repeatedly. This *might* be more performant than using this facility repeatedly, since
115 * (internally) that approach creates a #Timer each time. Note: We have no data about the cost of initializing a new
116 * #Timer, other than the fact I peeked at the boost.asio source and saw that the construction of a #Timer isn't
117 * obviously trivial/cheap -- which does NOT mean it isn't just cheap in reality, only that that's not immediately
118 * clear.
119 * - A rule of thumb in answering the question, 'Is this facility fine to just call repeatedly, or should we reuse
120 * a `Timer` for performance?': It *might* not be fine if and only if timer firings and/or cancellations occur
121 * many times a second in a performance-sensitive environment. E.g., if it's something fired and/or canceled
122 * every second repeatedly, it's fine; it it's packet pacing that must sensitively fire near the resolution limit
123 * of the native #Timer facility, it *might* not be fine, and it's safer to use #Timer repeatedly.
124 * - A #Timer can be re-scheduled to fire at an arbitrary different time than originally set. We provide no such
125 * facility. (We could provide such an API at the cost of API and implementation complexity; it's a judgment call,
126 * but I feel at that point just use a #Timer.)
127 * - However, we provide one special case of this: the timer can be fired
128 * ASAP, a/k/a short-fired, via scheduled_task_short_fire().
129 * - One can (incrementally) schedule 2+ tasks to fire at the scheduled time on one #Timer; this facility only takes
130 * exactly 1 task, up-front. (We could provide such an API, but again this feels like it defeats the point.)
131 *
132 * @todo We could eliminate schedule_task_from_now() potential limitation versus #Timer wherein each call constructs
133 * (internally) a new #Timer. A pool of `Timer`s can be internally maintained to implement this. This may or may not
134 * be worth the complexity, but if the API can remain identically simple while cleanly eliminating the one perf-related
135 * reason to choose #Timer over this simpler facility, then that is a clean win from the API user's point of view.
136 * By comparison, other possible improvements mentioned *complicate* the API which makes them less attractive.
137 *
138 * @see util::Timer doc header for native #Timer resolution limitations (which apply to quite-low `from_now` values).
139 * @note Design note: This is a small, somewhat C-style set of functions -- C-style in that it returns a handle
140 * on which to potentially call more functions as opposed to just being a class with methods. This is
141 * intentional, because #Timer already provides the stateful `class`. The picture is slightly muddled because
142 * we DO provide some "methods" -- so why not make it a `class` after all, just a simpler one than #Timer?
143 * Answer: I believe this keeps the API simple: Step 1: Schedule it. Step 2 (optional): Short-fire or cancel it.
144 * There are no corner cases introduced as might have been via increased potential statefulness inherent with a
145 * class. But see the following to-do.
146 *
147 * @todo schedule_task_from_now() and surrounding API provides an easy way to schedule a thing into the future, but
148 * it is built on top of boost.asio util::Timer directly; an intermediate wrapper class around this would be quite
149 * useful in its own right so that all boost.asio features including its perf-friendliness would be retained along
150 * with eliminating its annoyances (around canceling-but-not-really and similar). Then scheduled_task_from_now()
151 * would be internally even simpler, while a non-annoying util::Timer would become available for more advanced use
152 * cases. echan may have such a class (in a different project) ready to adapt (called `Serial_task_timer`).
153 * I believe it internally uses integer "task ID" to distinguish between scheduled tasks issued in some chronological
154 * order, so that boost.asio firing a task after it has been canceled/pre-fired can be easily detected.
155 *
156 * @param logger_ptr
157 * Logging, if any -- including in the background -- will be done via this logger.
158 * @param from_now
159 * Fire ASAP once this time period passes (0 to fire ASAP). A negative value has the same effect as 0.
160 * @param single_threaded
161 * Set to a true value if and only if, basically, `*task_engine` is single-threaded, and you promise not
162 * to call anything on the returned `Scheduled_task_handle` except from that same thread. More formally, see
163 * above.
164 * @param task_engine
165 * The `Task_engine` onto which the given task may be `post()`ed (or equivalent).
166 * @param task_body_moved
167 * The task to execute within `*task_engine` unless successfully canceled. See template param doc below also
168 * regarding `Strand` and other executor binding.
169 * @return Handle to the scheduled task which can be ignored in most cases. If you want to cancel, short-fire, etc.
170 * subsequently, save this (by value) and operate on it subsequently, e.g., with scheduled_task_cancel().
171 *
172 * @tparam Scheduled_task_handler
173 * Completion handler with signature compatible with `void (bool short_fired)`.
174 * This allows for standard boost.asio semantics, including associating with an executor such as a
175 * `boost::asio::io_context::strand`. In particular you may pass in: `bind_executor(S, F)`, where
176 * `F(bool short_fire)` is the handler, and `S` is a #Strand (or other executor).
177 * Binding to a #Strand will ensure the fired or short-fired body will not execute concurrently with
178 * any other handler also bound to it.
179 */
180template<typename Scheduled_task_handler>
182 const Fine_duration& from_now, bool single_threaded,
183 Task_engine* task_engine,
184 Scheduled_task_handler&& task_body_moved);
185
186/**
187 * Identical to schedule_task_from_now() except the time is specified in absolute terms.
188 *
189 * ### Performance note ###
190 * The current implementation is such that there is no performance benefit to using
191 * `schedule_task_from_now(at - Fine_clock::now(), ...)` over `schedule_task_at(at, ...)`. Therefore, if it
192 * is convenient for caller's code reuse to do the former, there is no perf downside to it, so feel free.
193 *
194 * @internal
195 * ### Maintenance reminder ###
196 * Ensure the "Performance note" is accurate w/r/t to the body of the function; as of this writing it is; keep
197 * the two places in sync with each other.
198 * @endinternal
199 *
200 * @param logger_ptr
201 * See schedule_task_from_now().
202 * @param at
203 * Fire at this absolute time. If this is in the past, it will fire ASAP.
204 * @param single_threaded
205 * See schedule_task_from_now().
206 * @param task_engine
207 * See schedule_task_from_now().
208 * @param task_body_moved
209 * See schedule_task_from_now().
210 * @return See schedule_task_from_now().
211 *
212 * @tparam Scheduled_task_handler
213 * See schedule_task_from_now().
214 */
215template<typename Scheduled_task_handler>
217 const Fine_time_pt& at, bool single_threaded,
218 Task_engine* task_engine,
219 Scheduled_task_handler&& task_body_moved);
220
221/**
222 * Attempts to reschedule a previously scheduled (by schedule_task_from_now() or similar) task to fire immediately.
223 * For semantics, in the context of the entire facility, see schedule_task_from_now() doc header.
224 *
225 * @param logger_ptr
226 * See schedule_task_from_now().
227 * @param task
228 * (Copy of) the handle returned by a previous `schedule_task_*()` call.
229 * @return `true` if the task will indeed have executed soon with `short_fire == true`.
230 * `false` if it has or will soon have executed with `short_file == false`; or if it has already been
231 * successfully canceled via scheduled_task_cancel().
232 */
234
235/**
236 * Attempts to prevent the execution of a previously scheduled (by schedule_task_from_now() or similar) task.
237 * For semantics, in the context of the entire facility, see schedule_task_from_now() doc header.
238 *
239 * @param logger_ptr
240 * See scheduled_task_short_fire().
241 * @param task
242 * See scheduled_task_short_fire().
243 * @return `true` if the task has not executed and will NEVER have executed, AND no other scheduled_task_cancel()
244 * with the same arg has succeeded before this.
245 * `false` if it has or will soon have executed because the present call occurred too late to stop it.
246 * Namely, it may have fired or short-fired already.
247 */
249
250/**
251 * Returns how long remains until a previously scheduled (by schedule_task_from_now() or similar) task fires;
252 * or negative time if that point is in the past; or special value if the task has been canceled.
253 *
254 * This is based solely on what was specified when scheduling it; it may be different from when it will actually fire
255 * or has fired. However, a special value (see below) is returned, if the task has been canceled
256 * (scheduled_task_cancel()).
257 *
258 * @param logger_ptr
259 * Logging, if any, will be done synchronously via this logger.
260 * @param task
261 * (Copy of) the handle returned by a previous `schedule_task_*()` call.
262 * @return Positive duration if it is set to fire in the future; negative or zero duration otherwise; or
263 * special value `Fine_duration::max()` to indicate the task has been canceled.
264 * Note a non-`max()` (probably negative) duration will be returned even if it has already fired.
265 */
267
268/**
269 * Returns whether a previously scheduled (by schedule_task_from_now() or similar) task has already fired.
270 * Note that this cannot be `true` while scheduled_task_canceled() is `false` and vice versa (but see thread safety note
271 * below).
272 *
273 * Also note that, while thread-safe, if `!single_threaded` in the original scheduling call, then the value
274 * returned might be different even if checked immediately after this function exits, in the same thread.
275 * However, if `single_threaded` (and one indeed properly uses `task` from one thread only) then it is guaranteed
276 * this value is consistent/correct synchronously in the caller's thread, until code in that thread actively changes it
277 * (e.g., by canceling task).
278 *
279 * @param logger_ptr
280 * See scheduled_task_fires_from_now_or_canceled().
281 * @param task
282 * See scheduled_task_fires_from_now_or_canceled().
283 * @return See above.
284 */
286
287/**
288 * Returns whether a previously scheduled (by schedule_task_from_now() or similar) task has been canceled.
289 * Note that this cannot be `true` while scheduled_task_fired() is `false` and vice versa (but see thread safety note
290 * below).
291 *
292 * Thread safety notes in the scheduled_task_fired() doc header apply equally here.
293 *
294 * @param logger_ptr
295 * See scheduled_task_fired().
296 * @param task
297 * See scheduled_task_fired().
298 * @return See above.
299 */
301
302} // namespace flow::util
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
Definition: log.hpp:1286
Flow module containing miscellaneous general-use facilities that don't fit into any other Flow module...
Definition: basic_blob.hpp:31
Scheduled_task_handle schedule_task_from_now(log::Logger *logger_ptr, const Fine_duration &from_now, bool single_threaded, Task_engine *task_engine, Scheduled_task_handler &&task_body_moved)
Schedule the given function to execute in a certain amount of time: A handy wrapper around Timer (asi...
Definition: sched_task.hpp:34
bool scheduled_task_fired(log::Logger *logger_ptr, Scheduled_task_const_handle task)
Returns whether a previously scheduled (by schedule_task_from_now() or similar) task has already fire...
Definition: sched_task.cpp:238
Fine_duration scheduled_task_fires_from_now_or_canceled(log::Logger *logger_ptr, Scheduled_task_const_handle task)
Returns how long remains until a previously scheduled (by schedule_task_from_now() or similar) task f...
Definition: sched_task.cpp:200
boost::shared_ptr< Scheduled_task_handle_state > Scheduled_task_handle
Black-box type that represents a handle to a scheduled task as scheduled by schedule_task_at() or sch...
Scheduled_task_handle schedule_task_at(log::Logger *logger_ptr, const Fine_time_pt &at, bool single_threaded, Task_engine *task_engine, Scheduled_task_handler &&task_body_moved)
Identical to schedule_task_from_now() except the time is specified in absolute terms.
Definition: sched_task.hpp:245
bool scheduled_task_short_fire(log::Logger *logger_ptr, Scheduled_task_handle task)
Attempts to reschedule a previously scheduled (by schedule_task_from_now() or similar) task to fire i...
Definition: sched_task.cpp:95
boost::asio::io_context Task_engine
Short-hand for boost.asio event service, the central class of boost.asio.
Definition: util_fwd.hpp:147
bool scheduled_task_canceled(log::Logger *logger_ptr, Scheduled_task_const_handle task)
Returns whether a previously scheduled (by schedule_task_from_now() or similar) task has been cancele...
Definition: sched_task.cpp:259
bool scheduled_task_cancel(log::Logger *logger_ptr, Scheduled_task_handle task)
Attempts to prevent the execution of a previously scheduled (by schedule_task_from_now() or similar) ...
Definition: sched_task.cpp:26
boost::shared_ptr< const Scheduled_task_handle_state > Scheduled_task_const_handle
Equivalent to Scheduled_task_handle but refers to immutable version of a task.
Fine_clock::duration Fine_duration
A high-res time duration as computed from two Fine_time_pts.
Definition: common.hpp:405
Fine_clock::time_point Fine_time_pt
A high-res time point as returned by Fine_clock::now() and suitable for precise time math in general.
Definition: common.hpp:402