From 4e8f237be819424f922399f8d121d9867b675541 Mon Sep 17 00:00:00 2001
From: rtm <rtm>
Date: Wed, 12 Jul 2006 01:48:35 +0000
Subject: no more big kernel lock succeeds at usertests.c pipe test

---
 spinlock.c | 61 ++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 30 insertions(+), 31 deletions(-)

(limited to 'spinlock.c')

diff --git a/spinlock.c b/spinlock.c
index d73faff..9f840a8 100644
--- a/spinlock.c
+++ b/spinlock.c
@@ -2,51 +2,50 @@
 #include "defs.h"
 #include "x86.h"
 #include "mmu.h"
+#include "param.h"
+#include "proc.h"
+#include "spinlock.h"
 
-#define LOCK_FREE -1
 #define DEBUG 0
 
-uint32_t kernel_lock = LOCK_FREE;
-
 int getcallerpc(void *v) {
   return ((int*)v)[-1];
 }
 
-// lock = LOCK_FREE if free, else = cpu_id of owner CPU
 void
-acquire_spinlock(uint32_t* lock)
+acquire(struct spinlock * lock)
 {
-  int cpu_id = cpu();
+  struct proc * cp = curproc[cpu()];
 
   // on a real machine there would be a memory barrier here
-  if(DEBUG) cprintf("cpu%d: acquiring at %x\n", cpu_id, getcallerpc(&lock));
-  cli();
-  if (*lock == cpu_id)
-    panic("recursive lock");
-  
-  while ( cmpxchg(LOCK_FREE, cpu_id, lock) != cpu_id ) { ; }
-  if(DEBUG) cprintf("cpu%d: acquired at %x\n", cpu_id, getcallerpc(&lock));
+  if(DEBUG) cprintf("cpu%d: acquiring at %x\n", cpu(), getcallerpc(&lock));
+  if (cp && lock->p == cp && lock->locked){
+    lock->count += 1;
+  } else { 
+    cli();
+    while ( cmpxchg(0, 1, &lock->locked) != 1 ) { ; }
+    lock->locker_pc = getcallerpc(&lock);
+    lock->count = 1;
+    lock->p = cp;
+  }
+  if(DEBUG) cprintf("cpu%d: acquired at %x\n", cpu(), getcallerpc(&lock));
 }
 
 void
-release_spinlock(uint32_t* lock)
+release(struct spinlock * lock)
 {
-  int cpu_id = cpu();
-  if(DEBUG) cprintf ("cpu%d: releasing at %x\n", cpu_id, getcallerpc(&lock));
-  if (*lock != cpu_id)
-    panic("release_spinlock: releasing a lock that i don't own\n");
-  *lock = LOCK_FREE;
-  // on a real machine there would be a memory barrier here
-  sti();
-}
+  struct proc * cp = curproc[cpu()];
 
-void
-release_grant_spinlock(uint32_t* lock, int c)
-{
-  int cpu_id = cpu();
-  if(DEBUG) cprintf ("cpu%d: release_grant to %d at %x\n", cpu_id, c, getcallerpc(&lock));
-  if (*lock != cpu_id)
-    panic("release_spinlock: releasing a lock that i don't own\n");
-  *lock = c;
-}
+  if(DEBUG) cprintf ("cpu%d: releasing at %x\n", cpu(), getcallerpc(&lock));
 
+  if(lock->p != cp || lock->count < 1 || lock->locked != 1)
+    panic("release");
+
+  lock->count -= 1;
+  if(lock->count < 1){
+    lock->p = 0;
+    cmpxchg(1, 0, &lock->locked);
+    sti();
+    // on a real machine there would be a memory barrier here
+  }
+}
-- 
cgit v1.2.3