1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
NAME
rendezvous - user level process synchronization
SYNOPSIS
ulong rendezvous(ulong tag, ulong value)
DESCRIPTION
The rendezvous system call allows two processes to synchro-
nize and exchange a value. In conjunction with the shared
memory system calls (see segattach(2) and fork(2)), it
enables parallel programs to control their scheduling.
Two processes wishing to synchronize call rendezvous with a
common tag, typically an address in memory they share. One
process will arrive at the rendezvous first; it suspends
execution until a second arrives. When a second process
meets the rendezvous the value arguments are exchanged
between the processes and returned as the result of the
respective rendezvous system calls. Both processes are
awakened when the rendezvous succeeds.
The set of tag values which two processes may use to
rendezvous-their tag space-is inherited when a process
forks, unless RFREND is set in the argument to rfork; see
fork(2).
If a rendezvous is interrupted the return value is ~0, so
that value should not be used in normal communication.
* This simulates rendezvous with shared memory, sigsuspend, and SIGUSR1.
*/
#include <u.h>
#include <signal.h>
#include <libc.h>
#define DBG 0
enum
{
VOUSHASH = 257,
};
typedef struct Vous Vous;
struct Vous
{
Vous *link;
int pid;
int wokeup;
ulong tag;
ulong val1; /* value for the sleeper */
ulong val2; /* value for the waker */
};
static void
ign(int x)
{
USED(x);
}
void /*__attribute__((constructor))*/
ignusr1(int restart)
{
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_handler = ign;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGUSR1);
if(restart)
sa.sa_flags = SA_RESTART;
sigaction(SIGUSR1, &sa, nil);
}
static Vous vouspool[2048];
static int nvousused;
static Vous *vousfree;
static Vous *voushash[VOUSHASH];
static Lock vouslock;
static Vous*
getvous(void)
{
Vous *v;
if(vousfree){
v = vousfree;
vousfree = v->link;
}else if(nvousused < nelem(vouspool))
v = &vouspool[nvousused++];
else{
fprint(2, "rendezvous: out of vous!\n");
abort();
}
return v;
}
static void
putvous(Vous *v)
{
v->link = vousfree;
vousfree = v;
}
static Vous*
findvous(ulong tag)
{
int h;
Vous *v, **l;
h = tag%VOUSHASH;
for(l=&voushash[h], v=*l; v; l=&(*l)->link, v=*l){
if(v->tag == tag){
*l = v->link;
v->link = nil;
return v;
}
}
return nil;
}
static Vous*
mkvous(ulong tag)
{
Vous *v;
int h;
h = tag%VOUSHASH;
v = getvous();
v->link = voushash[h];
v->tag = tag;
voushash[h] = v;
return v;
}
ulong
rendezvous(ulong tag, ulong val)
{
int vpid, pid;
ulong rval;
Vous *v;
sigset_t mask;
pid = getpid();
lock(&vouslock);
if((v = findvous(tag)) == nil){
/*
* Go to sleep.
*
* Block USR1, set the handler to interrupt system calls,
* unlock the vouslock so our waker can wake us,
* and then suspend.
*/
v = mkvous(tag);
v->pid = pid;
v->val2 = val;
v->wokeup = 0;
sigprocmask(SIG_SETMASK, nil, &mask);
sigaddset(&mask, SIGUSR1);
sigprocmask(SIG_SETMASK, &mask, nil);
ignusr1(0);
if(DBG) fprint(2, "%d rv(%lux, %lux) -> s\n", pid, tag, val);
unlock(&vouslock);
sigdelset(&mask, SIGUSR1);
sigsuspend(&mask);
/*
* We're awake. Make USR1 not interrupt system calls.
* Were we awakened or interrupted?
*/
ignusr1(1);
lock(&vouslock);
if(v->wokeup){
rval = v->val1;
if(DBG) fprint(2, "%d rv(%lux, %lux) -> g %lux\n", pid, tag, val, rval);
}else{
if(findvous(tag) != v){
fprint(2, "rendezvous: interrupted but not found in hash table\n");
abort();
}
rval = ~(ulong)0;
if(DBG) fprint(2, "%d rv(%lux, %lux) -> g i\n", pid, tag, val);
}
putvous(v);
unlock(&vouslock);
}else{
/*
* Wake up sleeper.
*/
rval = v->val2;
v->val1 = val;
vpid = v->pid;
v->wokeup = 1;
if(DBG) fprint(2, "%d rv(%lux, %lux) -> g %lux, w %d\n", pid, tag, val, rval, vpid);
unlock(&vouslock);
kill(vpid, SIGUSR1);
}
return rval;
}
|