#include <u.h>
#include <libc.h>

void
rendsleep(Pmrendez *r, int (*f)(void*), void *arg)
{
	Pmproc *up;

	up = pm_getup();
	up->r = r;
	qlock(&r->lk);

	if(f(arg)){
		up->r = nil;
		qunlock(&r->lk);
		return;
	}
	if(r->p != nil)
		pm_panic("rendsleep");
	r->p = up;
	qunlock(&r->lk);
	rendezvous((ulong)up, 0);
}

Pmproc*
rendwakeup(Pmrendez *r)
{
	Pmproc *p;

	qlock(&r->lk);
	p = r->p;
	if(p != nil){
		r->p = nil;
		p->r = nil;
		rendezvous((ulong)p, 0);
	}
	qunlock(&r->lk);
	return p;
}

