• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PLATFORM_API_TASK_RUNNER_H_
6 #define PLATFORM_API_TASK_RUNNER_H_
7 
8 #include <future>
9 #include <utility>
10 
11 #include "platform/api/time.h"
12 
13 namespace openscreen {
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 the implementation class. The implementation must guarantee:
18 // (1) Tasks shall not overlap in time/CPU.
19 // (2) Tasks shall run sequentially, e.g. posting task A then B implies
20 //     that A shall run before B.
21 // (3) If task A is posted before task B, then any mutation in A happens-before
22 //     B runs (even if A and B run on different threads).
23 class TaskRunner {
24  public:
25 // Seem to get an error using clang when compiling with -fno-exceptions:
26 //   error: implicit instantiation of undefined template 'std::__1::packaged_task<void () noexcept>'
27 #if __has_feature(cxx_exceptions)
28   using Task = std::packaged_task<void() noexcept>;
29 #else
30   using Task = std::packaged_task<void()>;
31 #endif
32 
33   virtual ~TaskRunner() = default;
34 
35   // Takes any callable target (function, lambda-expression, std::bind result,
36   // etc.) that should be run at the first convenient time.
37   template <typename Functor>
PostTask(Functor f)38   inline void PostTask(Functor f) {
39     PostPackagedTask(Task(std::move(f)));
40   }
41 
42   // Takes any callable target (function, lambda-expression, std::bind result,
43   // etc.) that should be run no sooner than |delay| time from now. Note that
44   // the Task might run after an additional delay, especially under heavier
45   // system load. There is no deadline concept.
46   template <typename Functor>
PostTaskWithDelay(Functor f,Clock::duration delay)47   inline void PostTaskWithDelay(Functor f, Clock::duration delay) {
48     PostPackagedTaskWithDelay(Task(std::move(f)), delay);
49   }
50 
51   // Implementations should provide the behavior explained in the comments above
52   // for PostTask[WithDelay](). Client code may also call these directly when
53   // passing an existing Task object.
54   virtual void PostPackagedTask(Task task) = 0;
55   virtual void PostPackagedTaskWithDelay(Task task, Clock::duration delay) = 0;
56 
57   // Return true if the calling thread is the thread that task runner is using
58   // to run tasks, false otherwise.
59   virtual bool IsRunningOnTaskRunner() = 0;
60 };
61 
62 }  // namespace openscreen
63 
64 #endif  // PLATFORM_API_TASK_RUNNER_H_
65