Skip to contentSkip to conversation

Make semaphores callback-safe

PATCH New Samuel Thibault 1 participant 1 message Nov 23, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Make semaphores callback-safe

One may want to use semaphores in event handlers to wake threads waiting for a resource, so semaphores then need to be callback-safe.

---
Note: there is no such use in the current code, but I have an overflow fix for netfront which depends on this.

extras/mini-os/include/semaphore.h +12 -1
diff -r f2711b7eae95 -r 36bf1e737b87 extras/mini-os/include/semaphore.h
@@ -41,14 +41,25 @@
 
 static void inline down(struct semaphore *sem)
 {
-    wait_event(sem->wait, sem->count > 0);
+    unsigned long flags;
+    while (1) {
+        wait_event(sem->wait, sem->count > 0);
+        local_irq_save(flags);
+        if (sem->count > 0)
+            break;
+        local_irq_restore(flags);
+    }
     sem->count--;
+    local_irq_restore(flags);
 }
 
 static void inline up(struct semaphore *sem)
 {
+    unsigned long flags;
+    local_irq_save(flags);
     sem->count++;
     wake_up(&sem->wait);
+    local_irq_restore(flags);
 }
 
 /* FIXME! Thre read/write semaphores are unimplemented! */