1 | #define _XOPEN_SOURCE 500 |
|
|
2 | |
|
|
3 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
4 | #include "perl.h" |
2 | #include "perl.h" |
5 | #include "XSUB.h" |
3 | #include "XSUB.h" |
|
|
4 | |
|
|
5 | #include "autoconf/config.h" |
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> |
11 | #include <fcntl.h> |
11 | #include <fcntl.h> |
12 | #include <signal.h> |
12 | #include <signal.h> |
13 | #include <sched.h> |
13 | #include <sched.h> |
14 | #if __linux |
14 | |
15 | #include <sys/syscall.h> |
15 | #ifndef _REENTRANT |
|
|
16 | # define _REENTRANT 1 |
16 | #endif |
17 | #endif |
|
|
18 | #include <errno.h> |
17 | |
19 | |
18 | #include <pthread.h> |
20 | #include <pthread.h> |
19 | |
21 | |
20 | 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 ;) */ |
21 | 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 ;) */ |
… | |
… | |
44 | off_t offset; |
46 | off_t offset; |
45 | size_t length; |
47 | size_t length; |
46 | ssize_t result; |
48 | ssize_t result; |
47 | mode_t mode; /* open */ |
49 | mode_t mode; /* open */ |
48 | int errorno; |
50 | int errorno; |
49 | SV *data, *callback; |
51 | SV *data, *callback, *fh; |
50 | void *dataptr; |
52 | void *dataptr; |
51 | STRLEN dataoffset; |
53 | STRLEN dataoffset; |
52 | |
54 | |
53 | Stat_t *statdata; |
55 | Stat_t *statdata; |
54 | } aio_cb; |
56 | } aio_cb; |
… | |
… | |
117 | + req->result > 0 ? req->result : 0); |
119 | + req->result > 0 ? req->result : 0); |
118 | |
120 | |
119 | if (req->data) |
121 | if (req->data) |
120 | SvREFCNT_dec (req->data); |
122 | SvREFCNT_dec (req->data); |
121 | |
123 | |
|
|
124 | if (req->fh) |
|
|
125 | SvREFCNT_dec (req->fh); |
|
|
126 | |
122 | 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) |
123 | { |
128 | { |
124 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
129 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
125 | PL_laststatval = req->result; |
130 | PL_laststatval = req->result; |
126 | PL_statcache = *(req->statdata); |
131 | PL_statcache = *(req->statdata); |
127 | |
132 | |
128 | Safefree (req->statdata); |
133 | Safefree (req->statdata); |
129 | } |
134 | } |
130 | |
135 | |
|
|
136 | ENTER; |
131 | PUSHMARK (SP); |
137 | PUSHMARK (SP); |
132 | XPUSHs (sv_2mortal (newSViv (req->result))); |
138 | XPUSHs (sv_2mortal (newSViv (req->result))); |
133 | |
139 | |
134 | if (req->type == REQ_OPEN) |
140 | if (req->type == REQ_OPEN) |
135 | { |
141 | { |
… | |
… | |
138 | |
144 | |
139 | PUTBACK; |
145 | PUTBACK; |
140 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
146 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
141 | SPAGAIN; |
147 | SPAGAIN; |
142 | |
148 | |
143 | fh = POPs; |
149 | fh = SvREFCNT_inc (POPs); |
144 | |
150 | |
145 | PUSHMARK (SP); |
151 | PUSHMARK (SP); |
146 | XPUSHs (fh); |
152 | XPUSHs (sv_2mortal (fh)); |
147 | } |
153 | } |
148 | |
154 | |
149 | if (SvOK (req->callback)) |
155 | if (SvOK (req->callback)) |
150 | { |
156 | { |
151 | PUTBACK; |
157 | PUTBACK; |
152 | call_sv (req->callback, G_VOID | G_EVAL); |
158 | call_sv (req->callback, G_VOID | G_EVAL); |
153 | SPAGAIN; |
159 | SPAGAIN; |
154 | } |
160 | } |
|
|
161 | |
|
|
162 | LEAVE; |
155 | |
163 | |
156 | if (req->callback) |
164 | if (req->callback) |
157 | SvREFCNT_dec (req->callback); |
165 | SvREFCNT_dec (req->callback); |
158 | |
166 | |
159 | errno = errorno; |
167 | errno = errorno; |
… | |
… | |
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; |
… | |
… | |
311 | |
342 | |
312 | switch (type) |
343 | switch (type) |
313 | { |
344 | { |
314 | case REQ_READ: req->result = pread (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 = pwrite (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 | |