Add TaskRunner implementation to Open Screen
This patch contains an initial TaskRunner for use with Open Screen.
This initial version does not contain a Chromium wrapper, nor does
it permit cancelling. A unit test suite is included for this new
class.
Change-Id: I2404980acf3b40b78d9772925ebe11ed460cc0b6
Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/1544773
Reviewed-by: Yuri Wiitala <miu@chromium.org>
Commit-Queue: Jordan Bayles <jophba@chromium.org>
diff --git a/api/public/task_runner.h b/api/public/task_runner.h
new file mode 100644
index 0000000..2921032
--- /dev/null
+++ b/api/public/task_runner.h
@@ -0,0 +1,45 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef API_PUBLIC_TASK_RUNNER_H_
+#define API_PUBLIC_TASK_RUNNER_H_
+
+#include <deque>
+#include <functional>
+#include <list>
+#include <memory>
+#include <utility>
+
+#include "platform/api/time.h"
+
+namespace openscreen {
+namespace platform {
+
+// A thread-safe API surface that allows for posting tasks. The underlying
+// implementation may be single or multi-threaded, and all complication should
+// be handled by either the implementation class or the TaskRunnerFactory
+// method. It is the expectation of this API that the underlying impl gives
+// the following guarantees:
+// (1) Tasks shall not overlap in time/CPU.
+// (2) Tasks shall run sequentially, e.g. posting task A then B implies
+// that A shall run before B.
+// NOTE: we do not make any assumptions about what thread tasks shall run on.
+class TaskRunner {
+ public:
+ using Task = std::function<void()>;
+
+ virtual ~TaskRunner() = default;
+
+ // Takes a Task that should be run at the first convenient time.
+ virtual void PostTask(Task task) = 0;
+
+ // Takes a Task that should be run no sooner than "delay" time from now. Note
+ // that we do not guarantee it will run precisely "delay" later, merely that
+ // it will run no sooner than "delay" time from now.
+ virtual void PostTaskWithDelay(Task task, Clock::duration delay) = 0;
+};
+} // namespace platform
+} // namespace openscreen
+
+#endif // API_PUBLIC_TASK_RUNNER_H_