… | |
… | |
65 | # include <signal.h> |
65 | # include <signal.h> |
66 | # define EIO_STRUCT_DIRENT struct dirent |
66 | # define EIO_STRUCT_DIRENT struct dirent |
67 | |
67 | |
68 | #endif |
68 | #endif |
69 | |
69 | |
|
|
70 | /* perl stupidly overrides readdir and maybe others */ |
|
|
71 | /* with thread-unsafe versions, imagine that :( */ |
|
|
72 | #undef readdir |
|
|
73 | #undef opendir |
|
|
74 | #undef closedir |
|
|
75 | |
70 | #define EIO_STRUCT_STAT Stat_t |
76 | #define EIO_STRUCT_STAT Stat_t |
71 | |
77 | |
72 | /* use NV for 32 bit perls as it allows larger offsets */ |
78 | /* use NV for 32 bit perls as it allows larger offsets */ |
73 | #if IVSIZE >= 8 |
79 | #if IVSIZE >= 8 |
74 | # define SvVAL64 SvIV |
80 | # define SvVAL64 SvIV |
75 | #else |
81 | #else |
76 | # define SvVAL64 SvNV |
82 | # define SvVAL64 SvNV |
77 | #endif |
83 | #endif |
78 | |
84 | |
|
|
85 | /*****************************************************************************/ |
|
|
86 | |
|
|
87 | #if __GNUC__ >= 3 |
|
|
88 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
89 | #else |
|
|
90 | # define expect(expr,value) (expr) |
|
|
91 | #endif |
|
|
92 | |
|
|
93 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
94 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
95 | |
|
|
96 | /*****************************************************************************/ |
|
|
97 | |
79 | static HV *stash; |
98 | static HV *stash; |
80 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
99 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
81 | |
100 | |
82 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
101 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
83 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
102 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
… | |
… | |
92 | |
111 | |
93 | #include "libeio/eio.h" |
112 | #include "libeio/eio.h" |
94 | |
113 | |
95 | static int req_invoke (eio_req *req); |
114 | static int req_invoke (eio_req *req); |
96 | #define EIO_FINISH(req) req_invoke (req) |
115 | #define EIO_FINISH(req) req_invoke (req) |
97 | static void aio_grp_feed (eio_req *grp); |
|
|
98 | #define EIO_FEED(req) aio_grp_feed (req) |
|
|
99 | static void req_destroy (eio_req *grp); |
116 | static void req_destroy (eio_req *grp); |
100 | #define EIO_DESTROY(req) req_destroy (req) |
117 | #define EIO_DESTROY(req) req_destroy (req) |
101 | |
118 | |
102 | enum { |
119 | enum { |
103 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
120 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
… | |
… | |
175 | |
192 | |
176 | static void req_submit (eio_req *req) |
193 | static void req_submit (eio_req *req) |
177 | { |
194 | { |
178 | eio_submit (req); |
195 | eio_submit (req); |
179 | |
196 | |
180 | if (on_next_submit) |
197 | if (expect_false (on_next_submit)) |
181 | { |
198 | { |
182 | dSP; |
199 | dSP; |
183 | SV *cb = sv_2mortal (on_next_submit); |
200 | SV *cb = sv_2mortal (on_next_submit); |
184 | |
201 | |
185 | on_next_submit = 0; |
202 | on_next_submit = 0; |
… | |
… | |
195 | dSP; |
212 | dSP; |
196 | |
213 | |
197 | if (req->flags & FLAG_SV2_RO_OFF) |
214 | if (req->flags & FLAG_SV2_RO_OFF) |
198 | SvREADONLY_off (req->sv2); |
215 | SvREADONLY_off (req->sv2); |
199 | |
216 | |
200 | if (!EIO_CANCELLED (req) && SvOK (req->callback)) |
217 | if (!EIO_CANCELLED (req) && req->callback) |
201 | { |
218 | { |
202 | ENTER; |
219 | ENTER; |
203 | SAVETMPS; |
220 | SAVETMPS; |
204 | PUSHMARK (SP); |
221 | PUSHMARK (SP); |
205 | EXTEND (SP, 1); |
222 | EXTEND (SP, 1); |
… | |
… | |
423 | } |
440 | } |
424 | } |
441 | } |
425 | |
442 | |
426 | static int poll_cb (void) |
443 | static int poll_cb (void) |
427 | { |
444 | { |
428 | int res; |
445 | for (;;) |
429 | |
|
|
430 | do |
|
|
431 | { |
446 | { |
432 | res = eio_poll (); |
447 | int res = eio_poll (); |
433 | |
448 | |
434 | if (res > 0) |
449 | if (res > 0) |
435 | croak (0); |
450 | croak (0); |
|
|
451 | |
|
|
452 | if (!max_outstanding || max_outstanding > eio_nreqs ()) |
|
|
453 | return res; |
|
|
454 | |
|
|
455 | poll_wait (); |
436 | } |
456 | } |
437 | while (max_outstanding && max_outstanding <= eio_nreqs ()); |
|
|
438 | |
|
|
439 | return res; |
|
|
440 | } |
457 | } |
441 | |
458 | |
442 | static void atfork_child (void) |
459 | static void atfork_child (void) |
443 | { |
460 | { |
444 | create_respipe (); |
461 | create_respipe (); |
445 | } |
462 | } |
446 | |
463 | |
|
|
464 | static SV * |
|
|
465 | get_cb (SV *cb_sv) |
|
|
466 | { |
|
|
467 | HV *st; |
|
|
468 | GV *gvp; |
|
|
469 | CV *cv; |
|
|
470 | |
|
|
471 | if (!SvOK (cb_sv)) |
|
|
472 | return 0; |
|
|
473 | |
|
|
474 | cv = sv_2cv (cb_sv, &st, &gvp, 0); |
|
|
475 | |
|
|
476 | if (!cv) |
|
|
477 | croak ("IO::AIO callback must be undef or a CODE reference"); |
|
|
478 | |
|
|
479 | return (SV *)cv; |
|
|
480 | } |
|
|
481 | |
447 | #define dREQ \ |
482 | #define dREQ \ |
|
|
483 | SV *cb_cv; \ |
448 | aio_req req; \ |
484 | aio_req req; \ |
449 | int req_pri = next_pri; \ |
485 | int req_pri = next_pri; \ |
450 | next_pri = EIO_PRI_DEFAULT; \ |
486 | next_pri = EIO_PRI_DEFAULT; \ |
451 | \ |
487 | \ |
452 | if (SvOK (callback) && !SvROK (callback)) \ |
488 | cb_cv = get_cb (callback); \ |
453 | croak ("callback must be undef or of reference type"); \ |
|
|
454 | \ |
489 | \ |
455 | Newz (0, req, 1, eio_req); \ |
490 | Newz (0, req, 1, eio_req); \ |
456 | if (!req) \ |
491 | if (!req) \ |
457 | croak ("out of memory during eio_req allocation"); \ |
492 | croak ("out of memory during eio_req allocation"); \ |
458 | \ |
493 | \ |
459 | req->callback = newSVsv (callback); \ |
494 | req->callback = SvREFCNT_inc (cb_cv); \ |
460 | req->pri = req_pri |
495 | req->pri = req_pri |
461 | |
496 | |
462 | #define REQ_SEND \ |
497 | #define REQ_SEND \ |
|
|
498 | PUTBACK; \ |
463 | req_submit (req); \ |
499 | req_submit (req); \ |
|
|
500 | SPAGAIN; \ |
464 | \ |
501 | \ |
465 | if (GIMME_V != G_VOID) \ |
502 | if (GIMME_V != G_VOID) \ |
466 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
503 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
467 | |
504 | |
468 | MODULE = IO::AIO PACKAGE = IO::AIO |
505 | MODULE = IO::AIO PACKAGE = IO::AIO |
… | |
… | |
888 | { |
925 | { |
889 | dREQ; |
926 | dREQ; |
890 | |
927 | |
891 | req->type = EIO_GROUP; |
928 | req->type = EIO_GROUP; |
892 | |
929 | |
893 | eio_submit (req); |
930 | req_submit (req); |
894 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
931 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
895 | } |
932 | } |
896 | |
933 | |
897 | void |
934 | void |
898 | aio_nop (SV *callback=&PL_sv_undef) |
935 | aio_nop (SV *callback=&PL_sv_undef) |
… | |
… | |
1017 | CODE: |
1054 | CODE: |
1018 | eio_cancel (req); |
1055 | eio_cancel (req); |
1019 | |
1056 | |
1020 | void |
1057 | void |
1021 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
1058 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
1022 | CODE: |
1059 | PPCODE: |
|
|
1060 | { |
|
|
1061 | if (GIMME_V != G_VOID) |
|
|
1062 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
|
|
1063 | |
|
|
1064 | if (items > 1) |
|
|
1065 | { |
|
|
1066 | SV *cb_cv = get_cb (callback); |
|
|
1067 | |
1023 | SvREFCNT_dec (req->callback); |
1068 | SvREFCNT_dec (req->callback); |
1024 | req->callback = newSVsv (callback); |
1069 | req->callback = SvREFCNT_inc (cb_cv); |
|
|
1070 | } |
|
|
1071 | } |
1025 | |
1072 | |
1026 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1073 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1027 | |
1074 | |
1028 | void |
1075 | void |
1029 | add (aio_req grp, ...) |
1076 | add (aio_req grp, ...) |
… | |
… | |
1084 | void |
1131 | void |
1085 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1132 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1086 | CODE: |
1133 | CODE: |
1087 | { |
1134 | { |
1088 | SvREFCNT_dec (grp->sv2); |
1135 | SvREFCNT_dec (grp->sv2); |
1089 | grp->sv2 = newSVsv (callback); |
1136 | grp->sv2 = newSVsv (callback); |
|
|
1137 | grp->feed = aio_grp_feed; |
1090 | |
1138 | |
1091 | if (grp->int2 <= 0) |
1139 | if (grp->int2 <= 0) |
1092 | grp->int2 = 2; |
1140 | grp->int2 = 2; |
1093 | |
1141 | |
1094 | eio_grp_limit (grp, grp->int2); |
1142 | eio_grp_limit (grp, grp->int2); |