[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>
This commit is contained in:
Georgii Surkov
2024-06-05 20:04:03 +03:00
committed by GitHub
parent 03196fa110
commit 20c4121f25
28 changed files with 584 additions and 427 deletions

View File

@@ -5,129 +5,120 @@
#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());
SemaphoreHandle_t hMutex = NULL;
FuriMutex* instance = malloc(sizeof(FuriMutex));
SemaphoreHandle_t hMutex;
if(type == FuriMutexTypeNormal) {
hMutex = xSemaphoreCreateMutex();
hMutex = xSemaphoreCreateMutexStatic(&instance->container);
} else if(type == FuriMutexTypeRecursive) {
hMutex = xSemaphoreCreateRecursiveMutex();
hMutex = xSemaphoreCreateRecursiveMutexStatic(&instance->container);
} else {
furi_crash();
}
furi_check(hMutex != NULL);
furi_check(hMutex == (SemaphoreHandle_t)instance);
if(type == FuriMutexTypeRecursive) {
/* Set LSB as 'recursive mutex flag' */
hMutex = (SemaphoreHandle_t)((uint32_t)hMutex | 1U);
}
/* Return mutex ID */
return ((FuriMutex*)hMutex);
return instance;
}
void furi_mutex_free(FuriMutex* instance) {
furi_check(!FURI_IS_IRQ_MODE());
furi_check(instance);
vSemaphoreDelete((SemaphoreHandle_t)((uint32_t)instance & ~1U));
vSemaphoreDelete((SemaphoreHandle_t)instance);
free(instance);
}
FuriStatus furi_mutex_acquire(FuriMutex* instance, uint32_t timeout) {
furi_check(instance);
SemaphoreHandle_t hMutex;
FuriStatus stat;
uint32_t rmtx;
SemaphoreHandle_t hMutex = (SemaphoreHandle_t)(instance);
const uint8_t mutex_type = instance->container.ucQueueType;
hMutex = (SemaphoreHandle_t)((uint32_t)instance & ~1U);
/* Extract recursive mutex flag */
rmtx = (uint32_t)instance & 1U;
stat = FuriStatusOk;
FuriStatus stat = FuriStatusOk;
if(FURI_IS_IRQ_MODE()) {
stat = FuriStatusErrorISR;
} else if(hMutex == NULL) {
stat = FuriStatusErrorParameter;
} else {
if(rmtx != 0U) {
if(xSemaphoreTakeRecursive(hMutex, timeout) != pdPASS) {
if(timeout != 0U) {
stat = FuriStatusErrorTimeout;
} else {
stat = FuriStatusErrorResource;
}
}
} else {
if(xSemaphoreTake(hMutex, timeout) != pdPASS) {
if(timeout != 0U) {
stat = FuriStatusErrorTimeout;
} else {
stat = FuriStatusErrorResource;
}
} 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 execution status */
return (stat);
return stat;
}
FuriStatus furi_mutex_release(FuriMutex* instance) {
furi_check(instance);
SemaphoreHandle_t hMutex;
FuriStatus stat;
uint32_t rmtx;
SemaphoreHandle_t hMutex = (SemaphoreHandle_t)(instance);
const uint8_t mutex_type = instance->container.ucQueueType;
hMutex = (SemaphoreHandle_t)((uint32_t)instance & ~1U);
/* Extract recursive mutex flag */
rmtx = (uint32_t)instance & 1U;
stat = FuriStatusOk;
FuriStatus stat = FuriStatusOk;
if(FURI_IS_IRQ_MODE()) {
stat = FuriStatusErrorISR;
} else if(hMutex == NULL) {
stat = FuriStatusErrorParameter;
} else {
if(rmtx != 0U) {
if(xSemaphoreGiveRecursive(hMutex) != pdPASS) {
stat = FuriStatusErrorResource;
}
} else {
if(xSemaphoreGive(hMutex) != pdPASS) {
stat = FuriStatusErrorResource;
}
} 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 execution status */
return (stat);
return stat;
}
FuriThreadId furi_mutex_get_owner(FuriMutex* instance) {
furi_check(instance);
SemaphoreHandle_t hMutex;
SemaphoreHandle_t hMutex = (SemaphoreHandle_t)instance;
FuriThreadId owner;
hMutex = (SemaphoreHandle_t)((uint32_t)instance & ~1U);
if(hMutex == NULL) {
owner = 0;
} else if(FURI_IS_IRQ_MODE()) {
if(FURI_IS_IRQ_MODE()) {
owner = (FuriThreadId)xSemaphoreGetMutexHolderFromISR(hMutex);
} else {
owner = (FuriThreadId)xSemaphoreGetMutexHolder(hMutex);
}
/* Return owner thread ID */
return (owner);
return owner;
}