Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
btolsch | c92ba2f | 2019-04-10 11:46:01 -0700 | [diff] [blame] | 5 | #ifndef PLATFORM_API_TASK_RUNNER_H_ |
| 6 | #define PLATFORM_API_TASK_RUNNER_H_ |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 7 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 8 | #include <future> |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 9 | |
Ryan Keane | 32c88d0 | 2019-07-02 18:46:14 -0700 | [diff] [blame] | 10 | #include "absl/types/optional.h" |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 11 | #include "platform/api/time.h" |
| 12 | |
| 13 | namespace openscreen { |
| 14 | namespace platform { |
| 15 | |
| 16 | // A thread-safe API surface that allows for posting tasks. The underlying |
| 17 | // implementation may be single or multi-threaded, and all complication should |
Ryan Keane | 32c88d0 | 2019-07-02 18:46:14 -0700 | [diff] [blame] | 18 | // be handled by the implementation class. It is the expectation of this API |
| 19 | // that the underlying impl gives the following guarantees: |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 20 | // (1) Tasks shall not overlap in time/CPU. |
| 21 | // (2) Tasks shall run sequentially, e.g. posting task A then B implies |
| 22 | // that A shall run before B. |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 23 | class TaskRunner { |
| 24 | public: |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 25 | using Task = std::packaged_task<void() noexcept>; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 26 | |
| 27 | virtual ~TaskRunner() = default; |
| 28 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 29 | // Takes any callable target (function, lambda-expression, std::bind result, |
| 30 | // etc.) that should be run at the first convenient time. |
| 31 | template <typename Functor> |
| 32 | inline void PostTask(Functor f) { |
| 33 | PostPackagedTask(Task(std::move(f))); |
| 34 | } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 35 | |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 36 | // Takes any callable target (function, lambda-expression, std::bind result, |
| 37 | // etc.) that should be run no sooner than |delay| time from now. Note that |
| 38 | // the Task might run after an additional delay, especially under heavier |
| 39 | // system load. There is no deadline concept. |
| 40 | template <typename Functor> |
| 41 | inline void PostTaskWithDelay(Functor f, Clock::duration delay) { |
| 42 | PostPackagedTaskWithDelay(Task(std::move(f)), delay); |
| 43 | } |
| 44 | |
| 45 | // Implementations should provide the behavior explained in the comments above |
| 46 | // for PostTask[WithDelay](). Client code may also call these directly when |
| 47 | // passing an existing Task object. |
| 48 | virtual void PostPackagedTask(Task task) = 0; |
| 49 | virtual void PostPackagedTaskWithDelay(Task task, Clock::duration delay) = 0; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 50 | }; |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 51 | |
Ryan Keane | 32c88d0 | 2019-07-02 18:46:14 -0700 | [diff] [blame] | 52 | // Class used to post the same task repeatedly to the task runner, with the |
| 53 | // frequency of repetition determined by the result of the underlying function. |
| 54 | // TODO(rwkeane): Move to separate file in util directory. |
| 55 | class RepeatingFunction { |
| 56 | public: |
| 57 | // Posts a delayed task that will run repeatedly. The result of the function |
| 58 | // object will determine if the task should be reposted, in that it will be |
| 59 | // reposted if and only if the result is not absl::nullopt. |
| 60 | static inline void Post( |
| 61 | TaskRunner* task_runner, |
| 62 | std::function<absl::optional<Clock::duration>()> function, |
| 63 | Clock::duration delay = Clock::duration(0)) { |
| 64 | task_runner->PostTaskWithDelay(RepeatingFunction(task_runner, function), |
| 65 | delay); |
| 66 | } |
| 67 | |
| 68 | // Executes the underlying task and re-posts it to the task runner. |
| 69 | void operator()() { |
| 70 | absl::optional<Clock::duration> delay = function_(); |
| 71 | if (delay.has_value()) { |
| 72 | RepeatingFunction::Post(task_runner_, function_, delay.value()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | // Creates a new task that will be posted repeatedly to the task runner. If |
| 78 | // the function returns a valid Clock::duration, it will be reposted with |
| 79 | // that delay, and will not be reposted if absl::nullopt is instead |
| 80 | // returned. |
| 81 | // TODO(rwkeane): Should use a weak pointer once we support those. |
| 82 | RepeatingFunction(TaskRunner* task_runner, |
| 83 | std::function<absl::optional<Clock::duration>()> function) |
Jordan Bayles | d0b8fa3 | 2019-07-10 16:59:00 -0700 | [diff] [blame] | 84 | : task_runner_(task_runner), function_(function) {} |
Ryan Keane | 32c88d0 | 2019-07-02 18:46:14 -0700 | [diff] [blame] | 85 | |
| 86 | TaskRunner* task_runner_; |
| 87 | std::function<absl::optional<Clock::duration>()> function_; |
| 88 | }; |
| 89 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 90 | } // namespace platform |
| 91 | } // namespace openscreen |
| 92 | |
btolsch | c92ba2f | 2019-04-10 11:46:01 -0700 | [diff] [blame] | 93 | #endif // PLATFORM_API_TASK_RUNNER_H_ |