1 | #define _XOPEN_SOURCE 500 |
|
|
2 | |
|
|
3 | #include "EXTERN.h" |
|
|
4 | #include "perl.h" |
1 | #include "perl.h" |
5 | #include "XSUB.h" |
2 | #include "XSUB.h" |
6 | |
3 | |
7 | #include <sys/types.h> |
4 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
5 | #include <sys/stat.h> |
… | |
… | |
44 | off_t offset; |
41 | off_t offset; |
45 | size_t length; |
42 | size_t length; |
46 | ssize_t result; |
43 | ssize_t result; |
47 | mode_t mode; /* open */ |
44 | mode_t mode; /* open */ |
48 | int errorno; |
45 | int errorno; |
49 | SV *data, *callback; |
46 | SV *data, *callback, *fh; |
50 | void *dataptr; |
47 | void *dataptr; |
51 | STRLEN dataoffset; |
48 | STRLEN dataoffset; |
52 | |
49 | |
53 | Stat_t *statdata; |
50 | Stat_t *statdata; |
54 | } aio_cb; |
51 | } aio_cb; |
… | |
… | |
117 | + req->result > 0 ? req->result : 0); |
114 | + req->result > 0 ? req->result : 0); |
118 | |
115 | |
119 | if (req->data) |
116 | if (req->data) |
120 | SvREFCNT_dec (req->data); |
117 | SvREFCNT_dec (req->data); |
121 | |
118 | |
|
|
119 | if (req->fh) |
|
|
120 | SvREFCNT_dec (req->fh); |
|
|
121 | |
122 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
122 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
123 | { |
123 | { |
124 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
124 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
125 | PL_laststatval = req->result; |
125 | PL_laststatval = req->result; |
126 | PL_statcache = *(req->statdata); |
126 | PL_statcache = *(req->statdata); |
127 | |
127 | |
128 | Safefree (req->statdata); |
128 | Safefree (req->statdata); |
129 | } |
129 | } |
130 | |
130 | |
|
|
131 | ENTER; |
131 | PUSHMARK (SP); |
132 | PUSHMARK (SP); |
132 | XPUSHs (sv_2mortal (newSViv (req->result))); |
133 | XPUSHs (sv_2mortal (newSViv (req->result))); |
133 | |
134 | |
134 | if (req->type == REQ_OPEN) |
135 | if (req->type == REQ_OPEN) |
135 | { |
136 | { |
… | |
… | |
138 | |
139 | |
139 | PUTBACK; |
140 | PUTBACK; |
140 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
141 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
141 | SPAGAIN; |
142 | SPAGAIN; |
142 | |
143 | |
143 | fh = POPs; |
144 | fh = SvREFCNT_inc (POPs); |
144 | |
145 | |
145 | PUSHMARK (SP); |
146 | PUSHMARK (SP); |
146 | XPUSHs (fh); |
147 | XPUSHs (sv_2mortal (fh)); |
147 | } |
148 | } |
148 | |
149 | |
149 | if (SvOK (req->callback)) |
150 | if (SvOK (req->callback)) |
150 | { |
151 | { |
151 | PUTBACK; |
152 | PUTBACK; |
152 | call_sv (req->callback, G_VOID | G_EVAL); |
153 | call_sv (req->callback, G_VOID | G_EVAL); |
153 | SPAGAIN; |
154 | SPAGAIN; |
154 | } |
155 | } |
|
|
156 | |
|
|
157 | LEAVE; |
155 | |
158 | |
156 | if (req->callback) |
159 | if (req->callback) |
157 | SvREFCNT_dec (req->callback); |
160 | SvREFCNT_dec (req->callback); |
158 | |
161 | |
159 | errno = errorno; |
162 | errno = errorno; |
… | |
… | |
223 | end_thread (void) |
226 | end_thread (void) |
224 | { |
227 | { |
225 | aio_req req; |
228 | aio_req req; |
226 | New (0, req, 1, aio_cb); |
229 | New (0, req, 1, aio_cb); |
227 | req->type = REQ_QUIT; |
230 | req->type = REQ_QUIT; |
228 | |
|
|
229 | send_req (req); |
|
|
230 | } |
|
|
231 | |
|
|
232 | static void |
|
|
233 | read_write (int dowrite, int fd, off_t offset, size_t length, |
|
|
234 | SV *data, STRLEN dataoffset, SV *callback) |
|
|
235 | { |
|
|
236 | aio_req req; |
|
|
237 | STRLEN svlen; |
|
|
238 | char *svptr = SvPV (data, svlen); |
|
|
239 | |
|
|
240 | SvUPGRADE (data, SVt_PV); |
|
|
241 | SvPOK_on (data); |
|
|
242 | |
|
|
243 | if (dataoffset < 0) |
|
|
244 | dataoffset += svlen; |
|
|
245 | |
|
|
246 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
247 | croak ("data offset outside of string"); |
|
|
248 | |
|
|
249 | if (dowrite) |
|
|
250 | { |
|
|
251 | /* write: check length and adjust. */ |
|
|
252 | if (length < 0 || length + dataoffset > svlen) |
|
|
253 | length = svlen - dataoffset; |
|
|
254 | } |
|
|
255 | else |
|
|
256 | { |
|
|
257 | /* read: grow scalar as necessary */ |
|
|
258 | svptr = SvGROW (data, length + dataoffset); |
|
|
259 | } |
|
|
260 | |
|
|
261 | if (length < 0) |
|
|
262 | croak ("length must not be negative"); |
|
|
263 | |
|
|
264 | Newz (0, req, 1, aio_cb); |
|
|
265 | |
|
|
266 | if (!req) |
|
|
267 | croak ("out of memory during aio_req allocation"); |
|
|
268 | |
|
|
269 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
270 | req->fd = fd; |
|
|
271 | req->offset = offset; |
|
|
272 | req->length = length; |
|
|
273 | req->data = SvREFCNT_inc (data); |
|
|
274 | req->dataptr = (char *)svptr + dataoffset; |
|
|
275 | req->callback = SvREFCNT_inc (callback); |
|
|
276 | |
231 | |
277 | send_req (req); |
232 | send_req (req); |
278 | } |
233 | } |
279 | |
234 | |
280 | static void * |
235 | static void * |
… | |
… | |
442 | send_req (req); |
397 | send_req (req); |
443 | } |
398 | } |
444 | |
399 | |
445 | void |
400 | void |
446 | aio_close(fh,callback=&PL_sv_undef) |
401 | aio_close(fh,callback=&PL_sv_undef) |
447 | InputStream fh |
402 | SV * fh |
448 | SV * callback |
403 | SV * callback |
449 | PROTOTYPE: $;$ |
404 | PROTOTYPE: $;$ |
450 | ALIAS: |
405 | ALIAS: |
451 | aio_close = REQ_CLOSE |
406 | aio_close = REQ_CLOSE |
452 | aio_fsync = REQ_FSYNC |
407 | aio_fsync = REQ_FSYNC |
453 | aio_fdatasync = REQ_FDATASYNC |
408 | aio_fdatasync = REQ_FDATASYNC |
… | |
… | |
459 | |
414 | |
460 | if (!req) |
415 | if (!req) |
461 | croak ("out of memory during aio_req allocation"); |
416 | croak ("out of memory during aio_req allocation"); |
462 | |
417 | |
463 | req->type = ix; |
418 | req->type = ix; |
|
|
419 | req->fh = newSVsv (fh); |
464 | req->fd = PerlIO_fileno (fh); |
420 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
465 | req->callback = SvREFCNT_inc (callback); |
421 | req->callback = SvREFCNT_inc (callback); |
466 | |
422 | |
467 | send_req (req); |
423 | send_req (req); |
468 | } |
424 | } |
469 | |
425 | |
470 | void |
426 | void |
471 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
427 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
472 | InputStream fh |
428 | SV * fh |
473 | UV offset |
429 | UV offset |
474 | IV length |
430 | IV length |
475 | SV * data |
431 | SV * data |
476 | IV dataoffset |
432 | IV dataoffset |
477 | SV * callback |
433 | SV * callback |
|
|
434 | ALIAS: |
|
|
435 | aio_read = REQ_READ |
|
|
436 | aio_write = REQ_WRITE |
478 | PROTOTYPE: $$$$$;$ |
437 | PROTOTYPE: $$$$$;$ |
479 | CODE: |
438 | CODE: |
480 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
439 | { |
|
|
440 | aio_req req; |
|
|
441 | STRLEN svlen; |
|
|
442 | char *svptr = SvPV (data, svlen); |
481 | |
443 | |
482 | void |
444 | SvUPGRADE (data, SVt_PV); |
483 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
445 | SvPOK_on (data); |
484 | OutputStream fh |
446 | |
485 | UV offset |
447 | if (dataoffset < 0) |
486 | IV length |
448 | dataoffset += svlen; |
487 | SV * data |
449 | |
488 | IV dataoffset |
450 | if (dataoffset < 0 || dataoffset > svlen) |
489 | SV * callback |
451 | croak ("data offset outside of string"); |
490 | PROTOTYPE: $$$$$;$ |
452 | |
491 | CODE: |
453 | if (ix == REQ_WRITE) |
492 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
454 | { |
|
|
455 | /* write: check length and adjust. */ |
|
|
456 | if (length < 0 || length + dataoffset > svlen) |
|
|
457 | length = svlen - dataoffset; |
|
|
458 | } |
|
|
459 | else |
|
|
460 | { |
|
|
461 | /* read: grow scalar as necessary */ |
|
|
462 | svptr = SvGROW (data, length + dataoffset); |
|
|
463 | } |
|
|
464 | |
|
|
465 | if (length < 0) |
|
|
466 | croak ("length must not be negative"); |
|
|
467 | |
|
|
468 | Newz (0, req, 1, aio_cb); |
|
|
469 | |
|
|
470 | if (!req) |
|
|
471 | croak ("out of memory during aio_req allocation"); |
|
|
472 | |
|
|
473 | req->type = ix; |
|
|
474 | req->fh = newSVsv (fh); |
|
|
475 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
|
|
476 | : IoOFP (sv_2io (fh))); |
|
|
477 | req->offset = offset; |
|
|
478 | req->length = length; |
|
|
479 | req->data = SvREFCNT_inc (data); |
|
|
480 | req->dataptr = (char *)svptr + dataoffset; |
|
|
481 | req->callback = SvREFCNT_inc (callback); |
|
|
482 | |
|
|
483 | send_req (req); |
|
|
484 | } |
493 | |
485 | |
494 | void |
486 | void |
495 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
487 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
496 | InputStream fh |
488 | SV * fh |
497 | UV offset |
489 | UV offset |
498 | IV length |
490 | IV length |
499 | SV * callback |
491 | SV * callback |
500 | PROTOTYPE: $$$;$ |
492 | PROTOTYPE: $$$;$ |
501 | CODE: |
493 | CODE: |
502 | { |
494 | { |
503 | aio_req req; |
495 | aio_req req; |
504 | |
496 | |
… | |
… | |
509 | |
501 | |
510 | if (!req) |
502 | if (!req) |
511 | croak ("out of memory during aio_req allocation"); |
503 | croak ("out of memory during aio_req allocation"); |
512 | |
504 | |
513 | req->type = REQ_READAHEAD; |
505 | req->type = REQ_READAHEAD; |
|
|
506 | req->fh = newSVsv (fh); |
514 | req->fd = PerlIO_fileno (fh); |
507 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
515 | req->offset = offset; |
508 | req->offset = offset; |
516 | req->length = length; |
509 | req->length = length; |
517 | req->callback = SvREFCNT_inc (callback); |
510 | req->callback = SvREFCNT_inc (callback); |
518 | |
511 | |
519 | send_req (req); |
512 | send_req (req); |
… | |
… | |
547 | req->dataptr = SvPV_nolen (req->data); |
540 | req->dataptr = SvPV_nolen (req->data); |
548 | } |
541 | } |
549 | else |
542 | else |
550 | { |
543 | { |
551 | req->type = REQ_FSTAT; |
544 | req->type = REQ_FSTAT; |
|
|
545 | req->fh = newSVsv (fh_or_path); |
552 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
546 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
553 | } |
547 | } |
554 | |
548 | |
555 | req->callback = SvREFCNT_inc (callback); |
549 | req->callback = SvREFCNT_inc (callback); |
556 | |
550 | |