|
|
1 | #define _REENTRANT 1 |
|
|
2 | #include <errno.h> |
|
|
3 | |
1 | #include "EXTERN.h" |
4 | #include "EXTERN.h" |
2 | #include "perl.h" |
5 | #include "perl.h" |
3 | #include "XSUB.h" |
6 | #include "XSUB.h" |
4 | |
7 | |
5 | #define _XOPEN_SOURCE 500 |
8 | #include "autoconf/config.h" |
6 | |
9 | |
7 | #include <sys/types.h> |
10 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
11 | #include <sys/stat.h> |
9 | |
12 | |
10 | #include <unistd.h> |
13 | #include <unistd.h> |
11 | #include <fcntl.h> |
14 | #include <fcntl.h> |
12 | #include <signal.h> |
15 | #include <signal.h> |
13 | #include <sched.h> |
16 | #include <sched.h> |
14 | #if __linux |
|
|
15 | #include <sys/syscall.h> |
|
|
16 | #endif |
|
|
17 | |
17 | |
18 | #include <pthread.h> |
18 | #include <pthread.h> |
19 | |
19 | |
20 | 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 ;) */ |
21 | 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 ;) */ |
… | |
… | |
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 | |
… | |
… | |
231 | req->type = REQ_QUIT; |
233 | req->type = REQ_QUIT; |
232 | |
234 | |
233 | send_req (req); |
235 | send_req (req); |
234 | } |
236 | } |
235 | |
237 | |
236 | static void |
238 | /* work around various missing functions */ |
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 | |
239 | |
244 | SvUPGRADE (data, SVt_PV); |
240 | #if !HAVE_PREADWRITE |
245 | SvPOK_on (data); |
241 | # define pread aio_pread |
|
|
242 | # define pwrite aio_pwrite |
246 | |
243 | |
247 | if (dataoffset < 0) |
244 | /* |
248 | dataoffset += svlen; |
245 | * make our pread/pwrite safe against themselves, but not against |
|
|
246 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
247 | * but that's your problem, not mine. |
|
|
248 | */ |
|
|
249 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
249 | |
250 | |
250 | if (dataoffset < 0 || dataoffset > svlen) |
251 | static ssize_t |
251 | croak ("data offset outside of string"); |
252 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
253 | { |
|
|
254 | ssize_t res; |
|
|
255 | off_t ooffset; |
252 | |
256 | |
253 | if (dowrite) |
257 | pthread_mutex_lock (&iolock); |
|
|
258 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
259 | lseek (fd, offset, SEEK_SET); |
|
|
260 | res = read (fd, buf, count); |
|
|
261 | lseek (fd, ooffset, SEEK_SET); |
|
|
262 | pthread_mutex_unlock (&iolock); |
|
|
263 | |
|
|
264 | return res; |
|
|
265 | } |
|
|
266 | |
|
|
267 | static ssize_t |
|
|
268 | pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
269 | { |
|
|
270 | ssize_t res; |
|
|
271 | off_t ooffset; |
|
|
272 | |
|
|
273 | pthread_mutex_lock (&iolock); |
|
|
274 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
275 | lseek (fd, offset, SEEK_SET); |
|
|
276 | res = write (fd, buf, count); |
|
|
277 | lseek (fd, offset, SEEK_SET); |
|
|
278 | pthread_mutex_unlock (&iolock); |
|
|
279 | |
|
|
280 | return res; |
|
|
281 | } |
|
|
282 | #endif |
|
|
283 | |
|
|
284 | #if !HAVE_FDATASYNC |
|
|
285 | # define fdatasync fsync |
|
|
286 | #endif |
|
|
287 | |
|
|
288 | #if !HAVE_READAHEAD |
|
|
289 | # define readahead aio_readahead |
|
|
290 | |
|
|
291 | static char readahead_buf[4096]; |
|
|
292 | |
|
|
293 | static ssize_t |
|
|
294 | readahead (int fd, off_t offset, size_t count) |
|
|
295 | { |
|
|
296 | while (count > 0) |
254 | { |
297 | { |
255 | /* write: check length and adjust. */ |
298 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
256 | if (length < 0 || length + dataoffset > svlen) |
299 | |
257 | length = svlen - dataoffset; |
300 | pread (fd, readahead_buf, len, offset); |
|
|
301 | offset += len; |
|
|
302 | count -= len; |
258 | } |
303 | } |
259 | else |
|
|
260 | { |
|
|
261 | /* read: grow scalar as necessary */ |
|
|
262 | svptr = SvGROW (data, length + dataoffset); |
|
|
263 | } |
|
|
264 | |
304 | |
265 | if (length < 0) |
305 | errno = 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 | |
|
|
281 | send_req (req); |
|
|
282 | } |
306 | } |
|
|
307 | #endif |
283 | |
308 | |
284 | static void * |
309 | static void * |
285 | aio_proc (void *thr_arg) |
310 | aio_proc (void *thr_arg) |
286 | { |
311 | { |
287 | aio_req req; |
312 | aio_req req; |
… | |
… | |
315 | |
340 | |
316 | switch (type) |
341 | switch (type) |
317 | { |
342 | { |
318 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
343 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
319 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
344 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
320 | #if SYS_readahead |
345 | |
321 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
346 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
322 | #else |
|
|
323 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
|
|
324 | #endif |
|
|
325 | |
347 | |
326 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
348 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
327 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
349 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
328 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
350 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
329 | |
351 | |
330 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
352 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
331 | case REQ_CLOSE: req->result = close (req->fd); break; |
353 | case REQ_CLOSE: req->result = close (req->fd); break; |
332 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
354 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
333 | |
355 | |
|
|
356 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
334 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
357 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
335 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
336 | |
358 | |
337 | case REQ_QUIT: |
359 | case REQ_QUIT: |
338 | break; |
360 | break; |
339 | |
361 | |
340 | default: |
362 | default: |
… | |
… | |
446 | send_req (req); |
468 | send_req (req); |
447 | } |
469 | } |
448 | |
470 | |
449 | void |
471 | void |
450 | aio_close(fh,callback=&PL_sv_undef) |
472 | aio_close(fh,callback=&PL_sv_undef) |
451 | InputStream fh |
473 | SV * fh |
452 | SV * callback |
474 | SV * callback |
453 | PROTOTYPE: $;$ |
475 | PROTOTYPE: $;$ |
454 | ALIAS: |
476 | ALIAS: |
455 | aio_close = REQ_CLOSE |
477 | aio_close = REQ_CLOSE |
456 | aio_fsync = REQ_FSYNC |
478 | aio_fsync = REQ_FSYNC |
457 | aio_fdatasync = REQ_FDATASYNC |
479 | aio_fdatasync = REQ_FDATASYNC |
… | |
… | |
463 | |
485 | |
464 | if (!req) |
486 | if (!req) |
465 | croak ("out of memory during aio_req allocation"); |
487 | croak ("out of memory during aio_req allocation"); |
466 | |
488 | |
467 | req->type = ix; |
489 | req->type = ix; |
|
|
490 | req->fh = newSVsv (fh); |
468 | req->fd = PerlIO_fileno (fh); |
491 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
469 | req->callback = SvREFCNT_inc (callback); |
492 | req->callback = SvREFCNT_inc (callback); |
470 | |
493 | |
471 | send_req (req); |
494 | send_req (req); |
472 | } |
495 | } |
473 | |
496 | |
474 | void |
497 | void |
475 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
498 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
476 | InputStream fh |
499 | SV * fh |
477 | UV offset |
500 | UV offset |
478 | IV length |
501 | IV length |
479 | SV * data |
502 | SV * data |
480 | IV dataoffset |
503 | IV dataoffset |
481 | SV * callback |
504 | SV * callback |
|
|
505 | ALIAS: |
|
|
506 | aio_read = REQ_READ |
|
|
507 | aio_write = REQ_WRITE |
482 | PROTOTYPE: $$$$$;$ |
508 | PROTOTYPE: $$$$$;$ |
483 | CODE: |
509 | CODE: |
484 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
510 | { |
|
|
511 | aio_req req; |
|
|
512 | STRLEN svlen; |
|
|
513 | char *svptr = SvPV (data, svlen); |
485 | |
514 | |
486 | void |
515 | SvUPGRADE (data, SVt_PV); |
487 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
516 | SvPOK_on (data); |
488 | OutputStream fh |
517 | |
489 | UV offset |
518 | if (dataoffset < 0) |
490 | IV length |
519 | dataoffset += svlen; |
491 | SV * data |
520 | |
492 | IV dataoffset |
521 | if (dataoffset < 0 || dataoffset > svlen) |
493 | SV * callback |
522 | croak ("data offset outside of string"); |
494 | PROTOTYPE: $$$$$;$ |
523 | |
495 | CODE: |
524 | if (ix == REQ_WRITE) |
496 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
525 | { |
|
|
526 | /* write: check length and adjust. */ |
|
|
527 | if (length < 0 || length + dataoffset > svlen) |
|
|
528 | length = svlen - dataoffset; |
|
|
529 | } |
|
|
530 | else |
|
|
531 | { |
|
|
532 | /* read: grow scalar as necessary */ |
|
|
533 | svptr = SvGROW (data, length + dataoffset); |
|
|
534 | } |
|
|
535 | |
|
|
536 | if (length < 0) |
|
|
537 | croak ("length must not be negative"); |
|
|
538 | |
|
|
539 | Newz (0, req, 1, aio_cb); |
|
|
540 | |
|
|
541 | if (!req) |
|
|
542 | croak ("out of memory during aio_req allocation"); |
|
|
543 | |
|
|
544 | req->type = ix; |
|
|
545 | req->fh = newSVsv (fh); |
|
|
546 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
|
|
547 | : IoOFP (sv_2io (fh))); |
|
|
548 | req->offset = offset; |
|
|
549 | req->length = length; |
|
|
550 | req->data = SvREFCNT_inc (data); |
|
|
551 | req->dataptr = (char *)svptr + dataoffset; |
|
|
552 | req->callback = SvREFCNT_inc (callback); |
|
|
553 | |
|
|
554 | send_req (req); |
|
|
555 | } |
497 | |
556 | |
498 | void |
557 | void |
499 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
558 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
500 | InputStream fh |
559 | SV * fh |
501 | UV offset |
560 | UV offset |
502 | IV length |
561 | IV length |
503 | SV * callback |
562 | SV * callback |
504 | PROTOTYPE: $$$;$ |
563 | PROTOTYPE: $$$;$ |
505 | CODE: |
564 | CODE: |
506 | { |
565 | { |
507 | aio_req req; |
566 | aio_req req; |
508 | |
567 | |
… | |
… | |
513 | |
572 | |
514 | if (!req) |
573 | if (!req) |
515 | croak ("out of memory during aio_req allocation"); |
574 | croak ("out of memory during aio_req allocation"); |
516 | |
575 | |
517 | req->type = REQ_READAHEAD; |
576 | req->type = REQ_READAHEAD; |
|
|
577 | req->fh = newSVsv (fh); |
518 | req->fd = PerlIO_fileno (fh); |
578 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
519 | req->offset = offset; |
579 | req->offset = offset; |
520 | req->length = length; |
580 | req->length = length; |
521 | req->callback = SvREFCNT_inc (callback); |
581 | req->callback = SvREFCNT_inc (callback); |
522 | |
582 | |
523 | send_req (req); |
583 | send_req (req); |
… | |
… | |
551 | req->dataptr = SvPV_nolen (req->data); |
611 | req->dataptr = SvPV_nolen (req->data); |
552 | } |
612 | } |
553 | else |
613 | else |
554 | { |
614 | { |
555 | req->type = REQ_FSTAT; |
615 | req->type = REQ_FSTAT; |
|
|
616 | req->fh = newSVsv (fh_or_path); |
556 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
617 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
557 | } |
618 | } |
558 | |
619 | |
559 | req->callback = SvREFCNT_inc (callback); |
620 | req->callback = SvREFCNT_inc (callback); |
560 | |
621 | |