xref: /aosp_15_r20/external/pigweed/pw_async2/system_time_provider.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 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 
15 #include "pw_async2/system_time_provider.h"
16 
17 #include "pw_chrono/system_timer.h"
18 #include "pw_toolchain/no_destructor.h"
19 
20 namespace pw::async2 {
21 namespace {
22 
23 using ::pw::chrono::SystemClock;
24 
25 class SystemTimeProvider final : public TimeProvider<SystemClock> {
26  public:
SystemTimeProvider()27   SystemTimeProvider()
28       : timer_(
29             [this](SystemClock::time_point expired) { RunExpired(expired); }) {}
30 
31  private:
now()32   SystemClock::time_point now() final { return SystemClock::now(); }
33 
DoInvokeAt(SystemClock::time_point time_point)34   void DoInvokeAt(SystemClock::time_point time_point) final {
35     timer_.InvokeAt(time_point);
36   }
37 
DoCancel()38   void DoCancel() final { timer_.Cancel(); }
39 
40   pw::chrono::SystemTimer timer_;
41 };
42 
43 }  // namespace
44 
GetSystemTimeProvider()45 TimeProvider<SystemClock>& GetSystemTimeProvider() {
46   static pw::NoDestructor<SystemTimeProvider> time_provider;
47   return *time_provider;
48 }
49 
50 }  // namespace pw::async2
51