[freenos] r413 committed - Simplified the implementation of the (x86/pc) spinlock.

  • From: freenos@xxxxxxxxxxxxxx
  • To: freenos@xxxxxxxxxxxxx
  • Date: Thu, 21 Oct 2010 21:34:26 +0000

Revision: 413
Author: nieklinnenbank
Date: Thu Oct 21 14:33:28 2010
Log: Simplified the implementation of the (x86/pc) spinlock.

http://code.google.com/p/freenos/source/detail?r=413

Modified:
 /branches/scratch/include/kernel/spinlock.h
 /branches/scratch/system/x86/pc/spinlock.c

=======================================
--- /branches/scratch/include/kernel/spinlock.h Sun Oct 10 16:06:55 2010
+++ /branches/scratch/include/kernel/spinlock.h Thu Oct 21 14:33:28 2010
@@ -21,10 +21,12 @@
 #include "types.h"
 #include "macro.h"

+#define SPINLOCK_LOCKED   1
+#define SPINLOCK_UNLOCKED 0
+
 typedef struct spinlock
 {
-    u32 cpu_id;
-    u32 context;
+    u32 state;
 }
 PACKED spinlock_t;

@@ -33,4 +35,3 @@
 extern void spinlock_leave(spinlock_t *spinlock);

 #endif /* __SPINLOCK_H */
-
=======================================
--- /branches/scratch/system/x86/pc/spinlock.c  Sun Oct 10 16:06:55 2010
+++ /branches/scratch/system/x86/pc/spinlock.c  Thu Oct 21 14:33:28 2010
@@ -22,34 +22,21 @@

 void spinlock_init(spinlock_t *spinlock)
 {
-    spinlock->cpu_id  = -1;
-    spinlock->context = 0;
+    spinlock->state = SPINLOCK_UNLOCKED;
 }

 void spinlock_enter(spinlock_t *spinlock)
 {
-    /* Obtain current EFLAGS. */
-    u32 eflags = get_eflags();
-    u32 my_id  = smp_id();
-
-    /* Disable IRQ's. */
-    irq_disable();
-
-    /* Try to obtain the lock. Wait for other CPU if needed. */
-    __asm__ volatile("1: movl $-1,%%eax; lock; cmpxchgl %0,(%1); jne 1b"
-                    : : "r" (my_id), "r" (&spinlock->cpu_id) : "eax");
-
-    /* Save EFLAGS register in spinlock. */
-    spinlock->context = eflags;
+    __asm__ volatile ("1: movl %0, %%eax;"
+                     "   lock;"
+                     "   cmpxchgl %1,(%2);"
+                     "   jne 1b"
+                     :: "r" (SPINLOCK_UNLOCKED),
+                        "r" (SPINLOCK_LOCKED),
+                        "r" (&spinlock->state) : "eax");
 }

 void spinlock_leave(spinlock_t *spinlock)
 {
-    /* Unlock the global spin lock. */
-    __asm__ volatile("movl %0,(%1)"
-                    : : "r" (-1), "r" (&spinlock->cpu_id));
-
-    /* Restore IRQ state. */
-    set_eflags(spinlock->context);
-}
-
+    spinlock->state = SPINLOCK_UNLOCKED;
+}

Other related posts:

  • » [freenos] r413 committed - Simplified the implementation of the (x86/pc) spinlock. - freenos