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.
45 typedef struct Vous Vous;
52 pthread_mutex_t mutex;
61 void /*__attribute__((constructor))*/
67 static Vous vouspool[2048];
69 static Vous *vousfree;
70 static Vous *voushash[VOUSHASH];
81 }else if(nvousused < nelem(vouspool)){
82 v = &vouspool[nvousused++];
83 pthread_mutex_init(&v->mutex, NULL);
99 findvous(ulong tag, ulong val, int *found)
106 for(l=&voushash[h], v=*l; v; l=&(*l)->link, v=*l){
115 v->link = voushash[h];
127 rendezvous(ulong tag, ulong val)
133 v = findvous(tag, val, &found);
135 if(DBG)fprint(2, "tag %lux, sleeping on %p\n", tag, v);
137 * No rendezvous partner was found; the next guy
138 * through will find v and wake us, so we must go
139 * to sleep. Do this by locking the mutex (it is
140 * unlocked) and then locking it again (our waker will
143 if(pthread_mutex_lock(&v->mutex) != 0)
146 if(pthread_mutex_lock(&v->mutex) != 0)
149 pthread_mutex_unlock(&v->mutex);
150 if(DBG)fprint(2, " awake on %p\n", v);
155 * Found someone to meet. Wake him:
157 * A. lock v->lk (waits for him to lock the mutex once.
158 * B. unlock the mutex (wakes him up)
160 if(DBG)fprint(2, "found tag %lux on %p, waking\n", tag, v);
164 if(pthread_mutex_unlock(&v->mutex) != 0)
166 /* lock passes to him */