blob: b10e5ec74a76b986b1fd134c104d4bdcb896872c [file] [log] [blame]
Jordan Baylesb0c191e2019-03-26 15:49:57 -07001// 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
btolschc92ba2f2019-04-10 11:46:01 -07005#ifndef PLATFORM_API_TASK_RUNNER_H_
6#define PLATFORM_API_TASK_RUNNER_H_
Jordan Baylesb0c191e2019-03-26 15:49:57 -07007
Yuri Wiitala75ea15d2019-12-03 16:01:48 -08008#include <future> // NOLINT
9#include <utility>
Jordan Baylesb0c191e2019-03-26 15:49:57 -070010
11#include "platform/api/time.h"
12
13namespace openscreen {
Jordan Baylesb0c191e2019-03-26 15:49:57 -070014
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
Ryan Keane32c88d02019-07-02 18:46:14 -070017// be handled by the implementation class. It is the expectation of this API
18// that the underlying impl gives the following guarantees:
Jordan Baylesb0c191e2019-03-26 15:49:57 -070019// (1) Tasks shall not overlap in time/CPU.
20// (2) Tasks shall run sequentially, e.g. posting task A then B implies
21// that A shall run before B.
Jordan Baylesb0c191e2019-03-26 15:49:57 -070022class TaskRunner {
23 public:
Yuri Wiitalab929b832019-06-05 17:13:15 -070024 using Task = std::packaged_task<void() noexcept>;
Jordan Baylesb0c191e2019-03-26 15:49:57 -070025
26 virtual ~TaskRunner() = default;
27
Yuri Wiitalab929b832019-06-05 17:13:15 -070028 // Takes any callable target (function, lambda-expression, std::bind result,
29 // etc.) that should be run at the first convenient time.
30 template <typename Functor>
31 inline void PostTask(Functor f) {
32 PostPackagedTask(Task(std::move(f)));
33 }
Jordan Baylesb0c191e2019-03-26 15:49:57 -070034
Yuri Wiitalab929b832019-06-05 17:13:15 -070035 // Takes any callable target (function, lambda-expression, std::bind result,
36 // etc.) that should be run no sooner than |delay| time from now. Note that
37 // the Task might run after an additional delay, especially under heavier
38 // system load. There is no deadline concept.
39 template <typename Functor>
40 inline void PostTaskWithDelay(Functor f, Clock::duration delay) {
41 PostPackagedTaskWithDelay(Task(std::move(f)), delay);
42 }
43
44 // Implementations should provide the behavior explained in the comments above
45 // for PostTask[WithDelay](). Client code may also call these directly when
46 // passing an existing Task object.
47 virtual void PostPackagedTask(Task task) = 0;
48 virtual void PostPackagedTaskWithDelay(Task task, Clock::duration delay) = 0;
Max Yakimakhabf567dc2019-09-20 13:37:04 -070049
50 // Return true if the calling thread is the thread that task runner is using
51 // to run tasks, false otherwise.
Max Yakimakha05eb5402019-10-04 11:15:37 -070052 virtual bool IsRunningOnTaskRunner() { return true; }
Jordan Baylesb0c191e2019-03-26 15:49:57 -070053};
Yuri Wiitalab929b832019-06-05 17:13:15 -070054
Jordan Baylesb0c191e2019-03-26 15:49:57 -070055} // namespace openscreen
56
btolschc92ba2f2019-04-10 11:46:01 -070057#endif // PLATFORM_API_TASK_RUNNER_H_