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 simulates rendezvous with shared memory, sigsuspend, and SIGUSR1.
32 */
34 #include <u.h>
35 #include <signal.h>
36 #include <libc.h>
38 #define DBG 0
40 enum
41 {
42 VOUSHASH = 257,
43 };
45 typedef struct Vous Vous;
46 struct Vous
47 {
48 Vous *link;
49 int pid;
50 int wokeup;
51 ulong tag;
52 ulong val1; /* value for the sleeper */
53 ulong val2; /* value for the waker */
54 };
56 static void
57 ign(int x)
58 {
59 USED(x);
60 }
62 void /*__attribute__((constructor))*/
63 ignusr1(int restart)
64 {
65 struct sigaction sa;
67 memset(&sa, 0, sizeof sa);
68 sa.sa_handler = ign;
69 sigemptyset(&sa.sa_mask);
70 sigaddset(&sa.sa_mask, SIGUSR1);
71 if(restart)
72 sa.sa_flags = SA_RESTART;
73 sigaction(SIGUSR1, &sa, nil);
74 }
76 static Vous vouspool[2048];
77 static int nvousused;
78 static Vous *vousfree;
79 static Vous *voushash[VOUSHASH];
80 static Lock vouslock;
82 static Vous*
83 getvous(void)
84 {
85 Vous *v;
87 if(vousfree){
88 v = vousfree;
89 vousfree = v->link;
90 }else if(nvousused < nelem(vouspool))
91 v = &vouspool[nvousused++];
92 else{
93 fprint(2, "rendezvous: out of vous!\n");
94 abort();
95 }
96 return v;
97 }
99 static void
100 putvous(Vous *v)
102 v->link = vousfree;
103 vousfree = v;
106 static Vous*
107 findvous(ulong tag)
109 int h;
110 Vous *v, **l;
112 h = tag%VOUSHASH;
113 for(l=&voushash[h], v=*l; v; l=&(*l)->link, v=*l){
114 if(v->tag == tag){
115 *l = v->link;
116 v->link = nil;
117 return v;
120 return nil;
123 static Vous*
124 mkvous(ulong tag)
126 Vous *v;
127 int h;
129 h = tag%VOUSHASH;
130 v = getvous();
131 v->link = voushash[h];
132 v->tag = tag;
133 voushash[h] = v;
134 return v;
137 ulong
138 rendezvous(ulong tag, ulong val)
140 int vpid, pid;
141 ulong rval;
142 Vous *v;
143 sigset_t mask;
145 pid = getpid();
146 lock(&vouslock);
147 if((v = findvous(tag)) == nil){
148 /*
149 * Go to sleep.
151 * Block USR1, set the handler to interrupt system calls,
152 * unlock the vouslock so our waker can wake us,
153 * and then suspend.
154 */
155 v = mkvous(tag);
156 v->pid = pid;
157 v->val2 = val;
158 v->wokeup = 0;
159 sigprocmask(SIG_SETMASK, nil, &mask);
160 sigaddset(&mask, SIGUSR1);
161 sigprocmask(SIG_SETMASK, &mask, nil);
162 ignusr1(0);
163 if(DBG) fprint(2, "%d rv(%lux, %lux) -> s\n", pid, tag, val);
164 unlock(&vouslock);
165 sigdelset(&mask, SIGUSR1);
166 sigsuspend(&mask);
168 /*
169 * We're awake. Make USR1 not interrupt system calls.
170 * Were we awakened or interrupted?
171 */
172 ignusr1(1);
173 lock(&vouslock);
174 if(v->wokeup){
175 rval = v->val1;
176 if(DBG) fprint(2, "%d rv(%lux, %lux) -> g %lux\n", pid, tag, val, rval);
177 }else{
178 if(findvous(tag) != v){
179 fprint(2, "rendezvous: interrupted but not found in hash table\n");
180 abort();
182 rval = ~(ulong)0;
183 if(DBG) fprint(2, "%d rv(%lux, %lux) -> g i\n", pid, tag, val);
185 putvous(v);
186 unlock(&vouslock);
187 }else{
188 /*
189 * Wake up sleeper.
190 */
191 rval = v->val2;
192 v->val1 = val;
193 vpid = v->pid;
194 v->wokeup = 1;
195 if(DBG) fprint(2, "%d rv(%lux, %lux) -> g %lux, w %d\n", pid, tag, val, rval, vpid);
196 unlock(&vouslock);
197 kill(vpid, SIGUSR1);
199 return rval;