… | |
… | |
4 | #include "EXTERN.h" |
4 | #include "EXTERN.h" |
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 | |
|
|
10 | #include <pthread.h> |
9 | |
11 | |
10 | #include <sys/types.h> |
12 | #include <sys/types.h> |
11 | #include <sys/stat.h> |
13 | #include <sys/stat.h> |
12 | |
14 | |
13 | #include <unistd.h> |
15 | #include <unistd.h> |
14 | #include <fcntl.h> |
16 | #include <fcntl.h> |
15 | #include <signal.h> |
17 | #include <signal.h> |
16 | #include <sched.h> |
18 | #include <sched.h> |
17 | |
19 | |
18 | #include <pthread.h> |
20 | #if HAVE_SENDFILE |
19 | |
21 | # if __linux |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
22 | # include <sys/sendfile.h> |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
23 | # elif __freebsd |
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
24 | # include <sys/socket.h> |
|
|
25 | # include <sys/uio.h> |
|
|
26 | # elif __hpux |
|
|
27 | # include <sys/socket.h> |
|
|
28 | # endif |
|
|
29 | #endif |
23 | |
30 | |
24 | #if __ia64 |
31 | #if __ia64 |
25 | # define STACKSIZE 65536 |
32 | # define STACKSIZE 65536 |
26 | #else |
33 | #else |
27 | # define STACKSIZE 4096 |
34 | # define STACKSIZE 4096 |
… | |
… | |
29 | |
36 | |
30 | enum { |
37 | enum { |
31 | REQ_QUIT, |
38 | REQ_QUIT, |
32 | REQ_OPEN, REQ_CLOSE, |
39 | REQ_OPEN, REQ_CLOSE, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
40 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
41 | REQ_SENDFILE, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
42 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
43 | REQ_FSYNC, REQ_FDATASYNC, |
36 | REQ_UNLINK, REQ_RMDIR, |
44 | REQ_UNLINK, REQ_RMDIR, |
37 | REQ_SYMLINK, |
45 | REQ_SYMLINK, |
38 | }; |
46 | }; |
… | |
… | |
40 | typedef struct aio_cb { |
48 | typedef struct aio_cb { |
41 | struct aio_cb *volatile next; |
49 | struct aio_cb *volatile next; |
42 | |
50 | |
43 | int type; |
51 | int type; |
44 | |
52 | |
45 | int fd; |
53 | int fd, fd2; |
46 | off_t offset; |
54 | off_t offset; |
47 | size_t length; |
55 | size_t length; |
48 | ssize_t result; |
56 | ssize_t result; |
49 | mode_t mode; /* open */ |
57 | mode_t mode; /* open */ |
50 | int errorno; |
58 | int errorno; |
51 | SV *data, *callback, *fh; |
59 | SV *data, *callback; |
|
|
60 | SV *fh, *fh2; |
52 | void *dataptr, *data2ptr; |
61 | void *dataptr, *data2ptr; |
53 | STRLEN dataoffset; |
62 | STRLEN dataoffset; |
54 | |
63 | |
55 | Stat_t *statdata; |
64 | Stat_t *statdata; |
56 | } aio_cb; |
65 | } aio_cb; |
… | |
… | |
74 | if (req->data) |
83 | if (req->data) |
75 | SvREFCNT_dec (req->data); |
84 | SvREFCNT_dec (req->data); |
76 | |
85 | |
77 | if (req->fh) |
86 | if (req->fh) |
78 | SvREFCNT_dec (req->fh); |
87 | SvREFCNT_dec (req->fh); |
|
|
88 | |
|
|
89 | if (req->fh2) |
|
|
90 | SvREFCNT_dec (req->fh2); |
79 | |
91 | |
80 | if (req->statdata) |
92 | if (req->statdata) |
81 | Safefree (req->statdata); |
93 | Safefree (req->statdata); |
82 | |
94 | |
83 | if (req->callback) |
95 | if (req->callback) |
… | |
… | |
416 | |
428 | |
417 | errno = 0; |
429 | errno = 0; |
418 | } |
430 | } |
419 | #endif |
431 | #endif |
420 | |
432 | |
|
|
433 | /* 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 | |
421 | /*****************************************************************************/ |
506 | /*****************************************************************************/ |
422 | |
507 | |
423 | static void * |
508 | static void * |
424 | aio_proc (void *thr_arg) |
509 | aio_proc (void *thr_arg) |
425 | { |
510 | { |
… | |
… | |
456 | { |
541 | { |
457 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
542 | 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; |
543 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
459 | |
544 | |
460 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
545 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
546 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
461 | |
547 | |
462 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
548 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
463 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
549 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
464 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
550 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
465 | |
551 | |
… | |
… | |
588 | |
674 | |
589 | void |
675 | void |
590 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
676 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
591 | SV * fh |
677 | SV * fh |
592 | UV offset |
678 | UV offset |
593 | IV length |
679 | UV length |
594 | SV * data |
680 | SV * data |
595 | IV dataoffset |
681 | UV dataoffset |
596 | SV * callback |
682 | SV * callback |
597 | ALIAS: |
683 | ALIAS: |
598 | aio_read = REQ_READ |
684 | aio_read = REQ_READ |
599 | aio_write = REQ_WRITE |
685 | aio_write = REQ_WRITE |
600 | PROTOTYPE: $$$$$;$ |
686 | PROTOTYPE: $$$$$;$ |
… | |
… | |
646 | req->data2ptr = (void *)data; |
732 | req->data2ptr = (void *)data; |
647 | } |
733 | } |
648 | |
734 | |
649 | send_req (req); |
735 | send_req (req); |
650 | } |
736 | } |
|
|
737 | } |
|
|
738 | |
|
|
739 | void |
|
|
740 | 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); |
651 | } |
760 | } |
652 | |
761 | |
653 | void |
762 | void |
654 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
763 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
655 | SV * fh |
764 | SV * fh |