5#ifdef ROO_THREADS_USE_FREERTOS
9#include "freertos/FreeRTOS.h"
10#include "freertos/semphr.h"
21template <std::ptrdiff_t LeastMaxValue =
22 std::numeric_limits<UBaseType_t>::max()>
26class counting_semaphore {
29 explicit counting_semaphore(std::ptrdiff_t desired)
noexcept {
30 xSemaphoreCreateCountingStatic(LeastMaxValue, desired, &sem_);
33 counting_semaphore(
const counting_semaphore&) =
delete;
35 counting_semaphore& operator=(
const counting_semaphore&) =
delete;
38 void acquire() noexcept {
39 xSemaphoreTake((SemaphoreHandle_t)&sem_, portMAX_DELAY);
43 bool try_acquire() noexcept {
44 return xSemaphoreTake((SemaphoreHandle_t)&sem_, 0) == pdTRUE;
48 bool try_acquire_for(
const roo_time::Duration& duration)
noexcept {
49 return try_acquire_until(internal::CalculateDeadlineFromDuration(duration));
53 bool try_acquire_until(
const roo_time::Uptime& when)
noexcept {
55 roo_time::Uptime now = roo_time::Uptime::Now();
56 if (when <= now)
return false;
57 roo_time::Duration delta = when - now;
58 if (xSemaphoreTake((SemaphoreHandle_t)&sem_, internal::ToTicks(delta)) ==
66 void release() noexcept { xSemaphoreGive((SemaphoreHandle_t)&sem_); }
69 StaticSemaphore_t sem_;
80 xSemaphoreCreateBinaryStatic(&sem_);
82 xSemaphoreGive((SemaphoreHandle_t)&sem_);
91 void acquire() noexcept {
92 xSemaphoreTake((SemaphoreHandle_t)&sem_, portMAX_DELAY);
96 bool try_acquire() noexcept {
97 return xSemaphoreTake((SemaphoreHandle_t)&sem_, 0) == pdTRUE;
101 bool try_acquire_for(
const roo_time::Duration& duration)
noexcept {
102 return try_acquire_until(internal::CalculateDeadlineFromDuration(duration));
106 bool try_acquire_until(
const roo_time::Uptime& when)
noexcept {
108 roo_time::Uptime now = roo_time::Uptime::Now();
109 if (when <= now)
return false;
110 roo_time::Duration delta = when - now;
111 if (xSemaphoreTake((SemaphoreHandle_t)&sem_, internal::ToTicks(delta)) ==
119 void release() noexcept { xSemaphoreGive((SemaphoreHandle_t)&sem_); }
122 StaticSemaphore_t sem_;
counting_semaphore< 1 > binary_semaphore
Semaphore with maximum count of one.