Introduce simple "mutex" locking code.
authorKevin O'Connor <kevin@koconnor.net>
Sat, 27 Feb 2010 18:49:47 +0000 (13:49 -0500)
committerKevin O'Connor <kevin@koconnor.net>
Sun, 28 Feb 2010 22:26:25 +0000 (17:26 -0500)
Locks are not normally necessary because SeaBIOS uses a cooperative
multitasking system.  However, occasionally it is necessary to be able
to lock a resource across yield calls.  This patch introduces a simple
mechanism for doing that.

src/stacks.c
src/util.h

index a35ca3de7717a3bafed840689e3fdea40e5c3611..c783967cb7d115393aeec897f9e57604499772b2 100644 (file)
@@ -248,6 +248,26 @@ wait_threads(void)
         yield();
 }
 
+void
+mutex_lock(struct mutex_s *mutex)
+{
+    ASSERT32FLAT();
+    if (! CONFIG_THREADS)
+        return;
+    while (mutex->isLocked)
+        yield();
+    mutex->isLocked = 1;
+}
+
+void
+mutex_unlock(struct mutex_s *mutex)
+{
+    ASSERT32FLAT();
+    if (! CONFIG_THREADS)
+        return;
+    mutex->isLocked = 0;
+}
+
 
 /****************************************************************
  * Thread preemption
index 9f71d65a00434b70f45e17ceeb9658669fc09339..9b4fd3ab3c649f94e5617d29638037cf07ec7729 100644 (file)
@@ -208,6 +208,9 @@ struct thread_info *getCurThread(void);
 void yield(void);
 void run_thread(void (*func)(void*), void *data);
 void wait_threads(void);
+struct mutex_s { u32 isLocked; };
+void mutex_lock(struct mutex_s *mutex);
+void mutex_unlock(struct mutex_s *mutex);
 void start_preempt(void);
 void finish_preempt(void);
 void check_preempt(void);