From 44ea81f2bfc8e2cc60ba11adcf68527fce42b678 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Sun, 6 Oct 2019 20:34:13 +0200
Subject: [PATCH] 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: Rahix <rahix@rahix.de>
---
 epicardium/modules/stream.c | 56 ++++++++++++++-----------------------
 1 file changed, 21 insertions(+), 35 deletions(-)

diff --git a/epicardium/modules/stream.c b/epicardium/modules/stream.c
index 7453da11..07f55f3a 100644
--- a/epicardium/modules/stream.c
+++ b/epicardium/modules/stream.c
@@ -1,80 +1,71 @@
 #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;
 }
-- 
GitLab