blob: 2f0580303aa4dddaf64336d42fd1d1f3920d977d [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
Jordan Baylesb0c191e2019-03-26 15:49:57 -07008#include <functional>
Jordan Baylesb0c191e2019-03-26 15:49:57 -07009
10#include "platform/api/time.h"
11
12namespace openscreen {
13namespace 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.
24class TaskRunner {
25 public:
26 using Task = std::function<void()>;
27
28 virtual ~TaskRunner() = default;
29
30 // Takes a Task that should be run at the first convenient time.
31 virtual void PostTask(Task task) = 0;
32
33 // Takes a Task that should be run no sooner than "delay" time from now. Note
34 // that we do not guarantee it will run precisely "delay" later, merely that
35 // it will run no sooner than "delay" time from now.
36 virtual void PostTaskWithDelay(Task task, Clock::duration delay) = 0;
37};
38} // namespace platform
39} // namespace openscreen
40
btolschc92ba2f2019-04-10 11:46:01 -070041#endif // PLATFORM_API_TASK_RUNNER_H_