mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-23 13:03:13 +00:00
20c4121f25
* Use static mutex * Add static_assert checks * Use static semaphore * Fix formatting * Use static stream buffer * Use static timer * Use static event group * Increase allocation size for stream buffer * Remove recursive bit from the mutex before freeing * Prevent service tasks from ever returning * Use static threads * Do not realloc memory when changing stack size * Use FuriSemaphore instead of raw FreeRTOS one in rpc_test * Remove redundant includes * Abolish FreeRTOS dynamic allocation * Improve FuriMutex * Improve FuriMessageQueue * Remove redundant comments and parentheses * Clean up code more * Create service threads via a dedicated constructor * Minor code improvements * Update docs for FuriThread, FuriTimer * Fix doxygen typo * Use a bigger buffer for static StreamBuffer * Furi: remove timer control block only when timer thread have completed all operations --------- Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
122 lines
3 KiB
C
122 lines
3 KiB
C
#include "semaphore.h"
|
|
#include "check.h"
|
|
#include "common_defines.h"
|
|
|
|
#include <FreeRTOS.h>
|
|
#include <semphr.h>
|
|
|
|
struct FuriSemaphore {
|
|
StaticSemaphore_t container;
|
|
};
|
|
|
|
// IMPORTANT: container MUST be the FIRST struct member
|
|
static_assert(offsetof(FuriSemaphore, container) == 0);
|
|
|
|
FuriSemaphore* furi_semaphore_alloc(uint32_t max_count, uint32_t initial_count) {
|
|
furi_check(!FURI_IS_IRQ_MODE());
|
|
furi_check((max_count > 0U) && (initial_count <= max_count));
|
|
|
|
FuriSemaphore* instance = malloc(sizeof(FuriSemaphore));
|
|
|
|
SemaphoreHandle_t hSemaphore;
|
|
|
|
if(max_count == 1U) {
|
|
hSemaphore = xSemaphoreCreateBinaryStatic(&instance->container);
|
|
} else {
|
|
hSemaphore =
|
|
xSemaphoreCreateCountingStatic(max_count, initial_count, &instance->container);
|
|
}
|
|
|
|
furi_check(hSemaphore == (SemaphoreHandle_t)instance);
|
|
|
|
if(max_count == 1U && initial_count != 0U) {
|
|
furi_check(xSemaphoreGive(hSemaphore) == pdPASS);
|
|
}
|
|
|
|
return instance;
|
|
}
|
|
|
|
void furi_semaphore_free(FuriSemaphore* instance) {
|
|
furi_check(instance);
|
|
furi_check(!FURI_IS_IRQ_MODE());
|
|
|
|
vSemaphoreDelete((SemaphoreHandle_t)instance);
|
|
free(instance);
|
|
}
|
|
|
|
FuriStatus furi_semaphore_acquire(FuriSemaphore* instance, uint32_t timeout) {
|
|
furi_check(instance);
|
|
|
|
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
|
FuriStatus stat;
|
|
BaseType_t yield;
|
|
|
|
stat = FuriStatusOk;
|
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
|
if(timeout != 0U) {
|
|
stat = FuriStatusErrorParameter;
|
|
} else {
|
|
yield = pdFALSE;
|
|
|
|
if(xSemaphoreTakeFromISR(hSemaphore, &yield) != pdPASS) {
|
|
stat = FuriStatusErrorResource;
|
|
} else {
|
|
portYIELD_FROM_ISR(yield);
|
|
}
|
|
}
|
|
|
|
} else {
|
|
if(xSemaphoreTake(hSemaphore, (TickType_t)timeout) != pdPASS) {
|
|
if(timeout != 0U) {
|
|
stat = FuriStatusErrorTimeout;
|
|
} else {
|
|
stat = FuriStatusErrorResource;
|
|
}
|
|
}
|
|
}
|
|
|
|
return stat;
|
|
}
|
|
|
|
FuriStatus furi_semaphore_release(FuriSemaphore* instance) {
|
|
furi_check(instance);
|
|
|
|
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
|
FuriStatus stat;
|
|
BaseType_t yield;
|
|
|
|
stat = FuriStatusOk;
|
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
|
yield = pdFALSE;
|
|
|
|
if(xSemaphoreGiveFromISR(hSemaphore, &yield) != pdTRUE) {
|
|
stat = FuriStatusErrorResource;
|
|
} else {
|
|
portYIELD_FROM_ISR(yield);
|
|
}
|
|
|
|
} else {
|
|
if(xSemaphoreGive(hSemaphore) != pdPASS) {
|
|
stat = FuriStatusErrorResource;
|
|
}
|
|
}
|
|
|
|
return stat;
|
|
}
|
|
|
|
uint32_t furi_semaphore_get_count(FuriSemaphore* instance) {
|
|
furi_check(instance);
|
|
|
|
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
|
uint32_t count;
|
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
|
count = (uint32_t)uxSemaphoreGetCountFromISR(hSemaphore);
|
|
} else {
|
|
count = (uint32_t)uxSemaphoreGetCount(hSemaphore);
|
|
}
|
|
|
|
return count;
|
|
}
|