… | |
… | |
3 | #include <errno.h> |
3 | #include <errno.h> |
4 | |
4 | |
5 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
6 | #include "perl.h" |
6 | #include "perl.h" |
7 | #include "XSUB.h" |
7 | #include "XSUB.h" |
|
|
8 | |
|
|
9 | #include "schmorp.h" |
8 | |
10 | |
9 | #include <stddef.h> |
11 | #include <stddef.h> |
10 | #include <stdlib.h> |
12 | #include <stdlib.h> |
11 | #include <errno.h> |
13 | #include <errno.h> |
12 | #include <sys/types.h> |
14 | #include <sys/types.h> |
… | |
… | |
158 | |
160 | |
159 | static SV *on_next_submit; |
161 | static SV *on_next_submit; |
160 | static int next_pri = EIO_PRI_DEFAULT; |
162 | static int next_pri = EIO_PRI_DEFAULT; |
161 | static int max_outstanding; |
163 | static int max_outstanding; |
162 | |
164 | |
163 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
165 | static s_epipe respipe; |
164 | |
166 | |
165 | static void req_destroy (aio_req req); |
167 | static void req_destroy (aio_req req); |
166 | static void req_cancel (aio_req req); |
168 | static void req_cancel (aio_req req); |
167 | |
169 | |
168 | static void want_poll (void) |
170 | static void want_poll (void) |
169 | { |
171 | { |
170 | /* write a dummy byte to the pipe so fh becomes ready */ |
172 | /* write a dummy byte to the pipe so fh becomes ready */ |
171 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
173 | s_epipe_signal (&respipe); |
172 | } |
174 | } |
173 | |
175 | |
174 | static void done_poll (void) |
176 | static void done_poll (void) |
175 | { |
177 | { |
176 | /* read any signals sent by the worker threads */ |
178 | /* read any signals sent by the worker threads */ |
177 | char buf [4]; |
179 | s_epipe_drain (&respipe); |
178 | while (respipe_read (respipe [0], buf, 4) == 4) |
|
|
179 | ; |
|
|
180 | } |
180 | } |
181 | |
181 | |
182 | /* must be called at most once */ |
182 | /* must be called at most once */ |
183 | static SV *req_sv (aio_req req, const char *klass) |
183 | static SV *req_sv (aio_req req, const char *klass) |
184 | { |
184 | { |
… | |
… | |
439 | grp->sv2 = 0; |
439 | grp->sv2 = 0; |
440 | |
440 | |
441 | eio_grp_cancel (grp); |
441 | eio_grp_cancel (grp); |
442 | } |
442 | } |
443 | |
443 | |
444 | #ifdef USE_SOCKETS_AS_HANDLES |
|
|
445 | # define TO_SOCKET(x) (win32_get_osfhandle (x)) |
|
|
446 | #else |
|
|
447 | # define TO_SOCKET(x) (x) |
|
|
448 | #endif |
|
|
449 | |
|
|
450 | static void |
444 | static void |
451 | create_respipe (void) |
445 | create_respipe (void) |
452 | { |
446 | { |
453 | int old_readfd = respipe [0]; |
|
|
454 | |
|
|
455 | if (respipe [1] >= 0) |
|
|
456 | respipe_close (TO_SOCKET (respipe [1])); |
|
|
457 | |
|
|
458 | #ifdef _WIN32 |
|
|
459 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) |
|
|
460 | #else |
|
|
461 | if (pipe (respipe)) |
447 | if (s_epipe_renew (&respipe)) |
462 | #endif |
|
|
463 | croak ("unable to initialize result pipe"); |
448 | croak ("IO::AIO: unable to initialize result pipe"); |
464 | |
|
|
465 | if (old_readfd >= 0) |
|
|
466 | { |
|
|
467 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
|
|
468 | croak ("unable to initialize result pipe(2)"); |
|
|
469 | |
|
|
470 | respipe_close (respipe [0]); |
|
|
471 | respipe [0] = old_readfd; |
|
|
472 | } |
|
|
473 | |
|
|
474 | #ifdef _WIN32 |
|
|
475 | int arg = 1; |
|
|
476 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
|
|
477 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
|
|
478 | #else |
|
|
479 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
|
|
480 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
481 | #endif |
|
|
482 | croak ("unable to initialize result pipe(3)"); |
|
|
483 | |
|
|
484 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
|
|
485 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
|
|
486 | } |
449 | } |
487 | |
450 | |
488 | static void poll_wait (void) |
451 | static void poll_wait (void) |
489 | { |
452 | { |
490 | fd_set rfd; |
|
|
491 | |
|
|
492 | while (eio_nreqs ()) |
453 | while (eio_nreqs ()) |
493 | { |
454 | { |
494 | int size; |
455 | int size; |
495 | |
456 | |
496 | X_LOCK (reslock); |
457 | X_LOCK (reslock); |
… | |
… | |
500 | if (size) |
461 | if (size) |
501 | return; |
462 | return; |
502 | |
463 | |
503 | etp_maybe_start_thread (); |
464 | etp_maybe_start_thread (); |
504 | |
465 | |
505 | FD_ZERO (&rfd); |
466 | s_epipe_wait (&respipe); |
506 | FD_SET (respipe [0], &rfd); |
|
|
507 | |
|
|
508 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
509 | } |
467 | } |
510 | } |
468 | } |
511 | |
469 | |
512 | static int poll_cb (void) |
470 | static int poll_cb (void) |
513 | { |
471 | { |
… | |
… | |
531 | } |
489 | } |
532 | |
490 | |
533 | static SV * |
491 | static SV * |
534 | get_cb (SV *cb_sv) |
492 | get_cb (SV *cb_sv) |
535 | { |
493 | { |
536 | HV *st; |
494 | SvGETMAGIC (cb_sv); |
537 | GV *gvp; |
495 | return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; |
538 | CV *cv; |
|
|
539 | |
|
|
540 | if (!SvOK (cb_sv)) |
|
|
541 | return 0; |
|
|
542 | |
|
|
543 | cv = sv_2cv (cb_sv, &st, &gvp, 0); |
|
|
544 | |
|
|
545 | if (!cv) |
|
|
546 | croak ("IO::AIO callback must be undef or a CODE reference"); |
|
|
547 | |
|
|
548 | return (SV *)cv; |
|
|
549 | } |
496 | } |
550 | |
497 | |
551 | #define dREQ \ |
498 | #define dREQ \ |
552 | SV *cb_cv; \ |
499 | SV *cb_cv; \ |
553 | aio_req req; \ |
500 | aio_req req; \ |
… | |
… | |
568 | req_submit (req); \ |
515 | req_submit (req); \ |
569 | SPAGAIN; \ |
516 | SPAGAIN; \ |
570 | \ |
517 | \ |
571 | if (GIMME_V != G_VOID) \ |
518 | if (GIMME_V != G_VOID) \ |
572 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
519 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
573 | |
|
|
574 | static int |
|
|
575 | extract_fd (SV *fh, int wr) |
|
|
576 | { |
|
|
577 | int fd = PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh))); |
|
|
578 | |
|
|
579 | if (fd < 0) |
|
|
580 | croak ("illegal fh argument, either not an OS file or read/write mode mismatch"); |
|
|
581 | |
|
|
582 | return fd; |
|
|
583 | } |
|
|
584 | |
520 | |
585 | MODULE = IO::AIO PACKAGE = IO::AIO |
521 | MODULE = IO::AIO PACKAGE = IO::AIO |
586 | |
522 | |
587 | PROTOTYPES: ENABLE |
523 | PROTOTYPES: ENABLE |
588 | |
524 | |
… | |
… | |
702 | ALIAS: |
638 | ALIAS: |
703 | aio_fsync = EIO_FSYNC |
639 | aio_fsync = EIO_FSYNC |
704 | aio_fdatasync = EIO_FDATASYNC |
640 | aio_fdatasync = EIO_FDATASYNC |
705 | PPCODE: |
641 | PPCODE: |
706 | { |
642 | { |
707 | int fd = extract_fd (fh, 0); |
643 | int fd = s_fileno_croak (fh, 0); |
708 | dREQ; |
644 | dREQ; |
709 | |
645 | |
710 | req->type = ix; |
646 | req->type = ix; |
711 | req->sv1 = newSVsv (fh); |
647 | req->sv1 = newSVsv (fh); |
712 | req->int1 = fd; |
648 | req->int1 = fd; |
… | |
… | |
717 | void |
653 | void |
718 | aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) |
654 | aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) |
719 | PROTOTYPE: $$$$;$ |
655 | PROTOTYPE: $$$$;$ |
720 | PPCODE: |
656 | PPCODE: |
721 | { |
657 | { |
722 | int fd = extract_fd (fh, 0); |
658 | int fd = s_fileno_croak (fh, 0); |
723 | dREQ; |
659 | dREQ; |
724 | |
660 | |
725 | req->type = EIO_SYNC_FILE_RANGE; |
661 | req->type = EIO_SYNC_FILE_RANGE; |
726 | req->sv1 = newSVsv (fh); |
662 | req->sv1 = newSVsv (fh); |
727 | req->int1 = fd; |
663 | req->int1 = fd; |
… | |
… | |
736 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
672 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
737 | PROTOTYPE: $;$ |
673 | PROTOTYPE: $;$ |
738 | PPCODE: |
674 | PPCODE: |
739 | { |
675 | { |
740 | static int close_pipe = -1; /* dummy fd to close fds via dup2 */ |
676 | static int close_pipe = -1; /* dummy fd to close fds via dup2 */ |
741 | int fd = extract_fd (fh, 0); |
677 | int fd = s_fileno_croak (fh, 0); |
742 | dREQ; |
678 | dREQ; |
743 | |
679 | |
744 | if (close_pipe < 0) |
680 | if (close_pipe < 0) |
745 | { |
681 | { |
746 | int pipefd [2]; |
682 | int pipefd [2]; |
… | |
… | |
768 | aio_write = EIO_WRITE |
704 | aio_write = EIO_WRITE |
769 | PROTOTYPE: $$$$$;$ |
705 | PROTOTYPE: $$$$$;$ |
770 | PPCODE: |
706 | PPCODE: |
771 | { |
707 | { |
772 | STRLEN svlen; |
708 | STRLEN svlen; |
773 | int fd = extract_fd (fh, ix == EIO_WRITE); |
709 | int fd = s_fileno_croak (fh, ix == EIO_WRITE); |
774 | char *svptr = SvPVbyte (data, svlen); |
710 | char *svptr = SvPVbyte (data, svlen); |
775 | UV len = SvUV (length); |
711 | UV len = SvUV (length); |
776 | |
712 | |
777 | if (dataoffset < 0) |
713 | if (dataoffset < 0) |
778 | dataoffset += svlen; |
714 | dataoffset += svlen; |
… | |
… | |
833 | void |
769 | void |
834 | aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) |
770 | aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) |
835 | PROTOTYPE: $$$$;$ |
771 | PROTOTYPE: $$$$;$ |
836 | PPCODE: |
772 | PPCODE: |
837 | { |
773 | { |
838 | int ifd = extract_fd (in_fh , 0); |
774 | int ifd = s_fileno_croak (in_fh , 0); |
839 | int ofd = extract_fd (out_fh, 0); |
775 | int ofd = s_fileno_croak (out_fh, 1); |
840 | dREQ; |
776 | dREQ; |
841 | |
777 | |
842 | req->type = EIO_SENDFILE; |
778 | req->type = EIO_SENDFILE; |
843 | req->sv1 = newSVsv (out_fh); |
779 | req->sv1 = newSVsv (out_fh); |
844 | req->int1 = ofd; |
780 | req->int1 = ofd; |
… | |
… | |
853 | void |
789 | void |
854 | aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) |
790 | aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) |
855 | PROTOTYPE: $$$;$ |
791 | PROTOTYPE: $$$;$ |
856 | PPCODE: |
792 | PPCODE: |
857 | { |
793 | { |
858 | int fd = extract_fd (fh, 0); |
794 | int fd = s_fileno_croak (fh, 0); |
859 | dREQ; |
795 | dREQ; |
860 | |
796 | |
861 | req->type = EIO_READAHEAD; |
797 | req->type = EIO_READAHEAD; |
862 | req->sv1 = newSVsv (fh); |
798 | req->sv1 = newSVsv (fh); |
863 | req->int1 = fd; |
799 | req->int1 = fd; |
… | |
… | |
1139 | |
1075 | |
1140 | int |
1076 | int |
1141 | poll_fileno() |
1077 | poll_fileno() |
1142 | PROTOTYPE: |
1078 | PROTOTYPE: |
1143 | CODE: |
1079 | CODE: |
1144 | RETVAL = respipe [0]; |
1080 | RETVAL = s_epipe_fd (&respipe); |
1145 | OUTPUT: |
1081 | OUTPUT: |
1146 | RETVAL |
1082 | RETVAL |
1147 | |
1083 | |
1148 | int |
1084 | int |
1149 | poll_cb(...) |
1085 | poll_cb(...) |
… | |
… | |
1193 | |
1129 | |
1194 | int |
1130 | int |
1195 | fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) |
1131 | fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) |
1196 | PROTOTYPE: $$$$ |
1132 | PROTOTYPE: $$$$ |
1197 | CODE: |
1133 | CODE: |
1198 | { |
|
|
1199 | #if _XOPEN_SOURCE >= 600 && !NO_FADVISE |
1134 | #if _XOPEN_SOURCE >= 600 && !NO_FADVISE |
1200 | RETVAL = posix_fadvise (fh, offset, length, advice); |
1135 | RETVAL = posix_fadvise (fh, offset, length, advice); |
1201 | #else |
1136 | #else |
1202 | RETVAL = errno = ENOSYS; |
1137 | RETVAL = errno = ENOSYS; |
1203 | #endif |
1138 | #endif |
1204 | } |
|
|
1205 | OUTPUT: |
1139 | OUTPUT: |
1206 | RETVAL |
1140 | RETVAL |
1207 | |
1141 | |
1208 | ssize_t |
1142 | ssize_t |
1209 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
1143 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
… | |
… | |
1232 | if (GIMME_V != G_VOID) |
1166 | if (GIMME_V != G_VOID) |
1233 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
1167 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
1234 | |
1168 | |
1235 | if (items > 1) |
1169 | if (items > 1) |
1236 | { |
1170 | { |
1237 | SV *cb_cv = get_cb (callback); |
1171 | SV *cb_cv =get_cb (callback); |
1238 | |
1172 | |
1239 | SvREFCNT_dec (req->callback); |
1173 | SvREFCNT_dec (req->callback); |
1240 | req->callback = SvREFCNT_inc (cb_cv); |
1174 | req->callback = SvREFCNT_inc (cb_cv); |
1241 | } |
1175 | } |
1242 | } |
1176 | } |