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 <u.h>
35 typedef u32int u32;
36 #include <errno.h>
37 #include <sys/time.h>
38 #define __user
39 #include <linux/linkage.h>
40 #include <linux/futex.h>
41 #include <libc.h>
43 enum
44 {
45 VOUSHASH = 257,
46 };
48 typedef struct Vous Vous;
49 struct Vous
50 {
51 Vous *link;
52 Lock lk;
53 int pid;
54 ulong val;
55 ulong tag;
56 };
58 static Vous vouspool[2048];
59 static int nvousused;
60 static Vous *vousfree;
61 static Vous *voushash[VOUSHASH];
62 static Lock vouslock;
64 static Vous*
65 getvous(void)
66 {
67 Vous *v;
69 if(vousfree){
70 v = vousfree;
71 vousfree = v->link;
72 }else if(nvousused < nelem(vouspool))
73 v = &vouspool[nvousused++];
74 else
75 abort();
76 return v;
77 }
79 static void
80 putvous(Vous *v)
81 {
82 lock(&vouslock);
83 v->link = vousfree;
84 vousfree = v;
85 unlock(&vouslock);
86 }
88 static Vous*
89 findvous(ulong tag, ulong val, int pid)
90 {
91 int h;
92 Vous *v, **l;
94 lock(&vouslock);
95 h = tag%VOUSHASH;
96 for(l=&voushash[h], v=*l; v; l=&(*l)->link, v=*l){
97 if(v->tag == tag){
98 *l = v->link;
99 unlock(&vouslock);
100 return v;
103 v = getvous();
104 v->pid = pid;
105 v->link = voushash[h];
106 v->val = val;
107 v->tag = tag;
108 lock(&v->lk);
109 voushash[h] = v;
110 unlock(&vouslock);
111 return v;
114 #define DBG 0
115 ulong
116 rendezvous(ulong tag, ulong val)
118 int me, vpid;
119 ulong rval;
120 Vous *v;
122 me = getpid();
123 v = findvous(tag, val, me);
124 if(v->pid == me){
125 if(DBG)fprint(2, "pid is %d tag %lux, sleeping\n", me, tag);
126 /*
127 * No rendezvous partner was found; the next guy
128 * through will find v and wake us, so we must go
129 * to sleep.
131 * To go to sleep:
132 * 1. disable USR1 signals.
133 * 2. unlock v->lk (tells waker okay to signal us).
134 * 3. atomically suspend and enable USR1 signals.
136 * The call to ignusr1() could be done once at
137 * process creation instead of every time through rendezvous.
138 */
139 v->val = val;
140 unlock(&v->lk);
141 while(sys_futex((u32int*)&v->tag, FUTEX_WAIT, tag, nil, nil) < 0 && errno==EINTR)
143 rval = v->val;
144 if(DBG)fprint(2, "pid is %d, awake\n", me);
145 putvous(v);
146 }else{
147 /*
148 * Found someone to meet. Wake him:
150 * A. lock v->lk (waits for him to get to his step 2)
151 * B. send a USR1
153 * He won't get the USR1 until he suspends, which
154 * means it must wake him up (it can't get delivered
155 * before he sleeps).
156 */
157 vpid = v->pid;
158 lock(&v->lk);
159 rval = v->val;
160 v->val = val;
161 v->tag++;
162 unlock(&v->lk);
163 sys_futex((u32int*)&v->tag, FUTEX_WAKE, 1, nil, nil);
165 return rval;