diff options
author | rtm <rtm> | 2006-07-12 01:48:35 +0000 |
---|---|---|
committer | rtm <rtm> | 2006-07-12 01:48:35 +0000 |
commit | 4e8f237be819424f922399f8d121d9867b675541 (patch) | |
tree | 53459cfde9630b3ae0d2d46d0ce3d4c1ac423944 /spinlock.c | |
parent | b41b38d0da0854f3fa92967b70180ea1156154d4 (diff) | |
download | xv6-labs-4e8f237be819424f922399f8d121d9867b675541.tar.gz xv6-labs-4e8f237be819424f922399f8d121d9867b675541.tar.bz2 xv6-labs-4e8f237be819424f922399f8d121d9867b675541.zip |
no more big kernel lock
succeeds at usertests.c pipe test
Diffstat (limited to 'spinlock.c')
-rw-r--r-- | spinlock.c | 61 |
1 files changed, 30 insertions, 31 deletions
@@ -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 + } +} |