diff options
author | Frans Kaashoek <[email protected]> | 2011-08-26 10:08:29 -0400 |
---|---|---|
committer | Frans Kaashoek <[email protected]> | 2011-08-26 10:08:29 -0400 |
commit | 3a5fa7ed9020eaf8ab843a16d26db7393b2ec072 (patch) | |
tree | bfa4ad4ae03d7d21796bacaa7eab8e3d3e4ab365 /spinlock.c | |
parent | 8a9b6dbd4468f6312f1d07226a623879f970bd4b (diff) | |
download | xv6-labs-3a5fa7ed9020eaf8ab843a16d26db7393b2ec072.tar.gz xv6-labs-3a5fa7ed9020eaf8ab843a16d26db7393b2ec072.tar.bz2 xv6-labs-3a5fa7ed9020eaf8ab843a16d26db7393b2ec072.zip |
Introduce and use sleeplocks instead of BUSY flags
Remove I_BUSY, B_BUSY, and intrans defs and usages
One spinlock per buf to avoid ugly loop in bget
fix race in filewrite (don't update f->off after releasing lock)
Diffstat (limited to 'spinlock.c')
-rw-r--r-- | spinlock.c | 42 |
1 files changed, 38 insertions, 4 deletions
@@ -17,10 +17,9 @@ initlock(struct spinlock *lk, char *name) lk->cpu = 0; } -// Acquire the lock. -// Loops (spins) until the lock is acquired. -// Holding a lock for a long time may cause -// other CPUs to waste time spinning to acquire it. +// Acquire a spin lock. Loops (spins) until the lock is acquired. +// Holding a lock for a long time may cause other CPUs to waste time spinning to acquire it. +// Spinlocks shouldn't be held across sleep(); for those cases, use sleeplocks. void acquire(struct spinlock *lk) { @@ -115,3 +114,38 @@ popcli(void) sti(); } +void +initsleeplock(struct sleeplock *l) +{ + l->locked = 0; +} + +// Grab the sleeplock that is protected by spinl. Sleeplocks allow a process to lock +// a data structure for long times, including across sleeps. Other processes that try +// to acquire a sleeplock will be put to sleep when another process hold the sleeplock. +// To update status of the sleeplock atomically, the caller must hold spinl +void +acquire_sleeplock(struct sleeplock *sleepl, struct spinlock *spinl) +{ + while (sleepl->locked) { + sleep(sleepl, spinl); + } + sleepl->locked = 1; +} + +// Release the sleeplock that is protected by a spin lock +// Caller must hold the spinlock that protects the sleeplock +void +release_sleeplock(struct sleeplock *sleepl) +{ + sleepl->locked = 0; + wakeup(sleepl); +} + +// Is the sleeplock acquired? +// Caller must hold the spinlock that protects the sleeplock +int +acquired_sleeplock(struct sleeplock *sleepl) +{ + return sleepl->locked; +} |