export process use stdlib.h # for NULL use sched # for start() # rendezvous system, which is the basis of inter-process channels struct rendezvous process *waiting rendezvous_init(rendezvous *r) r->waiting = NULL # here are the general rendezvous methods: # meet - if the partner is here, returns 0, # otherwise we wait for the partner to arrive, returns 1, # (caller must then yield) int meet_f(rendezvous *r, process *p) int must_wait = !r->waiting if must_wait r->waiting = p return must_wait # pass - give control to the partner # (this works whether or not they are here already) pass_f(rendezvous *r, process *p) if r->waiting start_f(r->waiting) r->waiting = p # part - finish the rendezvous # (this assumes they are here) part_f(rendezvous *r) start_f(r->waiting) r->waiting = NULL # peer - get the partner's process pointer # or NULL if they are not here process *peer_f(rendezvous *r) return r->waiting # here - check if the partner is here int here_f(rendezvous *r) return peer_f(r) != NULL define meet(ch) if meet_f(r(ch), p) wait define pass(ch) pass_f(r(ch), p) wait define part(ch) part_f(r(ch)) define peer(ch) peer_f(r(ch)) define here(ch) here_f(r(ch)) define c(foo) (s(foo)->data) define r(foo) (&s(foo)->r) define channel(type) struct ch(type) rendezvous r type data define ch(type) channel_^^type define ch_init(c) rendezvous_init(&ch.r) define wr(ch, v) meet(ch) c(ch) = v part(ch) define rd(ch, v) pass(ch) v = c(ch) define WR(ch, v) c(ch) = v pass(ch) define RD(ch, v) meet(ch) v = c(ch) part(ch)