• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include "pw_chrono/system_clock.h"
17 
18 namespace pw::async {
19 
20 class Task;
21 
22 /// Asynchronous Dispatcher abstract class. A default implementation is provided
23 /// in pw_async_basic.
24 ///
25 /// Dispatcher implements VirtualSystemClock so the Dispatcher's time can be
26 /// injected into other modules under test. This is useful for consistently
27 /// simulating time when using FakeDispatcher (rather than using
28 /// chrono::SimulatedSystemClock separately).
29 class Dispatcher : public chrono::VirtualSystemClock {
30  public:
31   ~Dispatcher() override = default;
32 
33   /// Post caller owned |task|.
34   virtual void Post(Task& task) = 0;
35 
36   /// Post caller owned |task| to be run after |delay|.
37   virtual void PostAfter(Task& task, chrono::SystemClock::duration delay) = 0;
38 
39   /// Post caller owned |task| to be run at |time|.
40   virtual void PostAt(Task& task, chrono::SystemClock::time_point time) = 0;
41 
42   /// Post caller owned |task| to be run immediately then rerun at a regular
43   /// |interval|.
44   virtual void PostPeriodic(Task& task,
45                             chrono::SystemClock::duration interval) = 0;
46   /// Post caller owned |task| to be run at |time| then rerun at a regular
47   /// |interval|. |interval| must not be zero.
48   virtual void PostPeriodicAt(Task& task,
49                               chrono::SystemClock::duration interval,
50                               chrono::SystemClock::time_point time) = 0;
51 
52   /// Returns true if |task| is succesfully canceled.
53   /// If cancelation fails, the task may be running or completed.
54   /// Periodic tasks may be posted once more after they are canceled.
55   virtual bool Cancel(Task& task) = 0;
56 };
57 
58 }  // namespace pw::async
59