unleashed-firmware/furi/core/mutex.c
Georgii Surkov 20c4121f25
[FL-3832] Use static synchronisation primitives (#3679)
* 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>
2024-06-05 18:04:03 +01:00

125 lines
3.0 KiB
C

#include "mutex.h"
#include "check.h"
#include "common_defines.h"
#include <FreeRTOS.h>
#include <semphr.h>
// Internal FreeRTOS member names
#define ucQueueType ucDummy9
struct FuriMutex {
StaticSemaphore_t container;
};
// IMPORTANT: container MUST be the FIRST struct member
static_assert(offsetof(FuriMutex, container) == 0);
FuriMutex* furi_mutex_alloc(FuriMutexType type) {
furi_check(!FURI_IS_IRQ_MODE());
FuriMutex* instance = malloc(sizeof(FuriMutex));
SemaphoreHandle_t hMutex;
if(type == FuriMutexTypeNormal) {
hMutex = xSemaphoreCreateMutexStatic(&instance->container);
} else if(type == FuriMutexTypeRecursive) {
hMutex = xSemaphoreCreateRecursiveMutexStatic(&instance->container);
} else {
furi_crash();
}
furi_check(hMutex == (SemaphoreHandle_t)instance);
return instance;
}
void furi_mutex_free(FuriMutex* instance) {
furi_check(!FURI_IS_IRQ_MODE());
furi_check(instance);
vSemaphoreDelete((SemaphoreHandle_t)instance);
free(instance);
}
FuriStatus furi_mutex_acquire(FuriMutex* instance, uint32_t timeout) {
furi_check(instance);
SemaphoreHandle_t hMutex = (SemaphoreHandle_t)(instance);
const uint8_t mutex_type = instance->container.ucQueueType;
FuriStatus stat = FuriStatusOk;
if(FURI_IS_IRQ_MODE()) {
stat = FuriStatusErrorISR;
} else if(mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
if(xSemaphoreTakeRecursive(hMutex, timeout) != pdPASS) {
if(timeout != 0U) {
stat = FuriStatusErrorTimeout;
} else {
stat = FuriStatusErrorResource;
}
}
} else if(mutex_type == queueQUEUE_TYPE_MUTEX) {
if(xSemaphoreTake(hMutex, timeout) != pdPASS) {
if(timeout != 0U) {
stat = FuriStatusErrorTimeout;
} else {
stat = FuriStatusErrorResource;
}
}
} else {
furi_crash();
}
return stat;
}
FuriStatus furi_mutex_release(FuriMutex* instance) {
furi_check(instance);
SemaphoreHandle_t hMutex = (SemaphoreHandle_t)(instance);
const uint8_t mutex_type = instance->container.ucQueueType;
FuriStatus stat = FuriStatusOk;
if(FURI_IS_IRQ_MODE()) {
stat = FuriStatusErrorISR;
} else if(mutex_type == queueQUEUE_TYPE_RECURSIVE_MUTEX) {
if(xSemaphoreGiveRecursive(hMutex) != pdPASS) {
stat = FuriStatusErrorResource;
}
} else if(mutex_type == queueQUEUE_TYPE_MUTEX) {
if(xSemaphoreGive(hMutex) != pdPASS) {
stat = FuriStatusErrorResource;
}
} else {
furi_crash();
}
return stat;
}
FuriThreadId furi_mutex_get_owner(FuriMutex* instance) {
furi_check(instance);
SemaphoreHandle_t hMutex = (SemaphoreHandle_t)instance;
FuriThreadId owner;
if(FURI_IS_IRQ_MODE()) {
owner = (FuriThreadId)xSemaphoreGetMutexHolderFromISR(hMutex);
} else {
owner = (FuriThreadId)xSemaphoreGetMutexHolder(hMutex);
}
return owner;
}