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, pause, and SIGUSR1.
32 */
34 #include <signal.h>
35 #include <lib9.h>
37 enum
38 {
39 VOUSHASH = 257,
40 };
42 typedef struct Vous Vous;
43 struct Vous
44 {
45 Vous *link;
46 Lock lk;
47 int pid;
48 ulong val;
49 ulong tag;
50 };
52 static void
53 ign(int x)
54 {
55 USED(x);
56 }
58 void /*__attribute__((constructor))*/
59 ignusr1(void)
60 {
61 signal(SIGUSR1, ign);
62 }
64 static Vous vouspool[2048];
65 static int nvousused;
66 static Vous *vousfree;
67 static Vous *voushash[VOUSHASH];
68 static Lock vouslock;
70 static Vous*
71 getvous(void)
72 {
73 Vous *v;
75 if(vousfree){
76 v = vousfree;
77 vousfree = v->link;
78 }else if(nvousused < nelem(vouspool))
79 v = &vouspool[nvousused++];
80 else
81 abort();
82 return v;
83 }
85 static void
86 putvous(Vous *v)
87 {
88 lock(&vouslock);
89 v->link = vousfree;
90 vousfree = v;
91 unlock(&vouslock);
92 }
94 static Vous*
95 findvous(ulong tag, ulong val, int pid)
96 {
97 int h;
98 Vous *v, **l;
100 lock(&vouslock);
101 h = tag%VOUSHASH;
102 for(l=&voushash[h], v=*l; v; l=&(*l)->link, v=*l){
103 if(v->tag == tag){
104 *l = v->link;
105 unlock(&vouslock);
106 return v;
109 v = getvous();
110 v->pid = pid;
111 v->link = voushash[h];
112 v->val = val;
113 v->tag = tag;
114 lock(&v->lk);
115 voushash[h] = v;
116 unlock(&vouslock);
117 return v;
120 #define DBG 0
121 ulong
122 rendezvous(ulong tag, ulong val)
124 int me, vpid;
125 ulong rval;
126 Vous *v;
127 sigset_t mask;
129 me = getpid();
130 v = findvous(tag, val, me);
131 if(v->pid == me){
132 if(DBG)fprint(2, "pid is %d tag %lux, sleeping\n", me, tag);
133 /*
134 * No rendezvous partner was found; the next guy
135 * through will find v and wake us, so we must go
136 * to sleep.
138 * To go to sleep:
139 * 1. disable USR1 signals.
140 * 2. unlock v->lk (tells waker okay to signal us).
141 * 3. atomically suspend and enable USR1 signals.
143 * The call to ignusr1() could be done once at
144 * process creation instead of every time through rendezvous.
145 */
146 v->val = val;
147 ignusr1();
148 sigprocmask(SIG_SETMASK, NULL, &mask);
149 sigaddset(&mask, SIGUSR1);
150 sigprocmask(SIG_SETMASK, &mask, NULL);
151 sigdelset(&mask, SIGUSR1);
152 unlock(&v->lk);
153 sigsuspend(&mask);
154 rval = v->val;
155 if(DBG)fprint(2, "pid is %d, awake\n", me);
156 putvous(v);
157 }else{
158 /*
159 * Found someone to meet. Wake him:
161 * A. lock v->lk (waits for him to get to his step 2)
162 * B. send a USR1
164 * He won't get the USR1 until he suspends, which
165 * means it must wake him up (it can't get delivered
166 * before he sleeps).
167 */
168 vpid = v->pid;
169 lock(&v->lk);
170 rval = v->val;
171 v->val = val;
172 unlock(&v->lk);
173 if(kill(vpid, SIGUSR1) < 0){
174 if(DBG)fprint(2, "pid is %d, kill %d failed: %r\n", me, vpid);
175 abort();
178 return rval;