From c1bd3aa131d89abc128e2e9ddc24fd80c86c203e Mon Sep 17 00:00:00 2001
From: Christian Cunningham <cc@localhost>
Date: Sat, 19 Mar 2022 10:30:45 -0700
Subject: Fixed Deadlock Mechanism

---
 src/util/mutex.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

(limited to 'src')

diff --git a/src/util/mutex.c b/src/util/mutex.c
index 0525a3e..0a47a35 100644
--- a/src/util/mutex.c
+++ b/src/util/mutex.c
@@ -53,32 +53,46 @@ unsigned char delete_mutex(struct Mutex* m)
 	return 0;
 }
 
+void uart_mutexes(void)
+{
+	struct Entry* entry = mutex_manager.used.start.next;
+	while (entry->entry_type == VALUE_ENTRY)
+	{
+		struct Mutex* m = entry->value;
+		uart_hex(m);
+		uart_char(' ');
+		uart_hex(m->pid);
+		uart_char(' ');
+		uart_hexn(m->addr);
+		entry = entry->next;
+	}
+}
+
 void lock_mutex(struct Mutex* m)
 {
 	struct Thread* rthread = scheduler.rthread;
 	unsigned long rpid = rthread->pid;
 	unsigned long mode = getmode() & 0x1F;
 	if (mode == 0x10) {
-		sys1(SYS_LOCK, m);
 		// Find this mutex
 		struct Entry* mentry = find_value(m, &mutex_manager.used);
 		// If it is not a managed mutex, break away
 		if (mentry == 0)
 			return;
-		// Get the next entry
-		mentry = mentry->next->next;
+		struct Entry* entry = mutex_manager.used.start.next;
 		// Ensure this thread locks all mutexs sequentially
 		//  To avoid a deadlock
-		while (mentry->entry_type == VALUE_ENTRY) {
-			struct Mutex* vmutex = mentry->value;
+		while (entry->entry_type == VALUE_ENTRY) {
+			struct Mutex* vmutex = entry->value;
 			// If this thread had locked it
 			//  Toggle the lock to prevent deadlock
 			if (vmutex->pid == rpid) {
-				sys1(SYS_UNLOCK, m);
-				sys1(SYS_LOCK, m);
+				sys1(SYS_UNLOCK, vmutex);
+				sys1(SYS_LOCK, vmutex);
 			}
-			mentry = mentry->next;
+			entry = entry->next;
 		}
+		sys1(SYS_LOCK, m);
 	}
 }
 
-- 
cgit v1.2.1