… | |
… | |
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; |
… | |
… | |
84 | poll_cb () |
84 | poll_cb () |
85 | { |
85 | { |
86 | dSP; |
86 | dSP; |
87 | int count = 0; |
87 | int count = 0; |
88 | aio_req req, prv; |
88 | aio_req req, prv; |
89 | |
|
|
90 | static int rl;//D |
|
|
91 | //printf ("%d ENTER\n", ++rl);//D |
|
|
92 | |
89 | |
93 | pthread_mutex_lock (&reslock); |
90 | pthread_mutex_lock (&reslock); |
94 | |
91 | |
95 | { |
92 | { |
96 | /* read any signals sent by the worker threads */ |
93 | /* read any signals sent by the worker threads */ |
… | |
… | |
105 | pthread_mutex_unlock (&reslock); |
102 | pthread_mutex_unlock (&reslock); |
106 | |
103 | |
107 | while (req) |
104 | while (req) |
108 | { |
105 | { |
109 | nreqs--; |
106 | nreqs--; |
110 | //printf ("%d count %d %p->%p\n", rl, count, req, req->next);//D |
|
|
111 | |
107 | |
112 | if (req->type == REQ_QUIT) |
108 | if (req->type == REQ_QUIT) |
113 | started--; |
109 | started--; |
114 | else |
110 | else |
115 | { |
111 | { |
… | |
… | |
121 | + req->result > 0 ? req->result : 0); |
117 | + req->result > 0 ? req->result : 0); |
122 | |
118 | |
123 | if (req->data) |
119 | if (req->data) |
124 | SvREFCNT_dec (req->data); |
120 | SvREFCNT_dec (req->data); |
125 | |
121 | |
|
|
122 | if (req->fh) |
|
|
123 | SvREFCNT_dec (req->fh); |
|
|
124 | |
126 | 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) |
127 | { |
126 | { |
128 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
127 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
129 | PL_laststatval = req->result; |
128 | PL_laststatval = req->result; |
130 | PL_statcache = *(req->statdata); |
129 | PL_statcache = *(req->statdata); |
131 | |
130 | |
132 | Safefree (req->statdata); |
131 | Safefree (req->statdata); |
133 | } |
132 | } |
134 | |
133 | |
|
|
134 | ENTER; |
135 | PUSHMARK (SP); |
135 | PUSHMARK (SP); |
136 | XPUSHs (sv_2mortal (newSViv (req->result))); |
136 | XPUSHs (sv_2mortal (newSViv (req->result))); |
137 | |
137 | |
138 | if (req->type == REQ_OPEN) |
138 | if (req->type == REQ_OPEN) |
139 | { |
139 | { |
… | |
… | |
142 | |
142 | |
143 | PUTBACK; |
143 | PUTBACK; |
144 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
144 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
145 | SPAGAIN; |
145 | SPAGAIN; |
146 | |
146 | |
147 | fh = POPs; |
147 | fh = SvREFCNT_inc (POPs); |
148 | |
148 | |
149 | PUSHMARK (SP); |
149 | PUSHMARK (SP); |
150 | XPUSHs (fh); |
150 | XPUSHs (sv_2mortal (fh)); |
151 | } |
151 | } |
152 | |
152 | |
153 | if (SvOK (req->callback)) |
153 | if (SvOK (req->callback)) |
154 | { |
154 | { |
155 | PUTBACK; |
155 | PUTBACK; |
156 | call_sv (req->callback, G_VOID | G_EVAL); |
156 | call_sv (req->callback, G_VOID | G_EVAL); |
157 | SPAGAIN; |
157 | SPAGAIN; |
158 | } |
158 | } |
|
|
159 | |
|
|
160 | LEAVE; |
159 | |
161 | |
160 | if (req->callback) |
162 | if (req->callback) |
161 | SvREFCNT_dec (req->callback); |
163 | SvREFCNT_dec (req->callback); |
162 | |
164 | |
163 | errno = errorno; |
165 | errno = errorno; |
… | |
… | |
169 | Safefree (prv); |
171 | Safefree (prv); |
170 | |
172 | |
171 | /* TODO: croak on errors? */ |
173 | /* TODO: croak on errors? */ |
172 | } |
174 | } |
173 | |
175 | |
174 | //printf ("%d LEAVE %p %p\n", rl--, ress, rese);//D |
|
|
175 | return count; |
176 | return count; |
176 | } |
177 | } |
177 | |
178 | |
178 | static void *aio_proc(void *arg); |
179 | static void *aio_proc(void *arg); |
179 | |
180 | |
… | |
… | |
228 | end_thread (void) |
229 | end_thread (void) |
229 | { |
230 | { |
230 | aio_req req; |
231 | aio_req req; |
231 | New (0, req, 1, aio_cb); |
232 | New (0, req, 1, aio_cb); |
232 | req->type = REQ_QUIT; |
233 | req->type = REQ_QUIT; |
233 | |
|
|
234 | send_req (req); |
|
|
235 | } |
|
|
236 | |
|
|
237 | static void |
|
|
238 | read_write (int dowrite, int fd, off_t offset, size_t length, |
|
|
239 | SV *data, STRLEN dataoffset, SV *callback) |
|
|
240 | { |
|
|
241 | aio_req req; |
|
|
242 | STRLEN svlen; |
|
|
243 | char *svptr = SvPV (data, svlen); |
|
|
244 | |
|
|
245 | SvUPGRADE (data, SVt_PV); |
|
|
246 | SvPOK_on (data); |
|
|
247 | |
|
|
248 | if (dataoffset < 0) |
|
|
249 | dataoffset += svlen; |
|
|
250 | |
|
|
251 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
252 | croak ("data offset outside of string"); |
|
|
253 | |
|
|
254 | if (dowrite) |
|
|
255 | { |
|
|
256 | /* write: check length and adjust. */ |
|
|
257 | if (length < 0 || length + dataoffset > svlen) |
|
|
258 | length = svlen - dataoffset; |
|
|
259 | } |
|
|
260 | else |
|
|
261 | { |
|
|
262 | /* read: grow scalar as necessary */ |
|
|
263 | svptr = SvGROW (data, length + dataoffset); |
|
|
264 | } |
|
|
265 | |
|
|
266 | if (length < 0) |
|
|
267 | croak ("length must not be negative"); |
|
|
268 | |
|
|
269 | Newz (0, req, 1, aio_cb); |
|
|
270 | |
|
|
271 | if (!req) |
|
|
272 | croak ("out of memory during aio_req allocation"); |
|
|
273 | |
|
|
274 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
275 | req->fd = fd; |
|
|
276 | req->offset = offset; |
|
|
277 | req->length = length; |
|
|
278 | req->data = SvREFCNT_inc (data); |
|
|
279 | req->dataptr = (char *)svptr + dataoffset; |
|
|
280 | req->callback = SvREFCNT_inc (callback); |
|
|
281 | |
234 | |
282 | send_req (req); |
235 | send_req (req); |
283 | } |
236 | } |
284 | |
237 | |
285 | static void * |
238 | static void * |
… | |
… | |
447 | send_req (req); |
400 | send_req (req); |
448 | } |
401 | } |
449 | |
402 | |
450 | void |
403 | void |
451 | aio_close(fh,callback=&PL_sv_undef) |
404 | aio_close(fh,callback=&PL_sv_undef) |
452 | InputStream fh |
405 | SV * fh |
453 | SV * callback |
406 | SV * callback |
454 | PROTOTYPE: $;$ |
407 | PROTOTYPE: $;$ |
455 | ALIAS: |
408 | ALIAS: |
456 | aio_close = REQ_CLOSE |
409 | aio_close = REQ_CLOSE |
457 | aio_fsync = REQ_FSYNC |
410 | aio_fsync = REQ_FSYNC |
458 | aio_fdatasync = REQ_FDATASYNC |
411 | aio_fdatasync = REQ_FDATASYNC |
… | |
… | |
464 | |
417 | |
465 | if (!req) |
418 | if (!req) |
466 | croak ("out of memory during aio_req allocation"); |
419 | croak ("out of memory during aio_req allocation"); |
467 | |
420 | |
468 | req->type = ix; |
421 | req->type = ix; |
|
|
422 | req->fh = newSVsv (fh); |
469 | req->fd = PerlIO_fileno (fh); |
423 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
470 | req->callback = SvREFCNT_inc (callback); |
424 | req->callback = SvREFCNT_inc (callback); |
471 | |
425 | |
472 | send_req (req); |
426 | send_req (req); |
473 | } |
427 | } |
474 | |
428 | |
475 | void |
429 | void |
476 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
430 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
477 | InputStream fh |
431 | SV * fh |
478 | UV offset |
432 | UV offset |
479 | IV length |
433 | IV length |
480 | SV * data |
434 | SV * data |
481 | IV dataoffset |
435 | IV dataoffset |
482 | SV * callback |
436 | SV * callback |
|
|
437 | ALIAS: |
|
|
438 | aio_read = REQ_READ |
|
|
439 | aio_write = REQ_WRITE |
483 | PROTOTYPE: $$$$$;$ |
440 | PROTOTYPE: $$$$$;$ |
484 | CODE: |
441 | CODE: |
485 | 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); |
486 | |
446 | |
487 | void |
447 | SvUPGRADE (data, SVt_PV); |
488 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
448 | SvPOK_on (data); |
489 | OutputStream fh |
449 | |
490 | UV offset |
450 | if (dataoffset < 0) |
491 | IV length |
451 | dataoffset += svlen; |
492 | SV * data |
452 | |
493 | IV dataoffset |
453 | if (dataoffset < 0 || dataoffset > svlen) |
494 | SV * callback |
454 | croak ("data offset outside of string"); |
495 | PROTOTYPE: $$$$$;$ |
455 | |
496 | CODE: |
456 | if (ix == REQ_WRITE) |
497 | 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 | } |
498 | |
488 | |
499 | void |
489 | void |
500 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
490 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
501 | InputStream fh |
491 | SV * fh |
502 | UV offset |
492 | UV offset |
503 | IV length |
493 | IV length |
504 | SV * callback |
494 | SV * callback |
505 | PROTOTYPE: $$$;$ |
495 | PROTOTYPE: $$$;$ |
506 | CODE: |
496 | CODE: |
507 | { |
497 | { |
508 | aio_req req; |
498 | aio_req req; |
509 | |
499 | |
… | |
… | |
514 | |
504 | |
515 | if (!req) |
505 | if (!req) |
516 | croak ("out of memory during aio_req allocation"); |
506 | croak ("out of memory during aio_req allocation"); |
517 | |
507 | |
518 | req->type = REQ_READAHEAD; |
508 | req->type = REQ_READAHEAD; |
|
|
509 | req->fh = newSVsv (fh); |
519 | req->fd = PerlIO_fileno (fh); |
510 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
520 | req->offset = offset; |
511 | req->offset = offset; |
521 | req->length = length; |
512 | req->length = length; |
522 | req->callback = SvREFCNT_inc (callback); |
513 | req->callback = SvREFCNT_inc (callback); |
523 | |
514 | |
524 | send_req (req); |
515 | send_req (req); |
… | |
… | |
552 | req->dataptr = SvPV_nolen (req->data); |
543 | req->dataptr = SvPV_nolen (req->data); |
553 | } |
544 | } |
554 | else |
545 | else |
555 | { |
546 | { |
556 | req->type = REQ_FSTAT; |
547 | req->type = REQ_FSTAT; |
|
|
548 | req->fh = newSVsv (fh_or_path); |
557 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
549 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
558 | } |
550 | } |
559 | |
551 | |
560 | req->callback = SvREFCNT_inc (callback); |
552 | req->callback = SvREFCNT_inc (callback); |
561 | |
553 | |