ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.184 by root, Tue Jul 5 20:34:42 2011 UTC vs.
Revision 1.209 by root, Sun Apr 1 17:46:02 2012 UTC

11#include <stddef.h> 11#include <stddef.h>
12#include <stdlib.h> 12#include <stdlib.h>
13#include <errno.h> 13#include <errno.h>
14#include <sys/types.h> 14#include <sys/types.h>
15#include <sys/stat.h> 15#include <sys/stat.h>
16#include <sys/statvfs.h>
17#include <limits.h> 16#include <limits.h>
18#include <fcntl.h> 17#include <fcntl.h>
19#include <sched.h> 18#include <sched.h>
20 19
21#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES 20#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
22# include <sys/mman.h> 21# include <sys/mman.h>
23#endif 22#endif
24 23
25/* perl namespace pollution */ 24/* perl namespace pollution */
26#undef VERSION 25#undef VERSION
27
28#ifdef _WIN32
29
30# define EIO_STRUCT_DIRENT Direntry_t
31# undef malloc
32# undef free
33
34// perl overrides all those nice win32 functions
35# undef open
36# undef read
37# undef write
38# undef send
39# undef recv
40# undef stat
41# undef fstat
42# define lstat stat
43# undef truncate
44# undef ftruncate
45# undef open
46# undef close
47# undef unlink
48# undef rmdir
49# undef rename
50# undef lseek
51
52# define chown(a,b,c) (errno = ENOSYS, -1)
53# define fchown(a,b,c) (errno = ENOSYS, -1)
54# define fchmod(a,b) (errno = ENOSYS, -1)
55# define symlink(a,b) (errno = ENOSYS, -1)
56# define readlink(a,b,c) (errno = ENOSYS, -1)
57# define mknod(a,b,c) (errno = ENOSYS, -1)
58# define truncate(a,b) (errno = ENOSYS, -1)
59# define ftruncate(fd,o) chsize ((fd), (o))
60# define fsync(fd) _commit (fd)
61# define opendir(fd) (errno = ENOSYS, 0)
62# define readdir(fd) (errno = ENOSYS, -1)
63# define closedir(fd) (errno = ENOSYS, -1)
64# define mkdir(a,b) mkdir (a)
65
66#else
67
68# include <sys/time.h>
69# include <sys/select.h>
70# include <unistd.h>
71# include <utime.h>
72# include <signal.h>
73# define EIO_STRUCT_DIRENT struct dirent
74
75#endif
76 26
77/* perl stupidly overrides readdir and maybe others */ 27/* perl stupidly overrides readdir and maybe others */
78/* with thread-unsafe versions, imagine that :( */ 28/* with thread-unsafe versions, imagine that :( */
79#undef readdir 29#undef readdir
80#undef opendir 30#undef opendir
81#undef closedir 31#undef closedir
82 32
33#ifdef _WIN32
34
35 // perl overrides all those nice libc functions
36
37 #undef malloc
38 #undef free
39 #undef open
40 #undef read
41 #undef write
42 #undef send
43 #undef recv
44 #undef stat
45 #undef lstat
46 #undef fstat
47 #undef truncate
48 #undef ftruncate
49 #undef open
50 #undef link
51 #undef close
52 #undef unlink
53 #undef mkdir
54 #undef rmdir
55 #undef rename
56 #undef lseek
57 #undef opendir
58 #undef readdir
59 #undef closedir
60 #undef chmod
61 #undef fchmod
62 #undef dup
63 #undef dup2
64 #undef abort
65 #undef pipe
66
67 #define EIO_STRUCT_STAT struct _stati64
68 #define EIO_STRUCT_STATI64
69
70#else
71
72 #include <sys/time.h>
73 #include <sys/select.h>
74 #include <unistd.h>
75 #include <utime.h>
76 #include <signal.h>
77
83#define EIO_STRUCT_STAT Stat_t 78 #define EIO_STRUCT_STAT Stat_t
79
80#endif
84 81
85/* use NV for 32 bit perls as it allows larger offsets */ 82/* use NV for 32 bit perls as it allows larger offsets */
86#if IVSIZE >= 8 83#if IVSIZE >= 8
87# define VAL64 IV 84# define VAL64 IV
88# define SvVAL64 SvIV 85# define SvVAL64 SvIV
108 105
109typedef SV SV8; /* byte-sv, used for argument-checking */ 106typedef SV SV8; /* byte-sv, used for argument-checking */
110typedef int aio_rfd; /* read file desriptor */ 107typedef int aio_rfd; /* read file desriptor */
111typedef int aio_wfd; /* write file descriptor */ 108typedef int aio_wfd; /* write file descriptor */
112 109
113static HV *aio_stash, *aio_req_stash, *aio_grp_stash; 110static HV *aio_stash, *aio_req_stash, *aio_grp_stash, *aio_wd_stash;
114 111
115#define EIO_REQ_MEMBERS \ 112#define EIO_REQ_MEMBERS \
116 SV *callback; \ 113 SV *callback; \
117 SV *sv1, *sv2; \ 114 SV *sv1, *sv2; \
115 SV *sv3, *sv4; \
118 STRLEN stroffset; \ 116 STRLEN stroffset; \
119 SV *self; 117 SV *self;
120 118
121#define EIO_NO_WRAPPERS 1 119#define EIO_NO_WRAPPERS 1
122 120
123#include "libeio/config.h" 121#include "libeio/config.h"
124#include "libeio/eio.h" 122#include "libeio/eio.h"
123
124static int req_invoke (eio_req *req);
125#define EIO_FINISH(req) req_invoke (req)
126static void req_destroy (eio_req *grp);
127#define EIO_DESTROY(req) req_destroy (req)
128
129#include "libeio/eio.c"
125 130
126/* Linux/others */ 131/* Linux/others */
127#ifndef O_ASYNC 132#ifndef O_ASYNC
128# define O_ASYNC 0 133# define O_ASYNC 0
129#endif 134#endif
223#endif 228#endif
224#ifndef PROT_EXEC 229#ifndef PROT_EXEC
225# define PROT_EXEC 0 230# define PROT_EXEC 0
226#endif 231#endif
227 232
233#ifndef ST_RDONLY
234# define ST_RDONLY 0
235#endif
236#ifndef ST_NOSUID
237# define ST_NOSUID 0
238#endif
228#ifndef ST_NODEV 239#ifndef ST_NODEV
229# define ST_NODEV 0 240# define ST_NODEV 0
230#endif 241#endif
231#ifndef ST_NOEXEC 242#ifndef ST_NOEXEC
232# define ST_NOEXEC 0 243# define ST_NOEXEC 0
316 327
317#ifndef PAGESIZE 328#ifndef PAGESIZE
318# define PAGESIZE sysconf (_SC_PAGESIZE) 329# define PAGESIZE sysconf (_SC_PAGESIZE)
319#endif 330#endif
320 331
321static int req_invoke (eio_req *req);
322#define EIO_FINISH(req) req_invoke (req)
323static void req_destroy (eio_req *grp);
324#define EIO_DESTROY(req) req_destroy (req)
325
326enum { 332enum {
327 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ 333 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
328}; 334};
329 335
330#include "libeio/eio.c"
331
332typedef eio_req *aio_req; 336typedef eio_req *aio_req;
333typedef eio_req *aio_req_ornot; 337typedef eio_req *aio_req_ornot;
338typedef eio_wd aio_wd;
334 339
335static SV *on_next_submit; 340static SV *on_next_submit;
336static int next_pri = EIO_PRI_DEFAULT; 341static int next_pri = EIO_PRI_DEFAULT;
337static int max_outstanding; 342static int max_outstanding;
338 343
339static s_epipe respipe; 344static s_epipe respipe;
340 345
341static void req_destroy (aio_req req); 346static void req_destroy (aio_req req);
342static void req_cancel (aio_req req); 347static void req_cancel (aio_req req);
343 348
344static void want_poll (void) 349static void
350want_poll (void)
345{ 351{
346 /* write a dummy byte to the pipe so fh becomes ready */ 352 /* write a dummy byte to the pipe so fh becomes ready */
347 s_epipe_signal (&respipe); 353 s_epipe_signal (&respipe);
348} 354}
349 355
350static void done_poll (void) 356static void
357done_poll (void)
351{ 358{
352 /* read any signals sent by the worker threads */ 359 /* read any signals sent by the worker threads */
353 s_epipe_drain (&respipe); 360 s_epipe_drain (&respipe);
354} 361}
355 362
356/* must be called at most once */ 363/* must be called at most once */
364static SV *
357static SV *req_sv (aio_req req, HV *stash) 365req_sv (aio_req req, HV *stash)
358{ 366{
359 if (!req->self) 367 if (!req->self)
360 { 368 {
361 req->self = (SV *)newHV (); 369 req->self = (SV *)newHV ();
362 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); 370 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
363 } 371 }
364 372
365 return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); 373 return sv_2mortal (sv_bless (newRV_inc (req->self), stash));
366} 374}
367 375
368static aio_req SvAIO_REQ (SV *sv) 376static SV *
377newSVaio_wd (aio_wd wd)
378{
379 return sv_bless (newRV_noinc (newSViv ((IV)wd)), aio_wd_stash);
380}
381
382static aio_req
383SvAIO_REQ (SV *sv)
369{ 384{
370 MAGIC *mg; 385 MAGIC *mg;
371 386
372 if (!SvROK (sv) 387 if (!SvROK (sv)
388 /* for speed reasons, we do not verify that SvROK actually has a stash ptr */
373 || (SvSTASH (SvRV (sv)) != aio_grp_stash 389 || (SvSTASH (SvRV (sv)) != aio_grp_stash
374 && SvSTASH (SvRV (sv)) != aio_req_stash 390 && SvSTASH (SvRV (sv)) != aio_req_stash
375 && !sv_derived_from (sv, "IO::AIO::REQ"))) 391 && !sv_derived_from (sv, "IO::AIO::REQ")))
376 croak ("object of class IO::AIO::REQ expected"); 392 croak ("object of class IO::AIO::REQ expected");
377 393
378 mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 394 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
379 395
380 return mg ? (aio_req)mg->mg_ptr : 0; 396 return mg ? (aio_req)mg->mg_ptr : 0;
381} 397}
382 398
399static aio_wd
400SvAIO_WD (SV *sv)
401{
402 if (!SvROK (sv)
403 || SvTYPE (SvRV (sv)) != SVt_PVMG
404 || SvSTASH (SvRV (sv)) != aio_wd_stash)
405 croak ("IO::AIO: expected a working directory object as returned by aio_wd");
406
407 return (aio_wd)(long)SvIVX (SvRV (sv));
408}
409
410static void
383static void aio_grp_feed (aio_req grp) 411aio_grp_feed (aio_req grp)
384{ 412{
385 if (grp->sv2 && SvOK (grp->sv2)) 413 if (grp->sv2 && SvOK (grp->sv2))
386 { 414 {
387 dSP; 415 dSP;
388 416
396 FREETMPS; 424 FREETMPS;
397 LEAVE; 425 LEAVE;
398 } 426 }
399} 427}
400 428
429static void
401static void req_submit (eio_req *req) 430req_submit (eio_req *req)
402{ 431{
403 eio_submit (req); 432 eio_submit (req);
404 433
405 if (expect_false (on_next_submit)) 434 if (expect_false (on_next_submit))
406 { 435 {
413 PUTBACK; 442 PUTBACK;
414 call_sv (cb, G_DISCARD | G_EVAL); 443 call_sv (cb, G_DISCARD | G_EVAL);
415 } 444 }
416} 445}
417 446
447static int
418static int req_invoke (eio_req *req) 448req_invoke (eio_req *req)
419{ 449{
420 if (req->flags & FLAG_SV2_RO_OFF) 450 if (req->flags & FLAG_SV2_RO_OFF)
421 SvREADONLY_off (req->sv2); 451 SvREADONLY_off (req->sv2);
422 452
423 if (!EIO_CANCELLED (req) && req->callback) 453 if (!EIO_CANCELLED (req) && req->callback)
444 SvREADONLY_on (sv_result); 474 SvREADONLY_on (sv_result);
445 } 475 }
446 476
447 switch (req->type) 477 switch (req->type)
448 { 478 {
479 case EIO_WD_OPEN:
480 PUSHs (req->result ? &PL_sv_undef : sv_2mortal (newSVaio_wd (req->wd)));
481 break;
482
449 case EIO_READDIR: 483 case EIO_READDIR:
450 { 484 {
451 SV *rv = &PL_sv_undef; 485 SV *rv = &PL_sv_undef;
452 486
453 if (req->result >= 0) 487 if (req->result >= 0)
541 case EIO_STATVFS: 575 case EIO_STATVFS:
542 case EIO_FSTATVFS: 576 case EIO_FSTATVFS:
543 { 577 {
544 SV *rv = &PL_sv_undef; 578 SV *rv = &PL_sv_undef;
545 579
580#ifndef _WIN32
546 if (req->result >= 0) 581 if (req->result >= 0)
547 { 582 {
548 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); 583 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req);
549 HV *hv = newHV (); 584 HV *hv = newHV ();
550 585
560 hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); 595 hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0);
561 hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); 596 hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0);
562 hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); 597 hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0);
563 hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); 598 hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0);
564 } 599 }
600#endif
565 601
566 PUSHs (rv); 602 PUSHs (rv);
567 } 603 }
568 604
569 break; 605 break;
593 break; 629 break;
594 630
595 case EIO_STAT: 631 case EIO_STAT:
596 case EIO_LSTAT: 632 case EIO_LSTAT:
597 case EIO_FSTAT: 633 case EIO_FSTAT:
598 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; 634 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
635
599 PL_laststatval = req->result; 636 if (!(PL_laststatval = req->result))
637 /* if compilation fails here then perl's Stat_t is not struct _stati64 */
600 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); 638 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
639
601 PUSHs (sv_result); 640 PUSHs (sv_result);
641 break;
642
643 case EIO_SEEK:
644 PUSHs (req->result ? sv_result : sv_2mortal (newSVval64 (req->offs)));
602 break; 645 break;
603 646
604 case EIO_READ: 647 case EIO_READ:
605 { 648 {
606 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); 649 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
639 } 682 }
640 683
641 return !!SvTRUE (ERRSV); 684 return !!SvTRUE (ERRSV);
642} 685}
643 686
687static void
644static void req_destroy (aio_req req) 688req_destroy (aio_req req)
645{ 689{
646 if (req->self) 690 if (req->self)
647 { 691 {
648 sv_unmagic (req->self, PERL_MAGIC_ext); 692 sv_unmagic (req->self, PERL_MAGIC_ext);
649 SvREFCNT_dec (req->self); 693 SvREFCNT_dec (req->self);
650 } 694 }
651 695
652 SvREFCNT_dec (req->sv1); 696 SvREFCNT_dec (req->sv1);
653 SvREFCNT_dec (req->sv2); 697 SvREFCNT_dec (req->sv2);
698 SvREFCNT_dec (req->sv3);
699 SvREFCNT_dec (req->sv4);
654 SvREFCNT_dec (req->callback); 700 SvREFCNT_dec (req->callback);
655 701
656 Safefree (req); 702 free (req);
657} 703}
658 704
705static void
659static void req_cancel_subs (aio_req grp) 706req_cancel_subs (aio_req grp)
660{ 707{
661 aio_req sub;
662
663 if (grp->type != EIO_GROUP) 708 if (grp->type != EIO_GROUP)
664 return; 709 return;
665 710
666 SvREFCNT_dec (grp->sv2); 711 SvREFCNT_dec (grp->sv2);
667 grp->sv2 = 0; 712 grp->sv2 = 0;
668 713
669 eio_grp_cancel (grp); 714 eio_grp_cancel (grp);
670} 715}
671 716
717static void ecb_cold
672static void create_respipe (void) 718create_respipe (void)
673{ 719{
674 if (s_epipe_renew (&respipe)) 720 if (s_epipe_renew (&respipe))
675 croak ("IO::AIO: unable to initialize result pipe"); 721 croak ("IO::AIO: unable to initialize result pipe");
676} 722}
677 723
678static void poll_wait (void) 724static void
725poll_wait (void)
679{ 726{
680 while (eio_nreqs ()) 727 while (eio_nreqs ())
681 { 728 {
682 int size; 729 int size;
683 730
692 739
693 s_epipe_wait (&respipe); 740 s_epipe_wait (&respipe);
694 } 741 }
695} 742}
696 743
697static int poll_cb (void) 744static int
745poll_cb (void)
698{ 746{
699 for (;;) 747 for (;;)
700 { 748 {
701 int res = eio_poll (); 749 int res = eio_poll ();
702 750
708 756
709 poll_wait (); 757 poll_wait ();
710 } 758 }
711} 759}
712 760
713static void atfork_child (void) 761static void ecb_cold
762reinit (void)
714{ 763{
715 create_respipe (); 764 create_respipe ();
765
766 if (eio_init (want_poll, done_poll) < 0)
767 croak ("IO::AIO: unable to initialise eio library");
716} 768}
717 769
718/*****************************************************************************/ 770/*****************************************************************************/
719 771
720#if !_POSIX_MAPPED_FILES 772#if !_POSIX_MAPPED_FILES
731# define MAP_FIXED 0 783# define MAP_FIXED 0
732#endif 784#endif
733 785
734#define MMAP_MAGIC PERL_MAGIC_ext 786#define MMAP_MAGIC PERL_MAGIC_ext
735 787
788static int ecb_cold
736static int mmap_free (pTHX_ SV *sv, MAGIC *mg) 789mmap_free (pTHX_ SV *sv, MAGIC *mg)
737{ 790{
738 int old_errno = errno; 791 int old_errno = errno;
739 munmap (mg->mg_ptr, (size_t)mg->mg_obj); 792 munmap (mg->mg_ptr, (size_t)mg->mg_obj);
740 errno = old_errno; 793 errno = old_errno;
741 794
757 0, 0, 0, 0, mmap_free 810 0, 0, 0, 0, mmap_free
758}; 811};
759 812
760/*****************************************************************************/ 813/*****************************************************************************/
761 814
815static SV *
762static SV * get_cb (SV *cb_sv) 816get_cb (SV *cb_sv)
763{ 817{
764 SvGETMAGIC (cb_sv); 818 SvGETMAGIC (cb_sv);
765 return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; 819 return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0;
766} 820}
767 821
822static aio_req ecb_noinline
823dreq (SV *callback)
824{
825 SV *cb_cv;
826 aio_req req;
827 int req_pri = next_pri;
828 next_pri = EIO_PRI_DEFAULT;
829
830 cb_cv = get_cb (callback);
831
832 req = calloc (sizeof (*req), 1);
833 if (!req)
834 croak ("out of memory during eio_req allocation");
835
836 req->callback = SvREFCNT_inc (cb_cv);
837 req->pri = req_pri;
838
839 return req;
840}
841
768#define dREQ \ 842#define dREQ \
769 SV *cb_cv; \ 843 aio_req req = dreq (callback); \
770 aio_req req; \
771 int req_pri = next_pri; \
772 next_pri = EIO_PRI_DEFAULT; \
773 \
774 cb_cv = get_cb (callback); \
775 \
776 Newz (0, req, 1, eio_req); \
777 if (!req) \
778 croak ("out of memory during eio_req allocation"); \
779 \
780 req->callback = SvREFCNT_inc (cb_cv); \
781 req->pri = req_pri
782 844
783#define REQ_SEND \ 845#define REQ_SEND \
784 PUTBACK; \ 846 PUTBACK; \
785 req_submit (req); \ 847 req_submit (req); \
786 SPAGAIN; \ 848 SPAGAIN; \
787 \ 849 \
788 if (GIMME_V != G_VOID) \ 850 if (GIMME_V != G_VOID) \
789 XPUSHs (req_sv (req, aio_req_stash)); 851 XPUSHs (req_sv (req, aio_req_stash));
852
853ecb_inline void
854req_set_path (aio_req req, SV *path, SV **wdsv, SV **pathsv, eio_wd *wd, void **ptr)
855{
856 if (expect_false (SvROK (path)))
857 {
858 SV *rv = SvRV (path);
859 SV *wdob;
860
861 if (SvTYPE (rv) == SVt_PVAV && AvFILLp (rv) == 1)
862 {
863 path = AvARRAY (rv)[1];
864 wdob = AvARRAY (rv)[0];
865
866 if (SvOK (wdob))
867 {
868 *wd = SvAIO_WD (wdob);
869 *wdsv = SvREFCNT_inc_NN (SvRV (wdob));
870 }
871 else
872 *wd = EIO_INVALID_WD;
873 }
874 else if (SvTYPE (rv) == SVt_PVMG && SvSTASH (rv) == aio_wd_stash)
875 {
876 *wd = (aio_wd)(long)SvIVX (rv);
877 *wdsv = SvREFCNT_inc_NN (rv);
878 *ptr = ".";
879 return; /* path set to "." */
880 }
881 else
882 croak ("IO::AIO: pathname arguments must be specified as a string, an IO::AIO::WD object or a [IO::AIO::WD, path] pair");
883 }
884
885 *pathsv = newSVsv (path);
886 *ptr = SvPVbyte_nolen (*pathsv);
887}
888
889static void ecb_noinline
890req_set_path1 (aio_req req, SV *path)
891{
892 req_set_path (req, path, &req->sv1, &req->sv3, &req->wd, &req->ptr1);
893}
894
895static void ecb_noinline
896req_set_fh_or_path (aio_req req, int type_path, int type_fh, SV *fh_or_path)
897{
898 SV *rv = SvROK (fh_or_path) ? SvRV (fh_or_path) : fh_or_path;
899
900 switch (SvTYPE (rv))
901 {
902 case SVt_PVIO:
903 case SVt_PVLV:
904 case SVt_PVGV:
905 req->type = type_fh;
906 req->sv1 = newSVsv (fh_or_path);
907 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
908 break;
909
910 default:
911 req->type = type_path;
912 req_set_path1 (req, fh_or_path);
913 break;
914 }
915}
916
917XS(boot_IO__AIO) ecb_cold;
790 918
791MODULE = IO::AIO PACKAGE = IO::AIO 919MODULE = IO::AIO PACKAGE = IO::AIO
792 920
793PROTOTYPES: ENABLE 921PROTOTYPES: ENABLE
794 922
878 const_iv (MAP_LOCKED) 1006 const_iv (MAP_LOCKED)
879 const_iv (MAP_NORESERVE) 1007 const_iv (MAP_NORESERVE)
880 const_iv (MAP_POPULATE) 1008 const_iv (MAP_POPULATE)
881 const_iv (MAP_NONBLOCK) 1009 const_iv (MAP_NONBLOCK)
882 1010
1011 const_eio (SEEK_SET)
1012 const_eio (SEEK_CUR)
1013 const_eio (SEEK_END)
1014
883 const_eio (MCL_FUTURE) 1015 const_eio (MCL_FUTURE)
884 const_eio (MCL_CURRENT) 1016 const_eio (MCL_CURRENT)
885 1017
886 const_eio (MS_ASYNC) 1018 const_eio (MS_ASYNC)
887 const_eio (MS_INVALIDATE) 1019 const_eio (MS_INVALIDATE)
890 const_eio (MT_MODIFY) 1022 const_eio (MT_MODIFY)
891 1023
892 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) 1024 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
893 const_eio (SYNC_FILE_RANGE_WRITE) 1025 const_eio (SYNC_FILE_RANGE_WRITE)
894 const_eio (SYNC_FILE_RANGE_WAIT_AFTER) 1026 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
1027
1028 const_eio (FALLOC_FL_KEEP_SIZE)
895 1029
896 const_eio (READDIR_DENTS) 1030 const_eio (READDIR_DENTS)
897 const_eio (READDIR_DIRS_FIRST) 1031 const_eio (READDIR_DIRS_FIRST)
898 const_eio (READDIR_STAT_ORDER) 1032 const_eio (READDIR_STAT_ORDER)
899 const_eio (READDIR_FOUND_UNKNOWN) 1033 const_eio (READDIR_FOUND_UNKNOWN)
910 }; 1044 };
911 1045
912 aio_stash = gv_stashpv ("IO::AIO" , 1); 1046 aio_stash = gv_stashpv ("IO::AIO" , 1);
913 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); 1047 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1);
914 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); 1048 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1);
1049 aio_wd_stash = gv_stashpv ("IO::AIO::WD" , 1);
915 1050
916 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1051 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
917 newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); 1052 newCONSTSUB (aio_stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
918 1053
919 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); 1054 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE));
920 1055
921 create_respipe (); 1056 reinit ();
922
923 if (eio_init (want_poll, done_poll) < 0)
924 croak ("IO::AIO: unable to initialise eio library");
925
926 /* atfork child called in fifo order, so before eio's handler */
927 X_THREAD_ATFORK (0, 0, atfork_child);
928} 1057}
1058
1059void
1060reinit ()
1061 PROTOTYPE:
929 1062
930void 1063void
931max_poll_reqs (unsigned int nreqs) 1064max_poll_reqs (unsigned int nreqs)
932 PROTOTYPE: $ 1065 PROTOTYPE: $
933 CODE: 1066 CODE:
968 PROTOTYPE: $ 1101 PROTOTYPE: $
969 CODE: 1102 CODE:
970 max_outstanding = maxreqs; 1103 max_outstanding = maxreqs;
971 1104
972void 1105void
1106aio_wd (SV8 *pathname, SV *callback=&PL_sv_undef)
1107 PPCODE:
1108{
1109 dREQ;
1110
1111 req->type = EIO_WD_OPEN;
1112 req_set_path1 (req, pathname);
1113
1114 REQ_SEND;
1115}
1116
1117void
973aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) 1118aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
974 PPCODE: 1119 PPCODE:
975{ 1120{
976 dREQ; 1121 dREQ;
977 1122
978 req->type = EIO_OPEN; 1123 req->type = EIO_OPEN;
979 req->sv1 = newSVsv (pathname); 1124 req_set_path1 (req, pathname);
980 req->ptr1 = SvPVbyte_nolen (req->sv1);
981 req->int1 = flags; 1125 req->int1 = flags;
982 req->int2 = mode; 1126 req->int2 = mode;
983 1127
984 REQ_SEND; 1128 REQ_SEND;
985} 1129}
987void 1131void
988aio_fsync (SV *fh, SV *callback=&PL_sv_undef) 1132aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
989 ALIAS: 1133 ALIAS:
990 aio_fsync = EIO_FSYNC 1134 aio_fsync = EIO_FSYNC
991 aio_fdatasync = EIO_FDATASYNC 1135 aio_fdatasync = EIO_FDATASYNC
1136 aio_syncfs = EIO_SYNCFS
992 PPCODE: 1137 PPCODE:
993{ 1138{
994 int fd = s_fileno_croak (fh, 0); 1139 int fd = s_fileno_croak (fh, 0);
995 dREQ; 1140 dREQ;
996 1141
997 req->type = ix; 1142 req->type = ix;
998 req->sv1 = newSVsv (fh); 1143 req->sv1 = newSVsv (fh);
999 req->int1 = fd; 1144 req->int1 = fd;
1000 1145
1001 REQ_SEND (req); 1146 REQ_SEND;
1002} 1147}
1003 1148
1004void 1149void
1005aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) 1150aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
1006 PPCODE: 1151 PPCODE:
1013 req->int1 = fd; 1158 req->int1 = fd;
1014 req->offs = offset; 1159 req->offs = offset;
1015 req->size = nbytes; 1160 req->size = nbytes;
1016 req->int2 = flags; 1161 req->int2 = flags;
1017 1162
1018 REQ_SEND (req); 1163 REQ_SEND;
1164}
1165
1166void
1167aio_fallocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef)
1168 PPCODE:
1169{
1170 int fd = s_fileno_croak (fh, 0);
1171 dREQ;
1172
1173 req->type = EIO_FALLOCATE;
1174 req->sv1 = newSVsv (fh);
1175 req->int1 = fd;
1176 req->int2 = mode;
1177 req->offs = offset;
1178 req->size = len;
1179
1180 REQ_SEND;
1019} 1181}
1020 1182
1021void 1183void
1022aio_close (SV *fh, SV *callback=&PL_sv_undef) 1184aio_close (SV *fh, SV *callback=&PL_sv_undef)
1023 PPCODE: 1185 PPCODE:
1024{ 1186{
1025 static int close_pipe = -1; /* dummy fd to close fds via dup2 */ 1187 static int close_fd = -1; /* dummy fd to close fds via dup2 */
1026 int fd = s_fileno_croak (fh, 0); 1188 int fd = s_fileno_croak (fh, 0);
1027 dREQ; 1189 dREQ;
1028 1190
1029 if (close_pipe < 0) 1191 if (expect_false (close_fd < 0))
1030 { 1192 {
1031 int pipefd [2]; 1193 int pipefd [2];
1032 1194
1195 if (
1196#ifdef _WIN32
1197 _pipe (pipefd, 1, _O_BINARY) < 0
1198#else
1033 if (pipe (pipefd) < 0 1199 pipe (pipefd) < 0
1034 || close (pipefd [1]) < 0
1035 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0) 1200 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0
1201#endif
1202 || close (pipefd [1]) < 0
1203 )
1036 abort (); /*D*/ 1204 abort (); /*D*/
1037 1205
1038 close_pipe = pipefd [0]; 1206 close_fd = pipefd [0];
1039 } 1207 }
1040 1208
1041 req->type = EIO_DUP2; 1209 req->type = EIO_DUP2;
1042 req->int1 = close_pipe; 1210 req->int1 = close_fd;
1043 req->sv2 = newSVsv (fh); 1211 req->sv2 = newSVsv (fh);
1044 req->int2 = fd; 1212 req->int2 = fd;
1045 1213
1046 REQ_SEND (req); 1214 REQ_SEND;
1215}
1216
1217void
1218aio_seek (SV *fh, SV *offset, int whence, SV *callback=&PL_sv_undef)
1219 PPCODE:
1220{
1221 STRLEN svlen;
1222 int fd = s_fileno_croak (fh, 0);
1223 dREQ;
1224
1225 req->type = EIO_SEEK;
1226 req->sv1 = newSVsv (fh);
1227 req->int1 = fd;
1228 req->offs = SvVAL64 (offset);
1229 req->int2 = whence;
1230
1231 REQ_SEND;
1047} 1232}
1048 1233
1049void 1234void
1050aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) 1235aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
1051 ALIAS: 1236 ALIAS:
1098 REQ_SEND; 1283 REQ_SEND;
1099 } 1284 }
1100} 1285}
1101 1286
1102void 1287void
1103aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) 1288aio_readlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1104 ALIAS: 1289 ALIAS:
1105 aio_readlink = EIO_READLINK 1290 aio_readlink = EIO_READLINK
1106 aio_realpath = EIO_REALPATH 1291 aio_realpath = EIO_REALPATH
1107 PPCODE: 1292 PPCODE:
1108{ 1293{
1109 SV *data;
1110 dREQ; 1294 dREQ;
1111 1295
1112 req->type = ix; 1296 req->type = ix;
1113 req->sv1 = newSVsv (path); 1297 req_set_path1 (req, pathname);
1114 req->ptr1 = SvPVbyte_nolen (req->sv1);
1115 1298
1116 REQ_SEND; 1299 REQ_SEND;
1117} 1300}
1118 1301
1119void 1302void
1159 aio_statvfs = EIO_STATVFS 1342 aio_statvfs = EIO_STATVFS
1160 PPCODE: 1343 PPCODE:
1161{ 1344{
1162 dREQ; 1345 dREQ;
1163 1346
1164 req->sv1 = newSVsv (fh_or_path); 1347 req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path);
1165
1166 if (SvPOK (req->sv1))
1167 {
1168 req->type = ix;
1169 req->ptr1 = SvPVbyte_nolen (req->sv1);
1170 }
1171 else
1172 {
1173 req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT;
1174 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1175 }
1176 1348
1177 REQ_SEND; 1349 REQ_SEND;
1178} 1350}
1179 1351
1180UV 1352UV
1199{ 1371{
1200 dREQ; 1372 dREQ;
1201 1373
1202 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; 1374 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
1203 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; 1375 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
1204 req->sv1 = newSVsv (fh_or_path); 1376 req_set_fh_or_path (req, EIO_UTIME, EIO_FUTIME, fh_or_path);
1205
1206 if (SvPOK (req->sv1))
1207 {
1208 req->type = EIO_UTIME;
1209 req->ptr1 = SvPVbyte_nolen (req->sv1);
1210 }
1211 else
1212 {
1213 req->type = EIO_FUTIME;
1214 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1215 }
1216 1377
1217 REQ_SEND; 1378 REQ_SEND;
1218} 1379}
1219 1380
1220void 1381void
1221aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) 1382aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1222 PPCODE: 1383 PPCODE:
1223{ 1384{
1224 dREQ; 1385 dREQ;
1225 1386
1226 req->sv1 = newSVsv (fh_or_path);
1227 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; 1387 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1228 1388 req_set_fh_or_path (req, EIO_TRUNCATE, EIO_FTRUNCATE, fh_or_path);
1229 if (SvPOK (req->sv1))
1230 {
1231 req->type = EIO_TRUNCATE;
1232 req->ptr1 = SvPVbyte_nolen (req->sv1);
1233 }
1234 else
1235 {
1236 req->type = EIO_FTRUNCATE;
1237 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1238 }
1239 1389
1240 REQ_SEND; 1390 REQ_SEND;
1241} 1391}
1242 1392
1243void 1393void
1244aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) 1394aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
1245 ALIAS:
1246 aio_chmod = EIO_CHMOD
1247 aio_mkdir = EIO_MKDIR
1248 PPCODE: 1395 PPCODE:
1249{ 1396{
1250 dREQ; 1397 dREQ;
1251 1398
1252 req->int2 = mode; 1399 req->int2 = mode;
1253 req->sv1 = newSVsv (fh_or_path); 1400 req_set_fh_or_path (req, EIO_CHMOD, EIO_FCHMOD, fh_or_path);
1254
1255 if (SvPOK (req->sv1))
1256 {
1257 req->type = ix;
1258 req->ptr1 = SvPVbyte_nolen (req->sv1);
1259 }
1260 else
1261 {
1262 req->type = EIO_FCHMOD;
1263 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1264 }
1265 1401
1266 REQ_SEND; 1402 REQ_SEND;
1267} 1403}
1268 1404
1269void 1405void
1272{ 1408{
1273 dREQ; 1409 dREQ;
1274 1410
1275 req->int2 = SvOK (uid) ? SvIV (uid) : -1; 1411 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
1276 req->int3 = SvOK (gid) ? SvIV (gid) : -1; 1412 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
1277 req->sv1 = newSVsv (fh_or_path); 1413 req_set_fh_or_path (req, EIO_CHOWN, EIO_FCHOWN, fh_or_path);
1278
1279 if (SvPOK (req->sv1))
1280 {
1281 req->type = EIO_CHOWN;
1282 req->ptr1 = SvPVbyte_nolen (req->sv1);
1283 }
1284 else
1285 {
1286 req->type = EIO_FCHOWN;
1287 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1288 }
1289 1414
1290 REQ_SEND; 1415 REQ_SEND;
1291} 1416}
1292 1417
1293void 1418void
1295 PPCODE: 1420 PPCODE:
1296{ 1421{
1297 dREQ; 1422 dREQ;
1298 1423
1299 req->type = EIO_READDIR; 1424 req->type = EIO_READDIR;
1300 req->sv1 = newSVsv (pathname);
1301 req->ptr1 = SvPVbyte_nolen (req->sv1);
1302 req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1; 1425 req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1;
1303 1426
1304 if (flags & EIO_READDIR_DENTS) 1427 if (flags & EIO_READDIR_DENTS)
1305 req->int1 |= EIO_READDIR_CUSTOM2; 1428 req->int1 |= EIO_READDIR_CUSTOM2;
1306 1429
1430 req_set_path1 (req, pathname);
1431
1307 REQ_SEND; 1432 REQ_SEND;
1433}
1434
1435void
1436aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef)
1437 PPCODE:
1438{
1439 dREQ;
1440
1441 req->type = EIO_MKDIR;
1442 req->int2 = mode;
1443 req_set_path1 (req, pathname);
1444
1445 REQ_SEND;
1308} 1446}
1309 1447
1310void 1448void
1311aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) 1449aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1312 ALIAS: 1450 ALIAS:
1316 PPCODE: 1454 PPCODE:
1317{ 1455{
1318 dREQ; 1456 dREQ;
1319 1457
1320 req->type = ix; 1458 req->type = ix;
1321 req->sv1 = newSVsv (pathname); 1459 req_set_path1 (req, pathname);
1322 req->ptr1 = SvPVbyte_nolen (req->sv1);
1323 1460
1324 REQ_SEND; 1461 REQ_SEND;
1325} 1462}
1326 1463
1327void 1464void
1330 aio_link = EIO_LINK 1467 aio_link = EIO_LINK
1331 aio_symlink = EIO_SYMLINK 1468 aio_symlink = EIO_SYMLINK
1332 aio_rename = EIO_RENAME 1469 aio_rename = EIO_RENAME
1333 PPCODE: 1470 PPCODE:
1334{ 1471{
1472 eio_wd wd2 = 0;
1335 dREQ; 1473 dREQ;
1336 1474
1337 req->type = ix; 1475 req->type = ix;
1338 req->sv1 = newSVsv (oldpath); 1476 req_set_path1 (req, oldpath);
1339 req->ptr1 = SvPVbyte_nolen (req->sv1); 1477 req_set_path (req, newpath, &req->sv2, &req->sv4, &wd2, &req->ptr2);
1340 req->sv2 = newSVsv (newpath); 1478 req->int3 = (long)wd2;
1341 req->ptr2 = SvPVbyte_nolen (req->sv2);
1342 1479
1343 REQ_SEND; 1480 REQ_SEND;
1344} 1481}
1345 1482
1346void 1483void
1348 PPCODE: 1485 PPCODE:
1349{ 1486{
1350 dREQ; 1487 dREQ;
1351 1488
1352 req->type = EIO_MKNOD; 1489 req->type = EIO_MKNOD;
1353 req->sv1 = newSVsv (pathname);
1354 req->ptr1 = SvPVbyte_nolen (req->sv1);
1355 req->int2 = (mode_t)mode; 1490 req->int2 = (mode_t)mode;
1356 req->offs = dev; 1491 req->offs = dev;
1492 req_set_path1 (req, pathname);
1357 1493
1358 REQ_SEND; 1494 REQ_SEND;
1359} 1495}
1360 1496
1361void 1497void
1561 CODE: 1697 CODE:
1562 RETVAL = posix_fadvise (fh, offset, length, advice); 1698 RETVAL = posix_fadvise (fh, offset, length, advice);
1563 OUTPUT: 1699 OUTPUT:
1564 RETVAL 1700 RETVAL
1565 1701
1566ssize_t 1702IV
1567sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) 1703sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1568 CODE: 1704 CODE:
1569 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); 1705 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count);
1570 OUTPUT: 1706 OUTPUT:
1571 RETVAL 1707 RETVAL
1683 SvREFCNT_dec (on_next_submit); 1819 SvREFCNT_dec (on_next_submit);
1684 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; 1820 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1685 1821
1686PROTOTYPES: DISABLE 1822PROTOTYPES: DISABLE
1687 1823
1824MODULE = IO::AIO PACKAGE = IO::AIO::WD
1825
1826BOOT:
1827{
1828 newCONSTSUB (aio_stash, "CWD" , newSVaio_wd (EIO_CWD ));
1829 newCONSTSUB (aio_stash, "INVALID_WD", newSVaio_wd (EIO_INVALID_WD));
1830}
1831
1832void
1833DESTROY (SV *self)
1834 CODE:
1835{
1836 aio_wd wd = SvAIO_WD (self);
1837#if HAVE_AT
1838 {
1839 SV *callback = &PL_sv_undef;
1840 dREQ; /* clobbers next_pri :/ */
1841 next_pri = req->pri; /* restore next_pri */
1842 req->pri = EIO_PRI_MAX; /* better use max. priority to conserve fds */
1843 req->type = EIO_WD_CLOSE;
1844 req->wd = wd;
1845 REQ_SEND;
1846 }
1847#else
1848 eio_wd_close_sync (wd);
1849#endif
1850}
1851
1688MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1852MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1689 1853
1690void 1854void
1691cancel (aio_req_ornot req) 1855cancel (aio_req_ornot req)
1692 CODE: 1856 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines