3 rendezvous - user level process synchronization
6 ulong rendezvous(ulong tag, ulong value)
9 The rendezvous system call allows two processes to synchro-
10 nize and exchange a value. In conjunction with the shared
11 memory system calls (see segattach(2) and fork(2)), it
12 enables parallel programs to control their scheduling.
14 Two processes wishing to synchronize call rendezvous with a
15 common tag, typically an address in memory they share. One
16 process will arrive at the rendezvous first; it suspends
17 execution until a second arrives. When a second process
18 meets the rendezvous the value arguments are exchanged
19 between the processes and returned as the result of the
20 respective rendezvous system calls. Both processes are
21 awakened when the rendezvous succeeds.
23 The set of tag values which two processes may use to
24 rendezvous-their tag space-is inherited when a process
25 forks, unless RFREND is set in the argument to rfork; see
28 If a rendezvous is interrupted the return value is ~0, so
29 that value should not be used in normal communication.
31 * This assumes we're using pthreads and simulates rendezvous using
32 * shared memory and mutexes.
44 typedef struct Vous Vous;
51 pthread_mutex_t mutex;
60 void /*__attribute__((constructor))*/
66 static Vous vouspool[2048];
68 static Vous *vousfree;
69 static Vous *voushash[VOUSHASH];
80 }else if(nvousused < nelem(vouspool)){
81 v = &vouspool[nvousused++];
82 pthread_mutex_init(&v->mutex, NULL);
98 findvous(ulong tag, ulong val, int *found)
105 for(l=&voushash[h], v=*l; v; l=&(*l)->link, v=*l){
114 v->link = voushash[h];
126 rendezvous(ulong tag, ulong val)
132 v = findvous(tag, val, &found);
134 if(DBG)fprint(2, "tag %lux, sleeping on %p\n", tag, v);
136 * No rendezvous partner was found; the next guy
137 * through will find v and wake us, so we must go
138 * to sleep. Do this by locking the mutex (it is
139 * unlocked) and then locking it again (our waker will
142 if(pthread_mutex_lock(&v->mutex) != 0)
145 if(pthread_mutex_lock(&v->mutex) != 0)
148 pthread_mutex_unlock(&v->mutex);
149 if(DBG)fprint(2, " awake on %p\n", v);
154 * Found someone to meet. Wake him:
156 * A. lock v->lk (waits for him to lock the mutex once.
157 * B. unlock the mutex (wakes him up)
159 if(DBG)fprint(2, "found tag %lux on %p, waking\n", tag, v);
163 if(pthread_mutex_unlock(&v->mutex) != 0)
165 /* lock passes to him */