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