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 | |
| 5 | #ifndef API_PUBLIC_TASK_RUNNER_H_ |
| 6 | #define API_PUBLIC_TASK_RUNNER_H_ |
| 7 | |
| 8 | #include <deque> |
| 9 | #include <functional> |
| 10 | #include <list> |
| 11 | #include <memory> |
| 12 | #include <utility> |
| 13 | |
| 14 | #include "platform/api/time.h" |
| 15 | |
| 16 | namespace openscreen { |
| 17 | namespace platform { |
| 18 | |
| 19 | // A thread-safe API surface that allows for posting tasks. The underlying |
| 20 | // implementation may be single or multi-threaded, and all complication should |
| 21 | // be handled by either the implementation class or the TaskRunnerFactory |
| 22 | // method. It is the expectation of this API that the underlying impl gives |
| 23 | // the following guarantees: |
| 24 | // (1) Tasks shall not overlap in time/CPU. |
| 25 | // (2) Tasks shall run sequentially, e.g. posting task A then B implies |
| 26 | // that A shall run before B. |
| 27 | // NOTE: we do not make any assumptions about what thread tasks shall run on. |
| 28 | class TaskRunner { |
| 29 | public: |
| 30 | using Task = std::function<void()>; |
| 31 | |
| 32 | virtual ~TaskRunner() = default; |
| 33 | |
| 34 | // Takes a Task that should be run at the first convenient time. |
| 35 | virtual void PostTask(Task task) = 0; |
| 36 | |
| 37 | // Takes a Task that should be run no sooner than "delay" time from now. Note |
| 38 | // that we do not guarantee it will run precisely "delay" later, merely that |
| 39 | // it will run no sooner than "delay" time from now. |
| 40 | virtual void PostTaskWithDelay(Task task, Clock::duration delay) = 0; |
| 41 | }; |
| 42 | } // namespace platform |
| 43 | } // namespace openscreen |
| 44 | |
| 45 | #endif // API_PUBLIC_TASK_RUNNER_H_ |