1 | #include "EXTERN.h" |
|
|
2 | #include "perl.h" |
1 | #include "perl.h" |
3 | #include "XSUB.h" |
2 | #include "XSUB.h" |
4 | |
|
|
5 | #define _XOPEN_SOURCE 500 |
|
|
6 | |
3 | |
7 | #include <sys/types.h> |
4 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
5 | #include <sys/stat.h> |
9 | |
6 | |
10 | #include <unistd.h> |
7 | #include <unistd.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; |
55 | |
52 | |
56 | typedef aio_cb *aio_req; |
53 | typedef aio_cb *aio_req; |
57 | |
54 | |
58 | static int started; |
55 | static int started; |
59 | static int nreqs; |
56 | static volatile int nreqs; |
60 | static int max_outstanding = 1<<30; |
57 | static int max_outstanding = 1<<30; |
61 | static int respipe [2]; |
58 | static int respipe [2]; |
62 | |
59 | |
63 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
60 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
64 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
61 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
68 | static volatile aio_req ress, rese; /* queue start, queue end */ |
65 | static volatile aio_req ress, rese; /* queue start, queue end */ |
69 | |
66 | |
70 | static void |
67 | static void |
71 | poll_wait () |
68 | poll_wait () |
72 | { |
69 | { |
73 | if (!nreqs) |
70 | if (nreqs && !ress) |
74 | return; |
71 | { |
75 | |
|
|
76 | fd_set rfd; |
72 | fd_set rfd; |
77 | FD_ZERO(&rfd); |
73 | FD_ZERO(&rfd); |
78 | FD_SET(respipe [0], &rfd); |
74 | FD_SET(respipe [0], &rfd); |
79 | |
75 | |
80 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
76 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
77 | } |
81 | } |
78 | } |
82 | |
79 | |
83 | static int |
80 | static int |
84 | poll_cb () |
81 | poll_cb () |
85 | { |
82 | { |
86 | dSP; |
83 | dSP; |
87 | int count = 0; |
84 | int count = 0; |
88 | aio_req req; |
85 | aio_req req, prv; |
89 | |
86 | |
|
|
87 | pthread_mutex_lock (&reslock); |
|
|
88 | |
90 | { |
89 | { |
91 | /* read and signals sent by the worker threads */ |
90 | /* read any signals sent by the worker threads */ |
92 | char buf [32]; |
91 | char buf [32]; |
93 | while (read (respipe [0], buf, 32) > 0) |
92 | while (read (respipe [0], buf, 32) > 0) |
94 | ; |
93 | ; |
95 | } |
94 | } |
96 | |
95 | |
97 | for (;;) |
96 | req = ress; |
|
|
97 | ress = rese = 0; |
|
|
98 | |
|
|
99 | pthread_mutex_unlock (&reslock); |
|
|
100 | |
|
|
101 | while (req) |
98 | { |
102 | { |
99 | pthread_mutex_lock (&reslock); |
|
|
100 | |
|
|
101 | req = ress; |
|
|
102 | |
|
|
103 | if (ress) |
|
|
104 | { |
|
|
105 | ress = ress->next; |
|
|
106 | if (!ress) rese = 0; |
|
|
107 | } |
|
|
108 | |
|
|
109 | pthread_mutex_unlock (&reslock); |
|
|
110 | |
|
|
111 | if (!req) |
|
|
112 | break; |
|
|
113 | |
|
|
114 | nreqs--; |
103 | nreqs--; |
115 | |
104 | |
116 | if (req->type == REQ_QUIT) |
105 | if (req->type == REQ_QUIT) |
117 | started--; |
106 | started--; |
118 | else |
107 | else |
… | |
… | |
125 | + req->result > 0 ? req->result : 0); |
114 | + req->result > 0 ? req->result : 0); |
126 | |
115 | |
127 | if (req->data) |
116 | if (req->data) |
128 | SvREFCNT_dec (req->data); |
117 | SvREFCNT_dec (req->data); |
129 | |
118 | |
|
|
119 | if (req->fh) |
|
|
120 | SvREFCNT_dec (req->fh); |
|
|
121 | |
130 | 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) |
131 | { |
123 | { |
132 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
124 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
133 | PL_laststatval = req->result; |
125 | PL_laststatval = req->result; |
134 | PL_statcache = *(req->statdata); |
126 | PL_statcache = *(req->statdata); |
135 | |
127 | |
136 | Safefree (req->statdata); |
128 | Safefree (req->statdata); |
137 | } |
129 | } |
138 | |
130 | |
|
|
131 | ENTER; |
139 | PUSHMARK (SP); |
132 | PUSHMARK (SP); |
140 | XPUSHs (sv_2mortal (newSViv (req->result))); |
133 | XPUSHs (sv_2mortal (newSViv (req->result))); |
141 | |
134 | |
142 | if (req->type == REQ_OPEN) |
135 | if (req->type == REQ_OPEN) |
143 | { |
136 | { |
… | |
… | |
146 | |
139 | |
147 | PUTBACK; |
140 | PUTBACK; |
148 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
141 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
149 | SPAGAIN; |
142 | SPAGAIN; |
150 | |
143 | |
151 | fh = POPs; |
144 | fh = SvREFCNT_inc (POPs); |
152 | |
145 | |
153 | PUSHMARK (SP); |
146 | PUSHMARK (SP); |
154 | XPUSHs (fh); |
147 | XPUSHs (sv_2mortal (fh)); |
155 | } |
148 | } |
156 | |
149 | |
157 | if (SvOK (req->callback)) |
150 | if (SvOK (req->callback)) |
158 | { |
151 | { |
159 | PUTBACK; |
152 | PUTBACK; |
160 | call_sv (req->callback, G_VOID | G_EVAL); |
153 | call_sv (req->callback, G_VOID | G_EVAL); |
161 | SPAGAIN; |
154 | SPAGAIN; |
162 | } |
155 | } |
|
|
156 | |
|
|
157 | LEAVE; |
163 | |
158 | |
164 | if (req->callback) |
159 | if (req->callback) |
165 | SvREFCNT_dec (req->callback); |
160 | SvREFCNT_dec (req->callback); |
166 | |
161 | |
167 | errno = errorno; |
162 | errno = errorno; |
168 | count++; |
163 | count++; |
169 | } |
164 | } |
170 | |
165 | |
|
|
166 | prv = req; |
|
|
167 | req = req->next; |
171 | Safefree (req); |
168 | Safefree (prv); |
|
|
169 | |
|
|
170 | /* TODO: croak on errors? */ |
172 | } |
171 | } |
173 | |
172 | |
174 | return count; |
173 | return count; |
175 | } |
174 | } |
176 | |
175 | |
… | |
… | |
227 | end_thread (void) |
226 | end_thread (void) |
228 | { |
227 | { |
229 | aio_req req; |
228 | aio_req req; |
230 | New (0, req, 1, aio_cb); |
229 | New (0, req, 1, aio_cb); |
231 | req->type = REQ_QUIT; |
230 | req->type = REQ_QUIT; |
232 | |
|
|
233 | send_req (req); |
|
|
234 | } |
|
|
235 | |
|
|
236 | static void |
|
|
237 | read_write (int dowrite, int fd, off_t offset, size_t length, |
|
|
238 | SV *data, STRLEN dataoffset, SV *callback) |
|
|
239 | { |
|
|
240 | aio_req req; |
|
|
241 | STRLEN svlen; |
|
|
242 | char *svptr = SvPV (data, svlen); |
|
|
243 | |
|
|
244 | SvUPGRADE (data, SVt_PV); |
|
|
245 | SvPOK_on (data); |
|
|
246 | |
|
|
247 | if (dataoffset < 0) |
|
|
248 | dataoffset += svlen; |
|
|
249 | |
|
|
250 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
251 | croak ("data offset outside of string"); |
|
|
252 | |
|
|
253 | if (dowrite) |
|
|
254 | { |
|
|
255 | /* write: check length and adjust. */ |
|
|
256 | if (length < 0 || length + dataoffset > svlen) |
|
|
257 | length = svlen - dataoffset; |
|
|
258 | } |
|
|
259 | else |
|
|
260 | { |
|
|
261 | /* read: grow scalar as necessary */ |
|
|
262 | svptr = SvGROW (data, length + dataoffset); |
|
|
263 | } |
|
|
264 | |
|
|
265 | if (length < 0) |
|
|
266 | croak ("length must not be negative"); |
|
|
267 | |
|
|
268 | Newz (0, req, 1, aio_cb); |
|
|
269 | |
|
|
270 | if (!req) |
|
|
271 | croak ("out of memory during aio_req allocation"); |
|
|
272 | |
|
|
273 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
274 | req->fd = fd; |
|
|
275 | req->offset = offset; |
|
|
276 | req->length = length; |
|
|
277 | req->data = SvREFCNT_inc (data); |
|
|
278 | req->dataptr = (char *)svptr + dataoffset; |
|
|
279 | req->callback = SvREFCNT_inc (callback); |
|
|
280 | |
231 | |
281 | send_req (req); |
232 | send_req (req); |
282 | } |
233 | } |
283 | |
234 | |
284 | static void * |
235 | static void * |
… | |
… | |
446 | send_req (req); |
397 | send_req (req); |
447 | } |
398 | } |
448 | |
399 | |
449 | void |
400 | void |
450 | aio_close(fh,callback=&PL_sv_undef) |
401 | aio_close(fh,callback=&PL_sv_undef) |
451 | InputStream fh |
402 | SV * fh |
452 | SV * callback |
403 | SV * callback |
453 | PROTOTYPE: $;$ |
404 | PROTOTYPE: $;$ |
454 | ALIAS: |
405 | ALIAS: |
455 | aio_close = REQ_CLOSE |
406 | aio_close = REQ_CLOSE |
456 | aio_fsync = REQ_FSYNC |
407 | aio_fsync = REQ_FSYNC |
457 | aio_fdatasync = REQ_FDATASYNC |
408 | aio_fdatasync = REQ_FDATASYNC |
… | |
… | |
463 | |
414 | |
464 | if (!req) |
415 | if (!req) |
465 | croak ("out of memory during aio_req allocation"); |
416 | croak ("out of memory during aio_req allocation"); |
466 | |
417 | |
467 | req->type = ix; |
418 | req->type = ix; |
|
|
419 | req->fh = newSVsv (fh); |
468 | req->fd = PerlIO_fileno (fh); |
420 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
469 | req->callback = SvREFCNT_inc (callback); |
421 | req->callback = SvREFCNT_inc (callback); |
470 | |
422 | |
471 | send_req (req); |
423 | send_req (req); |
472 | } |
424 | } |
473 | |
425 | |
474 | void |
426 | void |
475 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
427 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
476 | InputStream fh |
428 | SV * fh |
477 | UV offset |
429 | UV offset |
478 | IV length |
430 | IV length |
479 | SV * data |
431 | SV * data |
480 | IV dataoffset |
432 | IV dataoffset |
481 | SV * callback |
433 | SV * callback |
|
|
434 | ALIAS: |
|
|
435 | aio_read = REQ_READ |
|
|
436 | aio_write = REQ_WRITE |
482 | PROTOTYPE: $$$$$;$ |
437 | PROTOTYPE: $$$$$;$ |
483 | CODE: |
438 | CODE: |
484 | 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); |
485 | |
443 | |
486 | void |
444 | SvUPGRADE (data, SVt_PV); |
487 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
445 | SvPOK_on (data); |
488 | OutputStream fh |
446 | |
489 | UV offset |
447 | if (dataoffset < 0) |
490 | IV length |
448 | dataoffset += svlen; |
491 | SV * data |
449 | |
492 | IV dataoffset |
450 | if (dataoffset < 0 || dataoffset > svlen) |
493 | SV * callback |
451 | croak ("data offset outside of string"); |
494 | PROTOTYPE: $$$$$;$ |
452 | |
495 | CODE: |
453 | if (ix == REQ_WRITE) |
496 | 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 | } |
497 | |
485 | |
498 | void |
486 | void |
499 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
487 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
500 | InputStream fh |
488 | SV * fh |
501 | UV offset |
489 | UV offset |
502 | IV length |
490 | IV length |
503 | SV * callback |
491 | SV * callback |
504 | PROTOTYPE: $$$;$ |
492 | PROTOTYPE: $$$;$ |
505 | CODE: |
493 | CODE: |
506 | { |
494 | { |
507 | aio_req req; |
495 | aio_req req; |
508 | |
496 | |
… | |
… | |
513 | |
501 | |
514 | if (!req) |
502 | if (!req) |
515 | croak ("out of memory during aio_req allocation"); |
503 | croak ("out of memory during aio_req allocation"); |
516 | |
504 | |
517 | req->type = REQ_READAHEAD; |
505 | req->type = REQ_READAHEAD; |
|
|
506 | req->fh = newSVsv (fh); |
518 | req->fd = PerlIO_fileno (fh); |
507 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
519 | req->offset = offset; |
508 | req->offset = offset; |
520 | req->length = length; |
509 | req->length = length; |
521 | req->callback = SvREFCNT_inc (callback); |
510 | req->callback = SvREFCNT_inc (callback); |
522 | |
511 | |
523 | send_req (req); |
512 | send_req (req); |
… | |
… | |
551 | req->dataptr = SvPV_nolen (req->data); |
540 | req->dataptr = SvPV_nolen (req->data); |
552 | } |
541 | } |
553 | else |
542 | else |
554 | { |
543 | { |
555 | req->type = REQ_FSTAT; |
544 | req->type = REQ_FSTAT; |
|
|
545 | req->fh = newSVsv (fh_or_path); |
556 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
546 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
557 | } |
547 | } |
558 | |
548 | |
559 | req->callback = SvREFCNT_inc (callback); |
549 | req->callback = SvREFCNT_inc (callback); |
560 | |
550 | |