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