Flow 1.0.1
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 * - #Timer has certain informational accessors (like one that returns the scheduled firing time) that we lack.
132 * (Again, we could provide this also -- but why?)
133 *
134 * @todo We could eliminate schedule_task_from_now() potential limitation versus #Timer wherein each call constructs
135 * (internally) a new #Timer. A pool of `Timer`s can be internally maintained to implement this. This may or may not
136 * be worth the complexity, but if the API can remain identically simple while cleanly eliminating the one perf-related
137 * reason to choose #Timer over this simpler facility, then that is a clean win from the API user's point of view.
138 * By comparison, other possible improvements mentioned *complicate* the API which makes them less attractive.
139 *
140 * @see util::Timer doc header for native #Timer resolution limitations (which apply to quite-low `from_now` values).
141 * @note Design note: This is a small, somewhat C-style set of functions -- C-style in that it returns a handle
142 * on which to potentially call more functions as opposed to just being a class with methods. This is
143 * intentional, because #Timer already provides the stateful `class`. The picture is slightly muddled because
144 * we DO provide some "methods" -- so why not make it a `class` after all, just a simpler one than #Timer?
145 * Answer: I believe this keeps the API simple: Step 1: Schedule it. Step 2 (optional): Short-fire or cancel it.
146 * There are no corner cases introduced as might have been via increased potential statefulness inherent with a
147 * class. But see the following to-do.
148 *
149 * @todo schedule_task_from_now() and surrounding API provides an easy way to schedule a thing into the future, but
150 * it is built on top of boost.asio util::Timer directly; an intermediate wrapper class around this would be quite
151 * useful in its own right so that all boost.asio features including its perf-friendliness would be retained along
152 * with eliminating its annoyances (around canceling-but-not-really and similar). Then scheduled_task_from_now()
153 * would be internally even simpler, while a non-annoying util::Timer would become available for more advanced use
154 * cases. echan may have such a class (in a different project) ready to adapt (called `Serial_task_timer`).
155 * I believe it internally uses integer "task ID" to distinguish between scheduled tasks issued in some chronological
156 * order, so that boost.asio firing a task after it has been canceled/pre-fired can be easily detected.
157 *
158 * @param logger_ptr
159 * Logging, if any -- including in the background -- will be done via this logger.
160 * @param from_now
161 * Fire ASAP once this time period passes (0 to fire ASAP). A negative value has the same effect as 0.
162 * @param single_threaded
163 * Set to a true value if and only if, basically, `*task_engine` is single-threaded, and you promise not
164 * to call anything on the returned `Scheduled_task_handle` except from that same thread. More formally, see
165 * above.
166 * @param task_engine
167 * The `Task_engine` onto which the given task may be `post()`ed (or equivalent).
168 * @param task_body_moved
169 * The task to execute within `*task_engine` unless successfully canceled. See template param doc below also
170 * regarding `Strand` and other executor binding.
171 * @return Handle to the scheduled task which can be ignored in most cases. If you want to cancel, short-fire, etc.
172 * subsequently, save this (by value) and operate on it subsequently, e.g., with scheduled_task_cancel().
173 *
174 * @tparam Scheduled_task_handler
175 * Completion handler with signature compatible with `void (bool short_fired)`.
176 * This allows for standard boost.asio semantics, including associating with an executor such as a
177 * `boost::asio::strand`. In particular you may pass in: `bind_executor(S, F)`, where
178 * `F(bool short_fire)` is the handler, and `S` is a #Strand (or other executor).
179 * Binding to a #Strand will ensure the fired or short-fired body will not execute concurrently with
180 * any other handler also bound to it.
181 */
182template<typename Scheduled_task_handler>
184 const Fine_duration& from_now, bool single_threaded,
185 Task_engine* task_engine,
186 Scheduled_task_handler&& task_body_moved);
187
188/**
189 * Identical to schedule_task_from_now() except the time is specified in absolute terms.
190 *
191 * ### Performance note ###
192 * The current implementation is such that there is no performance benefit to using
193 * `schedule_task_from_now(at - Fine_clock::now(), ...)` over `schedule_task_at(at, ...)`. Therefore, if it
194 * is convenient for caller's code reuse to do the former, there is no perf downside to it, so feel free.
195 *
196 * @internal
197 * ### Maintenance reminder ###
198 * Ensure the "Performance note" is accurate w/r/t to the body of the function; as of this writing it is; keep
199 * the two places in sync with each other.
200 * @endinternal
201 *
202 * @param logger_ptr
203 * See schedule_task_from_now().
204 * @param at
205 * Fire at this absolute time. If this is in the past, it will fire ASAP.
206 * @param single_threaded
207 * See schedule_task_from_now().
208 * @param task_engine
209 * See schedule_task_from_now().
210 * @param task_body_moved
211 * See schedule_task_from_now().
212 * @return See schedule_task_from_now().
213 *
214 * @tparam Scheduled_task_handler
215 * See schedule_task_from_now().
216 */
217template<typename Scheduled_task_handler>
219 const Fine_time_pt& at, bool single_threaded,
220 Task_engine* task_engine,
221 Scheduled_task_handler&& task_body_moved);
222
223/**
224 * Attempts to reschedule a previously scheduled (by schedule_task_from_now() or similar) task to fire immediately.
225 * For semantics, in the context of the entire facility, see schedule_task_from_now() doc header.
226 *
227 * @param logger_ptr
228 * See schedule_task_from_now().
229 * @param task
230 * (Copy of) the handle returned by a previous `schedule_task_*()` call.
231 * @return `true` if the task will indeed have executed soon with `short_fire == true`.
232 * `false` if it has or will soon have executed with `short_file == false`; or if it has already been
233 * successfully canceled via scheduled_task_cancel().
234 */
236
237/**
238 * Attempts to prevent the execution of a previously scheduled (by schedule_task_from_now() or similar) task.
239 * For semantics, in the context of the entire facility, see schedule_task_from_now() doc header.
240 *
241 * @param logger_ptr
242 * See scheduled_task_short_fire().
243 * @param task
244 * See scheduled_task_short_fire().
245 * @return `true` if the task has not executed and will NEVER have executed, AND no other scheduled_task_cancel()
246 * with the same arg has succeeded before this.
247 * `false` if it has or will soon have executed because the present call occurred too late to stop it.
248 * Namely, it may have fired or short-fired already.
249 */
251
252/**
253 * Returns how long remains until a previously scheduled (by schedule_task_from_now() or similar) task fires;
254 * or negative time if that point is in the past; or special value if the task has been canceled.
255 *
256 * This is based solely on what was specified when scheduling it; it may be different from when it will actually fire
257 * or has fired. However, a special value (see below) is returned, if the task has been canceled
258 * (scheduled_task_cancel()).
259 *
260 * @param logger_ptr
261 * Logging, if any, will be done synchronously via this logger.
262 * @param task
263 * (Copy of) the handle returned by a previous `schedule_task_*()` call.
264 * @return Positive duration if it is set to fire in the future; negative or zero duration otherwise; or
265 * special value `Fine_duration::max()` to indicate the task has been canceled.
266 * Note a non-`max()` (probably negative) duration will be returned even if it has already fired.
267 */
269
270/**
271 * Returns whether a previously scheduled (by schedule_task_from_now() or similar) task has already fired.
272 * Note that this cannot be `true` while scheduled_task_canceled() is `false` and vice versa (but see thread safety note
273 * below).
274 *
275 * Also note that, while thread-safe, if `!single_threaded` in the original scheduling call, then the value
276 * returned might be different even if checked immediately after this function exits, in the same thread.
277 * However, if `single_threaded` (and one indeed properly uses `task` from one thread only) then it is guaranteed
278 * this value is consistent/correct synchronously in the caller's thread, until code in that thread actively changes it
279 * (e.g., by canceling task).
280 *
281 * @param logger_ptr
282 * See scheduled_task_fires_from_now_or_canceled().
283 * @param task
284 * See scheduled_task_fires_from_now_or_canceled().
285 * @return See above.
286 */
288
289/**
290 * Returns whether a previously scheduled (by schedule_task_from_now() or similar) task has been canceled.
291 * Note that this cannot be `true` while scheduled_task_fired() is `false` and vice versa (but see thread safety note
292 * below).
293 *
294 * Thread safety notes in the scheduled_task_fired() doc header apply equally here.
295 *
296 * @param logger_ptr
297 * See scheduled_task_fired().
298 * @param task
299 * See scheduled_task_fired().
300 * @return See above.
301 */
303
304} // namespace flow::util
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
Definition: log.hpp:1291
Flow module containing miscellaneous general-use facilities that don't fit into any other Flow module...
Definition: basic_blob.hpp:29
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
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.
boost::asio::io_service Task_engine
Short-hand for boost.asio event service, the central class of boost.asio.
Definition: util_fwd.hpp:135
Fine_clock::duration Fine_duration
A high-res time duration as computed from two Fine_time_pts.
Definition: common.hpp:410
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:407