1 // Copyright 2020 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 "FreeRTOS.h"
17 #include "pw_assert/light.h"
18 #include "pw_chrono/system_clock.h"
19 #include "pw_chrono_freertos/system_clock_constants.h"
20 #include "pw_interrupt/context.h"
21 #include "pw_sync/binary_semaphore.h"
22 #include "semphr.h"
23 
24 namespace pw::sync {
25 
BinarySemaphore()26 inline BinarySemaphore::BinarySemaphore() : native_type_() {
27   const SemaphoreHandle_t handle = xSemaphoreCreateBinaryStatic(&native_type_);
28   // This should never fail since the pointer provided was not null and it
29   // should return a pointer to the StaticSemaphore_t.
30   PW_DASSERT(handle == &native_type_);
31 }
32 
~BinarySemaphore()33 inline BinarySemaphore::~BinarySemaphore() { vSemaphoreDelete(&native_type_); }
34 
release()35 inline void BinarySemaphore::release() {
36   if (interrupt::InInterruptContext()) {
37     BaseType_t woke_higher_task = pdFALSE;
38     // It's perfectly fine if the semaphore already has a count of 1.
39     [[maybe_unused]] BaseType_t already_full =
40         xSemaphoreGiveFromISR(&native_type_, &woke_higher_task);
41     portYIELD_FROM_ISR(woke_higher_task);
42   } else {  // Task context
43     // It's perfectly fine if the semaphore already has a count of 1.
44     [[maybe_unused]] BaseType_t already_full = xSemaphoreGive(&native_type_);
45   }
46 }
47 
acquire()48 inline void BinarySemaphore::acquire() {
49   PW_ASSERT(!interrupt::InInterruptContext());
50 #if INCLUDE_vTaskSuspend == 1  // This means portMAX_DELAY is indefinite.
51   const BaseType_t result = xSemaphoreTake(&native_type_, portMAX_DELAY);
52   PW_DASSERT(result == pdTRUE);
53 #else
54   // In case we need to block for longer than the FreeRTOS delay can represent
55   // repeatedly hit take until success.
56   while (xSemaphoreTake(&native_type_, chrono::freertos::kMaxTimeout.count()) ==
57          pdFALSE) {
58   }
59 #endif  // INCLUDE_vTaskSuspend
60 }
61 
try_acquire()62 inline bool BinarySemaphore::try_acquire() noexcept {
63   if (interrupt::InInterruptContext()) {
64     BaseType_t woke_higher_task = pdFALSE;
65     const bool success =
66         xSemaphoreTakeFromISR(&native_type_, &woke_higher_task) == pdTRUE;
67     portYIELD_FROM_ISR(woke_higher_task);
68     return success;
69   }
70 
71   // Task Context
72   return xSemaphoreTake(&native_type_, 0) == pdTRUE;
73 }
74 
try_acquire_until(chrono::SystemClock::time_point until_at_least)75 inline bool BinarySemaphore::try_acquire_until(
76     chrono::SystemClock::time_point until_at_least) {
77   // Note that if this deadline is in the future, it will get rounded up by
78   // one whole tick due to how try_acquire_for is implemented.
79   return try_acquire_for(until_at_least - chrono::SystemClock::now());
80 }
81 
native_handle()82 inline BinarySemaphore::native_handle_type BinarySemaphore::native_handle() {
83   return native_type_;
84 }
85 
86 }  // namespace pw::sync
87