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