2022-07-20 10:56:33 +00:00
|
|
|
#include "semaphore.h"
|
|
|
|
#include "check.h"
|
|
|
|
#include "common_defines.h"
|
|
|
|
|
2023-11-01 07:24:11 +00:00
|
|
|
#include <FreeRTOS.h>
|
2022-07-20 10:56:33 +00:00
|
|
|
#include <semphr.h>
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
struct FuriSemaphore {
|
|
|
|
StaticSemaphore_t container;
|
|
|
|
};
|
|
|
|
|
|
|
|
// IMPORTANT: container MUST be the FIRST struct member
|
|
|
|
static_assert(offsetof(FuriSemaphore, container) == 0);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriSemaphore* furi_semaphore_alloc(uint32_t max_count, uint32_t initial_count) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!FURI_IS_IRQ_MODE());
|
|
|
|
furi_check((max_count > 0U) && (initial_count <= max_count));
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
FuriSemaphore* instance = malloc(sizeof(FuriSemaphore));
|
|
|
|
|
|
|
|
SemaphoreHandle_t hSemaphore;
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
if(max_count == 1U) {
|
2024-06-05 17:04:03 +00:00
|
|
|
hSemaphore = xSemaphoreCreateBinaryStatic(&instance->container);
|
2022-07-20 10:56:33 +00:00
|
|
|
} else {
|
2024-06-05 17:04:03 +00:00
|
|
|
hSemaphore =
|
|
|
|
xSemaphoreCreateCountingStatic(max_count, initial_count, &instance->container);
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
furi_check(hSemaphore == (SemaphoreHandle_t)instance);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
if(max_count == 1U && initial_count != 0U) {
|
|
|
|
furi_check(xSemaphoreGive(hSemaphore) == pdPASS);
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_semaphore_free(FuriSemaphore* instance) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(instance);
|
|
|
|
furi_check(!FURI_IS_IRQ_MODE());
|
2022-07-20 10:56:33 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
vSemaphoreDelete((SemaphoreHandle_t)instance);
|
|
|
|
free(instance);
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FuriStatus furi_semaphore_acquire(FuriSemaphore* instance, uint32_t timeout) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(instance);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
|
|
|
FuriStatus stat;
|
|
|
|
BaseType_t yield;
|
|
|
|
|
|
|
|
stat = FuriStatusOk;
|
|
|
|
|
2022-11-05 15:07:24 +00:00
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
2022-07-20 10:56:33 +00:00
|
|
|
if(timeout != 0U) {
|
|
|
|
stat = FuriStatusErrorParameter;
|
|
|
|
} else {
|
|
|
|
yield = pdFALSE;
|
|
|
|
|
|
|
|
if(xSemaphoreTakeFromISR(hSemaphore, &yield) != pdPASS) {
|
|
|
|
stat = FuriStatusErrorResource;
|
|
|
|
} else {
|
|
|
|
portYIELD_FROM_ISR(yield);
|
|
|
|
}
|
|
|
|
}
|
2024-06-05 17:04:03 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
} else {
|
|
|
|
if(xSemaphoreTake(hSemaphore, (TickType_t)timeout) != pdPASS) {
|
|
|
|
if(timeout != 0U) {
|
|
|
|
stat = FuriStatusErrorTimeout;
|
|
|
|
} else {
|
|
|
|
stat = FuriStatusErrorResource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
return stat;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FuriStatus furi_semaphore_release(FuriSemaphore* instance) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(instance);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
|
|
|
FuriStatus stat;
|
|
|
|
BaseType_t yield;
|
|
|
|
|
|
|
|
stat = FuriStatusOk;
|
|
|
|
|
2022-11-05 15:07:24 +00:00
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
2022-07-20 10:56:33 +00:00
|
|
|
yield = pdFALSE;
|
|
|
|
|
|
|
|
if(xSemaphoreGiveFromISR(hSemaphore, &yield) != pdTRUE) {
|
|
|
|
stat = FuriStatusErrorResource;
|
|
|
|
} else {
|
|
|
|
portYIELD_FROM_ISR(yield);
|
|
|
|
}
|
2024-06-05 17:04:03 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
} else {
|
|
|
|
if(xSemaphoreGive(hSemaphore) != pdPASS) {
|
|
|
|
stat = FuriStatusErrorResource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
return stat;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t furi_semaphore_get_count(FuriSemaphore* instance) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(instance);
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
|
|
|
uint32_t count;
|
|
|
|
|
2022-11-05 15:07:24 +00:00
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
2022-07-20 10:56:33 +00:00
|
|
|
count = (uint32_t)uxSemaphoreGetCountFromISR(hSemaphore);
|
|
|
|
} else {
|
|
|
|
count = (uint32_t)uxSemaphoreGetCount(hSemaphore);
|
|
|
|
}
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
return count;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|