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 <u.h>
36 #include <pthread.h>
37 #include <signal.h>
38 #include <libc.h>
40 enum
41 {
42 VOUSHASH = 257,
43 };
45 typedef struct Vous Vous;
46 struct Vous
47 {
48 Vous *link;
49 Lock lk;
50 ulong val;
51 ulong tag;
52 pthread_mutex_t mutex;
53 };
55 static void
56 ign(int x)
57 {
58 USED(x);
59 }
61 void /*__attribute__((constructor))*/
62 ignusr1(void)
63 {
64 signal(SIGUSR1, ign);
65 }
67 static Vous vouspool[2048];
68 static int nvousused;
69 static Vous *vousfree;
70 static Vous *voushash[VOUSHASH];
71 static Lock vouslock;
73 static Vous*
74 getvous(void)
75 {
76 Vous *v;
78 if(vousfree){
79 v = vousfree;
80 vousfree = v->link;
81 }else if(nvousused < nelem(vouspool)){
82 v = &vouspool[nvousused++];
83 pthread_mutex_init(&v->mutex, NULL);
84 }else
85 abort();
86 return v;
87 }
89 static void
90 putvous(Vous *v)
91 {
92 lock(&vouslock);
93 v->link = vousfree;
94 vousfree = v;
95 unlock(&vouslock);
96 }
98 static Vous*
99 findvous(ulong tag, ulong val, int *found)
101 int h;
102 Vous *v, **l;
104 lock(&vouslock);
105 h = tag%VOUSHASH;
106 for(l=&voushash[h], v=*l; v; l=&(*l)->link, v=*l){
107 if(v->tag == tag){
108 *l = v->link;
109 *found = 1;
110 unlock(&vouslock);
111 return v;
114 v = getvous();
115 v->link = voushash[h];
116 v->val = val;
117 v->tag = tag;
118 lock(&v->lk);
119 voushash[h] = v;
120 unlock(&vouslock);
121 *found = 0;
122 return v;
125 #define DBG 0
126 ulong
127 rendezvous(ulong tag, ulong val)
129 int found;
130 ulong rval;
131 Vous *v;
133 v = findvous(tag, val, &found);
134 if(!found){
135 if(DBG)fprint(2, "tag %lux, sleeping on %p\n", tag, v);
136 /*
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
141 * unlock it for us).
142 */
143 if(pthread_mutex_lock(&v->mutex) != 0)
144 abort();
145 unlock(&v->lk);
146 if(pthread_mutex_lock(&v->mutex) != 0)
147 abort();
148 rval = v->val;
149 pthread_mutex_unlock(&v->mutex);
150 if(DBG)fprint(2, " awake on %p\n", v);
151 unlock(&v->lk);
152 putvous(v);
153 }else{
154 /*
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)
159 */
160 if(DBG)fprint(2, "found tag %lux on %p, waking\n", tag, v);
161 lock(&v->lk);
162 rval = v->val;
163 v->val = val;
164 if(pthread_mutex_unlock(&v->mutex) != 0)
165 abort();
166 /* lock passes to him */
168 return rval;