mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-23 13:03:13 +00:00
acc39a4bc0
* Api Symbols: replace asserts with checks * Api Symbols: replace asserts with checks part 2 * Update no args function signatures with void, to help compiler to track incorrect usage * More unavoidable void * Update PVS config and code to make it happy * Format sources * nfc: fix checks * dead code cleanup & include fixes Co-authored-by: gornekich <n.gorbadey@gmail.com> Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: hedger <hedger@nanode.su>
116 lines
2.9 KiB
C
116 lines
2.9 KiB
C
#include "semaphore.h"
|
|
#include "check.h"
|
|
#include "common_defines.h"
|
|
|
|
#include <FreeRTOS.h>
|
|
#include <semphr.h>
|
|
|
|
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));
|
|
|
|
SemaphoreHandle_t hSemaphore = NULL;
|
|
if(max_count == 1U) {
|
|
hSemaphore = xSemaphoreCreateBinary();
|
|
if((hSemaphore != NULL) && (initial_count != 0U)) {
|
|
if(xSemaphoreGive(hSemaphore) != pdPASS) {
|
|
vSemaphoreDelete(hSemaphore);
|
|
hSemaphore = NULL;
|
|
}
|
|
}
|
|
} else {
|
|
hSemaphore = xSemaphoreCreateCounting(max_count, initial_count);
|
|
}
|
|
|
|
furi_check(hSemaphore);
|
|
|
|
/* Return semaphore ID */
|
|
return ((FuriSemaphore*)hSemaphore);
|
|
}
|
|
|
|
void furi_semaphore_free(FuriSemaphore* instance) {
|
|
furi_check(instance);
|
|
furi_check(!FURI_IS_IRQ_MODE());
|
|
|
|
SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)instance;
|
|
|
|
vSemaphoreDelete(hSemaphore);
|
|
}
|
|
|
|
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 execution status */
|
|
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 execution status */
|
|
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 number of tokens */
|
|
return (count);
|
|
}
|