-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio.c
345 lines (316 loc) · 8.66 KB
/
io.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#define E_NEEDS_STRING
#define E_NEEDS_SOCKET
#define E_NEEDS_SELECT
#define E_NEEDS_FILE
#define E_NEEDS_ERRNO
#include "system.h"
#include "io.h"
#include "iisc.h"
#include "assert.h"
#include "timer.h"
#include "trace.h"
#if !defined(NTRACE)
static char *EIONames[] = {
"EIO_Read",
"EIO_Write",
"EIO_Except"
};
#endif
static fd_set io_sets[3];
static fd_set data_available;
static IISc io_scs[3];
static int nfds = 0;
static struct timeval zerot;
#ifdef FAKE_SELECT
#define N_SELECT_FDS 10
static struct select_info {
fd_set *reads, *writes, *excepts;
struct {
int fd;
EDirection dir;
} select_fds[N_SELECT_FDS];
int n_select_fds;
struct timeval pause;
} *si;
#endif
struct timeval sipause;
/*
* Mark data available. Called back when all we want is to know when to do
* the real read.
*/
static void markDataAvailable(int fd, EDirection d, void *state)
{
assert(d == EIO_Read || d == EIO_Except);
FD_SET((unsigned)fd, &data_available);
}
ssize_t readFromSocket(int d, void *buf, size_t nbytes)
{
return recv(d, buf, nbytes, 0);
}
ssize_t writeToSocket(int d, void *buf, size_t nbytes)
{
return send(d, buf, nbytes, 0);
}
ssize_t readFromSocketN(int d, void *buf, size_t nbytes)
{
ssize_t res, nread = 0;
do {
res = recv(d, (char *)buf + nread, nbytes - nread, 0);
if (res < 0) return res;
if (res == 0) break;
nread += res;
} while (nread < nbytes);
return nread;
}
ssize_t writeToSocketN(int d, void *buf, size_t nbytes)
{
ssize_t res, nwrit = 0;
do {
res = send(d, (char *)buf + nwrit, nbytes - nwrit, 0);
if (res < 0) return res;
nwrit += res;
} while (nwrit < nbytes);
return nwrit;
}
void IOInit(void)
{
EDirection i;
for (i = EIO_Read; i <= EIO_Except; i++) {
io_scs[i] = IIScCreate();
}
}
typedef struct ioinfo {
IoHandler h;
void *state;
struct ioinfo *prev;
} ioinfo;
void setHandler(int fd, IoHandler h, EDirection direction, void *state)
{
ioinfo *ioi = (ioinfo *)vmMalloc(sizeof (*ioi));
FD_SET((unsigned)fd, &io_sets[direction]);
ioi->h = h;
ioi->state = state;
ioi->prev = (struct ioinfo *)IIScLookup(io_scs[direction], fd);
if (IIScIsNIL(ioi->prev)) {
ioi->prev = NULL;
}
IIScInsert(io_scs[direction], fd, (int)ioi);
if (fd >= nfds) nfds = fd + 1;
}
void resetHandler(int fd, EDirection direction)
{
ioinfo *ioi = (ioinfo *)IIScLookup(io_scs[direction], fd);
if (IIScIsNIL(ioi)) return;
if (ioi->prev) {
IIScInsert(io_scs[direction], fd, (int)ioi->prev);
} else {
FD_CLR((unsigned)fd, &io_sets[direction]);
IIScDelete(io_scs[direction], fd);
}
vmFree(ioi);
}
void checkForIO(int wait)
{
EDirection d;
fd_set local[3];
int i, res;
struct timeval *selectpause;
struct timeval pause, then;
local[EIO_Read] = io_sets[EIO_Read];
local[EIO_Write] = io_sets[EIO_Write];
local[EIO_Except] = io_sets[EIO_Except];
if (!wait) {
selectpause = &zerot;
} else {
/*
* Under win32, we don't get a signal when the time expires, so we have
* to make sure not to sleep too long. Under Unix we use SIGALRM, so
* the select will be interrupted.
*/
pause = nextWakeup();
if (!pause.tv_sec && !pause.tv_usec && !sipause.tv_sec && !sipause.tv_usec) {
selectpause = 0;
} else {
TRACE(dist, 8, ("Next wake at %d.%06d", pause.tv_sec, pause.tv_usec));
gettimeofday(&then, 0);
TRACE(dist, 8, ("Then %d.%06d", then.tv_sec, then.tv_usec));
pause = TimeMinus(pause, then);
TRACE(dist, 8, ("Difference = %d.%06d", pause.tv_sec, pause.tv_usec));
if ((sipause.tv_sec || sipause.tv_usec) &&
((!pause.tv_sec && !pause.tv_usec) ||
sipause.tv_sec < pause.tv_sec ||
(sipause.tv_sec == pause.tv_sec &&
sipause.tv_usec < pause.tv_usec))) {
pause = sipause;
}
selectpause = &pause;
}
}
TRACE(dist, 4, ("Select delaying for %d.%06d",
selectpause ? selectpause->tv_sec : -1,
selectpause ? selectpause->tv_usec : 0));
res = real_select(nfds, S_A(local[EIO_Read]), S_A(local[EIO_Write]),
S_A(local[EIO_Except]), selectpause);
TRACE(dist, 4, ("Select returned %d", res));
if (res < 0 && errno != EINTR) {
perror("checkForIO[select]");
} else if (res > 0) {
/*
* This is slow, but works. Consider making it faster.
*/
for (i = 0; res > 0 && i < nfds; i++) {
for (d = EIO_Read; d <= EIO_Except; d++) {
if (FD_ISSET(i, &local[d])) {
ioinfo *ioi = (ioinfo *)IIScLookup(io_scs[d], i);
assert(!IIScIsNIL(ioi));
TRACE(dist, 13, ("Calling back %s on %d",
EIONames[d], i));
ioi->h(i, d, ioi->state);
res--;
}
}
}
}
}
void setupReadBuffer(readBuffer *rb, void *buf, int goal, int acceptless,
ssize_t (*reader)(int, void *, size_t))
{
rb->buffer = buf;
rb->nread = 0;
rb->goal = goal;
rb->acceptless = acceptless;
rb->reader = reader;
}
int tryReading(readBuffer *rb, int s)
{
int res;
TRACE(dist, 9, ("tryReading on %d goal %d", s, rb->goal));
res = rb->reader(s, rb->buffer + rb->nread, rb->goal - rb->nread);
if (res > 0) {
rb->nread += res;
if (rb->nread == rb->goal || (rb->acceptless && rb->nread > 0)) {
res = rb->nread;
} else {
res = 0;
}
} else if ((res < 0 && errno == EINTR) || (res <= 0 && errno == ETIMEDOUT)) {
res = 0;
} else {
TRACE(dist, 1, ("tryReading on %d got %d, errno = %d", s, res, errno));
res = -1;
}
TRACE(dist, 10, ("tryReading on %d returning %d", s, res));
return res;
}
/*
* Do a read, but wait until it can be accomplished without blocking. If it
* tries to block, then just call processEverything until it is available to
* read.
*/
ssize_t io_read(int d, void *buf, size_t nbytes)
{
extern void processEverythingOnce(void);
ssize_t res;
setHandler(d, markDataAvailable, EIO_Read, 0);
setHandler(d, markDataAvailable, EIO_Except, 0);
do {
processEverythingOnce();
} while (!FD_ISSET(d, &data_available));
resetHandler(d, EIO_Read);
resetHandler(d, EIO_Except);
FD_CLR(d, &data_available);
res = read(d, buf, nbytes);
return res;
}
#ifdef FAKE_SELECT
static int selectDone;
static void terminateSelect(int fd, EDirection d, void *state)
{
selectDone++;
FD_SET(fd, d == EIO_Read ? si->reads : d == EIO_Write ? si->writes : si->excepts);
}
static void resetHandlers(struct select_info *si)
{
int i;
for (i = 0; i < si->n_select_fds; i++) {
resetHandler(si->select_fds[i].fd, si->select_fds[i].dir);
}
sipause = si->pause;
}
static void setHandlers(struct select_info *si)
{
int i;
for (i = 0; i < si->n_select_fds; i++) {
setHandler(si->select_fds[i].fd, terminateSelect, si->select_fds[i].dir, 0);
}
}
/*
* Do a select, but wait until it can be accomplished without blocking. If it
* tries to block, then just call processEverything until it is available to
* read.
*/
#if defined(__alpha__) || defined(alpha)
# define ssize_t int
#endif
ssize_t io_select(int nfds, fd_set *reads, fd_set *writes, fd_set *excepts, struct timeval *pause)
{
extern void processEverythingOnce(void);
struct timeval ioselectwake, now;
struct select_info my_si, *o_si;
int i;
TRACE(sys, 3, ("Doing select, nfds = %d, pause = %d.%06d", nfds, pause ? pause->tv_sec : -1, pause ? pause->tv_usec : 0));
my_si.reads = reads;
my_si.writes = writes;
my_si.excepts = excepts;
/*
* This is slow, but works. Consider making it faster.
*/
selectDone = 0;
my_si.n_select_fds = 0;
for (i = 0; i < nfds; i++) {
if (reads && FD_ISSET(i, reads)) {
FD_CLR(i, reads);
setHandler(i, terminateSelect, EIO_Read, 0);
my_si.select_fds[my_si.n_select_fds].fd = i;
my_si.select_fds[my_si.n_select_fds++].dir = EIO_Read;
}
if (writes && FD_ISSET(i, writes)) {
FD_CLR(i, writes);
setHandler(i, terminateSelect, EIO_Write, 0);
my_si.select_fds[my_si.n_select_fds].fd = i;
my_si.select_fds[my_si.n_select_fds++].dir = EIO_Write;
}
if (excepts && FD_ISSET(i, excepts)) {
FD_CLR(i, excepts);
setHandler(i, terminateSelect, EIO_Except, 0);
my_si.select_fds[my_si.n_select_fds].fd = i;
my_si.select_fds[my_si.n_select_fds++].dir = EIO_Except;
}
}
if (pause) {
my_si.pause = *pause;
gettimeofday(&ioselectwake, 0);
ioselectwake = TimePlus(ioselectwake, *pause);
} else {
my_si.pause.tv_sec = my_si.pause.tv_usec = 0;
}
sipause = my_si.pause;
if (si) resetHandlers(si);
o_si = si;
si = &my_si;
do {
processEverythingOnce();
if (!selectDone && pause) {
gettimeofday(&now, 0);
if (TimeLess(ioselectwake, now)) selectDone = 1;
}
} while (!selectDone);
resetHandlers(si);
si = o_si;
if (si) setHandlers(si);
return selectDone;
}
#if defined(__alpha__) || defined(alpha)
# undef ssize_t
#endif
#endif