diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/unix/drawterm/kern/sleep.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/unix/drawterm/kern/sleep.c')
-rwxr-xr-x | sys/src/cmd/unix/drawterm/kern/sleep.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/sys/src/cmd/unix/drawterm/kern/sleep.c b/sys/src/cmd/unix/drawterm/kern/sleep.c new file mode 100755 index 000000000..6a9ad174e --- /dev/null +++ b/sys/src/cmd/unix/drawterm/kern/sleep.c @@ -0,0 +1,90 @@ +#include "u.h" +#include "lib.h" +#include "dat.h" +#include "fns.h" +#include "error.h" + +void +sleep(Rendez *r, int (*f)(void*), void *arg) +{ + int s; + + s = splhi(); + + lock(&r->lk); + lock(&up->rlock); + if(r->p){ + print("double sleep %lud %lud\n", r->p->pid, up->pid); + dumpstack(); + } + + /* + * Wakeup only knows there may be something to do by testing + * r->p in order to get something to lock on. + * Flush that information out to memory in case the sleep is + * committed. + */ + r->p = up; + + if((*f)(arg) || up->notepending){ + /* + * if condition happened or a note is pending + * never mind + */ + r->p = nil; + unlock(&up->rlock); + unlock(&r->lk); + } else { + /* + * now we are committed to + * change state and call scheduler + */ + up->state = Wakeme; + up->r = r; + + /* statistics */ + /* m->cs++; */ + + unlock(&up->rlock); + unlock(&r->lk); + + procsleep(); + } + + if(up->notepending) { + up->notepending = 0; + splx(s); + error(Eintr); + } + + splx(s); +} + +Proc* +wakeup(Rendez *r) +{ + Proc *p; + int s; + + s = splhi(); + + lock(&r->lk); + p = r->p; + + if(p != nil){ + lock(&p->rlock); + if(p->state != Wakeme || p->r != r) + panic("wakeup: state"); + r->p = nil; + p->r = nil; + p->state = Running; + procwakeup(p); + unlock(&p->rlock); + } + unlock(&r->lk); + + splx(s); + + return p; +} + |