2022-07-20 13:56:33 +03:00
|
|
|
#include "semaphore.h"
|
|
|
|
|
|
2023-11-01 16:24:11 +09:00
|
|
|
#include <FreeRTOS.h>
|
2022-07-20 13:56:33 +03:00
|
|
|
#include <semphr.h>
|
|
|
|
|
|
2024-08-07 04:49:41 +01:00
|
|
|
#include "check.h"
|
|
|
|
|
#include "kernel.h"
|
|
|
|
|
|
|
|
|
|
#include "event_loop_link_i.h"
|
|
|
|
|
|
|
|
|
|
// Internal FreeRTOS member names
|
|
|
|
|
#define uxMessagesWaiting uxDummy4[0]
|
|
|
|
|
#define uxLength uxDummy4[1]
|
|
|
|
|
|
2024-06-05 20:04:03 +03:00
|
|
|
struct FuriSemaphore {
|
|
|
|
|
StaticSemaphore_t container;
|
2024-08-07 04:49:41 +01:00
|
|
|
FuriEventLoopLink event_loop_link;
|
2024-06-05 20:04:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// IMPORTANT: container MUST be the FIRST struct member
|
|
|
|
|
static_assert(offsetof(FuriSemaphore, container) == 0);
|
|
|
|
|
|
2022-07-20 13:56:33 +03:00
|
|
|
FuriSemaphore* furi_semaphore_alloc(uint32_t max_count, uint32_t initial_count) {
|
2024-03-19 23:43:52 +09:00
|
|
|
furi_check(!FURI_IS_IRQ_MODE());
|
|
|
|
|
furi_check((max_count > 0U) && (initial_count <= max_count));
|
2022-07-20 13:56:33 +03:00
|
|
|
|
2024-06-05 20:04:03 +03:00
|
|
|
FuriSemaphore* instance = malloc(sizeof(FuriSemaphore));
|
|
|
|
|
|
|
|
|
|
SemaphoreHandle_t hSemaphore;
|
|
|
|
|
|
2022-07-20 13:56:33 +03:00
|
|
|
if(max_count == 1U) {
|
2024-06-05 20:04:03 +03:00
|
|
|
hSemaphore = xSemaphoreCreateBinaryStatic(&instance->container);
|
2022-07-20 13:56:33 +03:00
|
|
|
} else {
|
2024-06-05 20:04:03 +03:00
|
|
|
hSemaphore =
|
|
|
|
|
xSemaphoreCreateCountingStatic(max_count, initial_count, &instance->container);
|
2022-07-20 13:56:33 +03:00
|
|
|
}
|
|
|
|
|
|
2024-06-05 20:04:03 +03:00
|
|
|
furi_check(hSemaphore == (SemaphoreHandle_t)instance);
|
2022-07-20 13:56:33 +03:00
|
|
|
|
2024-06-05 20:04:03 +03:00
|
|
|
if(max_count == 1U && initial_count != 0U) {
|
|
|
|
|
furi_check(xSemaphoreGive(hSemaphore) == pdPASS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return instance;
|
2022-07-20 13:56:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void furi_semaphore_free(FuriSemaphore* instance) {
|
2024-03-19 23:43:52 +09:00
|
|
|
furi_check(instance);
|
|
|
|
|
furi_check(!FURI_IS_IRQ_MODE());
|
2022-07-20 13:56:33 +03:00
|
|
|
|
2024-08-07 04:49:41 +01:00
|
|
|
// Event Loop must be disconnected
|
|
|
|
|
furi_check(!instance->event_loop_link.item_in);
|
|
|
|
|
furi_check(!instance->event_loop_link.item_out);
|
|
|
|
|
|
2024-06-05 20:04:03 +03:00
|
|
|
vSemaphoreDelete((SemaphoreHandle_t)instance);
|
|
|
|
|
free(instance);
|
2022-07-20 13:56:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FuriStatus furi_semaphore_acquire(FuriSemaphore* instance, uint32_t timeout) {
|
2024-03-19 23:43:52 +09:00
|
|
|
furi_check(instance);
|
2022-07-20 13:56:33 +03:00
|
|
|
|
|
|
|
|
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
|
|
|
|
FuriStatus stat;
|
|
|
|
|
BaseType_t yield;
|
|
|
|
|
|
|
|
|
|
stat = FuriStatusOk;
|
|
|
|
|
|
2022-11-06 00:07:24 +09:00
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
2022-07-20 13:56:33 +03:00
|
|
|
if(timeout != 0U) {
|
|
|
|
|
stat = FuriStatusErrorParameter;
|
|
|
|
|
} else {
|
|
|
|
|
yield = pdFALSE;
|
|
|
|
|
|
|
|
|
|
if(xSemaphoreTakeFromISR(hSemaphore, &yield) != pdPASS) {
|
|
|
|
|
stat = FuriStatusErrorResource;
|
|
|
|
|
} else {
|
|
|
|
|
portYIELD_FROM_ISR(yield);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-05 20:04:03 +03:00
|
|
|
|
2022-07-20 13:56:33 +03:00
|
|
|
} else {
|
|
|
|
|
if(xSemaphoreTake(hSemaphore, (TickType_t)timeout) != pdPASS) {
|
|
|
|
|
if(timeout != 0U) {
|
|
|
|
|
stat = FuriStatusErrorTimeout;
|
|
|
|
|
} else {
|
|
|
|
|
stat = FuriStatusErrorResource;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 04:49:41 +01:00
|
|
|
if(stat == FuriStatusOk) {
|
|
|
|
|
furi_event_loop_link_notify(&instance->event_loop_link, FuriEventLoopEventOut);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 20:04:03 +03:00
|
|
|
return stat;
|
2022-07-20 13:56:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FuriStatus furi_semaphore_release(FuriSemaphore* instance) {
|
2024-03-19 23:43:52 +09:00
|
|
|
furi_check(instance);
|
2022-07-20 13:56:33 +03:00
|
|
|
|
|
|
|
|
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
|
|
|
|
FuriStatus stat;
|
|
|
|
|
BaseType_t yield;
|
|
|
|
|
|
|
|
|
|
stat = FuriStatusOk;
|
|
|
|
|
|
2022-11-06 00:07:24 +09:00
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
2022-07-20 13:56:33 +03:00
|
|
|
yield = pdFALSE;
|
|
|
|
|
|
|
|
|
|
if(xSemaphoreGiveFromISR(hSemaphore, &yield) != pdTRUE) {
|
|
|
|
|
stat = FuriStatusErrorResource;
|
|
|
|
|
} else {
|
|
|
|
|
portYIELD_FROM_ISR(yield);
|
|
|
|
|
}
|
2024-06-05 20:04:03 +03:00
|
|
|
|
2022-07-20 13:56:33 +03:00
|
|
|
} else {
|
|
|
|
|
if(xSemaphoreGive(hSemaphore) != pdPASS) {
|
|
|
|
|
stat = FuriStatusErrorResource;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 04:49:41 +01:00
|
|
|
if(stat == FuriStatusOk) {
|
|
|
|
|
furi_event_loop_link_notify(&instance->event_loop_link, FuriEventLoopEventIn);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 20:04:03 +03:00
|
|
|
return stat;
|
2022-07-20 13:56:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t furi_semaphore_get_count(FuriSemaphore* instance) {
|
2024-03-19 23:43:52 +09:00
|
|
|
furi_check(instance);
|
2022-07-20 13:56:33 +03:00
|
|
|
|
|
|
|
|
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
|
|
|
|
uint32_t count;
|
|
|
|
|
|
2022-11-06 00:07:24 +09:00
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
2022-07-20 13:56:33 +03:00
|
|
|
count = (uint32_t)uxSemaphoreGetCountFromISR(hSemaphore);
|
|
|
|
|
} else {
|
|
|
|
|
count = (uint32_t)uxSemaphoreGetCount(hSemaphore);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 20:04:03 +03:00
|
|
|
return count;
|
2022-07-20 13:56:33 +03:00
|
|
|
}
|
2024-08-07 04:49:41 +01:00
|
|
|
|
|
|
|
|
uint32_t furi_semaphore_get_space(FuriSemaphore* instance) {
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
|
|
uint32_t space;
|
|
|
|
|
|
|
|
|
|
if(furi_kernel_is_irq_or_masked() != 0U) {
|
|
|
|
|
uint32_t isrm = taskENTER_CRITICAL_FROM_ISR();
|
|
|
|
|
|
|
|
|
|
space = instance->container.uxLength - instance->container.uxMessagesWaiting;
|
|
|
|
|
|
|
|
|
|
taskEXIT_CRITICAL_FROM_ISR(isrm);
|
|
|
|
|
} else {
|
|
|
|
|
space = uxQueueSpacesAvailable((QueueHandle_t)instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return space;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static FuriEventLoopLink* furi_semaphore_event_loop_get_link(FuriEventLoopObject* object) {
|
|
|
|
|
FuriSemaphore* instance = object;
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
return &instance->event_loop_link;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-31 05:58:16 +04:00
|
|
|
static bool
|
2024-08-07 04:49:41 +01:00
|
|
|
furi_semaphore_event_loop_get_level(FuriEventLoopObject* object, FuriEventLoopEvent event) {
|
|
|
|
|
FuriSemaphore* instance = object;
|
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
|
|
if(event == FuriEventLoopEventIn) {
|
|
|
|
|
return furi_semaphore_get_count(instance);
|
|
|
|
|
} else if(event == FuriEventLoopEventOut) {
|
|
|
|
|
return furi_semaphore_get_space(instance);
|
|
|
|
|
} else {
|
|
|
|
|
furi_crash();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FuriEventLoopContract furi_semaphore_event_loop_contract = {
|
|
|
|
|
.get_link = furi_semaphore_event_loop_get_link,
|
|
|
|
|
.get_level = furi_semaphore_event_loop_get_level,
|
|
|
|
|
};
|