Blob


1 /*
2 NAME
3 rendezvous - user level process synchronization
5 SYNOPSIS
6 ulong rendezvous(ulong tag, ulong value)
8 DESCRIPTION
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
26 fork(2).
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.
33 */
35 #include <pthread.h>
36 #include <signal.h>
37 #include <lib9.h>
39 enum
40 {
41 VOUSHASH = 257,
42 };
44 typedef struct Vous Vous;
45 struct Vous
46 {
47 Vous *link;
48 Lock lk;
49 ulong val;
50 ulong tag;
51 pthread_mutex_t mutex;
52 };
54 static void
55 ign(int x)
56 {
57 USED(x);
58 }
60 void /*__attribute__((constructor))*/
61 ignusr1(void)
62 {
63 signal(SIGUSR1, ign);
64 }
66 static Vous vouspool[2048];
67 static int nvousused;
68 static Vous *vousfree;
69 static Vous *voushash[VOUSHASH];
70 static Lock vouslock;
72 static Vous*
73 getvous(void)
74 {
75 Vous *v;
77 if(vousfree){
78 v = vousfree;
79 vousfree = v->link;
80 }else if(nvousused < nelem(vouspool)){
81 v = &vouspool[nvousused++];
82 pthread_mutex_init(&v->mutex, NULL);
83 }else
84 abort();
85 return v;
86 }
88 static void
89 putvous(Vous *v)
90 {
91 lock(&vouslock);
92 v->link = vousfree;
93 vousfree = v;
94 unlock(&vouslock);
95 }
97 static Vous*
98 findvous(ulong tag, ulong val, int *found)
99 {
100 int h;
101 Vous *v, **l;
103 lock(&vouslock);
104 h = tag%VOUSHASH;
105 for(l=&voushash[h], v=*l; v; l=&(*l)->link, v=*l){
106 if(v->tag == tag){
107 *l = v->link;
108 *found = 1;
109 unlock(&vouslock);
110 return v;
113 v = getvous();
114 v->link = voushash[h];
115 v->val = val;
116 v->tag = tag;
117 lock(&v->lk);
118 voushash[h] = v;
119 unlock(&vouslock);
120 *found = 0;
121 return v;
124 #define DBG 0
125 ulong
126 rendezvous(ulong tag, ulong val)
128 int found;
129 ulong rval;
130 Vous *v;
132 v = findvous(tag, val, &found);
133 if(!found){
134 if(DBG)fprint(2, "tag %lux, sleeping on %p\n", tag, v);
135 /*
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
140 * unlock it for us).
141 */
142 if(pthread_mutex_lock(&v->mutex) != 0)
143 abort();
144 unlock(&v->lk);
145 if(pthread_mutex_lock(&v->mutex) != 0)
146 abort();
147 rval = v->val;
148 pthread_mutex_unlock(&v->mutex);
149 if(DBG)fprint(2, " awake on %p\n", v);
150 unlock(&v->lk);
151 putvous(v);
152 }else{
153 /*
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)
158 */
159 if(DBG)fprint(2, "found tag %lux on %p, waking\n", tag, v);
160 lock(&v->lk);
161 rval = v->val;
162 v->val = val;
163 if(pthread_mutex_unlock(&v->mutex) != 0)
164 abort();
165 /* lock passes to him */
167 return rval;