Verified Commit 75278836 authored by Rahix's avatar Rahix

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