1 // Copyright 2021 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_chrono/system_timer.h"
16
17 #include <kernel.h>
18 #include <sys/mutex.h>
19
20 #include <algorithm>
21
22 #include "pw_chrono_zephyr/system_clock_constants.h"
23 #include "pw_chrono_zephyr/system_timer_native.h"
24
25 namespace pw::chrono {
26 namespace {
27
28 constexpr SystemClock::duration kMinTimerPeriod = SystemClock::duration(1);
29 // Work synchronization objects must be in cache-coherent memory, which excludes
30 // stacks on some architectures.
31 static k_work_sync work_sync;
32
33 } // namespace
34
HandleTimerWork(k_work * item)35 void HandleTimerWork(k_work* item) {
36 k_work_delayable* delayable_item = k_work_delayable_from_work(item);
37 backend::NativeSystemTimer* native_type =
38 CONTAINER_OF(delayable_item, backend::ZephyrWorkWrapper, work)->owner;
39
40 sys_mutex_lock(&native_type->mutex, K_FOREVER);
41 #ifdef CONFIG_TIMEOUT_64BIT
42 native_type->user_callback(native_type->expiry_deadline);
43 #else
44 const SystemClock::duration time_until_deadline =
45 native_type->expiry_deadline - SystemClock::now();
46 if (time_until_deadline <= SystemClock::duration::zero()) {
47 native_type->user_callback(native_type->expiry_deadline);
48 } else {
49 // We haven't met the deadline yet, reschedule as far out as possible.
50 const SystemClock::duration period =
51 std::min(pw::chrono::zephyr::kMaxTimeout, time_until_deadline);
52 k_work_schedule(&native_type->work_wrapper.work, K_TICKS(period.count()));
53 }
54 #endif // CONFIG_TIMEOUT_64BIT
55 sys_mutex_unlock(&native_type->mutex);
56 }
57
SystemTimer(ExpiryCallback callback)58 SystemTimer::SystemTimer(ExpiryCallback callback)
59 : native_type_{.work_wrapper =
60 {
61 .work = {},
62 .owner = nullptr,
63 },
64 .mutex = {},
65 .expiry_deadline = SystemClock::time_point(),
66 .user_callback = std::move(callback)} {
67 k_work_init_delayable(&native_type_.work_wrapper.work, HandleTimerWork);
68 sys_mutex_init(&native_type_.mutex);
69 native_type_.work_wrapper.owner = &native_type_;
70 }
71
~SystemTimer()72 SystemTimer::~SystemTimer() {
73 k_work_cancel_sync(&native_type_.work_wrapper.work, &work_sync);
74 }
75
InvokeAt(SystemClock::time_point timestamp)76 void SystemTimer::InvokeAt(SystemClock::time_point timestamp) {
77 sys_mutex_lock(&native_type_.mutex, K_FOREVER);
78 native_type_.expiry_deadline = timestamp;
79
80 const SystemClock::duration time_until_deadline =
81 timestamp - SystemClock::now();
82 const SystemClock::duration period =
83 IS_ENABLED(CONFIG_TIMEOUT_64BIT)
84 ? time_until_deadline
85 : std::clamp(kMinTimerPeriod,
86 time_until_deadline,
87 pw::chrono::zephyr::kMaxTimeout);
88
89 k_work_schedule(&native_type_.work_wrapper.work, K_TICKS(period.count()));
90 sys_mutex_unlock(&native_type_.mutex);
91 }
92
93 } // namespace pw::chrono
94