xv6-65oo2/kernel/sleeplock.c

56 lines
797 B
C
Raw Permalink Normal View History

2016-09-12 01:01:24 +00:00
// Sleeping locks
#include "types.h"
2019-05-31 15:45:42 +00:00
#include "riscv.h"
2016-09-12 01:01:24 +00:00
#include "defs.h"
#include "param.h"
#include "memlayout.h"
#include "spinlock.h"
#include "proc.h"
2016-09-12 01:01:24 +00:00
#include "sleeplock.h"
void
initsleeplock(struct sleeplock *lk, char *name)
{
initlock(&lk->lk, "sleep lock");
lk->name = name;
lk->locked = 0;
lk->pid = 0;
}
void
acquiresleep(struct sleeplock *lk)
{
acquire(&lk->lk);
while (lk->locked) {
sleep(lk, &lk->lk);
}
lk->locked = 1;
lk->pid = myproc()->pid;
2016-09-12 01:01:24 +00:00
release(&lk->lk);
}
void
releasesleep(struct sleeplock *lk)
{
acquire(&lk->lk);
lk->locked = 0;
lk->pid = 0;
wakeup(lk);
release(&lk->lk);
}
int
holdingsleep(struct sleeplock *lk)
{
int r;
acquire(&lk->lk);
r = lk->locked && (lk->pid == myproc()->pid);
2016-09-12 01:01:24 +00:00
release(&lk->lk);
return r;
}