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 | |
| 10 | #include "platform/api/time.h" |
| 11 | |
| 12 | namespace openscreen { |
| 13 | namespace platform { |
| 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 |
| 17 | // be handled by either the implementation class or the TaskRunnerFactory |
| 18 | // method. It is the expectation of this API that the underlying impl gives |
| 19 | // the following guarantees: |
| 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. |
| 23 | // NOTE: we do not make any assumptions about what thread tasks shall run on. |
| 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; |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 51 | }; |
Yuri Wiitala | b929b83 | 2019-06-05 17:13:15 -0700 | [diff] [blame] | 52 | |
Jordan Bayles | b0c191e | 2019-03-26 15:49:57 -0700 | [diff] [blame] | 53 | } // namespace platform |
| 54 | } // namespace openscreen |
| 55 | |
btolsch | c92ba2f | 2019-04-10 11:46:01 -0700 | [diff] [blame] | 56 | #endif // PLATFORM_API_TASK_RUNNER_H_ |