-
Notifications
You must be signed in to change notification settings - Fork 65
/
sqExternalSemaphores.c
271 lines (229 loc) · 7.86 KB
/
sqExternalSemaphores.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* sqExternalSemaphores.c
* Cross-platform thread-safe external semaphore signalling.
*
* Authors: Eliot Miranda & Brad Fowlow & Pablo Tesone & Guillermo Polito
*
* Copyright (c) 2020 Pharo Consortium.
* Copyright (c) 2013 3D Immersive Collaboration Consulting, LLC.
*
* All rights reserved.
*
* This file is part of Squeak.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "sq.h"
#include "sqAssert.h"
#include "sqAtomicOps.h"
#include "sqMemoryFence.h"
#include "pharovm/semaphores/platformSemaphore.h"
#include "pharovm/interpreter.h"
#if !defined(_WIN32)
#include <signal.h>
#endif
#if !COGMTVM
sqOSThread ioVMThread; /* initialized in the various <plat>/vm/sqFooMain.c */
#endif
typedef struct {
int requests;
int responses;
} SignalRequest;
/* We would like to use something like the following for the tides
typedef int semidx_t;
* but apparently
#define MaxTide ((unsigned semidx_t)-1 >> 1)
* is not legal; at least clang complains of mismatched parentheses.
*/
static SignalRequest *signalRequests = 0;
static int numSignalRequests = 0;
static volatile sqInt checkSignalRequests;
Semaphore* requestMutex;
/* The tide marks define the minimum range of indices into signalRequests that
* the VM needs to scan. With potentially thousands of indices to scan this can
* save significant lengths of time. Every time the VM responds to requests in
* doSignalExternalSemaphores it switches tides by resetting the tides not in
* use to an empty interval, toggling useTideA, and reading the tides in use
* prior to the toggle. Hence initialize the tides to an empty interval.
*/
#define MaxTide ((unsigned int)-1 >> 1)
#define MinTide -1
static volatile char useTideA = 1;
static volatile int lowTideA = MaxTide, highTideA = MinTide;
static volatile int lowTideB = MaxTide, highTideB = MinTide;
#define max(a,b) (a > b ? a : b)
#define min(a,b) (a < b ? a : b)
int
ioGetMaxExtSemTableSize(void) { return numSignalRequests; }
int highBit(int);
/* Setting this at any time other than start-up can potentially lose requests.
* i.e. during the realloc new storage is allocated, the old contents are copied
* and then pointersd are switched. Requests occurring during copying won't
* be seen if they occur to indices already copied.
* We could make this safer in the linux case by disabling interrupts, but
* there is little point. The intended use is to set the table to some adequate
* maximum at start-up and avoid locking altogether.
*/
void
ioSetMaxExtSemTableSize(int n)
{
#if COGMTVM
/* initialization is a little different in MT. Hack around assert for now */
if (getVMOSThread())
#endif
if (numSignalRequests)
assert(ioOSThreadsEqual(ioCurrentOSThread(),getVMOSThread()));
if (numSignalRequests < n) {
int sz = 1 << highBit(n-1);
assert(sz >= n);
signalRequests = realloc(signalRequests, sz * sizeof(SignalRequest));
memset(signalRequests + numSignalRequests,
0,
(sz - numSignalRequests) * sizeof(SignalRequest));
numSignalRequests = sz;
}
}
void
ioInitExternalSemaphores(void)
{
ioSetMaxExtSemTableSize(INITIAL_EXT_SEM_TABLE_SIZE);
requestMutex = platform_semaphore_new(1);
}
// This is defined here as there is no common interface for unix AIO and windows AIO
void aioInterruptPoll();
/* Signal the external semaphore with the given index. Answer non-zero on
* success, zero otherwise. This function is (should be) thread-safe;
* multiple threads may attempt to signal the same semaphore without error.
* An index of zero should be and is silently ignored.
*/
sqInt
signalSemaphoreWithIndex(sqInt index)
{
int i = index - 1;
int v;
#if !defined(_WIN32)
sigset_t blockedSignalSet;
sigemptyset(&blockedSignalSet);
sigaddset(&blockedSignalSet, SIGCHLD);
sigaddset(&blockedSignalSet, SIGINT);
sigaddset(&blockedSignalSet, SIGSTOP);
sigaddset(&blockedSignalSet, SIGTSTP);
#endif
/* An index of zero should be and is silently ignored. */
assert(index >= 0 && index <= numSignalRequests);
if ((unsigned)i >= numSignalRequests)
return 0;
#if !defined(_WIN32)
sigprocmask(SIG_BLOCK, &blockedSignalSet, NULL);
#endif
requestMutex->wait(requestMutex);
sqLowLevelMFence();
signalRequests[i].requests += 1;
if (useTideA) {
if(lowTideA > i) lowTideA = i;
if (highTideA < i) highTideA = i;
} else {
if (lowTideB > i) lowTideB = i;
if (highTideB < i) highTideB = i;
}
checkSignalRequests = 1;
forceInterruptCheck();
requestMutex->signal(requestMutex);
#if !defined(_WIN32)
sigprocmask(SIG_UNBLOCK, &blockedSignalSet, NULL);
#endif
aioInterruptPoll();
return 1;
}
int isPendingSemaphores(){
return checkSignalRequests;
}
/* Signal any external semaphores for which signal requests exist.
* Answer whether a context switch occurred.
* Note we no longer ensure the lock table has at least minTableSize elements.
* Instead its size is settable at startup from a value in the image header via
* ioSetMaxExtSemTableSize. This avoids locks on the table while it grows
* (although a lock-free implementation is possible, if tricky). So for the
* moment externalSemaphoreTableSize is not used.
*/
sqInt
doSignalExternalSemaphores(sqInt externalSemaphoreTableSize)
{
volatile int i, lowTide, highTide;
char switched, signalled = 0;
#if !defined(_WIN32)
sigset_t blockedSignalSet;
sigemptyset(&blockedSignalSet);
sigaddset(&blockedSignalSet, SIGCHLD);
sigaddset(&blockedSignalSet, SIGINT);
sigaddset(&blockedSignalSet, SIGSTOP);
sigaddset(&blockedSignalSet, SIGTSTP);
sigprocmask(SIG_BLOCK, &blockedSignalSet, NULL);
#endif
requestMutex->wait(requestMutex);
sqLowLevelMFence();
if (!checkSignalRequests){
requestMutex->signal(requestMutex);
#if !defined(_WIN32)
sigprocmask(SIG_UNBLOCK, &blockedSignalSet, NULL);
#endif
return 0;
}
switched = 0;
checkSignalRequests = 0;
sqLowLevelMFence();
if (useTideA) {
lowTideB = MaxTide;
highTideB = MinTide;
useTideA = 0;
sqLowLevelMFence();
lowTide = lowTideA;
highTide = highTideA;
}
else {
lowTideA = MaxTide;
highTideA = MinTide;
useTideA = 1;
sqLowLevelMFence();
lowTide = lowTideB;
highTide = highTideB;
}
sqLowLevelMFence();
/* doing this here saves a bounds check in doSignalSemaphoreWithIndex */
if (highTide >= externalSemaphoreTableSize)
highTide = externalSemaphoreTableSize - 1;
for (i = lowTide; i <= highTide; i++)
while (signalRequests[i].responses != signalRequests[i].requests) {
if (doSignalSemaphoreWithIndex(i+1))
switched = 1;
++signalRequests[i].responses;
signalled = 1;
}
requestMutex->signal(requestMutex);
#if !defined(_WIN32)
sigprocmask(SIG_UNBLOCK, &blockedSignalSet, NULL);
#endif
return switched;
}
#define ExcessSignalsIndex 2
void waitOnExternalSemaphoreIndex(sqInt semaphoreIndex){
sqInt aSemaphoreOop;
sqInt xArray;
aSemaphoreOop = getExternalSemaphoreWithIndex(semaphoreIndex);
doWaitSemaphore(aSemaphoreOop);
}