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

chore(sensor-streams): Port to new mutex API


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

Signed-off-by: default avatarRahix <rahix@rahix.de>
parent cc5f0e29
No related branches found
No related tags found
No related merge requests found
#include <string.h>
#include "FreeRTOS.h"
#include "semphr.h"
#include "queue.h"
#include "epicardium.h"
#include "modules/log.h"
#include "modules/stream.h"
#include "modules/mutex.h"
/* Internal buffer of registered streams */
static struct stream_info *stream_table[SD_MAX];
/* Lock for modifying the stream info table */
static StaticSemaphore_t stream_table_lock_data;
static SemaphoreHandle_t stream_table_lock;
static struct mutex stream_table_lock;
int stream_init()
{
memset(stream_table, 0x00, sizeof(stream_table));
stream_table_lock =
xSemaphoreCreateMutexStatic(&stream_table_lock_data);
mutex_create(&stream_table_lock);
return 0;
}
int stream_register(int sd, struct stream_info *stream)
{
int ret = 0;
if (xSemaphoreTake(stream_table_lock, STREAM_MUTEX_WAIT) != pdTRUE) {
LOG_WARN("stream", "Lock contention error");
ret = -EBUSY;
goto out;
}
mutex_lock(&stream_table_lock);
if (sd < 0 || sd >= SD_MAX) {
ret = -EINVAL;
goto out_release;
goto out;
}
if (stream_table[sd] != NULL) {
/* Stream already registered */
ret = -EACCES;
goto out_release;
goto out;
}
stream_table[sd] = stream;
out_release:
xSemaphoreGive(stream_table_lock);
out:
mutex_unlock(&stream_table_lock);
return ret;
}
int stream_deregister(int sd, struct stream_info *stream)
{
int ret = 0;
if (xSemaphoreTake(stream_table_lock, STREAM_MUTEX_WAIT) != pdTRUE) {
LOG_WARN("stream", "Lock contention error");
ret = -EBUSY;
goto out;
}
mutex_lock(&stream_table_lock);
if (sd < 0 || sd >= SD_MAX) {
ret = -EINVAL;
goto out_release;
goto out;
}
if (stream_table[sd] != stream) {
/* Stream registered by someone else */
ret = -EACCES;
goto out_release;
goto out;
}
stream_table[sd] = NULL;
out_release:
xSemaphoreGive(stream_table_lock);
out:
mutex_unlock(&stream_table_lock);
return ret;
}
......@@ -86,35 +77,31 @@ int epic_stream_read(int sd, void *buf, size_t count)
* simulaneously. I don't know what the most efficient implementation
* of this would look like.
*/
if (xSemaphoreTake(stream_table_lock, STREAM_MUTEX_WAIT) != pdTRUE) {
LOG_WARN("stream", "Lock contention error");
ret = -EBUSY;
goto out;
}
mutex_lock(&stream_table_lock);
if (sd < 0 || sd >= SD_MAX) {
ret = -EBADF;
goto out_release;
goto out;
}
struct stream_info *stream = stream_table[sd];
if (stream == NULL) {
ret = -ENODEV;
goto out_release;
goto out;
}
/* Poll the stream, if a poll_stream function exists */
if (stream->poll_stream != NULL) {
int ret = stream->poll_stream();
ret = stream->poll_stream();
if (ret < 0) {
goto out_release;
goto out;
}
}
/* Check buffer size is a multiple of the data packet size */
if (count % stream->item_size != 0) {
ret = -EINVAL;
goto out_release;
goto out;
}
size_t i;
......@@ -126,8 +113,7 @@ int epic_stream_read(int sd, void *buf, size_t count)
ret = i / stream->item_size;
out_release:
xSemaphoreGive(stream_table_lock);
out:
mutex_unlock(&stream_table_lock);
return ret;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment