… | |
… | |
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 | |
… | |
… | |
619 | const_eio (DT_LNK) |
565 | const_eio (DT_LNK) |
620 | const_eio (DT_SOCK) |
566 | const_eio (DT_SOCK) |
621 | const_eio (DT_WHT) |
567 | const_eio (DT_WHT) |
622 | }; |
568 | }; |
623 | |
569 | |
624 | stash = gv_stashpv ("IO::AIO", 1); |
570 | aio_stash = gv_stashpv ("IO::AIO" , 1); |
|
|
571 | aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); |
|
|
572 | aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); |
625 | |
573 | |
626 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
574 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
627 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
575 | newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); |
628 | |
576 | |
629 | create_respipe (); |
577 | create_respipe (); |
630 | |
578 | |
631 | if (eio_init (want_poll, done_poll) < 0) |
579 | if (eio_init (want_poll, done_poll) < 0) |
632 | croak ("IO::AIO: unable to initialise eio library"); |
580 | croak ("IO::AIO: unable to initialise eio library"); |
… | |
… | |
1068 | dREQ; |
1016 | dREQ; |
1069 | |
1017 | |
1070 | req->type = EIO_GROUP; |
1018 | req->type = EIO_GROUP; |
1071 | |
1019 | |
1072 | req_submit (req); |
1020 | req_submit (req); |
1073 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1021 | XPUSHs (req_sv (req, aio_grp_stash)); |
1074 | } |
1022 | } |
1075 | |
1023 | |
1076 | void |
1024 | void |
1077 | aio_nop (SV *callback=&PL_sv_undef) |
1025 | aio_nop (SV *callback=&PL_sv_undef) |
1078 | ALIAS: |
1026 | ALIAS: |
… | |
… | |
1130 | |
1078 | |
1131 | int |
1079 | int |
1132 | poll_fileno() |
1080 | poll_fileno() |
1133 | PROTOTYPE: |
1081 | PROTOTYPE: |
1134 | CODE: |
1082 | CODE: |
1135 | RETVAL = respipe [0]; |
1083 | RETVAL = s_epipe_fd (&respipe); |
1136 | OUTPUT: |
1084 | OUTPUT: |
1137 | RETVAL |
1085 | RETVAL |
1138 | |
1086 | |
1139 | int |
1087 | int |
1140 | poll_cb(...) |
1088 | poll_cb(...) |
… | |
… | |
1221 | if (GIMME_V != G_VOID) |
1169 | if (GIMME_V != G_VOID) |
1222 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
1170 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
1223 | |
1171 | |
1224 | if (items > 1) |
1172 | if (items > 1) |
1225 | { |
1173 | { |
1226 | SV *cb_cv = get_cb (callback); |
1174 | SV *cb_cv =get_cb (callback); |
1227 | |
1175 | |
1228 | SvREFCNT_dec (req->callback); |
1176 | SvREFCNT_dec (req->callback); |
1229 | req->callback = SvREFCNT_inc (cb_cv); |
1177 | req->callback = SvREFCNT_inc (cb_cv); |
1230 | } |
1178 | } |
1231 | } |
1179 | } |