… | |
… | |
99 | #define expect_false(expr) expect ((expr) != 0, 0) |
99 | #define expect_false(expr) expect ((expr) != 0, 0) |
100 | #define expect_true(expr) expect ((expr) != 0, 1) |
100 | #define expect_true(expr) expect ((expr) != 0, 1) |
101 | |
101 | |
102 | /*****************************************************************************/ |
102 | /*****************************************************************************/ |
103 | |
103 | |
104 | static HV *stash; |
|
|
105 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
104 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
106 | typedef int aio_rfd; /* read file desriptor */ |
105 | typedef int aio_rfd; /* read file desriptor */ |
107 | typedef int aio_wfd; /* write file descriptor */ |
106 | typedef int aio_wfd; /* write file descriptor */ |
108 | |
107 | |
109 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
108 | static HV *aio_stash, *aio_req_stash, *aio_grp_stash; |
110 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
|
|
111 | |
109 | |
112 | #define EIO_REQ_MEMBERS \ |
110 | #define EIO_REQ_MEMBERS \ |
113 | SV *callback; \ |
111 | SV *callback; \ |
114 | SV *sv1, *sv2; \ |
112 | SV *sv1, *sv2; \ |
115 | STRLEN stroffset; \ |
113 | STRLEN stroffset; \ |
… | |
… | |
160 | |
158 | |
161 | static SV *on_next_submit; |
159 | static SV *on_next_submit; |
162 | static int next_pri = EIO_PRI_DEFAULT; |
160 | static int next_pri = EIO_PRI_DEFAULT; |
163 | static int max_outstanding; |
161 | static int max_outstanding; |
164 | |
162 | |
165 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
163 | static s_epipe respipe; |
166 | |
164 | |
167 | static void req_destroy (aio_req req); |
165 | static void req_destroy (aio_req req); |
168 | static void req_cancel (aio_req req); |
166 | static void req_cancel (aio_req req); |
169 | |
167 | |
170 | static void want_poll (void) |
168 | static void want_poll (void) |
171 | { |
169 | { |
172 | /* write a dummy byte to the pipe so fh becomes ready */ |
170 | /* write a dummy byte to the pipe so fh becomes ready */ |
173 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
171 | s_epipe_signal (&respipe); |
174 | } |
172 | } |
175 | |
173 | |
176 | static void done_poll (void) |
174 | static void done_poll (void) |
177 | { |
175 | { |
178 | /* read any signals sent by the worker threads */ |
176 | /* read any signals sent by the worker threads */ |
179 | char buf [4]; |
177 | s_epipe_drain (&respipe); |
180 | while (respipe_read (respipe [0], buf, 4) == 4) |
|
|
181 | ; |
|
|
182 | } |
178 | } |
183 | |
179 | |
184 | /* must be called at most once */ |
180 | /* must be called at most once */ |
185 | static SV *req_sv (aio_req req, const char *klass) |
181 | static SV *req_sv (aio_req req, HV *stash) |
186 | { |
182 | { |
187 | if (!req->self) |
183 | if (!req->self) |
188 | { |
184 | { |
189 | req->self = (SV *)newHV (); |
185 | req->self = (SV *)newHV (); |
190 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
186 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
191 | } |
187 | } |
192 | |
188 | |
193 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
189 | return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); |
194 | } |
190 | } |
195 | |
191 | |
196 | static aio_req SvAIO_REQ (SV *sv) |
192 | static aio_req SvAIO_REQ (SV *sv) |
197 | { |
193 | { |
198 | MAGIC *mg; |
194 | MAGIC *mg; |
199 | |
195 | |
200 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
196 | if (!SvROK (sv) |
|
|
197 | || (SvSTASH (SvRV (sv)) != aio_grp_stash |
|
|
198 | && SvSTASH (SvRV (sv)) != aio_req_stash |
|
|
199 | && !sv_derived_from (sv, "IO::AIO::REQ"))) |
201 | croak ("object of class " AIO_REQ_KLASS " expected"); |
200 | croak ("object of class IO::AIO::REQ expected"); |
202 | |
201 | |
203 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
202 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
204 | |
203 | |
205 | return mg ? (aio_req)mg->mg_ptr : 0; |
204 | return mg ? (aio_req)mg->mg_ptr : 0; |
206 | } |
205 | } |
… | |
… | |
212 | dSP; |
211 | dSP; |
213 | |
212 | |
214 | ENTER; |
213 | ENTER; |
215 | SAVETMPS; |
214 | SAVETMPS; |
216 | PUSHMARK (SP); |
215 | PUSHMARK (SP); |
217 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
216 | XPUSHs (req_sv (grp, aio_grp_stash)); |
218 | PUTBACK; |
217 | PUTBACK; |
219 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
218 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
220 | SPAGAIN; |
219 | SPAGAIN; |
221 | FREETMPS; |
220 | FREETMPS; |
222 | LEAVE; |
221 | LEAVE; |
… | |
… | |
328 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
327 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
329 | char sym [64]; |
328 | char sym [64]; |
330 | int symlen; |
329 | int symlen; |
331 | |
330 | |
332 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
331 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
333 | gv_init (gv, stash, sym, symlen, 0); |
332 | gv_init (gv, aio_stash, sym, symlen, 0); |
334 | |
333 | |
335 | symlen = snprintf ( |
334 | symlen = snprintf ( |
336 | sym, |
335 | sym, |
337 | sizeof (sym), |
336 | sizeof (sym), |
338 | "%s&=%d", |
337 | "%s&=%d", |
… | |
… | |
441 | grp->sv2 = 0; |
440 | grp->sv2 = 0; |
442 | |
441 | |
443 | eio_grp_cancel (grp); |
442 | eio_grp_cancel (grp); |
444 | } |
443 | } |
445 | |
444 | |
446 | #ifdef USE_SOCKETS_AS_HANDLES |
|
|
447 | # define TO_SOCKET(x) (win32_get_osfhandle (x)) |
|
|
448 | #else |
|
|
449 | # define TO_SOCKET(x) (x) |
|
|
450 | #endif |
|
|
451 | |
|
|
452 | static void |
445 | static void |
453 | create_respipe (void) |
446 | create_respipe (void) |
454 | { |
447 | { |
455 | int old_readfd = respipe [0]; |
|
|
456 | |
|
|
457 | if (respipe [1] >= 0) |
|
|
458 | respipe_close (TO_SOCKET (respipe [1])); |
|
|
459 | |
|
|
460 | #ifdef _WIN32 |
|
|
461 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) |
|
|
462 | #else |
|
|
463 | if (pipe (respipe)) |
448 | if (s_epipe_renew (&respipe)) |
464 | #endif |
|
|
465 | croak ("unable to initialize result pipe"); |
449 | croak ("IO::AIO: unable to initialize result pipe"); |
466 | |
|
|
467 | if (old_readfd >= 0) |
|
|
468 | { |
|
|
469 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
|
|
470 | croak ("unable to initialize result pipe(2)"); |
|
|
471 | |
|
|
472 | respipe_close (respipe [0]); |
|
|
473 | respipe [0] = old_readfd; |
|
|
474 | } |
|
|
475 | |
|
|
476 | #ifdef _WIN32 |
|
|
477 | int arg = 1; |
|
|
478 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
|
|
479 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
|
|
480 | #else |
|
|
481 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
|
|
482 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
483 | #endif |
|
|
484 | croak ("unable to initialize result pipe(3)"); |
|
|
485 | |
|
|
486 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
|
|
487 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
|
|
488 | } |
450 | } |
489 | |
451 | |
490 | static void poll_wait (void) |
452 | static void poll_wait (void) |
491 | { |
453 | { |
492 | fd_set rfd; |
|
|
493 | |
|
|
494 | while (eio_nreqs ()) |
454 | while (eio_nreqs ()) |
495 | { |
455 | { |
496 | int size; |
456 | int size; |
497 | |
457 | |
498 | X_LOCK (reslock); |
458 | X_LOCK (reslock); |
… | |
… | |
502 | if (size) |
462 | if (size) |
503 | return; |
463 | return; |
504 | |
464 | |
505 | etp_maybe_start_thread (); |
465 | etp_maybe_start_thread (); |
506 | |
466 | |
507 | FD_ZERO (&rfd); |
467 | s_epipe_wait (&respipe); |
508 | FD_SET (respipe [0], &rfd); |
|
|
509 | |
|
|
510 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
511 | } |
468 | } |
512 | } |
469 | } |
513 | |
470 | |
514 | static int poll_cb (void) |
471 | static int poll_cb (void) |
515 | { |
472 | { |
… | |
… | |
533 | } |
490 | } |
534 | |
491 | |
535 | static SV * |
492 | static SV * |
536 | get_cb (SV *cb_sv) |
493 | get_cb (SV *cb_sv) |
537 | { |
494 | { |
538 | HV *st; |
495 | SvGETMAGIC (cb_sv); |
539 | GV *gvp; |
496 | return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; |
540 | CV *cv; |
|
|
541 | |
|
|
542 | if (!SvOK (cb_sv)) |
|
|
543 | return 0; |
|
|
544 | |
|
|
545 | cv = sv_2cv (cb_sv, &st, &gvp, 0); |
|
|
546 | |
|
|
547 | if (!cv) |
|
|
548 | croak ("IO::AIO callback must be undef or a CODE reference"); |
|
|
549 | |
|
|
550 | return (SV *)cv; |
|
|
551 | } |
497 | } |
552 | |
498 | |
553 | #define dREQ \ |
499 | #define dREQ \ |
554 | SV *cb_cv; \ |
500 | SV *cb_cv; \ |
555 | aio_req req; \ |
501 | aio_req req; \ |
… | |
… | |
569 | PUTBACK; \ |
515 | PUTBACK; \ |
570 | req_submit (req); \ |
516 | req_submit (req); \ |
571 | SPAGAIN; \ |
517 | SPAGAIN; \ |
572 | \ |
518 | \ |
573 | if (GIMME_V != G_VOID) \ |
519 | if (GIMME_V != G_VOID) \ |
574 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
520 | XPUSHs (req_sv (req, aio_req_stash)); |
575 | |
521 | |
576 | MODULE = IO::AIO PACKAGE = IO::AIO |
522 | MODULE = IO::AIO PACKAGE = IO::AIO |
577 | |
523 | |
578 | PROTOTYPES: ENABLE |
524 | PROTOTYPES: ENABLE |
579 | |
525 | |
… | |
… | |
599 | const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) |
545 | const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) |
600 | const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
546 | const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
601 | const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
547 | const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
602 | const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
548 | const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
603 | |
549 | |
|
|
550 | const_eio (MS_ASYNC) |
|
|
551 | const_eio (MS_INVALIDATE) |
|
|
552 | const_eio (MS_SYNC) |
|
|
553 | |
|
|
554 | const_eio (MT_MODIFY) |
|
|
555 | |
604 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
556 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
605 | const_eio (SYNC_FILE_RANGE_WRITE) |
557 | const_eio (SYNC_FILE_RANGE_WRITE) |
606 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
558 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
607 | |
559 | |
608 | const_eio (READDIR_DENTS) |
560 | const_eio (READDIR_DENTS) |
… | |
… | |
619 | const_eio (DT_LNK) |
571 | const_eio (DT_LNK) |
620 | const_eio (DT_SOCK) |
572 | const_eio (DT_SOCK) |
621 | const_eio (DT_WHT) |
573 | const_eio (DT_WHT) |
622 | }; |
574 | }; |
623 | |
575 | |
624 | stash = gv_stashpv ("IO::AIO", 1); |
576 | aio_stash = gv_stashpv ("IO::AIO" , 1); |
|
|
577 | aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); |
|
|
578 | aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); |
625 | |
579 | |
626 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
580 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
627 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
581 | newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); |
628 | |
582 | |
629 | create_respipe (); |
583 | create_respipe (); |
630 | |
584 | |
631 | if (eio_init (want_poll, done_poll) < 0) |
585 | if (eio_init (want_poll, done_poll) < 0) |
632 | croak ("IO::AIO: unable to initialise eio library"); |
586 | croak ("IO::AIO: unable to initialise eio library"); |
… | |
… | |
1047 | |
1001 | |
1048 | REQ_SEND; |
1002 | REQ_SEND; |
1049 | } |
1003 | } |
1050 | |
1004 | |
1051 | void |
1005 | void |
|
|
1006 | aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef) |
|
|
1007 | ALIAS: |
|
|
1008 | aio_mtouch = EIO_MTOUCH |
|
|
1009 | aio_msync = EIO_MSYNC |
|
|
1010 | PROTOTYPE: $$$$;$ |
|
|
1011 | PPCODE: |
|
|
1012 | { |
|
|
1013 | STRLEN svlen; |
|
|
1014 | UV len = SvUV (length); |
|
|
1015 | char *svptr = SvPVbyte (data, svlen); |
|
|
1016 | |
|
|
1017 | if (offset < 0) |
|
|
1018 | offset += svlen; |
|
|
1019 | |
|
|
1020 | if (offset < 0 || offset > svlen) |
|
|
1021 | croak ("offset outside of scalar"); |
|
|
1022 | |
|
|
1023 | if (!SvOK (length) || len + offset > svlen) |
|
|
1024 | len = svlen - offset; |
|
|
1025 | |
|
|
1026 | { |
|
|
1027 | dREQ; |
|
|
1028 | |
|
|
1029 | req->type = ix; |
|
|
1030 | req->size = len; |
|
|
1031 | req->sv2 = SvREFCNT_inc (data); |
|
|
1032 | req->ptr2 = (char *)svptr + offset; |
|
|
1033 | req->int1 = flags; |
|
|
1034 | |
|
|
1035 | REQ_SEND; |
|
|
1036 | } |
|
|
1037 | } |
|
|
1038 | |
|
|
1039 | void |
1052 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
1040 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
1053 | PPCODE: |
1041 | PPCODE: |
1054 | { |
1042 | { |
1055 | dREQ; |
1043 | dREQ; |
1056 | |
1044 | |
… | |
… | |
1068 | dREQ; |
1056 | dREQ; |
1069 | |
1057 | |
1070 | req->type = EIO_GROUP; |
1058 | req->type = EIO_GROUP; |
1071 | |
1059 | |
1072 | req_submit (req); |
1060 | req_submit (req); |
1073 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1061 | XPUSHs (req_sv (req, aio_grp_stash)); |
1074 | } |
1062 | } |
1075 | |
1063 | |
1076 | void |
1064 | void |
1077 | aio_nop (SV *callback=&PL_sv_undef) |
1065 | aio_nop (SV *callback=&PL_sv_undef) |
1078 | ALIAS: |
1066 | ALIAS: |
… | |
… | |
1130 | |
1118 | |
1131 | int |
1119 | int |
1132 | poll_fileno() |
1120 | poll_fileno() |
1133 | PROTOTYPE: |
1121 | PROTOTYPE: |
1134 | CODE: |
1122 | CODE: |
1135 | RETVAL = respipe [0]; |
1123 | RETVAL = s_epipe_fd (&respipe); |
1136 | OUTPUT: |
1124 | OUTPUT: |
1137 | RETVAL |
1125 | RETVAL |
1138 | |
1126 | |
1139 | int |
1127 | int |
1140 | poll_cb(...) |
1128 | poll_cb(...) |
… | |
… | |
1196 | |
1184 | |
1197 | ssize_t |
1185 | ssize_t |
1198 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
1186 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
1199 | PROTOTYPE: $$$$ |
1187 | PROTOTYPE: $$$$ |
1200 | CODE: |
1188 | CODE: |
1201 | eio_sendfile_sync (ofh, ifh, offset, count); |
1189 | RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); |
|
|
1190 | OUTPUT: |
|
|
1191 | RETVAL |
1202 | |
1192 | |
1203 | void _on_next_submit (SV *cb) |
1193 | void _on_next_submit (SV *cb) |
1204 | CODE: |
1194 | CODE: |
1205 | SvREFCNT_dec (on_next_submit); |
1195 | SvREFCNT_dec (on_next_submit); |
1206 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
1196 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
… | |
… | |
1221 | if (GIMME_V != G_VOID) |
1211 | if (GIMME_V != G_VOID) |
1222 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
1212 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
1223 | |
1213 | |
1224 | if (items > 1) |
1214 | if (items > 1) |
1225 | { |
1215 | { |
1226 | SV *cb_cv = get_cb (callback); |
1216 | SV *cb_cv =get_cb (callback); |
1227 | |
1217 | |
1228 | SvREFCNT_dec (req->callback); |
1218 | SvREFCNT_dec (req->callback); |
1229 | req->callback = SvREFCNT_inc (cb_cv); |
1219 | req->callback = SvREFCNT_inc (cb_cv); |
1230 | } |
1220 | } |
1231 | } |
1221 | } |