Skip to content
Snippets Groups Projects
Verified Commit 75278836 authored by rahix's avatar rahix
Browse files

chore(api-lock): Port to new mutex API


Using a bare FreeRTOS mutex is deprecated.  Replace it with the new
`struct mutex`.  This should increase stability.

Additionally, fix a bug in `lifecycle.c:do_load()` where the function
would return without unlocking the API mutex if `hardware_reset()`
returns an error.

Signed-off-by: default avatarRahix <rahix@rahix.de>
parent 44ea81f2
No related branches found
No related tags found
No related merge requests found
#include "modules/log.h" #include "modules/log.h"
#include "modules/mutex.h"
#include "api/dispatcher.h" #include "api/dispatcher.h"
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "semphr.h"
#define TIMEOUT pdMS_TO_TICKS(2000) #define TIMEOUT pdMS_TO_TICKS(2000)
TaskHandle_t dispatcher_task_id; TaskHandle_t dispatcher_task_id;
static StaticSemaphore_t api_mutex_data; struct mutex api_mutex = { 0 };
SemaphoreHandle_t api_mutex = NULL;
void dispatcher_mutex_init(void) void dispatcher_mutex_init(void)
{ {
api_mutex = xSemaphoreCreateMutexStatic(&api_mutex_data); mutex_create(&api_mutex);
} }
/* /*
...@@ -27,12 +26,9 @@ void vApiDispatcher(void *pvParameters) ...@@ -27,12 +26,9 @@ void vApiDispatcher(void *pvParameters)
LOG_DEBUG("dispatcher", "Ready."); LOG_DEBUG("dispatcher", "Ready.");
while (1) { while (1) {
if (api_dispatcher_poll()) { if (api_dispatcher_poll()) {
if (xSemaphoreTake(api_mutex, TIMEOUT) != pdTRUE) { mutex_lock(&api_mutex);
LOG_ERR("dispatcher", "API mutex blocked");
continue;
}
api_dispatcher_exec(); api_dispatcher_exec();
xSemaphoreGive(api_mutex); mutex_unlock(&api_mutex);
} }
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
} }
......
...@@ -98,10 +98,7 @@ static int do_load(struct load_info *info) ...@@ -98,10 +98,7 @@ static int do_load(struct load_info *info)
LOG_INFO("lifecycle", "Loading \"%s\" ...", info->name); LOG_INFO("lifecycle", "Loading \"%s\" ...", info->name);
} }
if (xSemaphoreTake(api_mutex, BLOCK_WAIT) != pdTRUE) { mutex_lock(&api_mutex);
LOG_ERR("lifecycle", "API blocked");
return -EBUSY;
}
if (info->do_reset) { if (info->do_reset) {
LOG_DEBUG("lifecycle", "Triggering core 1 reset."); LOG_DEBUG("lifecycle", "Triggering core 1 reset.");
...@@ -119,7 +116,7 @@ static int do_load(struct load_info *info) ...@@ -119,7 +116,7 @@ static int do_load(struct load_info *info)
*/ */
res = hardware_reset(); res = hardware_reset();
if (res < 0) { if (res < 0) {
return res; goto out_free_api;
} }
switch (info->type) { switch (info->type) {
...@@ -133,8 +130,8 @@ static int do_load(struct load_info *info) ...@@ -133,8 +130,8 @@ static int do_load(struct load_info *info)
res = l0der_load_path(info->name, &l0dable); res = l0der_load_path(info->name, &l0dable);
if (res != 0) { if (res != 0) {
LOG_ERR("lifecycle", "l0der failed: %d\n", res); LOG_ERR("lifecycle", "l0der failed: %d\n", res);
xSemaphoreGive(api_mutex); res = -ENOEXEC;
return -ENOEXEC; goto out_free_api;
} }
core1_load(l0dable.isr_vector, ""); core1_load(l0dable.isr_vector, "");
} else { } else {
...@@ -148,12 +145,14 @@ static int do_load(struct load_info *info) ...@@ -148,12 +145,14 @@ static int do_load(struct load_info *info)
LOG_ERR("lifecyle", LOG_ERR("lifecyle",
"Attempted to load invalid payload (%s)", "Attempted to load invalid payload (%s)",
info->name); info->name);
xSemaphoreGive(api_mutex); res = -EINVAL;
return -EINVAL; goto out_free_api;
} }
xSemaphoreGive(api_mutex); res = 0;
return 0; out_free_api:
mutex_unlock(&api_mutex);
return res;
} }
/* /*
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
#define MODULES_H #define MODULES_H
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "semphr.h"
#include "gpio.h" #include "gpio.h"
#include "modules/mutex.h"
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -15,7 +15,7 @@ void panic(const char *format, ...) ...@@ -15,7 +15,7 @@ void panic(const char *format, ...)
/* ---------- Dispatcher --------------------------------------------------- */ /* ---------- Dispatcher --------------------------------------------------- */
void vApiDispatcher(void *pvParameters); void vApiDispatcher(void *pvParameters);
void dispatcher_mutex_init(void); void dispatcher_mutex_init(void);
extern SemaphoreHandle_t api_mutex; extern struct mutex api_mutex;
extern TaskHandle_t dispatcher_task_id; extern TaskHandle_t dispatcher_task_id;
/* ---------- Hardware Init & Reset ---------------------------------------- */ /* ---------- Hardware Init & Reset ---------------------------------------- */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment