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 | 75ea15d | 2019-12-03 16:01:48 -0800 | [diff] [blame] | 8 | #include <future> // NOLINT |
| 9 | #include <utility> |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 10 | |
| 11 | #include "platform/api/time.h" |
| 12 | |
| 13 | namespace openscreen { |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 14 | |
| 15 | // A thread-safe API surface that allows for posting tasks. The underlying |
| 16 | // implementation may be single or multi-threaded, and all complication should |
mark a. foltz | 39dda17 | 2020-02-05 16:44:03 -0800 | [diff] [blame] | 17 | // be handled by the implementation class. The implementation must guarantee: |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 18 | // (1) Tasks shall not overlap in time/CPU. |
| 19 | // (2) Tasks shall run sequentially, e.g. posting task A then B implies |
| 20 | // that A shall run before B. |
mark a. foltz | 39dda17 | 2020-02-05 16:44:03 -0800 | [diff] [blame] | 21 | // (3) If task A is posted before task B, then any mutation in A happens-before |
| 22 | // B runs (even if A and B run on different threads). |
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; |
Max Yakimakha | bf567dc | 2019-09-20 13:37:04 -0700 | [diff] [blame] | 50 | |
| 51 | // Return true if the calling thread is the thread that task runner is using |
| 52 | // to run tasks, false otherwise. |
Max Yakimakha | 05eb540 | 2019-10-04 11:15:37 -0700 | [diff] [blame] | 53 | virtual bool IsRunningOnTaskRunner() { return true; } |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 54 | }; |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 55 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 56 | } // namespace openscreen |
| 57 | |
btolsch | c92ba2f | 2019-04-10 11:46:01 -0700 | [diff] [blame] | 58 | #endif // PLATFORM_API_TASK_RUNNER_H_ |