1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
|
|
5 | #include "autoconf/config.h" |
|
|
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 | |
|
|
15 | #ifndef _REENTRANT |
|
|
16 | # define _REENTRANT 1 |
|
|
17 | #endif |
11 | #include <endian.h> |
18 | #include <errno.h> |
12 | |
19 | |
13 | #include <pthread.h> |
20 | #include <pthread.h> |
14 | #include <sys/syscall.h> |
|
|
15 | |
21 | |
16 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
22 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
17 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
23 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
18 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
24 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
19 | |
25 | |
… | |
… | |
40 | off_t offset; |
46 | off_t offset; |
41 | size_t length; |
47 | size_t length; |
42 | ssize_t result; |
48 | ssize_t result; |
43 | mode_t mode; /* open */ |
49 | mode_t mode; /* open */ |
44 | int errorno; |
50 | int errorno; |
45 | SV *data, *callback; |
51 | SV *data, *callback, *fh; |
46 | void *dataptr; |
52 | void *dataptr; |
47 | STRLEN dataoffset; |
53 | STRLEN dataoffset; |
48 | |
54 | |
49 | Stat_t *statdata; |
55 | Stat_t *statdata; |
50 | } aio_cb; |
56 | } aio_cb; |
51 | |
57 | |
52 | typedef aio_cb *aio_req; |
58 | typedef aio_cb *aio_req; |
53 | |
59 | |
54 | static int started; |
60 | static int started; |
55 | static int nreqs; |
61 | static volatile int nreqs; |
56 | static int max_outstanding = 1<<30; |
62 | static int max_outstanding = 1<<30; |
57 | static int respipe [2]; |
63 | static int respipe [2]; |
58 | |
64 | |
59 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
60 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
66 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
64 | static volatile aio_req ress, rese; /* queue start, queue end */ |
70 | static volatile aio_req ress, rese; /* queue start, queue end */ |
65 | |
71 | |
66 | static void |
72 | static void |
67 | poll_wait () |
73 | poll_wait () |
68 | { |
74 | { |
69 | if (!nreqs) |
75 | if (nreqs && !ress) |
70 | return; |
76 | { |
71 | |
|
|
72 | fd_set rfd; |
77 | fd_set rfd; |
73 | FD_ZERO(&rfd); |
78 | FD_ZERO(&rfd); |
74 | FD_SET(respipe [0], &rfd); |
79 | FD_SET(respipe [0], &rfd); |
75 | |
80 | |
76 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
81 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
82 | } |
77 | } |
83 | } |
78 | |
84 | |
79 | static int |
85 | static int |
80 | poll_cb () |
86 | poll_cb () |
81 | { |
87 | { |
82 | dSP; |
88 | dSP; |
83 | int count = 0; |
89 | int count = 0; |
84 | aio_req req; |
90 | aio_req req, prv; |
85 | |
91 | |
|
|
92 | pthread_mutex_lock (&reslock); |
|
|
93 | |
86 | { |
94 | { |
87 | /* read and signals sent by the worker threads */ |
95 | /* read any signals sent by the worker threads */ |
88 | char buf [32]; |
96 | char buf [32]; |
89 | while (read (respipe [0], buf, 32) > 0) |
97 | while (read (respipe [0], buf, 32) > 0) |
90 | ; |
98 | ; |
91 | } |
99 | } |
92 | |
100 | |
93 | for (;;) |
101 | req = ress; |
|
|
102 | ress = rese = 0; |
|
|
103 | |
|
|
104 | pthread_mutex_unlock (&reslock); |
|
|
105 | |
|
|
106 | while (req) |
94 | { |
107 | { |
95 | pthread_mutex_lock (&reslock); |
|
|
96 | |
|
|
97 | req = ress; |
|
|
98 | |
|
|
99 | if (ress) |
|
|
100 | { |
|
|
101 | ress = ress->next; |
|
|
102 | if (!ress) rese = 0; |
|
|
103 | } |
|
|
104 | |
|
|
105 | pthread_mutex_unlock (&reslock); |
|
|
106 | |
|
|
107 | if (!req) |
|
|
108 | break; |
|
|
109 | |
|
|
110 | nreqs--; |
108 | nreqs--; |
111 | |
109 | |
112 | if (req->type == REQ_QUIT) |
110 | if (req->type == REQ_QUIT) |
113 | started--; |
111 | started--; |
114 | else |
112 | else |
… | |
… | |
121 | + req->result > 0 ? req->result : 0); |
119 | + req->result > 0 ? req->result : 0); |
122 | |
120 | |
123 | if (req->data) |
121 | if (req->data) |
124 | SvREFCNT_dec (req->data); |
122 | SvREFCNT_dec (req->data); |
125 | |
123 | |
|
|
124 | if (req->fh) |
|
|
125 | SvREFCNT_dec (req->fh); |
|
|
126 | |
126 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
127 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
127 | { |
128 | { |
128 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
129 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
129 | PL_laststatval = req->result; |
130 | PL_laststatval = req->result; |
130 | PL_statcache = *(req->statdata); |
131 | PL_statcache = *(req->statdata); |
131 | |
132 | |
132 | Safefree (req->statdata); |
133 | Safefree (req->statdata); |
133 | } |
134 | } |
134 | |
135 | |
|
|
136 | ENTER; |
135 | PUSHMARK (SP); |
137 | PUSHMARK (SP); |
136 | XPUSHs (sv_2mortal (newSViv (req->result))); |
138 | XPUSHs (sv_2mortal (newSViv (req->result))); |
137 | |
139 | |
138 | if (req->type == REQ_OPEN) |
140 | if (req->type == REQ_OPEN) |
139 | { |
141 | { |
… | |
… | |
142 | |
144 | |
143 | PUTBACK; |
145 | PUTBACK; |
144 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
146 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
145 | SPAGAIN; |
147 | SPAGAIN; |
146 | |
148 | |
147 | fh = POPs; |
149 | fh = SvREFCNT_inc (POPs); |
148 | |
150 | |
149 | PUSHMARK (SP); |
151 | PUSHMARK (SP); |
150 | XPUSHs (fh); |
152 | XPUSHs (sv_2mortal (fh)); |
151 | } |
153 | } |
152 | |
154 | |
153 | if (SvOK (req->callback)) |
155 | if (SvOK (req->callback)) |
154 | { |
156 | { |
155 | PUTBACK; |
157 | PUTBACK; |
156 | call_sv (req->callback, G_VOID | G_EVAL); |
158 | call_sv (req->callback, G_VOID | G_EVAL); |
157 | SPAGAIN; |
159 | SPAGAIN; |
158 | } |
160 | } |
|
|
161 | |
|
|
162 | LEAVE; |
159 | |
163 | |
160 | if (req->callback) |
164 | if (req->callback) |
161 | SvREFCNT_dec (req->callback); |
165 | SvREFCNT_dec (req->callback); |
162 | |
166 | |
163 | errno = errorno; |
167 | errno = errorno; |
164 | count++; |
168 | count++; |
165 | } |
169 | } |
166 | |
170 | |
|
|
171 | prv = req; |
|
|
172 | req = req->next; |
167 | Safefree (req); |
173 | Safefree (prv); |
|
|
174 | |
|
|
175 | /* TODO: croak on errors? */ |
168 | } |
176 | } |
169 | |
177 | |
170 | return count; |
178 | return count; |
171 | } |
179 | } |
172 | |
180 | |
… | |
… | |
227 | req->type = REQ_QUIT; |
235 | req->type = REQ_QUIT; |
228 | |
236 | |
229 | send_req (req); |
237 | send_req (req); |
230 | } |
238 | } |
231 | |
239 | |
232 | static void |
240 | /* work around various missing functions */ |
233 | read_write (int dowrite, int fd, off_t offset, size_t length, |
|
|
234 | SV *data, STRLEN dataoffset, SV *callback) |
|
|
235 | { |
|
|
236 | aio_req req; |
|
|
237 | STRLEN svlen; |
|
|
238 | char *svptr = SvPV (data, svlen); |
|
|
239 | |
241 | |
240 | SvUPGRADE (data, SVt_PV); |
242 | #if !HAVE_PREADWRITE |
241 | SvPOK_on (data); |
243 | # define pread aio_pread |
|
|
244 | # define pwrite aio_pwrite |
242 | |
245 | |
243 | if (dataoffset < 0) |
246 | /* |
244 | dataoffset += svlen; |
247 | * make our pread/pwrite safe against themselves, but not against |
|
|
248 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
249 | * but that's your problem, not mine. |
|
|
250 | */ |
|
|
251 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
245 | |
252 | |
246 | if (dataoffset < 0 || dataoffset > svlen) |
253 | static ssize_t |
247 | croak ("data offset outside of string"); |
254 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
255 | { |
|
|
256 | ssize_t res; |
|
|
257 | off_t ooffset; |
248 | |
258 | |
249 | if (dowrite) |
259 | pthread_mutex_lock (&iolock); |
|
|
260 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
261 | lseek (fd, offset, SEEK_SET); |
|
|
262 | res = read (fd, buf, count); |
|
|
263 | lseek (fd, ooffset, SEEK_SET); |
|
|
264 | pthread_mutex_unlock (&iolock); |
|
|
265 | |
|
|
266 | return res; |
|
|
267 | } |
|
|
268 | |
|
|
269 | static ssize_t |
|
|
270 | pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
271 | { |
|
|
272 | ssize_t res; |
|
|
273 | off_t ooffset; |
|
|
274 | |
|
|
275 | pthread_mutex_lock (&iolock); |
|
|
276 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
277 | lseek (fd, offset, SEEK_SET); |
|
|
278 | res = write (fd, buf, count); |
|
|
279 | lseek (fd, offset, SEEK_SET); |
|
|
280 | pthread_mutex_unlock (&iolock); |
|
|
281 | |
|
|
282 | return res; |
|
|
283 | } |
|
|
284 | #endif |
|
|
285 | |
|
|
286 | #if !HAVE_FDATASYNC |
|
|
287 | # define fdatasync fsync |
|
|
288 | #endif |
|
|
289 | |
|
|
290 | #if !HAVE_READAHEAD |
|
|
291 | # define readahead aio_readahead |
|
|
292 | |
|
|
293 | static char readahead_buf[4096]; |
|
|
294 | |
|
|
295 | static ssize_t |
|
|
296 | readahead (int fd, off_t offset, size_t count) |
|
|
297 | { |
|
|
298 | while (count > 0) |
250 | { |
299 | { |
251 | /* write: check length and adjust. */ |
300 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
252 | if (length < 0 || length + dataoffset > svlen) |
301 | |
253 | length = svlen - dataoffset; |
302 | pread (fd, readahead_buf, len, offset); |
|
|
303 | offset += len; |
|
|
304 | count -= len; |
254 | } |
305 | } |
255 | else |
|
|
256 | { |
|
|
257 | /* read: grow scalar as necessary */ |
|
|
258 | svptr = SvGROW (data, length + dataoffset); |
|
|
259 | } |
|
|
260 | |
306 | |
261 | if (length < 0) |
307 | errno = 0; |
262 | croak ("length must not be negative"); |
|
|
263 | |
|
|
264 | Newz (0, req, 1, aio_cb); |
|
|
265 | |
|
|
266 | if (!req) |
|
|
267 | croak ("out of memory during aio_req allocation"); |
|
|
268 | |
|
|
269 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
270 | req->fd = fd; |
|
|
271 | req->offset = offset; |
|
|
272 | req->length = length; |
|
|
273 | req->data = SvREFCNT_inc (data); |
|
|
274 | req->dataptr = (char *)svptr + dataoffset; |
|
|
275 | req->callback = SvREFCNT_inc (callback); |
|
|
276 | |
|
|
277 | send_req (req); |
|
|
278 | } |
308 | } |
|
|
309 | #endif |
279 | |
310 | |
280 | static void * |
311 | static void * |
281 | aio_proc (void *thr_arg) |
312 | aio_proc (void *thr_arg) |
282 | { |
313 | { |
283 | aio_req req; |
314 | aio_req req; |
… | |
… | |
309 | |
340 | |
310 | type = req->type; |
341 | type = req->type; |
311 | |
342 | |
312 | switch (type) |
343 | switch (type) |
313 | { |
344 | { |
314 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
345 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
315 | case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; |
346 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
316 | #if SYS_readahead |
347 | |
317 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
348 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
318 | #else |
|
|
319 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
|
|
320 | #endif |
|
|
321 | |
349 | |
322 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
350 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
323 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
351 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
324 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
352 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
325 | |
353 | |
326 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
354 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
327 | case REQ_CLOSE: req->result = close (req->fd); break; |
355 | case REQ_CLOSE: req->result = close (req->fd); break; |
328 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
356 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
329 | |
357 | |
|
|
358 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
330 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
359 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
331 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
332 | |
360 | |
333 | case REQ_QUIT: |
361 | case REQ_QUIT: |
334 | break; |
362 | break; |
335 | |
363 | |
336 | default: |
364 | default: |
… | |
… | |
442 | send_req (req); |
470 | send_req (req); |
443 | } |
471 | } |
444 | |
472 | |
445 | void |
473 | void |
446 | aio_close(fh,callback=&PL_sv_undef) |
474 | aio_close(fh,callback=&PL_sv_undef) |
447 | InputStream fh |
475 | SV * fh |
448 | SV * callback |
476 | SV * callback |
449 | PROTOTYPE: $;$ |
477 | PROTOTYPE: $;$ |
450 | ALIAS: |
478 | ALIAS: |
451 | aio_close = REQ_CLOSE |
479 | aio_close = REQ_CLOSE |
452 | aio_fsync = REQ_FSYNC |
480 | aio_fsync = REQ_FSYNC |
453 | aio_fdatasync = REQ_FDATASYNC |
481 | aio_fdatasync = REQ_FDATASYNC |
… | |
… | |
459 | |
487 | |
460 | if (!req) |
488 | if (!req) |
461 | croak ("out of memory during aio_req allocation"); |
489 | croak ("out of memory during aio_req allocation"); |
462 | |
490 | |
463 | req->type = ix; |
491 | req->type = ix; |
|
|
492 | req->fh = newSVsv (fh); |
464 | req->fd = PerlIO_fileno (fh); |
493 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
465 | req->callback = SvREFCNT_inc (callback); |
494 | req->callback = SvREFCNT_inc (callback); |
466 | |
495 | |
467 | send_req (req); |
496 | send_req (req); |
468 | } |
497 | } |
469 | |
498 | |
470 | void |
499 | void |
471 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
500 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
472 | InputStream fh |
501 | SV * fh |
473 | UV offset |
502 | UV offset |
474 | IV length |
503 | IV length |
475 | SV * data |
504 | SV * data |
476 | IV dataoffset |
505 | IV dataoffset |
477 | SV * callback |
506 | SV * callback |
|
|
507 | ALIAS: |
|
|
508 | aio_read = REQ_READ |
|
|
509 | aio_write = REQ_WRITE |
478 | PROTOTYPE: $$$$$;$ |
510 | PROTOTYPE: $$$$$;$ |
479 | CODE: |
511 | CODE: |
480 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
512 | { |
|
|
513 | aio_req req; |
|
|
514 | STRLEN svlen; |
|
|
515 | char *svptr = SvPV (data, svlen); |
481 | |
516 | |
482 | void |
517 | SvUPGRADE (data, SVt_PV); |
483 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
518 | SvPOK_on (data); |
484 | OutputStream fh |
519 | |
485 | UV offset |
520 | if (dataoffset < 0) |
486 | IV length |
521 | dataoffset += svlen; |
487 | SV * data |
522 | |
488 | IV dataoffset |
523 | if (dataoffset < 0 || dataoffset > svlen) |
489 | SV * callback |
524 | croak ("data offset outside of string"); |
490 | PROTOTYPE: $$$$$;$ |
525 | |
491 | CODE: |
526 | if (ix == REQ_WRITE) |
492 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
527 | { |
|
|
528 | /* write: check length and adjust. */ |
|
|
529 | if (length < 0 || length + dataoffset > svlen) |
|
|
530 | length = svlen - dataoffset; |
|
|
531 | } |
|
|
532 | else |
|
|
533 | { |
|
|
534 | /* read: grow scalar as necessary */ |
|
|
535 | svptr = SvGROW (data, length + dataoffset); |
|
|
536 | } |
|
|
537 | |
|
|
538 | if (length < 0) |
|
|
539 | croak ("length must not be negative"); |
|
|
540 | |
|
|
541 | Newz (0, req, 1, aio_cb); |
|
|
542 | |
|
|
543 | if (!req) |
|
|
544 | croak ("out of memory during aio_req allocation"); |
|
|
545 | |
|
|
546 | req->type = ix; |
|
|
547 | req->fh = newSVsv (fh); |
|
|
548 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
|
|
549 | : IoOFP (sv_2io (fh))); |
|
|
550 | req->offset = offset; |
|
|
551 | req->length = length; |
|
|
552 | req->data = SvREFCNT_inc (data); |
|
|
553 | req->dataptr = (char *)svptr + dataoffset; |
|
|
554 | req->callback = SvREFCNT_inc (callback); |
|
|
555 | |
|
|
556 | send_req (req); |
|
|
557 | } |
493 | |
558 | |
494 | void |
559 | void |
495 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
560 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
496 | InputStream fh |
561 | SV * fh |
497 | UV offset |
562 | UV offset |
498 | IV length |
563 | IV length |
499 | SV * callback |
564 | SV * callback |
500 | PROTOTYPE: $$$;$ |
565 | PROTOTYPE: $$$;$ |
501 | CODE: |
566 | CODE: |
502 | { |
567 | { |
503 | aio_req req; |
568 | aio_req req; |
504 | |
569 | |
… | |
… | |
509 | |
574 | |
510 | if (!req) |
575 | if (!req) |
511 | croak ("out of memory during aio_req allocation"); |
576 | croak ("out of memory during aio_req allocation"); |
512 | |
577 | |
513 | req->type = REQ_READAHEAD; |
578 | req->type = REQ_READAHEAD; |
|
|
579 | req->fh = newSVsv (fh); |
514 | req->fd = PerlIO_fileno (fh); |
580 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
515 | req->offset = offset; |
581 | req->offset = offset; |
516 | req->length = length; |
582 | req->length = length; |
517 | req->callback = SvREFCNT_inc (callback); |
583 | req->callback = SvREFCNT_inc (callback); |
518 | |
584 | |
519 | send_req (req); |
585 | send_req (req); |
… | |
… | |
547 | req->dataptr = SvPV_nolen (req->data); |
613 | req->dataptr = SvPV_nolen (req->data); |
548 | } |
614 | } |
549 | else |
615 | else |
550 | { |
616 | { |
551 | req->type = REQ_FSTAT; |
617 | req->type = REQ_FSTAT; |
|
|
618 | req->fh = newSVsv (fh_or_path); |
552 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
619 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
553 | } |
620 | } |
554 | |
621 | |
555 | req->callback = SvREFCNT_inc (callback); |
622 | req->callback = SvREFCNT_inc (callback); |
556 | |
623 | |