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/counting_semaphore.h"
22 #include "semphr.h"
23
24 namespace pw::sync {
25
CountingSemaphore()26 inline CountingSemaphore::CountingSemaphore() : native_type_() {
27 const SemaphoreHandle_t handle =
28 xSemaphoreCreateCountingStatic(max(), 0, &native_type_);
29 // This should never fail since the pointer provided was not null and it
30 // should return a pointer to the StaticSemaphore_t.
31 PW_DASSERT(handle == reinterpret_cast<SemaphoreHandle_t>(&native_type_));
32 }
33
~CountingSemaphore()34 inline CountingSemaphore::~CountingSemaphore() {
35 vSemaphoreDelete(reinterpret_cast<SemaphoreHandle_t>(&native_type_));
36 }
37
acquire()38 inline void CountingSemaphore::acquire() {
39 // Enforce the pw::sync::CountingSemaphore IRQ contract.
40 PW_DASSERT(!interrupt::InInterruptContext());
41 #if INCLUDE_vTaskSuspend == 1 // This means portMAX_DELAY is indefinite.
42 const BaseType_t result = xSemaphoreTake(
43 reinterpret_cast<SemaphoreHandle_t>(&native_type_), portMAX_DELAY);
44 PW_DASSERT(result == pdTRUE);
45 #else
46 // In case we need to block for longer than the FreeRTOS delay can represent
47 // repeatedly hit take until success.
48 while (xSemaphoreTake(reinterpret_cast<SemaphoreHandle_t>(&native_type_),
49 chrono::freertos::kMaxTimeout.count()) == pdFALSE) {
50 }
51 #endif // INCLUDE_vTaskSuspend
52 }
53
try_acquire()54 inline bool CountingSemaphore::try_acquire() noexcept {
55 if (interrupt::InInterruptContext()) {
56 BaseType_t woke_higher_task = pdFALSE;
57 const bool success = xSemaphoreTakeFromISR(
58 reinterpret_cast<SemaphoreHandle_t>(&native_type_),
59 &woke_higher_task) == pdTRUE;
60 portYIELD_FROM_ISR(woke_higher_task);
61 return success;
62 }
63
64 // Task Context
65 return xSemaphoreTake(reinterpret_cast<SemaphoreHandle_t>(&native_type_),
66 0) == pdTRUE;
67 }
68
try_acquire_until(chrono::SystemClock::time_point deadline)69 inline bool CountingSemaphore::try_acquire_until(
70 chrono::SystemClock::time_point deadline) {
71 // Note that if this deadline is in the future, it will get rounded up by
72 // one whole tick due to how try_acquire_for is implemented.
73 return try_acquire_for(deadline - chrono::SystemClock::now());
74 }
75
76 inline CountingSemaphore::native_handle_type
native_handle()77 CountingSemaphore::native_handle() {
78 return native_type_;
79 }
80
81 } // namespace pw::sync
82