… | |
… | |
11 | #include <stddef.h> |
11 | #include <stddef.h> |
12 | #include <stdlib.h> |
12 | #include <stdlib.h> |
13 | #include <errno.h> |
13 | #include <errno.h> |
14 | #include <sys/types.h> |
14 | #include <sys/types.h> |
15 | #include <sys/stat.h> |
15 | #include <sys/stat.h> |
|
|
16 | #include <sys/statvfs.h> |
16 | #include <limits.h> |
17 | #include <limits.h> |
17 | #include <fcntl.h> |
18 | #include <fcntl.h> |
18 | #include <sched.h> |
19 | #include <sched.h> |
19 | |
20 | |
20 | /* perl namespace pollution */ |
21 | /* perl namespace pollution */ |
… | |
… | |
99 | #define expect_false(expr) expect ((expr) != 0, 0) |
100 | #define expect_false(expr) expect ((expr) != 0, 0) |
100 | #define expect_true(expr) expect ((expr) != 0, 1) |
101 | #define expect_true(expr) expect ((expr) != 0, 1) |
101 | |
102 | |
102 | /*****************************************************************************/ |
103 | /*****************************************************************************/ |
103 | |
104 | |
104 | static HV *stash; |
|
|
105 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
105 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
106 | typedef int aio_rfd; /* read file desriptor */ |
106 | typedef int aio_rfd; /* read file desriptor */ |
107 | typedef int aio_wfd; /* write file descriptor */ |
107 | typedef int aio_wfd; /* write file descriptor */ |
108 | |
108 | |
109 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
109 | static HV *aio_stash, *aio_req_stash, *aio_grp_stash; |
110 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
|
|
111 | |
110 | |
112 | #define EIO_REQ_MEMBERS \ |
111 | #define EIO_REQ_MEMBERS \ |
113 | SV *callback; \ |
112 | SV *callback; \ |
114 | SV *sv1, *sv2; \ |
113 | SV *sv1, *sv2; \ |
115 | STRLEN stroffset; \ |
114 | STRLEN stroffset; \ |
… | |
… | |
142 | |
141 | |
143 | #ifndef POSIX_FADV_DONTNEED |
142 | #ifndef POSIX_FADV_DONTNEED |
144 | # define POSIX_FADV_DONTNEED 0 |
143 | # define POSIX_FADV_DONTNEED 0 |
145 | #endif |
144 | #endif |
146 | |
145 | |
|
|
146 | #ifndef ST_NODEV |
|
|
147 | # define ST_NODEV 0 |
|
|
148 | #endif |
|
|
149 | |
|
|
150 | #ifndef ST_NOEXEC |
|
|
151 | # define ST_NOEXEC 0 |
|
|
152 | #endif |
|
|
153 | |
|
|
154 | #ifndef ST_SYNCHRONOUS |
|
|
155 | # define ST_SYNCHRONOUS 0 |
|
|
156 | #endif |
|
|
157 | |
|
|
158 | #ifndef ST_MANDLOCK |
|
|
159 | # define ST_MANDLOCK 0 |
|
|
160 | #endif |
|
|
161 | |
|
|
162 | #ifndef ST_WRITE |
|
|
163 | # define ST_WRITE 0 |
|
|
164 | #endif |
|
|
165 | |
|
|
166 | #ifndef ST_APPEND |
|
|
167 | # define ST_APPEND 0 |
|
|
168 | #endif |
|
|
169 | |
|
|
170 | #ifndef ST_IMMUTABLE |
|
|
171 | # define ST_IMMUTABLE 0 |
|
|
172 | #endif |
|
|
173 | |
|
|
174 | #ifndef ST_NOATIME |
|
|
175 | # define ST_NOATIME 0 |
|
|
176 | #endif |
|
|
177 | |
|
|
178 | #ifndef ST_NODIRATIME |
|
|
179 | # define ST_NODIRATIME 0 |
|
|
180 | #endif |
|
|
181 | |
|
|
182 | #ifndef ST_RELATIME |
|
|
183 | # define ST_RELATIME 0 |
|
|
184 | #endif |
|
|
185 | |
147 | static int req_invoke (eio_req *req); |
186 | static int req_invoke (eio_req *req); |
148 | #define EIO_FINISH(req) req_invoke (req) |
187 | #define EIO_FINISH(req) req_invoke (req) |
149 | static void req_destroy (eio_req *grp); |
188 | static void req_destroy (eio_req *grp); |
150 | #define EIO_DESTROY(req) req_destroy (req) |
189 | #define EIO_DESTROY(req) req_destroy (req) |
151 | |
190 | |
… | |
… | |
160 | |
199 | |
161 | static SV *on_next_submit; |
200 | static SV *on_next_submit; |
162 | static int next_pri = EIO_PRI_DEFAULT; |
201 | static int next_pri = EIO_PRI_DEFAULT; |
163 | static int max_outstanding; |
202 | static int max_outstanding; |
164 | |
203 | |
165 | static int respipe_osf [2]; |
|
|
166 | static s_epipe respipe; |
204 | static s_epipe respipe; |
167 | |
205 | |
168 | static void req_destroy (aio_req req); |
206 | static void req_destroy (aio_req req); |
169 | static void req_cancel (aio_req req); |
207 | static void req_cancel (aio_req req); |
170 | |
208 | |
… | |
… | |
179 | /* read any signals sent by the worker threads */ |
217 | /* read any signals sent by the worker threads */ |
180 | s_epipe_drain (&respipe); |
218 | s_epipe_drain (&respipe); |
181 | } |
219 | } |
182 | |
220 | |
183 | /* must be called at most once */ |
221 | /* must be called at most once */ |
184 | static SV *req_sv (aio_req req, const char *klass) |
222 | static SV *req_sv (aio_req req, HV *stash) |
185 | { |
223 | { |
186 | if (!req->self) |
224 | if (!req->self) |
187 | { |
225 | { |
188 | req->self = (SV *)newHV (); |
226 | req->self = (SV *)newHV (); |
189 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
227 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
190 | } |
228 | } |
191 | |
229 | |
192 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
230 | return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); |
193 | } |
231 | } |
194 | |
232 | |
195 | static aio_req SvAIO_REQ (SV *sv) |
233 | static aio_req SvAIO_REQ (SV *sv) |
196 | { |
234 | { |
197 | MAGIC *mg; |
235 | MAGIC *mg; |
198 | |
236 | |
199 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
237 | if (!SvROK (sv) |
|
|
238 | || (SvSTASH (SvRV (sv)) != aio_grp_stash |
|
|
239 | && SvSTASH (SvRV (sv)) != aio_req_stash |
|
|
240 | && !sv_derived_from (sv, "IO::AIO::REQ"))) |
200 | croak ("object of class " AIO_REQ_KLASS " expected"); |
241 | croak ("object of class IO::AIO::REQ expected"); |
201 | |
242 | |
202 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
243 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
203 | |
244 | |
204 | return mg ? (aio_req)mg->mg_ptr : 0; |
245 | return mg ? (aio_req)mg->mg_ptr : 0; |
205 | } |
246 | } |
… | |
… | |
211 | dSP; |
252 | dSP; |
212 | |
253 | |
213 | ENTER; |
254 | ENTER; |
214 | SAVETMPS; |
255 | SAVETMPS; |
215 | PUSHMARK (SP); |
256 | PUSHMARK (SP); |
216 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
257 | XPUSHs (req_sv (grp, aio_grp_stash)); |
217 | PUTBACK; |
258 | PUTBACK; |
218 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
259 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
219 | SPAGAIN; |
260 | SPAGAIN; |
220 | FREETMPS; |
261 | FREETMPS; |
221 | LEAVE; |
262 | LEAVE; |
… | |
… | |
327 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
368 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
328 | char sym [64]; |
369 | char sym [64]; |
329 | int symlen; |
370 | int symlen; |
330 | |
371 | |
331 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
372 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
332 | gv_init (gv, stash, sym, symlen, 0); |
373 | gv_init (gv, aio_stash, sym, symlen, 0); |
333 | |
374 | |
334 | symlen = snprintf ( |
375 | symlen = snprintf ( |
335 | sym, |
376 | sym, |
336 | sizeof (sym), |
377 | sizeof (sym), |
337 | "%s&=%d", |
378 | "%s&=%d", |
… | |
… | |
343 | fh = (SV *)gv; |
384 | fh = (SV *)gv; |
344 | } |
385 | } |
345 | |
386 | |
346 | PUSHs (fh); |
387 | PUSHs (fh); |
347 | } |
388 | } |
|
|
389 | break; |
|
|
390 | |
|
|
391 | case EIO_STATVFS: |
|
|
392 | case EIO_FSTATVFS: |
|
|
393 | { |
|
|
394 | SV *rv = &PL_sv_undef; |
|
|
395 | |
|
|
396 | if (req->result >= 0) |
|
|
397 | { |
|
|
398 | EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); |
|
|
399 | HV *hv = newHV (); |
|
|
400 | |
|
|
401 | rv = sv_2mortal (newRV_noinc ((SV *)hv)); |
|
|
402 | |
|
|
403 | hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0); |
|
|
404 | hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0); |
|
|
405 | hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0); |
|
|
406 | hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0); |
|
|
407 | hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0); |
|
|
408 | hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0); |
|
|
409 | hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0); |
|
|
410 | hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); |
|
|
411 | hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); |
|
|
412 | hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); |
|
|
413 | hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); |
|
|
414 | } |
|
|
415 | |
|
|
416 | PUSHs (rv); |
|
|
417 | } |
|
|
418 | |
348 | break; |
419 | break; |
349 | |
420 | |
350 | case EIO_GROUP: |
421 | case EIO_GROUP: |
351 | req->int1 = 2; /* mark group as finished */ |
422 | req->int1 = 2; /* mark group as finished */ |
352 | |
423 | |
… | |
… | |
444 | |
515 | |
445 | static void |
516 | static void |
446 | create_respipe (void) |
517 | create_respipe (void) |
447 | { |
518 | { |
448 | if (s_epipe_renew (&respipe)) |
519 | if (s_epipe_renew (&respipe)) |
449 | croak ("unable to initialize result pipe"); |
520 | 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 | } |
521 | } |
454 | |
522 | |
455 | static void poll_wait (void) |
523 | static void poll_wait (void) |
456 | { |
524 | { |
457 | while (eio_nreqs ()) |
525 | while (eio_nreqs ()) |
… | |
… | |
518 | PUTBACK; \ |
586 | PUTBACK; \ |
519 | req_submit (req); \ |
587 | req_submit (req); \ |
520 | SPAGAIN; \ |
588 | SPAGAIN; \ |
521 | \ |
589 | \ |
522 | if (GIMME_V != G_VOID) \ |
590 | if (GIMME_V != G_VOID) \ |
523 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
591 | XPUSHs (req_sv (req, aio_req_stash)); |
524 | |
592 | |
525 | MODULE = IO::AIO PACKAGE = IO::AIO |
593 | MODULE = IO::AIO PACKAGE = IO::AIO |
526 | |
594 | |
527 | PROTOTYPES: ENABLE |
595 | PROTOTYPES: ENABLE |
528 | |
596 | |
… | |
… | |
548 | const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) |
616 | const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) |
549 | const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
617 | const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
550 | const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
618 | const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
551 | const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
619 | const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
552 | |
620 | |
|
|
621 | const_iv (ST_RDONLY , ST_RDONLY) |
|
|
622 | const_iv (ST_NOSUID , ST_NOSUID) |
|
|
623 | const_iv (ST_NODEV , ST_NODEV) |
|
|
624 | const_iv (ST_NOEXEC , ST_NOEXEC) |
|
|
625 | const_iv (ST_SYNCHRONOUS , ST_SYNCHRONOUS) |
|
|
626 | const_iv (ST_MANDLOCK , ST_MANDLOCK) |
|
|
627 | const_iv (ST_WRITE , ST_WRITE) |
|
|
628 | const_iv (ST_APPEND , ST_APPEND) |
|
|
629 | const_iv (ST_IMMUTABLE , ST_IMMUTABLE) |
|
|
630 | const_iv (ST_NOATIME , ST_NOATIME) |
|
|
631 | const_iv (ST_NODIRATIME , ST_NODIRATIME) |
|
|
632 | const_iv (ST_RELATIME , ST_RELATIME) |
|
|
633 | |
|
|
634 | const_eio (MS_ASYNC) |
|
|
635 | const_eio (MS_INVALIDATE) |
|
|
636 | const_eio (MS_SYNC) |
|
|
637 | |
|
|
638 | const_eio (MT_MODIFY) |
|
|
639 | |
553 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
640 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
554 | const_eio (SYNC_FILE_RANGE_WRITE) |
641 | const_eio (SYNC_FILE_RANGE_WRITE) |
555 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
642 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
556 | |
643 | |
557 | const_eio (READDIR_DENTS) |
644 | const_eio (READDIR_DENTS) |
… | |
… | |
568 | const_eio (DT_LNK) |
655 | const_eio (DT_LNK) |
569 | const_eio (DT_SOCK) |
656 | const_eio (DT_SOCK) |
570 | const_eio (DT_WHT) |
657 | const_eio (DT_WHT) |
571 | }; |
658 | }; |
572 | |
659 | |
573 | stash = gv_stashpv ("IO::AIO", 1); |
660 | aio_stash = gv_stashpv ("IO::AIO" , 1); |
|
|
661 | aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); |
|
|
662 | aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); |
574 | |
663 | |
575 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
664 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
576 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
665 | newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); |
577 | |
666 | |
578 | create_respipe (); |
667 | create_respipe (); |
579 | |
668 | |
580 | if (eio_init (want_poll, done_poll) < 0) |
669 | if (eio_init (want_poll, done_poll) < 0) |
581 | croak ("IO::AIO: unable to initialise eio library"); |
670 | croak ("IO::AIO: unable to initialise eio library"); |
… | |
… | |
808 | } |
897 | } |
809 | |
898 | |
810 | void |
899 | void |
811 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
900 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
812 | ALIAS: |
901 | ALIAS: |
813 | aio_stat = EIO_STAT |
902 | aio_stat = EIO_STAT |
814 | aio_lstat = EIO_LSTAT |
903 | aio_lstat = EIO_LSTAT |
|
|
904 | aio_statvfs = EIO_STATVFS |
815 | PPCODE: |
905 | PPCODE: |
816 | { |
906 | { |
817 | dREQ; |
907 | dREQ; |
818 | |
908 | |
819 | req->sv1 = newSVsv (fh_or_path); |
909 | req->sv1 = newSVsv (fh_or_path); |
… | |
… | |
823 | req->type = ix; |
913 | req->type = ix; |
824 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
914 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
825 | } |
915 | } |
826 | else |
916 | else |
827 | { |
917 | { |
828 | req->type = EIO_FSTAT; |
918 | req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT; |
829 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
919 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
830 | } |
920 | } |
831 | |
921 | |
832 | REQ_SEND; |
922 | REQ_SEND; |
833 | } |
923 | } |
… | |
… | |
996 | |
1086 | |
997 | REQ_SEND; |
1087 | REQ_SEND; |
998 | } |
1088 | } |
999 | |
1089 | |
1000 | void |
1090 | void |
|
|
1091 | aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef) |
|
|
1092 | ALIAS: |
|
|
1093 | aio_mtouch = EIO_MTOUCH |
|
|
1094 | aio_msync = EIO_MSYNC |
|
|
1095 | PROTOTYPE: $$$$;$ |
|
|
1096 | PPCODE: |
|
|
1097 | { |
|
|
1098 | STRLEN svlen; |
|
|
1099 | UV len = SvUV (length); |
|
|
1100 | char *svptr = SvPVbyte (data, svlen); |
|
|
1101 | |
|
|
1102 | if (offset < 0) |
|
|
1103 | offset += svlen; |
|
|
1104 | |
|
|
1105 | if (offset < 0 || offset > svlen) |
|
|
1106 | croak ("offset outside of scalar"); |
|
|
1107 | |
|
|
1108 | if (!SvOK (length) || len + offset > svlen) |
|
|
1109 | len = svlen - offset; |
|
|
1110 | |
|
|
1111 | { |
|
|
1112 | dREQ; |
|
|
1113 | |
|
|
1114 | req->type = ix; |
|
|
1115 | req->size = len; |
|
|
1116 | req->sv2 = SvREFCNT_inc (data); |
|
|
1117 | req->ptr2 = (char *)svptr + offset; |
|
|
1118 | req->int1 = flags; |
|
|
1119 | |
|
|
1120 | REQ_SEND; |
|
|
1121 | } |
|
|
1122 | } |
|
|
1123 | |
|
|
1124 | void |
1001 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
1125 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
1002 | PPCODE: |
1126 | PPCODE: |
1003 | { |
1127 | { |
1004 | dREQ; |
1128 | dREQ; |
1005 | |
1129 | |
… | |
… | |
1017 | dREQ; |
1141 | dREQ; |
1018 | |
1142 | |
1019 | req->type = EIO_GROUP; |
1143 | req->type = EIO_GROUP; |
1020 | |
1144 | |
1021 | req_submit (req); |
1145 | req_submit (req); |
1022 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1146 | XPUSHs (req_sv (req, aio_grp_stash)); |
1023 | } |
1147 | } |
1024 | |
1148 | |
1025 | void |
1149 | void |
1026 | aio_nop (SV *callback=&PL_sv_undef) |
1150 | aio_nop (SV *callback=&PL_sv_undef) |
1027 | ALIAS: |
1151 | ALIAS: |
… | |
… | |
1145 | |
1269 | |
1146 | ssize_t |
1270 | ssize_t |
1147 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
1271 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
1148 | PROTOTYPE: $$$$ |
1272 | PROTOTYPE: $$$$ |
1149 | CODE: |
1273 | CODE: |
1150 | eio_sendfile_sync (ofh, ifh, offset, count); |
1274 | RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); |
|
|
1275 | OUTPUT: |
|
|
1276 | RETVAL |
1151 | |
1277 | |
1152 | void _on_next_submit (SV *cb) |
1278 | void _on_next_submit (SV *cb) |
1153 | CODE: |
1279 | CODE: |
1154 | SvREFCNT_dec (on_next_submit); |
1280 | SvREFCNT_dec (on_next_submit); |
1155 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
1281 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |