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

feat(streams): Add descriptor table lock


Signed-off-by: default avatarRahix <rahix@rahix.de>
parent 13386d1a
No related branches found
No related tags found
No related merge requests found
#include <string.h> #include <string.h>
#include "FreeRTOS.h"
#include "semphr.h"
#include "epicardium.h" #include "epicardium.h"
#include "stream.h" #include "stream.h"
/* Internal buffer of registered streams */
static struct stream_info *stream_table[SD_MAX]; 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;
int stream_init() int stream_init()
{ {
memset(stream_table, 0x00, sizeof(stream_table)); memset(stream_table, 0x00, sizeof(stream_table));
stream_table_lock =
xSemaphoreCreateMutexStatic(&stream_table_lock_data);
return 0; return 0;
} }
int stream_register(int sd, struct stream_info *stream) int stream_register(int sd, struct stream_info *stream)
{ {
if (xSemaphoreTake(stream_table_lock, STREAM_MUTEX_WAIT) != pdTRUE) {
return -EBUSY;
}
if (sd < 0 || sd >= SD_MAX) { if (sd < 0 || sd >= SD_MAX) {
return -EINVAL; return -EINVAL;
} }
...@@ -23,11 +37,17 @@ int stream_register(int sd, struct stream_info *stream) ...@@ -23,11 +37,17 @@ int stream_register(int sd, struct stream_info *stream)
} }
stream_table[sd] = stream; stream_table[sd] = stream;
xSemaphoreGive(stream_table_lock);
return 0; return 0;
} }
int stream_deregister(int sd, struct stream_info *stream) int stream_deregister(int sd, struct stream_info *stream)
{ {
if (xSemaphoreTake(stream_table_lock, STREAM_MUTEX_WAIT) != pdTRUE) {
return -EBUSY;
}
if (sd < 0 || sd >= SD_MAX) { if (sd < 0 || sd >= SD_MAX) {
return -EINVAL; return -EINVAL;
} }
...@@ -38,11 +58,22 @@ int stream_deregister(int sd, struct stream_info *stream) ...@@ -38,11 +58,22 @@ int stream_deregister(int sd, struct stream_info *stream)
} }
stream_table[sd] = NULL; stream_table[sd] = NULL;
xSemaphoreGive(stream_table_lock);
return 0; return 0;
} }
int epic_stream_read(int sd, void *buf, size_t count) int epic_stream_read(int sd, void *buf, size_t count)
{ {
/*
* TODO: In theory, multiple reads on different streams can happen
* simulaneously. I don't know what the most efficient implementation
* of this would look like.
*/
if (xSemaphoreTake(stream_table_lock, STREAM_MUTEX_WAIT) != pdTRUE) {
return -EBUSY;
}
if (sd < 0 || sd >= SD_MAX) { if (sd < 0 || sd >= SD_MAX) {
return -EBADF; return -EBADF;
} }
...@@ -72,5 +103,6 @@ int epic_stream_read(int sd, void *buf, size_t count) ...@@ -72,5 +103,6 @@ int epic_stream_read(int sd, void *buf, size_t count)
} }
} }
xSemaphoreGive(stream_table_lock);
return i / stream->item_size; return i / stream->item_size;
} }
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "queue.h" #include "queue.h"
/* Time to wait for the descriptor table lock to become available */
#define STREAM_MUTEX_WAIT pdMS_TO_TICKS(100)
/** /**
* **Stream Descriptors**: * **Stream Descriptors**:
* *
......
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