1 |
root |
1.20 |
#define _REENTRANT 1 |
2 |
root |
1.19 |
#include <errno.h> |
3 |
|
|
|
4 |
root |
1.15 |
#include "EXTERN.h" |
5 |
root |
1.1 |
#include "perl.h" |
6 |
|
|
#include "XSUB.h" |
7 |
|
|
|
8 |
root |
1.16 |
#include "autoconf/config.h" |
9 |
|
|
|
10 |
root |
1.32 |
#include <pthread.h> |
11 |
|
|
|
12 |
root |
1.1 |
#include <sys/types.h> |
13 |
|
|
#include <sys/stat.h> |
14 |
root |
1.10 |
|
15 |
root |
1.1 |
#include <unistd.h> |
16 |
|
|
#include <fcntl.h> |
17 |
|
|
#include <signal.h> |
18 |
|
|
#include <sched.h> |
19 |
|
|
|
20 |
root |
1.32 |
#if HAVE_SENDFILE |
21 |
|
|
# if __linux |
22 |
|
|
# include <sys/sendfile.h> |
23 |
|
|
# elif __freebsd |
24 |
|
|
# include <sys/socket.h> |
25 |
|
|
# include <sys/uio.h> |
26 |
|
|
# elif __hpux |
27 |
|
|
# include <sys/socket.h> |
28 |
|
|
# endif |
29 |
|
|
#endif |
30 |
root |
1.1 |
|
31 |
root |
1.4 |
#if __ia64 |
32 |
|
|
# define STACKSIZE 65536 |
33 |
root |
1.1 |
#else |
34 |
root |
1.4 |
# define STACKSIZE 4096 |
35 |
root |
1.1 |
#endif |
36 |
|
|
|
37 |
|
|
enum { |
38 |
|
|
REQ_QUIT, |
39 |
|
|
REQ_OPEN, REQ_CLOSE, |
40 |
|
|
REQ_READ, REQ_WRITE, REQ_READAHEAD, |
41 |
root |
1.32 |
REQ_SENDFILE, |
42 |
root |
1.22 |
REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
43 |
root |
1.1 |
REQ_FSYNC, REQ_FDATASYNC, |
44 |
root |
1.23 |
REQ_UNLINK, REQ_RMDIR, |
45 |
root |
1.22 |
REQ_SYMLINK, |
46 |
root |
1.1 |
}; |
47 |
|
|
|
48 |
|
|
typedef struct aio_cb { |
49 |
root |
1.3 |
struct aio_cb *volatile next; |
50 |
root |
1.1 |
|
51 |
|
|
int type; |
52 |
|
|
|
53 |
root |
1.32 |
int fd, fd2; |
54 |
root |
1.1 |
off_t offset; |
55 |
|
|
size_t length; |
56 |
|
|
ssize_t result; |
57 |
|
|
mode_t mode; /* open */ |
58 |
|
|
int errorno; |
59 |
root |
1.32 |
SV *data, *callback; |
60 |
|
|
SV *fh, *fh2; |
61 |
root |
1.22 |
void *dataptr, *data2ptr; |
62 |
root |
1.1 |
STRLEN dataoffset; |
63 |
|
|
|
64 |
|
|
Stat_t *statdata; |
65 |
|
|
} aio_cb; |
66 |
|
|
|
67 |
|
|
typedef aio_cb *aio_req; |
68 |
|
|
|
69 |
root |
1.30 |
static int started, wanted; |
70 |
root |
1.11 |
static volatile int nreqs; |
71 |
root |
1.4 |
static int max_outstanding = 1<<30; |
72 |
root |
1.3 |
static int respipe [2]; |
73 |
root |
1.1 |
|
74 |
root |
1.3 |
static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
75 |
|
|
static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
76 |
|
|
static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
77 |
|
|
|
78 |
|
|
static volatile aio_req reqs, reqe; /* queue start, queue end */ |
79 |
|
|
static volatile aio_req ress, rese; /* queue start, queue end */ |
80 |
root |
1.1 |
|
81 |
root |
1.26 |
static void free_req (aio_req req) |
82 |
|
|
{ |
83 |
|
|
if (req->data) |
84 |
|
|
SvREFCNT_dec (req->data); |
85 |
|
|
|
86 |
|
|
if (req->fh) |
87 |
|
|
SvREFCNT_dec (req->fh); |
88 |
|
|
|
89 |
root |
1.32 |
if (req->fh2) |
90 |
|
|
SvREFCNT_dec (req->fh2); |
91 |
|
|
|
92 |
root |
1.27 |
if (req->statdata) |
93 |
root |
1.26 |
Safefree (req->statdata); |
94 |
|
|
|
95 |
|
|
if (req->callback) |
96 |
|
|
SvREFCNT_dec (req->callback); |
97 |
|
|
|
98 |
|
|
Safefree (req); |
99 |
|
|
} |
100 |
|
|
|
101 |
root |
1.1 |
static void |
102 |
|
|
poll_wait () |
103 |
|
|
{ |
104 |
root |
1.11 |
if (nreqs && !ress) |
105 |
|
|
{ |
106 |
|
|
fd_set rfd; |
107 |
|
|
FD_ZERO(&rfd); |
108 |
|
|
FD_SET(respipe [0], &rfd); |
109 |
root |
1.3 |
|
110 |
root |
1.11 |
select (respipe [0] + 1, &rfd, 0, 0, 0); |
111 |
|
|
} |
112 |
root |
1.1 |
} |
113 |
|
|
|
114 |
|
|
static int |
115 |
root |
1.5 |
poll_cb () |
116 |
root |
1.1 |
{ |
117 |
|
|
dSP; |
118 |
|
|
int count = 0; |
119 |
root |
1.24 |
int do_croak = 0; |
120 |
root |
1.27 |
aio_req req; |
121 |
|
|
|
122 |
|
|
for (;;) |
123 |
|
|
{ |
124 |
|
|
pthread_mutex_lock (&reslock); |
125 |
|
|
req = ress; |
126 |
|
|
|
127 |
|
|
if (req) |
128 |
|
|
{ |
129 |
|
|
ress = req->next; |
130 |
root |
1.11 |
|
131 |
root |
1.27 |
if (!ress) |
132 |
|
|
{ |
133 |
|
|
/* read any signals sent by the worker threads */ |
134 |
|
|
char buf [32]; |
135 |
|
|
while (read (respipe [0], buf, 32) == 32) |
136 |
|
|
; |
137 |
root |
1.30 |
|
138 |
|
|
rese = 0; |
139 |
root |
1.27 |
} |
140 |
|
|
} |
141 |
root |
1.1 |
|
142 |
root |
1.27 |
pthread_mutex_unlock (&reslock); |
143 |
root |
1.3 |
|
144 |
root |
1.27 |
if (!req) |
145 |
|
|
break; |
146 |
root |
1.3 |
|
147 |
root |
1.1 |
nreqs--; |
148 |
|
|
|
149 |
|
|
if (req->type == REQ_QUIT) |
150 |
|
|
started--; |
151 |
|
|
else |
152 |
|
|
{ |
153 |
|
|
int errorno = errno; |
154 |
|
|
errno = req->errorno; |
155 |
|
|
|
156 |
|
|
if (req->type == REQ_READ) |
157 |
root |
1.27 |
SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
158 |
root |
1.1 |
|
159 |
root |
1.28 |
if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
160 |
|
|
SvREADONLY_off (req->data); |
161 |
|
|
|
162 |
root |
1.27 |
if (req->statdata) |
163 |
root |
1.1 |
{ |
164 |
|
|
PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
165 |
|
|
PL_laststatval = req->result; |
166 |
|
|
PL_statcache = *(req->statdata); |
167 |
|
|
} |
168 |
|
|
|
169 |
root |
1.13 |
ENTER; |
170 |
root |
1.1 |
PUSHMARK (SP); |
171 |
|
|
XPUSHs (sv_2mortal (newSViv (req->result))); |
172 |
root |
1.2 |
|
173 |
|
|
if (req->type == REQ_OPEN) |
174 |
|
|
{ |
175 |
|
|
/* convert fd to fh */ |
176 |
|
|
SV *fh; |
177 |
|
|
|
178 |
|
|
PUTBACK; |
179 |
|
|
call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
180 |
|
|
SPAGAIN; |
181 |
|
|
|
182 |
root |
1.13 |
fh = SvREFCNT_inc (POPs); |
183 |
root |
1.2 |
|
184 |
|
|
PUSHMARK (SP); |
185 |
root |
1.13 |
XPUSHs (sv_2mortal (fh)); |
186 |
root |
1.2 |
} |
187 |
|
|
|
188 |
root |
1.8 |
if (SvOK (req->callback)) |
189 |
|
|
{ |
190 |
|
|
PUTBACK; |
191 |
|
|
call_sv (req->callback, G_VOID | G_EVAL); |
192 |
|
|
SPAGAIN; |
193 |
root |
1.27 |
|
194 |
|
|
if (SvTRUE (ERRSV)) |
195 |
|
|
{ |
196 |
|
|
free_req (req); |
197 |
|
|
croak (0); |
198 |
|
|
} |
199 |
root |
1.8 |
} |
200 |
root |
1.13 |
|
201 |
root |
1.26 |
LEAVE; |
202 |
|
|
|
203 |
root |
1.1 |
errno = errorno; |
204 |
|
|
count++; |
205 |
|
|
} |
206 |
|
|
|
207 |
root |
1.27 |
free_req (req); |
208 |
root |
1.1 |
} |
209 |
|
|
|
210 |
|
|
return count; |
211 |
|
|
} |
212 |
|
|
|
213 |
root |
1.4 |
static void *aio_proc(void *arg); |
214 |
|
|
|
215 |
|
|
static void |
216 |
|
|
start_thread (void) |
217 |
|
|
{ |
218 |
|
|
sigset_t fullsigset, oldsigset; |
219 |
|
|
pthread_t tid; |
220 |
|
|
pthread_attr_t attr; |
221 |
|
|
|
222 |
|
|
pthread_attr_init (&attr); |
223 |
|
|
pthread_attr_setstacksize (&attr, STACKSIZE); |
224 |
root |
1.31 |
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
225 |
root |
1.4 |
|
226 |
|
|
sigfillset (&fullsigset); |
227 |
|
|
sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
228 |
|
|
|
229 |
|
|
if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
230 |
|
|
started++; |
231 |
|
|
|
232 |
|
|
sigprocmask (SIG_SETMASK, &oldsigset, 0); |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
static void |
236 |
|
|
send_req (aio_req req) |
237 |
|
|
{ |
238 |
root |
1.30 |
while (started < wanted && nreqs >= started) |
239 |
|
|
start_thread (); |
240 |
|
|
|
241 |
root |
1.4 |
nreqs++; |
242 |
|
|
|
243 |
|
|
pthread_mutex_lock (&reqlock); |
244 |
|
|
|
245 |
|
|
req->next = 0; |
246 |
|
|
|
247 |
|
|
if (reqe) |
248 |
|
|
{ |
249 |
|
|
reqe->next = req; |
250 |
|
|
reqe = req; |
251 |
|
|
} |
252 |
|
|
else |
253 |
|
|
reqe = reqs = req; |
254 |
|
|
|
255 |
|
|
pthread_cond_signal (&reqwait); |
256 |
|
|
pthread_mutex_unlock (&reqlock); |
257 |
|
|
|
258 |
root |
1.27 |
if (nreqs > max_outstanding) |
259 |
|
|
for (;;) |
260 |
|
|
{ |
261 |
|
|
poll_cb (); |
262 |
|
|
|
263 |
|
|
if (nreqs <= max_outstanding) |
264 |
|
|
break; |
265 |
|
|
|
266 |
|
|
poll_wait (); |
267 |
|
|
} |
268 |
root |
1.4 |
} |
269 |
|
|
|
270 |
|
|
static void |
271 |
|
|
end_thread (void) |
272 |
|
|
{ |
273 |
|
|
aio_req req; |
274 |
root |
1.26 |
Newz (0, req, 1, aio_cb); |
275 |
root |
1.4 |
req->type = REQ_QUIT; |
276 |
|
|
|
277 |
|
|
send_req (req); |
278 |
|
|
} |
279 |
|
|
|
280 |
root |
1.22 |
static void min_parallel (int nthreads) |
281 |
|
|
{ |
282 |
root |
1.30 |
if (wanted < nthreads) |
283 |
|
|
wanted = nthreads; |
284 |
root |
1.22 |
} |
285 |
|
|
|
286 |
|
|
static void max_parallel (int nthreads) |
287 |
|
|
{ |
288 |
|
|
int cur = started; |
289 |
root |
1.27 |
|
290 |
root |
1.30 |
if (wanted > nthreads) |
291 |
|
|
wanted = nthreads; |
292 |
|
|
|
293 |
|
|
while (cur > wanted) |
294 |
|
|
{ |
295 |
root |
1.22 |
end_thread (); |
296 |
|
|
cur--; |
297 |
|
|
} |
298 |
|
|
|
299 |
root |
1.30 |
while (started > wanted) |
300 |
root |
1.22 |
{ |
301 |
|
|
poll_wait (); |
302 |
|
|
poll_cb (); |
303 |
|
|
} |
304 |
|
|
} |
305 |
|
|
|
306 |
root |
1.26 |
static void create_pipe () |
307 |
root |
1.22 |
{ |
308 |
root |
1.26 |
if (pipe (respipe)) |
309 |
|
|
croak ("unable to initialize result pipe"); |
310 |
root |
1.22 |
|
311 |
root |
1.26 |
if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
312 |
|
|
croak ("cannot set result pipe to nonblocking mode"); |
313 |
root |
1.22 |
|
314 |
root |
1.26 |
if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
315 |
|
|
croak ("cannot set result pipe to nonblocking mode"); |
316 |
|
|
} |
317 |
root |
1.22 |
|
318 |
root |
1.26 |
static void atfork_prepare (void) |
319 |
|
|
{ |
320 |
root |
1.28 |
pthread_mutex_lock (&reqlock); |
321 |
root |
1.22 |
pthread_mutex_lock (&reslock); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
static void atfork_parent (void) |
325 |
|
|
{ |
326 |
|
|
pthread_mutex_unlock (&reslock); |
327 |
root |
1.26 |
pthread_mutex_unlock (&reqlock); |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
static void atfork_child (void) |
331 |
|
|
{ |
332 |
root |
1.28 |
aio_req prv; |
333 |
|
|
|
334 |
root |
1.26 |
started = 0; |
335 |
|
|
|
336 |
root |
1.28 |
while (reqs) |
337 |
|
|
{ |
338 |
|
|
prv = reqs; |
339 |
|
|
reqs = prv->next; |
340 |
|
|
free_req (prv); |
341 |
|
|
} |
342 |
|
|
|
343 |
|
|
reqs = reqe = 0; |
344 |
|
|
|
345 |
|
|
while (ress) |
346 |
|
|
{ |
347 |
|
|
prv = ress; |
348 |
|
|
ress = prv->next; |
349 |
|
|
free_req (prv); |
350 |
|
|
} |
351 |
|
|
|
352 |
|
|
ress = rese = 0; |
353 |
|
|
|
354 |
root |
1.29 |
close (respipe [0]); |
355 |
|
|
close (respipe [1]); |
356 |
|
|
create_pipe (); |
357 |
|
|
|
358 |
root |
1.26 |
atfork_parent (); |
359 |
root |
1.22 |
} |
360 |
|
|
|
361 |
|
|
/*****************************************************************************/ |
362 |
root |
1.17 |
/* work around various missing functions */ |
363 |
|
|
|
364 |
|
|
#if !HAVE_PREADWRITE |
365 |
|
|
# define pread aio_pread |
366 |
|
|
# define pwrite aio_pwrite |
367 |
|
|
|
368 |
|
|
/* |
369 |
|
|
* make our pread/pwrite safe against themselves, but not against |
370 |
|
|
* normal read/write by using a mutex. slows down execution a lot, |
371 |
|
|
* but that's your problem, not mine. |
372 |
|
|
*/ |
373 |
|
|
static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
374 |
|
|
|
375 |
|
|
static ssize_t |
376 |
|
|
pread (int fd, void *buf, size_t count, off_t offset) |
377 |
|
|
{ |
378 |
|
|
ssize_t res; |
379 |
|
|
off_t ooffset; |
380 |
|
|
|
381 |
|
|
pthread_mutex_lock (&iolock); |
382 |
|
|
ooffset = lseek (fd, 0, SEEK_CUR); |
383 |
|
|
lseek (fd, offset, SEEK_SET); |
384 |
|
|
res = read (fd, buf, count); |
385 |
|
|
lseek (fd, ooffset, SEEK_SET); |
386 |
|
|
pthread_mutex_unlock (&iolock); |
387 |
|
|
|
388 |
|
|
return res; |
389 |
|
|
} |
390 |
|
|
|
391 |
|
|
static ssize_t |
392 |
|
|
pwrite (int fd, void *buf, size_t count, off_t offset) |
393 |
|
|
{ |
394 |
|
|
ssize_t res; |
395 |
|
|
off_t ooffset; |
396 |
|
|
|
397 |
|
|
pthread_mutex_lock (&iolock); |
398 |
|
|
ooffset = lseek (fd, 0, SEEK_CUR); |
399 |
|
|
lseek (fd, offset, SEEK_SET); |
400 |
|
|
res = write (fd, buf, count); |
401 |
|
|
lseek (fd, offset, SEEK_SET); |
402 |
|
|
pthread_mutex_unlock (&iolock); |
403 |
|
|
|
404 |
|
|
return res; |
405 |
|
|
} |
406 |
|
|
#endif |
407 |
|
|
|
408 |
|
|
#if !HAVE_FDATASYNC |
409 |
|
|
# define fdatasync fsync |
410 |
|
|
#endif |
411 |
|
|
|
412 |
|
|
#if !HAVE_READAHEAD |
413 |
|
|
# define readahead aio_readahead |
414 |
|
|
|
415 |
|
|
static char readahead_buf[4096]; |
416 |
|
|
|
417 |
|
|
static ssize_t |
418 |
|
|
readahead (int fd, off_t offset, size_t count) |
419 |
|
|
{ |
420 |
|
|
while (count > 0) |
421 |
|
|
{ |
422 |
|
|
size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
423 |
|
|
|
424 |
|
|
pread (fd, readahead_buf, len, offset); |
425 |
|
|
offset += len; |
426 |
|
|
count -= len; |
427 |
|
|
} |
428 |
|
|
|
429 |
|
|
errno = 0; |
430 |
|
|
} |
431 |
|
|
#endif |
432 |
|
|
|
433 |
root |
1.32 |
/* sendfile always needs emulation */ |
434 |
|
|
static ssize_t |
435 |
|
|
sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
436 |
|
|
{ |
437 |
|
|
ssize_t res; |
438 |
|
|
|
439 |
|
|
if (!count) |
440 |
|
|
return 0; |
441 |
|
|
|
442 |
|
|
#if __linux |
443 |
|
|
res = sendfile (ofd, ifd, &offset, count); |
444 |
|
|
|
445 |
|
|
#elif __freebsd |
446 |
|
|
/* |
447 |
|
|
* Of course, the freebsd sendfile is a dire hack with no thoughts |
448 |
|
|
* wasted on making it similar to other i/o functions. |
449 |
|
|
*/ |
450 |
|
|
{ |
451 |
|
|
off_t sbytes; |
452 |
|
|
res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
453 |
|
|
|
454 |
|
|
if (!res && errno == EAGAIN) |
455 |
|
|
res = sbytes; |
456 |
|
|
} |
457 |
|
|
|
458 |
|
|
#elif __hpux |
459 |
|
|
res = sendfile (ofd, ifd, offset, count, 0, 0); |
460 |
|
|
|
461 |
|
|
#else |
462 |
|
|
res = -1; |
463 |
|
|
errno = ENOSYS; |
464 |
|
|
#endif |
465 |
|
|
|
466 |
|
|
if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK)) |
467 |
|
|
{ |
468 |
|
|
/* emulate sendfile. this is a major pain in the ass */ |
469 |
|
|
char *buf = malloc (4096); |
470 |
|
|
res = 0; |
471 |
|
|
|
472 |
|
|
for (;;) |
473 |
|
|
{ |
474 |
|
|
ssize_t cnt; |
475 |
|
|
|
476 |
|
|
cnt = pread (ifd, buf, 4096, offset); |
477 |
|
|
|
478 |
|
|
if (cnt <= 0) |
479 |
|
|
{ |
480 |
|
|
if (cnt && !res) res = -1; |
481 |
|
|
break; |
482 |
|
|
} |
483 |
|
|
|
484 |
|
|
cnt = write (ofd, buf, cnt); |
485 |
|
|
|
486 |
|
|
if (cnt <= 0) |
487 |
|
|
{ |
488 |
|
|
if (cnt && !res) res = -1; |
489 |
|
|
break; |
490 |
|
|
} |
491 |
|
|
|
492 |
|
|
offset += cnt; |
493 |
|
|
res += cnt; |
494 |
|
|
} |
495 |
|
|
|
496 |
|
|
{ |
497 |
|
|
int errorno = errno; |
498 |
|
|
free (buf); |
499 |
|
|
errno = errorno; |
500 |
|
|
} |
501 |
|
|
} |
502 |
|
|
|
503 |
|
|
return res; |
504 |
|
|
} |
505 |
|
|
|
506 |
root |
1.22 |
/*****************************************************************************/ |
507 |
|
|
|
508 |
root |
1.1 |
static void * |
509 |
|
|
aio_proc (void *thr_arg) |
510 |
|
|
{ |
511 |
|
|
aio_req req; |
512 |
root |
1.3 |
int type; |
513 |
root |
1.1 |
|
514 |
root |
1.3 |
do |
515 |
root |
1.1 |
{ |
516 |
root |
1.3 |
pthread_mutex_lock (&reqlock); |
517 |
|
|
|
518 |
|
|
for (;;) |
519 |
|
|
{ |
520 |
|
|
req = reqs; |
521 |
|
|
|
522 |
|
|
if (reqs) |
523 |
|
|
{ |
524 |
|
|
reqs = reqs->next; |
525 |
|
|
if (!reqs) reqe = 0; |
526 |
|
|
} |
527 |
|
|
|
528 |
|
|
if (req) |
529 |
|
|
break; |
530 |
|
|
|
531 |
|
|
pthread_cond_wait (&reqwait, &reqlock); |
532 |
|
|
} |
533 |
|
|
|
534 |
|
|
pthread_mutex_unlock (&reqlock); |
535 |
|
|
|
536 |
root |
1.1 |
errno = 0; /* strictly unnecessary */ |
537 |
|
|
|
538 |
root |
1.3 |
type = req->type; |
539 |
|
|
|
540 |
|
|
switch (type) |
541 |
root |
1.1 |
{ |
542 |
root |
1.10 |
case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
543 |
|
|
case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
544 |
root |
1.16 |
|
545 |
root |
1.1 |
case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
546 |
root |
1.32 |
case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
547 |
root |
1.1 |
|
548 |
|
|
case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
549 |
|
|
case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
550 |
|
|
case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
551 |
|
|
|
552 |
|
|
case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
553 |
|
|
case REQ_CLOSE: req->result = close (req->fd); break; |
554 |
|
|
case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
555 |
root |
1.22 |
case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
556 |
|
|
case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
557 |
root |
1.1 |
|
558 |
root |
1.17 |
case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
559 |
root |
1.1 |
case REQ_FSYNC: req->result = fsync (req->fd); break; |
560 |
|
|
|
561 |
|
|
case REQ_QUIT: |
562 |
root |
1.3 |
break; |
563 |
root |
1.1 |
|
564 |
|
|
default: |
565 |
|
|
req->result = ENOSYS; |
566 |
|
|
break; |
567 |
|
|
} |
568 |
|
|
|
569 |
|
|
req->errorno = errno; |
570 |
root |
1.3 |
|
571 |
|
|
pthread_mutex_lock (&reslock); |
572 |
|
|
|
573 |
|
|
req->next = 0; |
574 |
|
|
|
575 |
|
|
if (rese) |
576 |
|
|
{ |
577 |
|
|
rese->next = req; |
578 |
|
|
rese = req; |
579 |
|
|
} |
580 |
|
|
else |
581 |
|
|
{ |
582 |
|
|
rese = ress = req; |
583 |
|
|
|
584 |
|
|
/* write a dummy byte to the pipe so fh becomes ready */ |
585 |
|
|
write (respipe [1], &respipe, 1); |
586 |
|
|
} |
587 |
|
|
|
588 |
|
|
pthread_mutex_unlock (&reslock); |
589 |
root |
1.1 |
} |
590 |
root |
1.3 |
while (type != REQ_QUIT); |
591 |
root |
1.1 |
|
592 |
|
|
return 0; |
593 |
|
|
} |
594 |
|
|
|
595 |
root |
1.22 |
#define dREQ \ |
596 |
|
|
aio_req req; \ |
597 |
|
|
\ |
598 |
|
|
if (SvOK (callback) && !SvROK (callback)) \ |
599 |
|
|
croak ("clalback must be undef or of reference type"); \ |
600 |
|
|
\ |
601 |
|
|
Newz (0, req, 1, aio_cb); \ |
602 |
|
|
if (!req) \ |
603 |
|
|
croak ("out of memory during aio_req allocation"); \ |
604 |
|
|
\ |
605 |
root |
1.27 |
req->callback = newSVsv (callback); |
606 |
root |
1.22 |
|
607 |
root |
1.1 |
MODULE = IO::AIO PACKAGE = IO::AIO |
608 |
|
|
|
609 |
root |
1.8 |
PROTOTYPES: ENABLE |
610 |
|
|
|
611 |
root |
1.1 |
BOOT: |
612 |
|
|
{ |
613 |
root |
1.26 |
create_pipe (); |
614 |
root |
1.22 |
pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
615 |
root |
1.1 |
} |
616 |
|
|
|
617 |
|
|
void |
618 |
|
|
min_parallel(nthreads) |
619 |
|
|
int nthreads |
620 |
|
|
PROTOTYPE: $ |
621 |
|
|
|
622 |
|
|
void |
623 |
|
|
max_parallel(nthreads) |
624 |
|
|
int nthreads |
625 |
|
|
PROTOTYPE: $ |
626 |
|
|
|
627 |
root |
1.4 |
int |
628 |
|
|
max_outstanding(nreqs) |
629 |
|
|
int nreqs |
630 |
|
|
PROTOTYPE: $ |
631 |
|
|
CODE: |
632 |
|
|
RETVAL = max_outstanding; |
633 |
|
|
max_outstanding = nreqs; |
634 |
|
|
|
635 |
root |
1.1 |
void |
636 |
root |
1.8 |
aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
637 |
root |
1.1 |
SV * pathname |
638 |
|
|
int flags |
639 |
|
|
int mode |
640 |
|
|
SV * callback |
641 |
root |
1.8 |
PROTOTYPE: $$$;$ |
642 |
root |
1.1 |
CODE: |
643 |
|
|
{ |
644 |
root |
1.22 |
dREQ; |
645 |
root |
1.1 |
|
646 |
|
|
req->type = REQ_OPEN; |
647 |
|
|
req->data = newSVsv (pathname); |
648 |
root |
1.22 |
req->dataptr = SvPVbyte_nolen (req->data); |
649 |
root |
1.1 |
req->fd = flags; |
650 |
|
|
req->mode = mode; |
651 |
|
|
|
652 |
|
|
send_req (req); |
653 |
|
|
} |
654 |
|
|
|
655 |
|
|
void |
656 |
root |
1.8 |
aio_close(fh,callback=&PL_sv_undef) |
657 |
root |
1.13 |
SV * fh |
658 |
|
|
SV * callback |
659 |
root |
1.8 |
PROTOTYPE: $;$ |
660 |
root |
1.1 |
ALIAS: |
661 |
|
|
aio_close = REQ_CLOSE |
662 |
|
|
aio_fsync = REQ_FSYNC |
663 |
|
|
aio_fdatasync = REQ_FDATASYNC |
664 |
|
|
CODE: |
665 |
|
|
{ |
666 |
root |
1.22 |
dREQ; |
667 |
root |
1.1 |
|
668 |
|
|
req->type = ix; |
669 |
root |
1.13 |
req->fh = newSVsv (fh); |
670 |
|
|
req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
671 |
root |
1.1 |
|
672 |
|
|
send_req (req); |
673 |
|
|
} |
674 |
|
|
|
675 |
|
|
void |
676 |
root |
1.8 |
aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
677 |
root |
1.13 |
SV * fh |
678 |
|
|
UV offset |
679 |
root |
1.32 |
UV length |
680 |
root |
1.13 |
SV * data |
681 |
root |
1.32 |
UV dataoffset |
682 |
root |
1.13 |
SV * callback |
683 |
|
|
ALIAS: |
684 |
|
|
aio_read = REQ_READ |
685 |
|
|
aio_write = REQ_WRITE |
686 |
root |
1.8 |
PROTOTYPE: $$$$$;$ |
687 |
root |
1.1 |
CODE: |
688 |
root |
1.13 |
{ |
689 |
|
|
aio_req req; |
690 |
|
|
STRLEN svlen; |
691 |
root |
1.21 |
char *svptr = SvPVbyte (data, svlen); |
692 |
root |
1.13 |
|
693 |
|
|
SvUPGRADE (data, SVt_PV); |
694 |
|
|
SvPOK_on (data); |
695 |
root |
1.1 |
|
696 |
root |
1.13 |
if (dataoffset < 0) |
697 |
|
|
dataoffset += svlen; |
698 |
|
|
|
699 |
|
|
if (dataoffset < 0 || dataoffset > svlen) |
700 |
|
|
croak ("data offset outside of string"); |
701 |
|
|
|
702 |
|
|
if (ix == REQ_WRITE) |
703 |
|
|
{ |
704 |
|
|
/* write: check length and adjust. */ |
705 |
|
|
if (length < 0 || length + dataoffset > svlen) |
706 |
|
|
length = svlen - dataoffset; |
707 |
|
|
} |
708 |
|
|
else |
709 |
|
|
{ |
710 |
|
|
/* read: grow scalar as necessary */ |
711 |
|
|
svptr = SvGROW (data, length + dataoffset); |
712 |
|
|
} |
713 |
|
|
|
714 |
|
|
if (length < 0) |
715 |
|
|
croak ("length must not be negative"); |
716 |
|
|
|
717 |
root |
1.22 |
{ |
718 |
|
|
dREQ; |
719 |
root |
1.13 |
|
720 |
root |
1.22 |
req->type = ix; |
721 |
|
|
req->fh = newSVsv (fh); |
722 |
|
|
req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
723 |
|
|
: IoOFP (sv_2io (fh))); |
724 |
|
|
req->offset = offset; |
725 |
|
|
req->length = length; |
726 |
|
|
req->data = SvREFCNT_inc (data); |
727 |
|
|
req->dataptr = (char *)svptr + dataoffset; |
728 |
root |
1.13 |
|
729 |
root |
1.28 |
if (!SvREADONLY (data)) |
730 |
|
|
{ |
731 |
|
|
SvREADONLY_on (data); |
732 |
|
|
req->data2ptr = (void *)data; |
733 |
|
|
} |
734 |
|
|
|
735 |
root |
1.22 |
send_req (req); |
736 |
|
|
} |
737 |
root |
1.13 |
} |
738 |
root |
1.1 |
|
739 |
|
|
void |
740 |
root |
1.32 |
aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
741 |
|
|
SV * out_fh |
742 |
|
|
SV * in_fh |
743 |
|
|
UV in_offset |
744 |
|
|
UV length |
745 |
|
|
SV * callback |
746 |
|
|
PROTOTYPE: $$$$;$ |
747 |
|
|
CODE: |
748 |
|
|
{ |
749 |
|
|
dREQ; |
750 |
|
|
|
751 |
|
|
req->type = REQ_SENDFILE; |
752 |
|
|
req->fh = newSVsv (out_fh); |
753 |
|
|
req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
754 |
|
|
req->fh2 = newSVsv (in_fh); |
755 |
|
|
req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
756 |
|
|
req->offset = in_offset; |
757 |
|
|
req->length = length; |
758 |
|
|
|
759 |
|
|
send_req (req); |
760 |
|
|
} |
761 |
|
|
|
762 |
|
|
void |
763 |
root |
1.8 |
aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
764 |
root |
1.13 |
SV * fh |
765 |
|
|
UV offset |
766 |
|
|
IV length |
767 |
|
|
SV * callback |
768 |
root |
1.8 |
PROTOTYPE: $$$;$ |
769 |
root |
1.1 |
CODE: |
770 |
|
|
{ |
771 |
root |
1.22 |
dREQ; |
772 |
root |
1.1 |
|
773 |
|
|
req->type = REQ_READAHEAD; |
774 |
root |
1.13 |
req->fh = newSVsv (fh); |
775 |
|
|
req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
776 |
root |
1.1 |
req->offset = offset; |
777 |
|
|
req->length = length; |
778 |
|
|
|
779 |
|
|
send_req (req); |
780 |
|
|
} |
781 |
|
|
|
782 |
|
|
void |
783 |
root |
1.8 |
aio_stat(fh_or_path,callback=&PL_sv_undef) |
784 |
root |
1.1 |
SV * fh_or_path |
785 |
|
|
SV * callback |
786 |
|
|
ALIAS: |
787 |
root |
1.8 |
aio_stat = REQ_STAT |
788 |
|
|
aio_lstat = REQ_LSTAT |
789 |
root |
1.1 |
CODE: |
790 |
|
|
{ |
791 |
root |
1.22 |
dREQ; |
792 |
root |
1.1 |
|
793 |
|
|
New (0, req->statdata, 1, Stat_t); |
794 |
|
|
if (!req->statdata) |
795 |
root |
1.27 |
{ |
796 |
|
|
free_req (req); |
797 |
|
|
croak ("out of memory during aio_req->statdata allocation"); |
798 |
|
|
} |
799 |
root |
1.1 |
|
800 |
|
|
if (SvPOK (fh_or_path)) |
801 |
|
|
{ |
802 |
root |
1.8 |
req->type = ix; |
803 |
root |
1.1 |
req->data = newSVsv (fh_or_path); |
804 |
root |
1.22 |
req->dataptr = SvPVbyte_nolen (req->data); |
805 |
root |
1.1 |
} |
806 |
|
|
else |
807 |
|
|
{ |
808 |
|
|
req->type = REQ_FSTAT; |
809 |
root |
1.13 |
req->fh = newSVsv (fh_or_path); |
810 |
root |
1.1 |
req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
811 |
|
|
} |
812 |
|
|
|
813 |
|
|
send_req (req); |
814 |
|
|
} |
815 |
|
|
|
816 |
|
|
void |
817 |
root |
1.8 |
aio_unlink(pathname,callback=&PL_sv_undef) |
818 |
root |
1.1 |
SV * pathname |
819 |
|
|
SV * callback |
820 |
root |
1.22 |
ALIAS: |
821 |
|
|
aio_unlink = REQ_UNLINK |
822 |
|
|
aio_rmdir = REQ_RMDIR |
823 |
root |
1.1 |
CODE: |
824 |
|
|
{ |
825 |
root |
1.22 |
dREQ; |
826 |
root |
1.1 |
|
827 |
root |
1.22 |
req->type = ix; |
828 |
|
|
req->data = newSVsv (pathname); |
829 |
|
|
req->dataptr = SvPVbyte_nolen (req->data); |
830 |
root |
1.1 |
|
831 |
root |
1.22 |
send_req (req); |
832 |
|
|
} |
833 |
|
|
|
834 |
|
|
void |
835 |
|
|
aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
836 |
|
|
SV * oldpath |
837 |
|
|
SV * newpath |
838 |
|
|
SV * callback |
839 |
|
|
CODE: |
840 |
|
|
{ |
841 |
|
|
dREQ; |
842 |
root |
1.1 |
|
843 |
root |
1.22 |
req->type = REQ_SYMLINK; |
844 |
|
|
req->fh = newSVsv (oldpath); |
845 |
|
|
req->data2ptr = SvPVbyte_nolen (req->fh); |
846 |
|
|
req->data = newSVsv (newpath); |
847 |
|
|
req->dataptr = SvPVbyte_nolen (req->data); |
848 |
root |
1.1 |
|
849 |
|
|
send_req (req); |
850 |
|
|
} |
851 |
|
|
|
852 |
root |
1.6 |
void |
853 |
|
|
flush() |
854 |
|
|
PROTOTYPE: |
855 |
|
|
CODE: |
856 |
|
|
while (nreqs) |
857 |
|
|
{ |
858 |
|
|
poll_wait (); |
859 |
|
|
poll_cb (); |
860 |
|
|
} |
861 |
|
|
|
862 |
root |
1.7 |
void |
863 |
|
|
poll() |
864 |
|
|
PROTOTYPE: |
865 |
|
|
CODE: |
866 |
|
|
if (nreqs) |
867 |
|
|
{ |
868 |
|
|
poll_wait (); |
869 |
|
|
poll_cb (); |
870 |
|
|
} |
871 |
|
|
|
872 |
root |
1.1 |
int |
873 |
|
|
poll_fileno() |
874 |
|
|
PROTOTYPE: |
875 |
|
|
CODE: |
876 |
root |
1.3 |
RETVAL = respipe [0]; |
877 |
root |
1.1 |
OUTPUT: |
878 |
|
|
RETVAL |
879 |
|
|
|
880 |
|
|
int |
881 |
|
|
poll_cb(...) |
882 |
|
|
PROTOTYPE: |
883 |
|
|
CODE: |
884 |
root |
1.5 |
RETVAL = poll_cb (); |
885 |
root |
1.1 |
OUTPUT: |
886 |
|
|
RETVAL |
887 |
|
|
|
888 |
|
|
void |
889 |
|
|
poll_wait() |
890 |
|
|
PROTOTYPE: |
891 |
|
|
CODE: |
892 |
root |
1.3 |
if (nreqs) |
893 |
|
|
poll_wait (); |
894 |
root |
1.1 |
|
895 |
|
|
int |
896 |
|
|
nreqs() |
897 |
|
|
PROTOTYPE: |
898 |
|
|
CODE: |
899 |
|
|
RETVAL = nreqs; |
900 |
|
|
OUTPUT: |
901 |
|
|
RETVAL |
902 |
|
|
|