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