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