1 | #define PERL_NO_GET_CONTEXT |
|
|
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" |
6 | |
4 | |
7 | #include <sys/types.h> |
5 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
6 | #include <sys/stat.h> |
9 | #include <unistd.h> |
7 | #include <unistd.h> |
10 | #include <fcntl.h> |
8 | #include <fcntl.h> |
11 | #include <signal.h> |
9 | #include <signal.h> |
12 | #include <sched.h> |
10 | #include <sched.h> |
13 | #include <endian.h> |
|
|
14 | |
11 | |
15 | #include <pthread.h> |
12 | #include <pthread.h> |
16 | #include <sys/syscall.h> |
13 | #include <sys/syscall.h> |
17 | |
14 | |
18 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
15 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
… | |
… | |
77 | |
74 | |
78 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
75 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
79 | } |
76 | } |
80 | |
77 | |
81 | static int |
78 | static int |
82 | poll_cb (pTHX) |
79 | poll_cb () |
83 | { |
80 | { |
84 | dSP; |
81 | dSP; |
85 | int count = 0; |
82 | int count = 0; |
86 | aio_req req; |
83 | aio_req req; |
87 | |
84 | |
… | |
… | |
150 | |
147 | |
151 | PUSHMARK (SP); |
148 | PUSHMARK (SP); |
152 | XPUSHs (fh); |
149 | XPUSHs (fh); |
153 | } |
150 | } |
154 | |
151 | |
|
|
152 | if (SvOK (req->callback)) |
|
|
153 | { |
155 | PUTBACK; |
154 | PUTBACK; |
156 | call_sv (req->callback, G_VOID | G_EVAL); |
155 | call_sv (req->callback, G_VOID | G_EVAL); |
157 | SPAGAIN; |
156 | SPAGAIN; |
|
|
157 | } |
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; |
… | |
… | |
227 | |
227 | |
228 | send_req (req); |
228 | send_req (req); |
229 | } |
229 | } |
230 | |
230 | |
231 | static void |
231 | static void |
232 | read_write (pTHX_ |
|
|
233 | int dowrite, int fd, off_t offset, size_t length, |
232 | read_write (int dowrite, int fd, off_t offset, size_t length, |
234 | SV *data, STRLEN dataoffset, SV *callback) |
233 | SV *data, STRLEN dataoffset, SV *callback) |
235 | { |
234 | { |
236 | aio_req req; |
235 | aio_req req; |
237 | STRLEN svlen; |
236 | STRLEN svlen; |
238 | char *svptr = SvPV (data, svlen); |
237 | char *svptr = SvPV (data, svlen); |
… | |
… | |
364 | return 0; |
363 | return 0; |
365 | } |
364 | } |
366 | |
365 | |
367 | MODULE = IO::AIO PACKAGE = IO::AIO |
366 | MODULE = IO::AIO PACKAGE = IO::AIO |
368 | |
367 | |
|
|
368 | PROTOTYPES: ENABLE |
|
|
369 | |
369 | BOOT: |
370 | BOOT: |
370 | { |
371 | { |
371 | if (pipe (respipe)) |
372 | if (pipe (respipe)) |
372 | croak ("unable to initialize result pipe"); |
373 | croak ("unable to initialize result pipe"); |
373 | |
374 | |
… | |
… | |
400 | } |
401 | } |
401 | |
402 | |
402 | while (started > nthreads) |
403 | while (started > nthreads) |
403 | { |
404 | { |
404 | poll_wait (); |
405 | poll_wait (); |
405 | poll_cb (aTHX); |
406 | poll_cb (); |
406 | } |
407 | } |
407 | } |
408 | } |
408 | |
409 | |
409 | int |
410 | int |
410 | max_outstanding(nreqs) |
411 | max_outstanding(nreqs) |
… | |
… | |
413 | CODE: |
414 | CODE: |
414 | RETVAL = max_outstanding; |
415 | RETVAL = max_outstanding; |
415 | max_outstanding = nreqs; |
416 | max_outstanding = nreqs; |
416 | |
417 | |
417 | void |
418 | void |
418 | aio_open(pathname,flags,mode,callback) |
419 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
419 | SV * pathname |
420 | SV * pathname |
420 | int flags |
421 | int flags |
421 | int mode |
422 | int mode |
422 | SV * callback |
423 | SV * callback |
423 | PROTOTYPE: $$$$ |
424 | PROTOTYPE: $$$;$ |
424 | CODE: |
425 | CODE: |
425 | { |
426 | { |
426 | aio_req req; |
427 | aio_req req; |
427 | |
428 | |
428 | Newz (0, req, 1, aio_cb); |
429 | Newz (0, req, 1, aio_cb); |
… | |
… | |
439 | |
440 | |
440 | send_req (req); |
441 | send_req (req); |
441 | } |
442 | } |
442 | |
443 | |
443 | void |
444 | void |
444 | aio_close(fh,callback) |
445 | aio_close(fh,callback=&PL_sv_undef) |
445 | InputStream fh |
446 | InputStream fh |
446 | SV * callback |
447 | SV * callback |
447 | PROTOTYPE: $$ |
448 | PROTOTYPE: $;$ |
448 | ALIAS: |
449 | ALIAS: |
449 | aio_close = REQ_CLOSE |
450 | aio_close = REQ_CLOSE |
450 | aio_fsync = REQ_FSYNC |
451 | aio_fsync = REQ_FSYNC |
451 | aio_fdatasync = REQ_FDATASYNC |
452 | aio_fdatasync = REQ_FDATASYNC |
452 | CODE: |
453 | CODE: |
… | |
… | |
464 | |
465 | |
465 | send_req (req); |
466 | send_req (req); |
466 | } |
467 | } |
467 | |
468 | |
468 | void |
469 | void |
469 | aio_read(fh,offset,length,data,dataoffset,callback) |
470 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
470 | InputStream fh |
471 | InputStream fh |
471 | UV offset |
472 | UV offset |
472 | IV length |
473 | IV length |
473 | SV * data |
474 | SV * data |
474 | IV dataoffset |
475 | IV dataoffset |
475 | SV * callback |
476 | SV * callback |
476 | PROTOTYPE: $$$$$$ |
477 | PROTOTYPE: $$$$$;$ |
477 | CODE: |
478 | CODE: |
478 | read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
479 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
479 | |
480 | |
480 | void |
481 | void |
481 | aio_write(fh,offset,length,data,dataoffset,callback) |
482 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
482 | OutputStream fh |
483 | OutputStream fh |
483 | UV offset |
484 | UV offset |
484 | IV length |
485 | IV length |
485 | SV * data |
486 | SV * data |
486 | IV dataoffset |
487 | IV dataoffset |
487 | SV * callback |
488 | SV * callback |
488 | PROTOTYPE: $$$$$$ |
489 | PROTOTYPE: $$$$$;$ |
489 | CODE: |
490 | CODE: |
490 | read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
491 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
491 | |
492 | |
492 | void |
493 | void |
493 | aio_readahead(fh,offset,length,callback) |
494 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
494 | InputStream fh |
495 | InputStream fh |
495 | UV offset |
496 | UV offset |
496 | IV length |
497 | IV length |
497 | SV * callback |
498 | SV * callback |
498 | PROTOTYPE: $$$$ |
499 | PROTOTYPE: $$$;$ |
499 | CODE: |
500 | CODE: |
500 | { |
501 | { |
501 | aio_req req; |
502 | aio_req req; |
502 | |
503 | |
503 | if (length < 0) |
504 | if (length < 0) |
… | |
… | |
516 | |
517 | |
517 | send_req (req); |
518 | send_req (req); |
518 | } |
519 | } |
519 | |
520 | |
520 | void |
521 | void |
521 | aio_stat(fh_or_path,callback) |
522 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
522 | SV * fh_or_path |
523 | SV * fh_or_path |
523 | SV * callback |
524 | SV * callback |
524 | PROTOTYPE: $$ |
|
|
525 | ALIAS: |
525 | ALIAS: |
|
|
526 | aio_stat = REQ_STAT |
526 | aio_lstat = 1 |
527 | aio_lstat = REQ_LSTAT |
527 | CODE: |
528 | CODE: |
528 | { |
529 | { |
529 | aio_req req; |
530 | aio_req req; |
530 | |
531 | |
531 | Newz (0, req, 1, aio_cb); |
532 | Newz (0, req, 1, aio_cb); |
… | |
… | |
538 | if (!req->statdata) |
539 | if (!req->statdata) |
539 | croak ("out of memory during aio_req->statdata allocation"); |
540 | croak ("out of memory during aio_req->statdata allocation"); |
540 | |
541 | |
541 | if (SvPOK (fh_or_path)) |
542 | if (SvPOK (fh_or_path)) |
542 | { |
543 | { |
543 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
544 | req->type = ix; |
544 | req->data = newSVsv (fh_or_path); |
545 | req->data = newSVsv (fh_or_path); |
545 | req->dataptr = SvPV_nolen (req->data); |
546 | req->dataptr = SvPV_nolen (req->data); |
546 | } |
547 | } |
547 | else |
548 | else |
548 | { |
549 | { |
… | |
… | |
554 | |
555 | |
555 | send_req (req); |
556 | send_req (req); |
556 | } |
557 | } |
557 | |
558 | |
558 | void |
559 | void |
559 | aio_unlink(pathname,callback) |
560 | aio_unlink(pathname,callback=&PL_sv_undef) |
560 | SV * pathname |
561 | SV * pathname |
561 | SV * callback |
562 | SV * callback |
562 | PROTOTYPE: $$ |
|
|
563 | CODE: |
563 | CODE: |
564 | { |
564 | { |
565 | aio_req req; |
565 | aio_req req; |
566 | |
566 | |
567 | Newz (0, req, 1, aio_cb); |
567 | Newz (0, req, 1, aio_cb); |
… | |
… | |
575 | req->callback = SvREFCNT_inc (callback); |
575 | req->callback = SvREFCNT_inc (callback); |
576 | |
576 | |
577 | send_req (req); |
577 | send_req (req); |
578 | } |
578 | } |
579 | |
579 | |
|
|
580 | void |
|
|
581 | flush() |
|
|
582 | PROTOTYPE: |
|
|
583 | CODE: |
|
|
584 | while (nreqs) |
|
|
585 | { |
|
|
586 | poll_wait (); |
|
|
587 | poll_cb (); |
|
|
588 | } |
|
|
589 | |
|
|
590 | void |
|
|
591 | poll() |
|
|
592 | PROTOTYPE: |
|
|
593 | CODE: |
|
|
594 | if (nreqs) |
|
|
595 | { |
|
|
596 | poll_wait (); |
|
|
597 | poll_cb (); |
|
|
598 | } |
|
|
599 | |
580 | int |
600 | int |
581 | poll_fileno() |
601 | poll_fileno() |
582 | PROTOTYPE: |
602 | PROTOTYPE: |
583 | CODE: |
603 | CODE: |
584 | RETVAL = respipe [0]; |
604 | RETVAL = respipe [0]; |
… | |
… | |
587 | |
607 | |
588 | int |
608 | int |
589 | poll_cb(...) |
609 | poll_cb(...) |
590 | PROTOTYPE: |
610 | PROTOTYPE: |
591 | CODE: |
611 | CODE: |
592 | RETVAL = poll_cb (aTHX); |
612 | RETVAL = poll_cb (); |
593 | OUTPUT: |
613 | OUTPUT: |
594 | RETVAL |
614 | RETVAL |
595 | |
615 | |
596 | void |
616 | void |
597 | poll_wait() |
617 | poll_wait() |