… | |
… | |
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]; |
|
|
166 | static s_epipe respipe; |
163 | static s_epipe respipe; |
167 | |
164 | |
168 | static void req_destroy (aio_req req); |
165 | static void req_destroy (aio_req req); |
169 | static void req_cancel (aio_req req); |
166 | static void req_cancel (aio_req req); |
170 | |
167 | |
… | |
… | |
179 | /* read any signals sent by the worker threads */ |
176 | /* read any signals sent by the worker threads */ |
180 | s_epipe_drain (&respipe); |
177 | s_epipe_drain (&respipe); |
181 | } |
178 | } |
182 | |
179 | |
183 | /* must be called at most once */ |
180 | /* must be called at most once */ |
184 | static SV *req_sv (aio_req req, const char *klass) |
181 | static SV *req_sv (aio_req req, HV *stash) |
185 | { |
182 | { |
186 | if (!req->self) |
183 | if (!req->self) |
187 | { |
184 | { |
188 | req->self = (SV *)newHV (); |
185 | req->self = (SV *)newHV (); |
189 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
186 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
190 | } |
187 | } |
191 | |
188 | |
192 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
189 | return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); |
193 | } |
190 | } |
194 | |
191 | |
195 | static aio_req SvAIO_REQ (SV *sv) |
192 | static aio_req SvAIO_REQ (SV *sv) |
196 | { |
193 | { |
197 | MAGIC *mg; |
194 | MAGIC *mg; |
198 | |
195 | |
199 | 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"))) |
200 | croak ("object of class " AIO_REQ_KLASS " expected"); |
200 | croak ("object of class IO::AIO::REQ expected"); |
201 | |
201 | |
202 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
202 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
203 | |
203 | |
204 | return mg ? (aio_req)mg->mg_ptr : 0; |
204 | return mg ? (aio_req)mg->mg_ptr : 0; |
205 | } |
205 | } |
… | |
… | |
211 | dSP; |
211 | dSP; |
212 | |
212 | |
213 | ENTER; |
213 | ENTER; |
214 | SAVETMPS; |
214 | SAVETMPS; |
215 | PUSHMARK (SP); |
215 | PUSHMARK (SP); |
216 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
216 | XPUSHs (req_sv (grp, aio_grp_stash)); |
217 | PUTBACK; |
217 | PUTBACK; |
218 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
218 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
219 | SPAGAIN; |
219 | SPAGAIN; |
220 | FREETMPS; |
220 | FREETMPS; |
221 | LEAVE; |
221 | LEAVE; |
… | |
… | |
327 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
327 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
328 | char sym [64]; |
328 | char sym [64]; |
329 | int symlen; |
329 | int symlen; |
330 | |
330 | |
331 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
331 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
332 | gv_init (gv, stash, sym, symlen, 0); |
332 | gv_init (gv, aio_stash, sym, symlen, 0); |
333 | |
333 | |
334 | symlen = snprintf ( |
334 | symlen = snprintf ( |
335 | sym, |
335 | sym, |
336 | sizeof (sym), |
336 | sizeof (sym), |
337 | "%s&=%d", |
337 | "%s&=%d", |
… | |
… | |
444 | |
444 | |
445 | static void |
445 | static void |
446 | create_respipe (void) |
446 | create_respipe (void) |
447 | { |
447 | { |
448 | if (s_epipe_renew (&respipe)) |
448 | if (s_epipe_renew (&respipe)) |
449 | croak ("unable to initialize result pipe"); |
449 | croak ("IO::AIO: unable to initialize result pipe"); |
450 | |
|
|
451 | respipe_osf [0] = S_TO_SOCKET (respipe.fd [0]); |
|
|
452 | respipe_osf [1] = S_TO_SOCKET (respipe.fd [1]); |
|
|
453 | } |
450 | } |
454 | |
451 | |
455 | static void poll_wait (void) |
452 | static void poll_wait (void) |
456 | { |
453 | { |
457 | while (eio_nreqs ()) |
454 | while (eio_nreqs ()) |
… | |
… | |
518 | PUTBACK; \ |
515 | PUTBACK; \ |
519 | req_submit (req); \ |
516 | req_submit (req); \ |
520 | SPAGAIN; \ |
517 | SPAGAIN; \ |
521 | \ |
518 | \ |
522 | if (GIMME_V != G_VOID) \ |
519 | if (GIMME_V != G_VOID) \ |
523 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
520 | XPUSHs (req_sv (req, aio_req_stash)); |
524 | |
521 | |
525 | MODULE = IO::AIO PACKAGE = IO::AIO |
522 | MODULE = IO::AIO PACKAGE = IO::AIO |
526 | |
523 | |
527 | PROTOTYPES: ENABLE |
524 | PROTOTYPES: ENABLE |
528 | |
525 | |
… | |
… | |
568 | const_eio (DT_LNK) |
565 | const_eio (DT_LNK) |
569 | const_eio (DT_SOCK) |
566 | const_eio (DT_SOCK) |
570 | const_eio (DT_WHT) |
567 | const_eio (DT_WHT) |
571 | }; |
568 | }; |
572 | |
569 | |
573 | 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); |
574 | |
573 | |
575 | 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; ) |
576 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
575 | newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); |
577 | |
576 | |
578 | create_respipe (); |
577 | create_respipe (); |
579 | |
578 | |
580 | if (eio_init (want_poll, done_poll) < 0) |
579 | if (eio_init (want_poll, done_poll) < 0) |
581 | croak ("IO::AIO: unable to initialise eio library"); |
580 | croak ("IO::AIO: unable to initialise eio library"); |
… | |
… | |
1017 | dREQ; |
1016 | dREQ; |
1018 | |
1017 | |
1019 | req->type = EIO_GROUP; |
1018 | req->type = EIO_GROUP; |
1020 | |
1019 | |
1021 | req_submit (req); |
1020 | req_submit (req); |
1022 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1021 | XPUSHs (req_sv (req, aio_grp_stash)); |
1023 | } |
1022 | } |
1024 | |
1023 | |
1025 | void |
1024 | void |
1026 | aio_nop (SV *callback=&PL_sv_undef) |
1025 | aio_nop (SV *callback=&PL_sv_undef) |
1027 | ALIAS: |
1026 | ALIAS: |