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/assert.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 == reinterpret_cast<SemaphoreHandle_t>(&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 = xSemaphoreGiveFromISR(
40 reinterpret_cast<SemaphoreHandle_t>(&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 =
45 xSemaphoreGive(reinterpret_cast<SemaphoreHandle_t>(&native_type_));
46 }
47 }
48
acquire()49 inline void BinarySemaphore::acquire() {
50 // Enforce the pw::sync::BinarySemaphore IRQ contract.
51 PW_DASSERT(!interrupt::InInterruptContext());
52 #if INCLUDE_vTaskSuspend == 1 // This means portMAX_DELAY is indefinite.
53 const BaseType_t result = xSemaphoreTake(
54 reinterpret_cast<SemaphoreHandle_t>(&native_type_), portMAX_DELAY);
55 PW_DASSERT(result == pdTRUE);
56 #else
57 // In case we need to block for longer than the FreeRTOS delay can represent
58 // repeatedly hit take until success.
59 while (xSemaphoreTake(reinterpret_cast<SemaphoreHandle_t>(&native_type_),
60 chrono::freertos::kMaxTimeout.count()) == pdFALSE) {
61 }
62 #endif // INCLUDE_vTaskSuspend
63 }
64
try_acquire()65 inline bool BinarySemaphore::try_acquire() noexcept {
66 if (interrupt::InInterruptContext()) {
67 BaseType_t woke_higher_task = pdFALSE;
68 const bool success = xSemaphoreTakeFromISR(
69 reinterpret_cast<SemaphoreHandle_t>(&native_type_),
70 &woke_higher_task) == pdTRUE;
71 portYIELD_FROM_ISR(woke_higher_task);
72 return success;
73 }
74
75 // Task Context
76 return xSemaphoreTake(reinterpret_cast<SemaphoreHandle_t>(&native_type_),
77 0) == pdTRUE;
78 }
79
try_acquire_until(chrono::SystemClock::time_point deadline)80 inline bool BinarySemaphore::try_acquire_until(
81 chrono::SystemClock::time_point deadline) {
82 // Note that if this deadline is in the future, it will get rounded up by
83 // one whole tick due to how try_acquire_for is implemented.
84 return try_acquire_for(deadline - chrono::SystemClock::now());
85 }
86
native_handle()87 inline BinarySemaphore::native_handle_type BinarySemaphore::native_handle() {
88 return native_type_;
89 }
90
91 } // namespace pw::sync
92