提交 5b20acce 创建 作者: Silas Boyd-Wickizer's avatar Silas Boyd-Wickizer

spinlock tryacquire

上级 525f0d7d
......@@ -247,6 +247,7 @@ void sampconf(void);
// spinlock.c
void acquire(struct spinlock*);
int tryacquire(struct spinlock*);
void getcallerpcs(void*, uptr*);
int holding(struct spinlock*);
void initlock(struct spinlock*, char*);
......
......@@ -18,6 +18,33 @@ initlock(struct spinlock *lk, char *name)
lk->locked = 0;
}
int
tryacquire(struct spinlock *lk)
{
pushcli(); // disable interrupts to avoid deadlock.
#if SPINLOCK_DEBUG
if(holding(lk)) {
cprintf("%lx\n", __builtin_return_address(0));
panic("acquire");
}
#endif
mtlock(lk);
if (xchg32(&lk->locked, 1) != 0) {
popcli();
return 0;
}
mtacquired(lk);
#if SPINLOCK_DEBUG
// Record info about lock acquisition for debugging.
lk->cpu = mycpu();
getcallerpcs(&lk, lk->pcs);
#endif
return 1;
}
// Acquire the lock.
// Loops (spins) until the lock is acquired.
// Holding a lock for a long time may cause
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论