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 |
|
|
15 | #include <sys/syscall.h> |
|
|
16 | #endif |
|
|
17 | |
14 | |
18 | #include <pthread.h> |
15 | #include <pthread.h> |
19 | |
16 | |
20 | 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 ;) */ |
21 | 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 ;) */ |
… | |
… | |
44 | off_t offset; |
41 | off_t offset; |
45 | size_t length; |
42 | size_t length; |
46 | ssize_t result; |
43 | ssize_t result; |
47 | mode_t mode; /* open */ |
44 | mode_t mode; /* open */ |
48 | int errorno; |
45 | int errorno; |
49 | SV *data, *callback; |
46 | SV *data, *callback, *fh; |
50 | void *dataptr; |
47 | void *dataptr; |
51 | STRLEN dataoffset; |
48 | STRLEN dataoffset; |
52 | |
49 | |
53 | Stat_t *statdata; |
50 | Stat_t *statdata; |
54 | } aio_cb; |
51 | } aio_cb; |
… | |
… | |
117 | + req->result > 0 ? req->result : 0); |
114 | + req->result > 0 ? req->result : 0); |
118 | |
115 | |
119 | if (req->data) |
116 | if (req->data) |
120 | SvREFCNT_dec (req->data); |
117 | SvREFCNT_dec (req->data); |
121 | |
118 | |
|
|
119 | if (req->fh) |
|
|
120 | SvREFCNT_dec (req->fh); |
|
|
121 | |
122 | 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) |
123 | { |
123 | { |
124 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
124 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
125 | PL_laststatval = req->result; |
125 | PL_laststatval = req->result; |
126 | PL_statcache = *(req->statdata); |
126 | PL_statcache = *(req->statdata); |
127 | |
127 | |
128 | Safefree (req->statdata); |
128 | Safefree (req->statdata); |
129 | } |
129 | } |
130 | |
130 | |
|
|
131 | ENTER; |
131 | PUSHMARK (SP); |
132 | PUSHMARK (SP); |
132 | XPUSHs (sv_2mortal (newSViv (req->result))); |
133 | XPUSHs (sv_2mortal (newSViv (req->result))); |
133 | |
134 | |
134 | if (req->type == REQ_OPEN) |
135 | if (req->type == REQ_OPEN) |
135 | { |
136 | { |
… | |
… | |
138 | |
139 | |
139 | PUTBACK; |
140 | PUTBACK; |
140 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
141 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
141 | SPAGAIN; |
142 | SPAGAIN; |
142 | |
143 | |
143 | fh = POPs; |
144 | fh = SvREFCNT_inc (POPs); |
144 | |
145 | |
145 | PUSHMARK (SP); |
146 | PUSHMARK (SP); |
146 | XPUSHs (fh); |
147 | XPUSHs (sv_2mortal (fh)); |
147 | } |
148 | } |
148 | |
149 | |
149 | if (SvOK (req->callback)) |
150 | if (SvOK (req->callback)) |
150 | { |
151 | { |
151 | PUTBACK; |
152 | PUTBACK; |
152 | call_sv (req->callback, G_VOID | G_EVAL); |
153 | call_sv (req->callback, G_VOID | G_EVAL); |
153 | SPAGAIN; |
154 | SPAGAIN; |
154 | } |
155 | } |
|
|
156 | |
|
|
157 | LEAVE; |
155 | |
158 | |
156 | if (req->callback) |
159 | if (req->callback) |
157 | SvREFCNT_dec (req->callback); |
160 | SvREFCNT_dec (req->callback); |
158 | |
161 | |
159 | errno = errorno; |
162 | errno = errorno; |
… | |
… | |
227 | req->type = REQ_QUIT; |
230 | req->type = REQ_QUIT; |
228 | |
231 | |
229 | send_req (req); |
232 | send_req (req); |
230 | } |
233 | } |
231 | |
234 | |
232 | static void |
235 | /* 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 | |
236 | |
240 | SvUPGRADE (data, SVt_PV); |
237 | #if !HAVE_PREADWRITE |
241 | SvPOK_on (data); |
238 | # define pread aio_pread |
|
|
239 | # define pwrite aio_pwrite |
242 | |
240 | |
243 | if (dataoffset < 0) |
241 | /* |
244 | dataoffset += svlen; |
242 | * make our pread/pwrite safe against themselves, but not against |
|
|
243 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
244 | * but that's your problem, not mine. |
|
|
245 | */ |
|
|
246 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
245 | |
247 | |
246 | if (dataoffset < 0 || dataoffset > svlen) |
248 | static ssize_t |
247 | croak ("data offset outside of string"); |
249 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
250 | { |
|
|
251 | ssize_t res; |
|
|
252 | off_t ooffset; |
248 | |
253 | |
249 | if (dowrite) |
254 | pthread_mutex_lock (&iolock); |
|
|
255 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
256 | lseek (fd, offset, SEEK_SET); |
|
|
257 | res = read (fd, buf, count); |
|
|
258 | lseek (fd, ooffset, SEEK_SET); |
|
|
259 | pthread_mutex_unlock (&iolock); |
|
|
260 | |
|
|
261 | return res; |
|
|
262 | } |
|
|
263 | |
|
|
264 | static ssize_t |
|
|
265 | pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
266 | { |
|
|
267 | ssize_t res; |
|
|
268 | off_t ooffset; |
|
|
269 | |
|
|
270 | pthread_mutex_lock (&iolock); |
|
|
271 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
272 | lseek (fd, offset, SEEK_SET); |
|
|
273 | res = write (fd, buf, count); |
|
|
274 | lseek (fd, offset, SEEK_SET); |
|
|
275 | pthread_mutex_unlock (&iolock); |
|
|
276 | |
|
|
277 | return res; |
|
|
278 | } |
|
|
279 | #endif |
|
|
280 | |
|
|
281 | #if !HAVE_FDATASYNC |
|
|
282 | # define fdatasync fsync |
|
|
283 | #endif |
|
|
284 | |
|
|
285 | #if !HAVE_READAHEAD |
|
|
286 | # define readahead aio_readahead |
|
|
287 | |
|
|
288 | static char readahead_buf[4096]; |
|
|
289 | |
|
|
290 | static ssize_t |
|
|
291 | readahead (int fd, off_t offset, size_t count) |
|
|
292 | { |
|
|
293 | while (count > 0) |
250 | { |
294 | { |
251 | /* write: check length and adjust. */ |
295 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
252 | if (length < 0 || length + dataoffset > svlen) |
296 | |
253 | length = svlen - dataoffset; |
297 | pread (fd, readahead_buf, len, offset); |
|
|
298 | offset += len; |
|
|
299 | count -= len; |
254 | } |
300 | } |
255 | else |
|
|
256 | { |
|
|
257 | /* read: grow scalar as necessary */ |
|
|
258 | svptr = SvGROW (data, length + dataoffset); |
|
|
259 | } |
|
|
260 | |
301 | |
261 | if (length < 0) |
302 | 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 | } |
303 | } |
|
|
304 | #endif |
279 | |
305 | |
280 | static void * |
306 | static void * |
281 | aio_proc (void *thr_arg) |
307 | aio_proc (void *thr_arg) |
282 | { |
308 | { |
283 | aio_req req; |
309 | aio_req req; |
… | |
… | |
311 | |
337 | |
312 | switch (type) |
338 | switch (type) |
313 | { |
339 | { |
314 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
340 | 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; |
341 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
316 | #if SYS_readahead |
342 | |
317 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
343 | 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 | |
344 | |
322 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
345 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
323 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
346 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
324 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
347 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
325 | |
348 | |
326 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
349 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
327 | case REQ_CLOSE: req->result = close (req->fd); break; |
350 | case REQ_CLOSE: req->result = close (req->fd); break; |
328 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
351 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
329 | |
352 | |
|
|
353 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
330 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
354 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
331 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
332 | |
355 | |
333 | case REQ_QUIT: |
356 | case REQ_QUIT: |
334 | break; |
357 | break; |
335 | |
358 | |
336 | default: |
359 | default: |
… | |
… | |
442 | send_req (req); |
465 | send_req (req); |
443 | } |
466 | } |
444 | |
467 | |
445 | void |
468 | void |
446 | aio_close(fh,callback=&PL_sv_undef) |
469 | aio_close(fh,callback=&PL_sv_undef) |
447 | InputStream fh |
470 | SV * fh |
448 | SV * callback |
471 | SV * callback |
449 | PROTOTYPE: $;$ |
472 | PROTOTYPE: $;$ |
450 | ALIAS: |
473 | ALIAS: |
451 | aio_close = REQ_CLOSE |
474 | aio_close = REQ_CLOSE |
452 | aio_fsync = REQ_FSYNC |
475 | aio_fsync = REQ_FSYNC |
453 | aio_fdatasync = REQ_FDATASYNC |
476 | aio_fdatasync = REQ_FDATASYNC |
… | |
… | |
459 | |
482 | |
460 | if (!req) |
483 | if (!req) |
461 | croak ("out of memory during aio_req allocation"); |
484 | croak ("out of memory during aio_req allocation"); |
462 | |
485 | |
463 | req->type = ix; |
486 | req->type = ix; |
|
|
487 | req->fh = newSVsv (fh); |
464 | req->fd = PerlIO_fileno (fh); |
488 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
465 | req->callback = SvREFCNT_inc (callback); |
489 | req->callback = SvREFCNT_inc (callback); |
466 | |
490 | |
467 | send_req (req); |
491 | send_req (req); |
468 | } |
492 | } |
469 | |
493 | |
470 | void |
494 | void |
471 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
495 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
472 | InputStream fh |
496 | SV * fh |
473 | UV offset |
497 | UV offset |
474 | IV length |
498 | IV length |
475 | SV * data |
499 | SV * data |
476 | IV dataoffset |
500 | IV dataoffset |
477 | SV * callback |
501 | SV * callback |
|
|
502 | ALIAS: |
|
|
503 | aio_read = REQ_READ |
|
|
504 | aio_write = REQ_WRITE |
478 | PROTOTYPE: $$$$$;$ |
505 | PROTOTYPE: $$$$$;$ |
479 | CODE: |
506 | CODE: |
480 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
507 | { |
|
|
508 | aio_req req; |
|
|
509 | STRLEN svlen; |
|
|
510 | char *svptr = SvPV (data, svlen); |
481 | |
511 | |
482 | void |
512 | SvUPGRADE (data, SVt_PV); |
483 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
513 | SvPOK_on (data); |
484 | OutputStream fh |
514 | |
485 | UV offset |
515 | if (dataoffset < 0) |
486 | IV length |
516 | dataoffset += svlen; |
487 | SV * data |
517 | |
488 | IV dataoffset |
518 | if (dataoffset < 0 || dataoffset > svlen) |
489 | SV * callback |
519 | croak ("data offset outside of string"); |
490 | PROTOTYPE: $$$$$;$ |
520 | |
491 | CODE: |
521 | if (ix == REQ_WRITE) |
492 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
522 | { |
|
|
523 | /* write: check length and adjust. */ |
|
|
524 | if (length < 0 || length + dataoffset > svlen) |
|
|
525 | length = svlen - dataoffset; |
|
|
526 | } |
|
|
527 | else |
|
|
528 | { |
|
|
529 | /* read: grow scalar as necessary */ |
|
|
530 | svptr = SvGROW (data, length + dataoffset); |
|
|
531 | } |
|
|
532 | |
|
|
533 | if (length < 0) |
|
|
534 | croak ("length must not be negative"); |
|
|
535 | |
|
|
536 | Newz (0, req, 1, aio_cb); |
|
|
537 | |
|
|
538 | if (!req) |
|
|
539 | croak ("out of memory during aio_req allocation"); |
|
|
540 | |
|
|
541 | req->type = ix; |
|
|
542 | req->fh = newSVsv (fh); |
|
|
543 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
|
|
544 | : IoOFP (sv_2io (fh))); |
|
|
545 | req->offset = offset; |
|
|
546 | req->length = length; |
|
|
547 | req->data = SvREFCNT_inc (data); |
|
|
548 | req->dataptr = (char *)svptr + dataoffset; |
|
|
549 | req->callback = SvREFCNT_inc (callback); |
|
|
550 | |
|
|
551 | send_req (req); |
|
|
552 | } |
493 | |
553 | |
494 | void |
554 | void |
495 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
555 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
496 | InputStream fh |
556 | SV * fh |
497 | UV offset |
557 | UV offset |
498 | IV length |
558 | IV length |
499 | SV * callback |
559 | SV * callback |
500 | PROTOTYPE: $$$;$ |
560 | PROTOTYPE: $$$;$ |
501 | CODE: |
561 | CODE: |
502 | { |
562 | { |
503 | aio_req req; |
563 | aio_req req; |
504 | |
564 | |
… | |
… | |
509 | |
569 | |
510 | if (!req) |
570 | if (!req) |
511 | croak ("out of memory during aio_req allocation"); |
571 | croak ("out of memory during aio_req allocation"); |
512 | |
572 | |
513 | req->type = REQ_READAHEAD; |
573 | req->type = REQ_READAHEAD; |
|
|
574 | req->fh = newSVsv (fh); |
514 | req->fd = PerlIO_fileno (fh); |
575 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
515 | req->offset = offset; |
576 | req->offset = offset; |
516 | req->length = length; |
577 | req->length = length; |
517 | req->callback = SvREFCNT_inc (callback); |
578 | req->callback = SvREFCNT_inc (callback); |
518 | |
579 | |
519 | send_req (req); |
580 | send_req (req); |
… | |
… | |
547 | req->dataptr = SvPV_nolen (req->data); |
608 | req->dataptr = SvPV_nolen (req->data); |
548 | } |
609 | } |
549 | else |
610 | else |
550 | { |
611 | { |
551 | req->type = REQ_FSTAT; |
612 | req->type = REQ_FSTAT; |
|
|
613 | req->fh = newSVsv (fh_or_path); |
552 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
614 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
553 | } |
615 | } |
554 | |
616 | |
555 | req->callback = SvREFCNT_inc (callback); |
617 | req->callback = SvREFCNT_inc (callback); |
556 | |
618 | |