struct Thread void *f int pc typedef void (*func)(struct Thread *) struct Object Thread *holding Thread *waiting struct Int Object int i enum { MAXTHREADS = 10 } int qpush = 0 int qpop = 0 Thread *q[MAXTHREADS] sched(Thread *t) if t q[qpush] = t if ++qpush == MAXTHREADS qpush = 0 if qpush == qpop exits("too many threads") get(Object *o, Thread *t) if o->holding == 0 o->holding = t sched(t) else if o->waiting exits("broken") else o->waiting = t put(Object *o, Thread *t) if o->holding == t && o->waiting o->holding = o->waiting o->waiting = 0 sched(o->holding) yield(Object *o, Thread *t) put(o, t) get(o, t) connect(void **from, Object *obj, void **to) obj->holding = 0 obj->waiting = 0 *from = obj *to = obj step() Thread *t if qpop == qpush exits("no threads") t = q[qpop] if ++qpop == MAXTHREADS qpop = 0 (*(func)t->f)(t) start(Thread *t) t->pc = 0 sched(t) run() while 1 step()