1*61c4878aSAndroid Build Coastguard Worker // Copyright 2021 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_sync/thread_notification.h"
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker #include <mutex>
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker #include "FreeRTOS.h"
20*61c4878aSAndroid Build Coastguard Worker #include "pw_assert/check.h"
21*61c4878aSAndroid Build Coastguard Worker #include "pw_interrupt/context.h"
22*61c4878aSAndroid Build Coastguard Worker #include "pw_sync_freertos/config.h"
23*61c4878aSAndroid Build Coastguard Worker #include "task.h"
24*61c4878aSAndroid Build Coastguard Worker
25*61c4878aSAndroid Build Coastguard Worker namespace pw::sync {
26*61c4878aSAndroid Build Coastguard Worker namespace {
27*61c4878aSAndroid Build Coastguard Worker
WaitForNotification(TickType_t xTicksToWait)28*61c4878aSAndroid Build Coastguard Worker BaseType_t WaitForNotification(TickType_t xTicksToWait) {
29*61c4878aSAndroid Build Coastguard Worker #ifdef configTASK_NOTIFICATION_ARRAY_ENTRIES
30*61c4878aSAndroid Build Coastguard Worker return xTaskNotifyWaitIndexed(
31*61c4878aSAndroid Build Coastguard Worker pw::sync::freertos::config::kThreadNotificationIndex,
32*61c4878aSAndroid Build Coastguard Worker 0, // Clear no bits on entry.
33*61c4878aSAndroid Build Coastguard Worker 0, // Clear no bits on exit.
34*61c4878aSAndroid Build Coastguard Worker nullptr, // Don't care about the notification value.
35*61c4878aSAndroid Build Coastguard Worker xTicksToWait);
36*61c4878aSAndroid Build Coastguard Worker #else // !configTASK_NOTIFICATION_ARRAY_ENTRIES
37*61c4878aSAndroid Build Coastguard Worker return xTaskNotifyWait(0, // Clear no bits on entry.
38*61c4878aSAndroid Build Coastguard Worker 0, // Clear no bits on exit.
39*61c4878aSAndroid Build Coastguard Worker nullptr, // Don't care about the notification value.
40*61c4878aSAndroid Build Coastguard Worker xTicksToWait);
41*61c4878aSAndroid Build Coastguard Worker #endif // configTASK_NOTIFICATION_ARRAY_ENTRIES
42*61c4878aSAndroid Build Coastguard Worker }
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard Worker } // namespace
45*61c4878aSAndroid Build Coastguard Worker
acquire()46*61c4878aSAndroid Build Coastguard Worker void ThreadNotification::acquire() {
47*61c4878aSAndroid Build Coastguard Worker // Enforce the pw::sync::ThreadNotification IRQ contract.
48*61c4878aSAndroid Build Coastguard Worker PW_DCHECK(!interrupt::InInterruptContext());
49*61c4878aSAndroid Build Coastguard Worker
50*61c4878aSAndroid Build Coastguard Worker // Enforce that only a single thread can block at a time.
51*61c4878aSAndroid Build Coastguard Worker PW_DCHECK(native_type_.blocked_thread == nullptr);
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard Worker // Ensure that no one forgot to clean up nor corrupted the task notification
54*61c4878aSAndroid Build Coastguard Worker // state in the TCB.
55*61c4878aSAndroid Build Coastguard Worker PW_DCHECK(xTaskNotifyStateClear(nullptr) == pdFALSE);
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Worker {
58*61c4878aSAndroid Build Coastguard Worker std::lock_guard lock(native_type_.shared_spin_lock);
59*61c4878aSAndroid Build Coastguard Worker if (native_type_.notified) {
60*61c4878aSAndroid Build Coastguard Worker native_type_.notified = false;
61*61c4878aSAndroid Build Coastguard Worker return;
62*61c4878aSAndroid Build Coastguard Worker }
63*61c4878aSAndroid Build Coastguard Worker // Not notified yet, set the task handle for a one-time notification.
64*61c4878aSAndroid Build Coastguard Worker native_type_.blocked_thread = xTaskGetCurrentTaskHandle();
65*61c4878aSAndroid Build Coastguard Worker }
66*61c4878aSAndroid Build Coastguard Worker
67*61c4878aSAndroid Build Coastguard Worker // Even if INCLUDE_vTaskSuspend == 1 and ergo portMAX_DELAY means indefinite,
68*61c4878aSAndroid Build Coastguard Worker // vTaskSuspend() can abort xTaskNotifyWait() causing it to spuriously wake up
69*61c4878aSAndroid Build Coastguard Worker // after vTaskResume() returning pdFALSE as we were not actually notified.
70*61c4878aSAndroid Build Coastguard Worker while (WaitForNotification(portMAX_DELAY) == pdFALSE) {
71*61c4878aSAndroid Build Coastguard Worker }
72*61c4878aSAndroid Build Coastguard Worker
73*61c4878aSAndroid Build Coastguard Worker std::lock_guard lock(native_type_.shared_spin_lock);
74*61c4878aSAndroid Build Coastguard Worker // The task handle was cleared by the notifier.
75*61c4878aSAndroid Build Coastguard Worker // Note that this may hide another notification, however this is considered
76*61c4878aSAndroid Build Coastguard Worker // a form of notification saturation just like as if this happened before
77*61c4878aSAndroid Build Coastguard Worker // acquire() was invoked.
78*61c4878aSAndroid Build Coastguard Worker native_type_.notified = false;
79*61c4878aSAndroid Build Coastguard Worker }
80*61c4878aSAndroid Build Coastguard Worker
release()81*61c4878aSAndroid Build Coastguard Worker void ThreadNotification::release() {
82*61c4878aSAndroid Build Coastguard Worker if (!interrupt::InInterruptContext()) { // Task context
83*61c4878aSAndroid Build Coastguard Worker std::lock_guard lock(native_type_.shared_spin_lock);
84*61c4878aSAndroid Build Coastguard Worker if (native_type_.blocked_thread != nullptr) {
85*61c4878aSAndroid Build Coastguard Worker #ifdef configTASK_NOTIFICATION_ARRAY_ENTRIES
86*61c4878aSAndroid Build Coastguard Worker xTaskNotifyIndexed(native_type_.blocked_thread,
87*61c4878aSAndroid Build Coastguard Worker pw::sync::freertos::config::kThreadNotificationIndex,
88*61c4878aSAndroid Build Coastguard Worker 0u,
89*61c4878aSAndroid Build Coastguard Worker eNoAction);
90*61c4878aSAndroid Build Coastguard Worker #else // !configTASK_NOTIFICATION_ARRAY_ENTRIES
91*61c4878aSAndroid Build Coastguard Worker xTaskNotify(native_type_.blocked_thread, 0u, eNoAction);
92*61c4878aSAndroid Build Coastguard Worker #endif // configTASK_NOTIFICATION_ARRAY_ENTRIES
93*61c4878aSAndroid Build Coastguard Worker native_type_.blocked_thread = nullptr;
94*61c4878aSAndroid Build Coastguard Worker }
95*61c4878aSAndroid Build Coastguard Worker native_type_.notified = true;
96*61c4878aSAndroid Build Coastguard Worker return;
97*61c4878aSAndroid Build Coastguard Worker }
98*61c4878aSAndroid Build Coastguard Worker
99*61c4878aSAndroid Build Coastguard Worker // Interrupt context
100*61c4878aSAndroid Build Coastguard Worker std::lock_guard lock(native_type_.shared_spin_lock);
101*61c4878aSAndroid Build Coastguard Worker if (native_type_.blocked_thread != nullptr) {
102*61c4878aSAndroid Build Coastguard Worker BaseType_t woke_higher_task = pdFALSE;
103*61c4878aSAndroid Build Coastguard Worker
104*61c4878aSAndroid Build Coastguard Worker #ifdef configTASK_NOTIFICATION_ARRAY_ENTRIES
105*61c4878aSAndroid Build Coastguard Worker xTaskNotifyIndexedFromISR(
106*61c4878aSAndroid Build Coastguard Worker native_type_.blocked_thread,
107*61c4878aSAndroid Build Coastguard Worker pw::sync::freertos::config::kThreadNotificationIndex,
108*61c4878aSAndroid Build Coastguard Worker 0u,
109*61c4878aSAndroid Build Coastguard Worker eNoAction,
110*61c4878aSAndroid Build Coastguard Worker &woke_higher_task);
111*61c4878aSAndroid Build Coastguard Worker #else // !configTASK_NOTIFICATION_ARRAY_ENTRIES
112*61c4878aSAndroid Build Coastguard Worker xTaskNotifyFromISR(
113*61c4878aSAndroid Build Coastguard Worker native_type_.blocked_thread, 0u, eNoAction, &woke_higher_task);
114*61c4878aSAndroid Build Coastguard Worker #endif // configTASK_NOTIFICATION_ARRAY_ENTRIES
115*61c4878aSAndroid Build Coastguard Worker
116*61c4878aSAndroid Build Coastguard Worker native_type_.blocked_thread = nullptr;
117*61c4878aSAndroid Build Coastguard Worker portYIELD_FROM_ISR(woke_higher_task);
118*61c4878aSAndroid Build Coastguard Worker }
119*61c4878aSAndroid Build Coastguard Worker native_type_.notified = true;
120*61c4878aSAndroid Build Coastguard Worker }
121*61c4878aSAndroid Build Coastguard Worker
122*61c4878aSAndroid Build Coastguard Worker } // namespace pw::sync
123