From fc41d13dd0f1a7c6c1799fd8bdecf22e9fabe887 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Mon, 7 Dec 2020 02:43:06 +0100
Subject: [PATCH] fix(mutex): Ignore mutex handling until FreeRTOS is running

This allows to execute code which uses mutexes before the scheduler is
started. E.g. during early hardware init.
---
 epicardium/modules/mutex.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/epicardium/modules/mutex.c b/epicardium/modules/mutex.c
index 9d58eec0..4fa3eacf 100644
--- a/epicardium/modules/mutex.c
+++ b/epicardium/modules/mutex.c
@@ -23,6 +23,9 @@ void _mutex_create(struct mutex *m, const char *name)
 
 void mutex_lock(struct mutex *m)
 {
+	if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
+		return;
+	}
 	int ret = xSemaphoreTake(m->_rtos_mutex, portMAX_DELAY);
 
 	/* Ensure locking was actually successful */
@@ -31,12 +34,18 @@ void mutex_lock(struct mutex *m)
 
 bool mutex_trylock(struct mutex *m)
 {
+	if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
+		return true;
+	}
 	int ret = xSemaphoreTake(m->_rtos_mutex, 0);
 	return ret == pdTRUE;
 }
 
 void mutex_unlock(struct mutex *m)
 {
+	if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
+		return;
+	}
 	/* Ensure that only the owner can unlock a mutex */
 	assert(mutex_get_owner(m) == xTaskGetCurrentTaskHandle());
 
-- 
GitLab