xref: /aosp_15_r20/external/pigweed/pw_chrono_threadx/system_clock.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2020 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker 
15*61c4878aSAndroid Build Coastguard Worker #include "pw_chrono/system_clock.h"
16*61c4878aSAndroid Build Coastguard Worker 
17*61c4878aSAndroid Build Coastguard Worker #include <atomic>
18*61c4878aSAndroid Build Coastguard Worker #include <chrono>
19*61c4878aSAndroid Build Coastguard Worker #include <limits>
20*61c4878aSAndroid Build Coastguard Worker #include <mutex>
21*61c4878aSAndroid Build Coastguard Worker 
22*61c4878aSAndroid Build Coastguard Worker #include "pw_sync/interrupt_spin_lock.h"
23*61c4878aSAndroid Build Coastguard Worker #include "tx_api.h"
24*61c4878aSAndroid Build Coastguard Worker 
25*61c4878aSAndroid Build Coastguard Worker namespace pw::chrono::backend {
26*61c4878aSAndroid Build Coastguard Worker namespace {
27*61c4878aSAndroid Build Coastguard Worker 
28*61c4878aSAndroid Build Coastguard Worker #if defined(TX_NO_TIMER) && TX_NO_TIMER
29*61c4878aSAndroid Build Coastguard Worker #error "This backend is not compatible with TX_NO_TIMER"
30*61c4878aSAndroid Build Coastguard Worker #endif  // defined(TX_NO_TIMER) && TX_NO_TIMER
31*61c4878aSAndroid Build Coastguard Worker 
32*61c4878aSAndroid Build Coastguard Worker sync::InterruptSpinLock system_clock_interrupt_spin_lock;
33*61c4878aSAndroid Build Coastguard Worker int64_t overflow_tick_count = 0;
34*61c4878aSAndroid Build Coastguard Worker ULONG native_tick_count = 0;
35*61c4878aSAndroid Build Coastguard Worker static_assert(!SystemClock::is_nmi_safe,
36*61c4878aSAndroid Build Coastguard Worker               "global state is not atomic nor double buferred");
37*61c4878aSAndroid Build Coastguard Worker 
38*61c4878aSAndroid Build Coastguard Worker // The tick count resets to 0, ergo the overflow count is the max count + 1.
39*61c4878aSAndroid Build Coastguard Worker constexpr int64_t kNativeOverflowTickCount =
40*61c4878aSAndroid Build Coastguard Worker     static_cast<int64_t>(std::numeric_limits<ULONG>::max()) + 1;
41*61c4878aSAndroid Build Coastguard Worker 
42*61c4878aSAndroid Build Coastguard Worker }  // namespace
43*61c4878aSAndroid Build Coastguard Worker 
GetSystemClockTickCount()44*61c4878aSAndroid Build Coastguard Worker int64_t GetSystemClockTickCount() {
45*61c4878aSAndroid Build Coastguard Worker   std::lock_guard lock(system_clock_interrupt_spin_lock);
46*61c4878aSAndroid Build Coastguard Worker   const ULONG new_native_tick_count = tx_time_get();
47*61c4878aSAndroid Build Coastguard Worker   // WARNING: This must be called more than once per overflow period!
48*61c4878aSAndroid Build Coastguard Worker   if (new_native_tick_count < native_tick_count) {
49*61c4878aSAndroid Build Coastguard Worker     // Native tick count overflow detected!
50*61c4878aSAndroid Build Coastguard Worker     overflow_tick_count += kNativeOverflowTickCount;
51*61c4878aSAndroid Build Coastguard Worker   }
52*61c4878aSAndroid Build Coastguard Worker   native_tick_count = new_native_tick_count;
53*61c4878aSAndroid Build Coastguard Worker   return overflow_tick_count + native_tick_count;
54*61c4878aSAndroid Build Coastguard Worker }
55*61c4878aSAndroid Build Coastguard Worker 
56*61c4878aSAndroid Build Coastguard Worker }  // namespace pw::chrono::backend
57