… | |
… | |
5 | #include "perl.h" |
5 | #include "perl.h" |
6 | #include "XSUB.h" |
6 | #include "XSUB.h" |
7 | |
7 | |
8 | #include "autoconf/config.h" |
8 | #include "autoconf/config.h" |
9 | |
9 | |
|
|
10 | #include <pthread.h> |
|
|
11 | |
|
|
12 | #include <stddef.h> |
|
|
13 | #include <errno.h> |
10 | #include <sys/types.h> |
14 | #include <sys/types.h> |
11 | #include <sys/stat.h> |
15 | #include <sys/stat.h> |
12 | |
16 | #include <limits.h> |
13 | #include <unistd.h> |
17 | #include <unistd.h> |
14 | #include <fcntl.h> |
18 | #include <fcntl.h> |
15 | #include <signal.h> |
19 | #include <signal.h> |
16 | #include <sched.h> |
20 | #include <sched.h> |
17 | |
21 | |
18 | #include <pthread.h> |
22 | #if HAVE_SENDFILE |
|
|
23 | # if __linux |
|
|
24 | # include <sys/sendfile.h> |
|
|
25 | # elif __freebsd |
|
|
26 | # include <sys/socket.h> |
|
|
27 | # include <sys/uio.h> |
|
|
28 | # elif __hpux |
|
|
29 | # include <sys/socket.h> |
|
|
30 | # elif __solaris /* not yet */ |
|
|
31 | # include <sys/sendfile.h> |
|
|
32 | # else |
|
|
33 | # error sendfile support requested but not available |
|
|
34 | # endif |
|
|
35 | #endif |
19 | |
36 | |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
37 | /* used for struct dirent, AIX doesn't provide it */ |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
38 | #ifndef NAME_MAX |
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
39 | # define NAME_MAX 4096 |
|
|
40 | #endif |
23 | |
41 | |
24 | #if __ia64 |
42 | #if __ia64 |
25 | # define STACKSIZE 65536 |
43 | # define STACKSIZE 65536 |
26 | #else |
44 | #else |
27 | # define STACKSIZE 4096 |
45 | # define STACKSIZE 8192 |
28 | #endif |
46 | #endif |
29 | |
47 | |
30 | enum { |
48 | enum { |
31 | REQ_QUIT, |
49 | REQ_QUIT, |
32 | REQ_OPEN, REQ_CLOSE, |
50 | REQ_OPEN, REQ_CLOSE, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
51 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
52 | REQ_SENDFILE, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
53 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
54 | REQ_FSYNC, REQ_FDATASYNC, |
36 | REQ_UNLINK, REQ_RMDIR, |
55 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
37 | REQ_SYMLINK, |
56 | REQ_READDIR, |
|
|
57 | REQ_LINK, REQ_SYMLINK, |
38 | }; |
58 | }; |
39 | |
59 | |
40 | typedef struct aio_cb { |
60 | typedef struct aio_cb { |
41 | struct aio_cb *volatile next; |
61 | struct aio_cb *volatile next; |
42 | |
62 | |
43 | int type; |
63 | int type; |
44 | |
64 | |
|
|
65 | /* should receive a cleanup, with unions */ |
45 | int fd; |
66 | int fd, fd2; |
46 | off_t offset; |
67 | off_t offset; |
47 | size_t length; |
68 | size_t length; |
48 | ssize_t result; |
69 | ssize_t result; |
49 | mode_t mode; /* open */ |
70 | mode_t mode; /* open */ |
50 | int errorno; |
71 | int errorno; |
51 | SV *data, *callback, *fh; |
72 | SV *data, *callback; |
|
|
73 | SV *fh, *fh2; |
52 | void *dataptr, *data2ptr; |
74 | void *dataptr, *data2ptr; |
53 | STRLEN dataoffset; |
75 | STRLEN dataoffset; |
54 | |
76 | |
55 | Stat_t *statdata; |
77 | Stat_t *statdata; |
56 | } aio_cb; |
78 | } aio_cb; |
… | |
… | |
75 | SvREFCNT_dec (req->data); |
97 | SvREFCNT_dec (req->data); |
76 | |
98 | |
77 | if (req->fh) |
99 | if (req->fh) |
78 | SvREFCNT_dec (req->fh); |
100 | SvREFCNT_dec (req->fh); |
79 | |
101 | |
|
|
102 | if (req->fh2) |
|
|
103 | SvREFCNT_dec (req->fh2); |
|
|
104 | |
80 | if (req->statdata) |
105 | if (req->statdata) |
81 | Safefree (req->statdata); |
106 | Safefree (req->statdata); |
82 | |
107 | |
83 | if (req->callback) |
108 | if (req->callback) |
84 | SvREFCNT_dec (req->callback); |
109 | SvREFCNT_dec (req->callback); |
|
|
110 | |
|
|
111 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
112 | free (req->data2ptr); |
85 | |
113 | |
86 | Safefree (req); |
114 | Safefree (req); |
87 | } |
115 | } |
88 | |
116 | |
89 | static void |
117 | static void |
… | |
… | |
154 | PL_statcache = *(req->statdata); |
182 | PL_statcache = *(req->statdata); |
155 | } |
183 | } |
156 | |
184 | |
157 | ENTER; |
185 | ENTER; |
158 | PUSHMARK (SP); |
186 | PUSHMARK (SP); |
159 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
160 | |
187 | |
161 | if (req->type == REQ_OPEN) |
188 | if (req->type == REQ_READDIR) |
162 | { |
189 | { |
|
|
190 | SV *rv = &PL_sv_undef; |
|
|
191 | |
|
|
192 | if (req->result >= 0) |
|
|
193 | { |
|
|
194 | char *buf = req->data2ptr; |
|
|
195 | AV *av = newAV (); |
|
|
196 | |
|
|
197 | while (req->result) |
|
|
198 | { |
|
|
199 | SV *sv = newSVpv (buf, 0); |
|
|
200 | |
|
|
201 | av_push (av, sv); |
|
|
202 | buf += SvCUR (sv) + 1; |
|
|
203 | req->result--; |
|
|
204 | } |
|
|
205 | |
|
|
206 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
207 | } |
|
|
208 | |
|
|
209 | XPUSHs (rv); |
|
|
210 | } |
|
|
211 | else |
|
|
212 | { |
|
|
213 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
214 | |
|
|
215 | if (req->type == REQ_OPEN) |
|
|
216 | { |
163 | /* convert fd to fh */ |
217 | /* convert fd to fh */ |
164 | SV *fh; |
218 | SV *fh; |
165 | |
219 | |
166 | PUTBACK; |
220 | PUTBACK; |
167 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
221 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
168 | SPAGAIN; |
222 | SPAGAIN; |
169 | |
223 | |
170 | fh = SvREFCNT_inc (POPs); |
224 | fh = SvREFCNT_inc (POPs); |
171 | |
225 | |
172 | PUSHMARK (SP); |
226 | PUSHMARK (SP); |
173 | XPUSHs (sv_2mortal (fh)); |
227 | XPUSHs (sv_2mortal (fh)); |
|
|
228 | } |
174 | } |
229 | } |
175 | |
230 | |
176 | if (SvOK (req->callback)) |
231 | if (SvOK (req->callback)) |
177 | { |
232 | { |
178 | PUTBACK; |
233 | PUTBACK; |
… | |
… | |
207 | pthread_t tid; |
262 | pthread_t tid; |
208 | pthread_attr_t attr; |
263 | pthread_attr_t attr; |
209 | |
264 | |
210 | pthread_attr_init (&attr); |
265 | pthread_attr_init (&attr); |
211 | pthread_attr_setstacksize (&attr, STACKSIZE); |
266 | pthread_attr_setstacksize (&attr, STACKSIZE); |
212 | // pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
267 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
213 | |
268 | |
214 | sigfillset (&fullsigset); |
269 | sigfillset (&fullsigset); |
215 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
270 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
216 | |
271 | |
217 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
272 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
… | |
… | |
299 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
354 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
300 | croak ("cannot set result pipe to nonblocking mode"); |
355 | croak ("cannot set result pipe to nonblocking mode"); |
301 | |
356 | |
302 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
357 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
303 | croak ("cannot set result pipe to nonblocking mode"); |
358 | croak ("cannot set result pipe to nonblocking mode"); |
304 | } |
|
|
305 | |
|
|
306 | static void atfork_prepare (void) |
|
|
307 | { |
|
|
308 | pthread_mutex_lock (&reqlock); |
|
|
309 | pthread_mutex_lock (&reslock); |
|
|
310 | } |
|
|
311 | |
|
|
312 | static void atfork_parent (void) |
|
|
313 | { |
|
|
314 | pthread_mutex_unlock (&reslock); |
|
|
315 | pthread_mutex_unlock (&reqlock); |
|
|
316 | } |
|
|
317 | |
|
|
318 | static void atfork_child (void) |
|
|
319 | { |
|
|
320 | aio_req prv; |
|
|
321 | |
|
|
322 | started = 0; |
|
|
323 | |
|
|
324 | while (reqs) |
|
|
325 | { |
|
|
326 | prv = reqs; |
|
|
327 | reqs = prv->next; |
|
|
328 | free_req (prv); |
|
|
329 | } |
|
|
330 | |
|
|
331 | reqs = reqe = 0; |
|
|
332 | |
|
|
333 | while (ress) |
|
|
334 | { |
|
|
335 | prv = ress; |
|
|
336 | ress = prv->next; |
|
|
337 | free_req (prv); |
|
|
338 | } |
|
|
339 | |
|
|
340 | ress = rese = 0; |
|
|
341 | |
|
|
342 | close (respipe [0]); |
|
|
343 | close (respipe [1]); |
|
|
344 | create_pipe (); |
|
|
345 | |
|
|
346 | atfork_parent (); |
|
|
347 | } |
359 | } |
348 | |
360 | |
349 | /*****************************************************************************/ |
361 | /*****************************************************************************/ |
350 | /* work around various missing functions */ |
362 | /* work around various missing functions */ |
351 | |
363 | |
… | |
… | |
356 | /* |
368 | /* |
357 | * make our pread/pwrite safe against themselves, but not against |
369 | * make our pread/pwrite safe against themselves, but not against |
358 | * normal read/write by using a mutex. slows down execution a lot, |
370 | * normal read/write by using a mutex. slows down execution a lot, |
359 | * but that's your problem, not mine. |
371 | * but that's your problem, not mine. |
360 | */ |
372 | */ |
361 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
373 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
362 | |
374 | |
363 | static ssize_t |
375 | static ssize_t |
364 | pread (int fd, void *buf, size_t count, off_t offset) |
376 | pread (int fd, void *buf, size_t count, off_t offset) |
365 | { |
377 | { |
366 | ssize_t res; |
378 | ssize_t res; |
367 | off_t ooffset; |
379 | off_t ooffset; |
368 | |
380 | |
369 | pthread_mutex_lock (&iolock); |
381 | pthread_mutex_lock (&preadwritelock); |
370 | ooffset = lseek (fd, 0, SEEK_CUR); |
382 | ooffset = lseek (fd, 0, SEEK_CUR); |
371 | lseek (fd, offset, SEEK_SET); |
383 | lseek (fd, offset, SEEK_SET); |
372 | res = read (fd, buf, count); |
384 | res = read (fd, buf, count); |
373 | lseek (fd, ooffset, SEEK_SET); |
385 | lseek (fd, ooffset, SEEK_SET); |
374 | pthread_mutex_unlock (&iolock); |
386 | pthread_mutex_unlock (&preadwritelock); |
375 | |
387 | |
376 | return res; |
388 | return res; |
377 | } |
389 | } |
378 | |
390 | |
379 | static ssize_t |
391 | static ssize_t |
380 | pwrite (int fd, void *buf, size_t count, off_t offset) |
392 | pwrite (int fd, void *buf, size_t count, off_t offset) |
381 | { |
393 | { |
382 | ssize_t res; |
394 | ssize_t res; |
383 | off_t ooffset; |
395 | off_t ooffset; |
384 | |
396 | |
385 | pthread_mutex_lock (&iolock); |
397 | pthread_mutex_lock (&preadwritelock); |
386 | ooffset = lseek (fd, 0, SEEK_CUR); |
398 | ooffset = lseek (fd, 0, SEEK_CUR); |
387 | lseek (fd, offset, SEEK_SET); |
399 | lseek (fd, offset, SEEK_SET); |
388 | res = write (fd, buf, count); |
400 | res = write (fd, buf, count); |
389 | lseek (fd, offset, SEEK_SET); |
401 | lseek (fd, offset, SEEK_SET); |
390 | pthread_mutex_unlock (&iolock); |
402 | pthread_mutex_unlock (&preadwritelock); |
391 | |
403 | |
392 | return res; |
404 | return res; |
393 | } |
405 | } |
394 | #endif |
406 | #endif |
395 | |
407 | |
… | |
… | |
398 | #endif |
410 | #endif |
399 | |
411 | |
400 | #if !HAVE_READAHEAD |
412 | #if !HAVE_READAHEAD |
401 | # define readahead aio_readahead |
413 | # define readahead aio_readahead |
402 | |
414 | |
403 | static char readahead_buf[4096]; |
|
|
404 | |
|
|
405 | static ssize_t |
415 | static ssize_t |
406 | readahead (int fd, off_t offset, size_t count) |
416 | readahead (int fd, off_t offset, size_t count) |
407 | { |
417 | { |
|
|
418 | char readahead_buf[4096]; |
|
|
419 | |
408 | while (count > 0) |
420 | while (count > 0) |
409 | { |
421 | { |
410 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
422 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
411 | |
423 | |
412 | pread (fd, readahead_buf, len, offset); |
424 | pread (fd, readahead_buf, len, offset); |
… | |
… | |
415 | } |
427 | } |
416 | |
428 | |
417 | errno = 0; |
429 | errno = 0; |
418 | } |
430 | } |
419 | #endif |
431 | #endif |
|
|
432 | |
|
|
433 | #if !HAVE_READDIR_R |
|
|
434 | # define readdir_r aio_readdir_r |
|
|
435 | |
|
|
436 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
437 | |
|
|
438 | static int |
|
|
439 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
|
|
440 | { |
|
|
441 | struct dirent *e; |
|
|
442 | int errorno; |
|
|
443 | |
|
|
444 | pthread_mutex_lock (&readdirlock); |
|
|
445 | |
|
|
446 | e = readdir (dirp); |
|
|
447 | errorno = errno; |
|
|
448 | |
|
|
449 | if (e) |
|
|
450 | { |
|
|
451 | *res = ent; |
|
|
452 | strcpy (ent->d_name, e->d_name); |
|
|
453 | } |
|
|
454 | else |
|
|
455 | *res = 0; |
|
|
456 | |
|
|
457 | pthread_mutex_unlock (&readdirlock); |
|
|
458 | |
|
|
459 | errno = errorno; |
|
|
460 | return e ? 0 : -1; |
|
|
461 | } |
|
|
462 | #endif |
|
|
463 | |
|
|
464 | /* sendfile always needs emulation */ |
|
|
465 | static ssize_t |
|
|
466 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
|
|
467 | { |
|
|
468 | ssize_t res; |
|
|
469 | |
|
|
470 | if (!count) |
|
|
471 | return 0; |
|
|
472 | |
|
|
473 | #if HAVE_SENDFILE |
|
|
474 | # if __linux |
|
|
475 | res = sendfile (ofd, ifd, &offset, count); |
|
|
476 | |
|
|
477 | # elif __freebsd |
|
|
478 | /* |
|
|
479 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
|
|
480 | * wasted on making it similar to other I/O functions. |
|
|
481 | */ |
|
|
482 | { |
|
|
483 | off_t sbytes; |
|
|
484 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
|
|
485 | |
|
|
486 | if (res < 0 && sbytes) |
|
|
487 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
|
|
488 | res = sbytes; |
|
|
489 | } |
|
|
490 | |
|
|
491 | # elif __hpux |
|
|
492 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
|
|
493 | |
|
|
494 | # elif __solaris |
|
|
495 | { |
|
|
496 | struct sendfilevec vec; |
|
|
497 | size_t sbytes; |
|
|
498 | |
|
|
499 | vec.sfv_fd = ifd; |
|
|
500 | vec.sfv_flag = 0; |
|
|
501 | vec.sfv_off = offset; |
|
|
502 | vec.sfv_len = count; |
|
|
503 | |
|
|
504 | res = sendfilev (ofd, &vec, 1, &sbytes); |
|
|
505 | |
|
|
506 | if (res < 0 && sbytes) |
|
|
507 | res = sbytes; |
|
|
508 | } |
|
|
509 | |
|
|
510 | # endif |
|
|
511 | #else |
|
|
512 | res = -1; |
|
|
513 | errno = ENOSYS; |
|
|
514 | #endif |
|
|
515 | |
|
|
516 | if (res < 0 |
|
|
517 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
|
|
518 | #if __solaris |
|
|
519 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
|
|
520 | #endif |
|
|
521 | ) |
|
|
522 | ) |
|
|
523 | { |
|
|
524 | /* emulate sendfile. this is a major pain in the ass */ |
|
|
525 | char buf[4096]; |
|
|
526 | res = 0; |
|
|
527 | |
|
|
528 | while (count) |
|
|
529 | { |
|
|
530 | ssize_t cnt; |
|
|
531 | |
|
|
532 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
|
|
533 | |
|
|
534 | if (cnt <= 0) |
|
|
535 | { |
|
|
536 | if (cnt && !res) res = -1; |
|
|
537 | break; |
|
|
538 | } |
|
|
539 | |
|
|
540 | cnt = write (ofd, buf, cnt); |
|
|
541 | |
|
|
542 | if (cnt <= 0) |
|
|
543 | { |
|
|
544 | if (cnt && !res) res = -1; |
|
|
545 | break; |
|
|
546 | } |
|
|
547 | |
|
|
548 | offset += cnt; |
|
|
549 | res += cnt; |
|
|
550 | count -= cnt; |
|
|
551 | } |
|
|
552 | } |
|
|
553 | |
|
|
554 | return res; |
|
|
555 | } |
|
|
556 | |
|
|
557 | /* read a full directory */ |
|
|
558 | static int |
|
|
559 | scandir_ (const char *path, void **namesp) |
|
|
560 | { |
|
|
561 | DIR *dirp = opendir (path); |
|
|
562 | union |
|
|
563 | { |
|
|
564 | struct dirent d; |
|
|
565 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
|
|
566 | } u; |
|
|
567 | struct dirent *entp; |
|
|
568 | char *name, *names; |
|
|
569 | int memlen = 4096; |
|
|
570 | int memofs = 0; |
|
|
571 | int res = 0; |
|
|
572 | int errorno; |
|
|
573 | |
|
|
574 | if (!dirp) |
|
|
575 | return -1; |
|
|
576 | |
|
|
577 | names = malloc (memlen); |
|
|
578 | |
|
|
579 | for (;;) |
|
|
580 | { |
|
|
581 | errno = 0, readdir_r (dirp, &u.d, &entp); |
|
|
582 | |
|
|
583 | if (!entp) |
|
|
584 | break; |
|
|
585 | |
|
|
586 | name = entp->d_name; |
|
|
587 | |
|
|
588 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
589 | { |
|
|
590 | int len = strlen (name) + 1; |
|
|
591 | |
|
|
592 | res++; |
|
|
593 | |
|
|
594 | while (memofs + len > memlen) |
|
|
595 | { |
|
|
596 | memlen *= 2; |
|
|
597 | names = realloc (names, memlen); |
|
|
598 | if (!names) |
|
|
599 | break; |
|
|
600 | } |
|
|
601 | |
|
|
602 | memcpy (names + memofs, name, len); |
|
|
603 | memofs += len; |
|
|
604 | } |
|
|
605 | } |
|
|
606 | |
|
|
607 | errorno = errno; |
|
|
608 | closedir (dirp); |
|
|
609 | |
|
|
610 | if (errorno) |
|
|
611 | { |
|
|
612 | free (names); |
|
|
613 | errno = errorno; |
|
|
614 | res = -1; |
|
|
615 | } |
|
|
616 | |
|
|
617 | *namesp = (void *)names; |
|
|
618 | return res; |
|
|
619 | } |
420 | |
620 | |
421 | /*****************************************************************************/ |
621 | /*****************************************************************************/ |
422 | |
622 | |
423 | static void * |
623 | static void * |
424 | aio_proc (void *thr_arg) |
624 | aio_proc (void *thr_arg) |
… | |
… | |
456 | { |
656 | { |
457 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
657 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
458 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
658 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
459 | |
659 | |
460 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
660 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
661 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
461 | |
662 | |
462 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
663 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
463 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
664 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
464 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
665 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
465 | |
666 | |
466 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
667 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
467 | case REQ_CLOSE: req->result = close (req->fd); break; |
668 | case REQ_CLOSE: req->result = close (req->fd); break; |
468 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
669 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
469 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
670 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
671 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
|
|
672 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
470 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
673 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
471 | |
674 | |
472 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
675 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
473 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
676 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
677 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
474 | |
678 | |
475 | case REQ_QUIT: |
679 | case REQ_QUIT: |
476 | break; |
680 | break; |
477 | |
681 | |
478 | default: |
682 | default: |
… | |
… | |
502 | pthread_mutex_unlock (&reslock); |
706 | pthread_mutex_unlock (&reslock); |
503 | } |
707 | } |
504 | while (type != REQ_QUIT); |
708 | while (type != REQ_QUIT); |
505 | |
709 | |
506 | return 0; |
710 | return 0; |
|
|
711 | } |
|
|
712 | |
|
|
713 | /*****************************************************************************/ |
|
|
714 | |
|
|
715 | static void atfork_prepare (void) |
|
|
716 | { |
|
|
717 | pthread_mutex_lock (&reqlock); |
|
|
718 | pthread_mutex_lock (&reslock); |
|
|
719 | #if !HAVE_PREADWRITE |
|
|
720 | pthread_mutex_lock (&preadwritelock); |
|
|
721 | #endif |
|
|
722 | #if !HAVE_READDIR_R |
|
|
723 | pthread_mutex_lock (&readdirlock); |
|
|
724 | #endif |
|
|
725 | } |
|
|
726 | |
|
|
727 | static void atfork_parent (void) |
|
|
728 | { |
|
|
729 | #if !HAVE_READDIR_R |
|
|
730 | pthread_mutex_unlock (&readdirlock); |
|
|
731 | #endif |
|
|
732 | #if !HAVE_PREADWRITE |
|
|
733 | pthread_mutex_unlock (&preadwritelock); |
|
|
734 | #endif |
|
|
735 | pthread_mutex_unlock (&reslock); |
|
|
736 | pthread_mutex_unlock (&reqlock); |
|
|
737 | } |
|
|
738 | |
|
|
739 | static void atfork_child (void) |
|
|
740 | { |
|
|
741 | aio_req prv; |
|
|
742 | |
|
|
743 | started = 0; |
|
|
744 | |
|
|
745 | while (reqs) |
|
|
746 | { |
|
|
747 | prv = reqs; |
|
|
748 | reqs = prv->next; |
|
|
749 | free_req (prv); |
|
|
750 | } |
|
|
751 | |
|
|
752 | reqs = reqe = 0; |
|
|
753 | |
|
|
754 | while (ress) |
|
|
755 | { |
|
|
756 | prv = ress; |
|
|
757 | ress = prv->next; |
|
|
758 | free_req (prv); |
|
|
759 | } |
|
|
760 | |
|
|
761 | ress = rese = 0; |
|
|
762 | |
|
|
763 | close (respipe [0]); |
|
|
764 | close (respipe [1]); |
|
|
765 | create_pipe (); |
|
|
766 | |
|
|
767 | atfork_parent (); |
507 | } |
768 | } |
508 | |
769 | |
509 | #define dREQ \ |
770 | #define dREQ \ |
510 | aio_req req; \ |
771 | aio_req req; \ |
511 | \ |
772 | \ |
… | |
… | |
522 | |
783 | |
523 | PROTOTYPES: ENABLE |
784 | PROTOTYPES: ENABLE |
524 | |
785 | |
525 | BOOT: |
786 | BOOT: |
526 | { |
787 | { |
|
|
788 | HV *stash = gv_stashpv ("IO::AIO", 1); |
|
|
789 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
|
|
790 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
|
|
791 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
|
|
792 | |
527 | create_pipe (); |
793 | create_pipe (); |
528 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
794 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
529 | } |
795 | } |
530 | |
796 | |
531 | void |
797 | void |
532 | min_parallel(nthreads) |
798 | min_parallel (nthreads) |
533 | int nthreads |
799 | int nthreads |
534 | PROTOTYPE: $ |
800 | PROTOTYPE: $ |
535 | |
801 | |
536 | void |
802 | void |
537 | max_parallel(nthreads) |
803 | max_parallel (nthreads) |
538 | int nthreads |
804 | int nthreads |
539 | PROTOTYPE: $ |
805 | PROTOTYPE: $ |
540 | |
806 | |
541 | int |
807 | int |
542 | max_outstanding(nreqs) |
808 | max_outstanding (nreqs) |
543 | int nreqs |
809 | int nreqs |
544 | PROTOTYPE: $ |
810 | PROTOTYPE: $ |
545 | CODE: |
811 | CODE: |
546 | RETVAL = max_outstanding; |
812 | RETVAL = max_outstanding; |
547 | max_outstanding = nreqs; |
813 | max_outstanding = nreqs; |
548 | |
814 | |
549 | void |
815 | void |
550 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
816 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
551 | SV * pathname |
817 | SV * pathname |
552 | int flags |
818 | int flags |
553 | int mode |
819 | int mode |
554 | SV * callback |
820 | SV * callback |
555 | PROTOTYPE: $$$;$ |
821 | PROTOTYPE: $$$;$ |
… | |
… | |
565 | |
831 | |
566 | send_req (req); |
832 | send_req (req); |
567 | } |
833 | } |
568 | |
834 | |
569 | void |
835 | void |
570 | aio_close(fh,callback=&PL_sv_undef) |
836 | aio_close (fh,callback=&PL_sv_undef) |
571 | SV * fh |
837 | SV * fh |
572 | SV * callback |
838 | SV * callback |
573 | PROTOTYPE: $;$ |
839 | PROTOTYPE: $;$ |
574 | ALIAS: |
840 | ALIAS: |
575 | aio_close = REQ_CLOSE |
841 | aio_close = REQ_CLOSE |
… | |
… | |
585 | |
851 | |
586 | send_req (req); |
852 | send_req (req); |
587 | } |
853 | } |
588 | |
854 | |
589 | void |
855 | void |
590 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
856 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
591 | SV * fh |
857 | SV * fh |
592 | UV offset |
858 | UV offset |
593 | IV length |
859 | UV length |
594 | SV * data |
860 | SV * data |
595 | IV dataoffset |
861 | UV dataoffset |
596 | SV * callback |
862 | SV * callback |
597 | ALIAS: |
863 | ALIAS: |
598 | aio_read = REQ_READ |
864 | aio_read = REQ_READ |
599 | aio_write = REQ_WRITE |
865 | aio_write = REQ_WRITE |
600 | PROTOTYPE: $$$$$;$ |
866 | PROTOTYPE: $$$$$;$ |
… | |
… | |
649 | send_req (req); |
915 | send_req (req); |
650 | } |
916 | } |
651 | } |
917 | } |
652 | |
918 | |
653 | void |
919 | void |
|
|
920 | aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
|
|
921 | SV * out_fh |
|
|
922 | SV * in_fh |
|
|
923 | UV in_offset |
|
|
924 | UV length |
|
|
925 | SV * callback |
|
|
926 | PROTOTYPE: $$$$;$ |
|
|
927 | CODE: |
|
|
928 | { |
|
|
929 | dREQ; |
|
|
930 | |
|
|
931 | req->type = REQ_SENDFILE; |
|
|
932 | req->fh = newSVsv (out_fh); |
|
|
933 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
934 | req->fh2 = newSVsv (in_fh); |
|
|
935 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
936 | req->offset = in_offset; |
|
|
937 | req->length = length; |
|
|
938 | |
|
|
939 | send_req (req); |
|
|
940 | } |
|
|
941 | |
|
|
942 | void |
654 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
943 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
655 | SV * fh |
944 | SV * fh |
656 | UV offset |
945 | UV offset |
657 | IV length |
946 | IV length |
658 | SV * callback |
947 | SV * callback |
659 | PROTOTYPE: $$$;$ |
948 | PROTOTYPE: $$$;$ |
… | |
… | |
669 | |
958 | |
670 | send_req (req); |
959 | send_req (req); |
671 | } |
960 | } |
672 | |
961 | |
673 | void |
962 | void |
674 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
963 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
675 | SV * fh_or_path |
964 | SV * fh_or_path |
676 | SV * callback |
965 | SV * callback |
677 | ALIAS: |
966 | ALIAS: |
678 | aio_stat = REQ_STAT |
967 | aio_stat = REQ_STAT |
679 | aio_lstat = REQ_LSTAT |
968 | aio_lstat = REQ_LSTAT |
… | |
… | |
703 | |
992 | |
704 | send_req (req); |
993 | send_req (req); |
705 | } |
994 | } |
706 | |
995 | |
707 | void |
996 | void |
708 | aio_unlink(pathname,callback=&PL_sv_undef) |
997 | aio_unlink (pathname,callback=&PL_sv_undef) |
709 | SV * pathname |
998 | SV * pathname |
710 | SV * callback |
999 | SV * callback |
711 | ALIAS: |
1000 | ALIAS: |
712 | aio_unlink = REQ_UNLINK |
1001 | aio_unlink = REQ_UNLINK |
713 | aio_rmdir = REQ_RMDIR |
1002 | aio_rmdir = REQ_RMDIR |
|
|
1003 | aio_readdir = REQ_READDIR |
714 | CODE: |
1004 | CODE: |
715 | { |
1005 | { |
716 | dREQ; |
1006 | dREQ; |
717 | |
1007 | |
718 | req->type = ix; |
1008 | req->type = ix; |
… | |
… | |
721 | |
1011 | |
722 | send_req (req); |
1012 | send_req (req); |
723 | } |
1013 | } |
724 | |
1014 | |
725 | void |
1015 | void |
726 | aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
1016 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
727 | SV * oldpath |
1017 | SV * oldpath |
728 | SV * newpath |
1018 | SV * newpath |
729 | SV * callback |
1019 | SV * callback |
|
|
1020 | ALIAS: |
|
|
1021 | aio_link = REQ_LINK |
|
|
1022 | aio_symlink = REQ_SYMLINK |
|
|
1023 | aio_rename = REQ_RENAME |
730 | CODE: |
1024 | CODE: |
731 | { |
1025 | { |
732 | dREQ; |
1026 | dREQ; |
733 | |
1027 | |
734 | req->type = REQ_SYMLINK; |
1028 | req->type = ix; |
735 | req->fh = newSVsv (oldpath); |
1029 | req->fh = newSVsv (oldpath); |
736 | req->data2ptr = SvPVbyte_nolen (req->fh); |
1030 | req->data2ptr = SvPVbyte_nolen (req->fh); |
737 | req->data = newSVsv (newpath); |
1031 | req->data = newSVsv (newpath); |
738 | req->dataptr = SvPVbyte_nolen (req->data); |
1032 | req->dataptr = SvPVbyte_nolen (req->data); |
739 | |
1033 | |
740 | send_req (req); |
1034 | send_req (req); |
741 | } |
1035 | } |
742 | |
1036 | |
743 | void |
1037 | void |
744 | flush() |
1038 | flush () |
745 | PROTOTYPE: |
1039 | PROTOTYPE: |
746 | CODE: |
1040 | CODE: |
747 | while (nreqs) |
1041 | while (nreqs) |
748 | { |
1042 | { |
749 | poll_wait (); |
1043 | poll_wait (); |