--- IO-AIO/AIO.xs 2009/07/17 00:56:54 1.155 +++ IO-AIO/AIO.xs 2011/09/27 00:41:51 1.196 @@ -17,64 +17,63 @@ #include #include +#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES +# include +#endif + /* perl namespace pollution */ #undef VERSION +/* perl stupidly overrides readdir and maybe others */ +/* with thread-unsafe versions, imagine that :( */ +#undef readdir +#undef opendir +#undef closedir + #ifdef _WIN32 -# define EIO_STRUCT_DIRENT Direntry_t -# undef malloc -# undef free - -// perl overrides all those nice win32 functions -# undef open -# undef read -# undef write -# undef send -# undef recv -# undef stat -# undef fstat -# define lstat stat -# undef truncate -# undef ftruncate -# undef open -# undef close -# undef unlink -# undef rmdir -# undef rename -# undef lseek - -# define chown(a,b,c) (errno = ENOSYS, -1) -# define fchown(a,b,c) (errno = ENOSYS, -1) -# define fchmod(a,b) (errno = ENOSYS, -1) -# define symlink(a,b) (errno = ENOSYS, -1) -# define readlink(a,b,c) (errno = ENOSYS, -1) -# define mknod(a,b,c) (errno = ENOSYS, -1) -# define truncate(a,b) (errno = ENOSYS, -1) -# define ftruncate(fd,o) chsize ((fd), (o)) -# define fsync(fd) _commit (fd) -# define opendir(fd) (errno = ENOSYS, 0) -# define readdir(fd) (errno = ENOSYS, -1) -# define closedir(fd) (errno = ENOSYS, -1) -# define mkdir(a,b) mkdir (a) + // perl overrides all those nice libc functions + + #undef malloc + #undef free + #undef open + #undef read + #undef write + #undef send + #undef recv + #undef stat + #undef lstat + #undef fstat + #undef truncate + #undef ftruncate + #undef open + #undef link + #undef close + #undef unlink + #undef mkdir + #undef rmdir + #undef rename + #undef lseek + #undef opendir + #undef readdir + #undef closedir + #undef chmod + #undef fchmod + #undef dup + #undef dup2 + #undef abort + #undef pipe #else -# include -# include -# include -# include -# include -# define EIO_STRUCT_DIRENT struct dirent + #include + #include + #include + #include + #include #endif -/* perl stupidly overrides readdir and maybe others */ -/* with thread-unsafe versions, imagine that :( */ -#undef readdir -#undef opendir -#undef closedir - #define EIO_STRUCT_STAT Stat_t /* use NV for 32 bit perls as it allows larger offsets */ @@ -101,62 +100,239 @@ /*****************************************************************************/ -static HV *stash; typedef SV SV8; /* byte-sv, used for argument-checking */ typedef int aio_rfd; /* read file desriptor */ typedef int aio_wfd; /* write file descriptor */ -#define AIO_REQ_KLASS "IO::AIO::REQ" -#define AIO_GRP_KLASS "IO::AIO::GRP" +static HV *aio_stash, *aio_req_stash, *aio_grp_stash, *aio_wd_stash; #define EIO_REQ_MEMBERS \ SV *callback; \ SV *sv1, *sv2; \ + SV *sv3, *sv4; \ STRLEN stroffset; \ SV *self; #define EIO_NO_WRAPPERS 1 +#include "libeio/config.h" #include "libeio/eio.h" +static int req_invoke (eio_req *req); +#define EIO_FINISH(req) req_invoke (req) +static void req_destroy (eio_req *grp); +#define EIO_DESTROY(req) req_destroy (req) + +#include "libeio/eio.c" + +/* Linux/others */ +#ifndef O_ASYNC +# define O_ASYNC 0 +#endif +#ifndef O_DIRECT +# define O_DIRECT 0 +#endif +#ifndef O_NOATIME +# define O_NOATIME 0 +#endif + +/* POSIX */ +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif +#ifndef O_NOFOLLOW +# define O_NOFOLLOW 0 +#endif +#ifndef O_NOCTTY +# define O_NOCTTY 0 +#endif +#ifndef O_NONBLOCK +# define O_NONBLOCK 0 +#endif +#ifndef O_EXEC +# define O_EXEC 0 +#endif +#ifndef O_SEARCH +# define O_SEARCH 0 +#endif +#ifndef O_DIRECTORY +# define O_DIRECTORY 0 +#endif +#ifndef O_DSYNC +# define O_DSYNC 0 +#endif +#ifndef O_RSYNC +# define O_RSYNC 0 +#endif +#ifndef O_SYNC +# define O_SYNC 0 +#endif +#ifndef O_TTY_INIT +# define O_TTY_INIT 0 +#endif + #ifndef POSIX_FADV_NORMAL # define POSIX_FADV_NORMAL 0 -# define NO_FADVISE 1 #endif - #ifndef POSIX_FADV_SEQUENTIAL # define POSIX_FADV_SEQUENTIAL 0 #endif - #ifndef POSIX_FADV_RANDOM # define POSIX_FADV_RANDOM 0 #endif - #ifndef POSIX_FADV_NOREUSE # define POSIX_FADV_NOREUSE 0 #endif - #ifndef POSIX_FADV_WILLNEED # define POSIX_FADV_WILLNEED 0 #endif - #ifndef POSIX_FADV_DONTNEED # define POSIX_FADV_DONTNEED 0 #endif -static int req_invoke (eio_req *req); -#define EIO_FINISH(req) req_invoke (req) -static void req_destroy (eio_req *grp); -#define EIO_DESTROY(req) req_destroy (req) +#if !HAVE_POSIX_FADVISE +# define posix_fadvise(a,b,c,d) errno = ENOSYS /* also return ENOSYS */ +#endif + +#ifndef POSIX_MADV_NORMAL +# define POSIX_MADV_NORMAL 0 +#endif +#ifndef POSIX_MADV_SEQUENTIAL +# define POSIX_MADV_SEQUENTIAL 0 +#endif +#ifndef POSIX_MADV_RANDOM +# define POSIX_MADV_RANDOM 0 +#endif +#ifndef POSIX_MADV_WILLNEED +# define POSIX_MADV_WILLNEED 0 +#endif +#ifndef POSIX_MADV_DONTNEED +# define POSIX_MADV_DONTNEED 0 +#endif + +#if !HAVE_POSIX_MADVISE +# define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */ +#endif + +#ifndef PROT_NONE +# define PROT_NONE 0 +#endif +#ifndef PROT_READ +# define PROT_READ 0 +#endif +#ifndef PROT_WRITE +# define PROT_READ 0 +#endif +#ifndef PROT_EXEC +# define PROT_EXEC 0 +#endif + +#ifndef ST_RDONLY +# define ST_RDONLY 0 +#endif +#ifndef ST_NOSUID +# define ST_NOSUID 0 +#endif +#ifndef ST_NODEV +# define ST_NODEV 0 +#endif +#ifndef ST_NOEXEC +# define ST_NOEXEC 0 +#endif +#ifndef ST_SYNCHRONOUS +# define ST_SYNCHRONOUS 0 +#endif +#ifndef ST_MANDLOCK +# define ST_MANDLOCK 0 +#endif +#ifndef ST_WRITE +# define ST_WRITE 0 +#endif +#ifndef ST_APPEND +# define ST_APPEND 0 +#endif +#ifndef ST_IMMUTABLE +# define ST_IMMUTABLE 0 +#endif +#ifndef ST_NOATIME +# define ST_NOATIME 0 +#endif +#ifndef ST_NODIRATIME +# define ST_NODIRATIME 0 +#endif +#ifndef ST_RELATIME +# define ST_RELATIME 0 +#endif + +#ifndef S_IFIFO +# define S_IFIFO 0 +#endif +#ifndef S_IFCHR +# define S_IFCHR 0 +#endif +#ifndef S_IFBLK +# define S_IFBLK 0 +#endif +#ifndef S_IFLNK +# define S_IFLNK 0 +#endif +#ifndef S_IFREG +# define S_IFREG 0 +#endif +#ifndef S_IFDIR +# define S_IFDIR 0 +#endif +#ifndef S_IFWHT +# define S_IFWHT 0 +#endif +#ifndef S_IFSOCK +# define S_IFSOCK 0 +#endif + +#ifndef MAP_ANONYMOUS +# ifdef MAP_ANON +# define MAP_ANONYMOUS MAP_ANON +# else +# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ +# endif +#endif +#ifndef MAP_HUGETLB +# define MAP_HUGETLB 0 +#endif +#ifndef MAP_LOCKED +# define MAP_LOCKED 0 +#endif +#ifndef MAP_NORESERVE +# define MAP_NORESERVE 0 +#endif +#ifndef MAP_POPULATE +# define MAP_POPULATE 0 +#endif +#ifndef MAP_NONBLOCK +# define MAP_NONBLOCK 0 +#endif + +#ifndef makedev +# define makedev(maj,min) (((maj) << 8) | (min)) +#endif +#ifndef major +# define major(dev) ((dev) >> 8) +#endif +#ifndef minor +# define minor(dev) ((dev) & 0xff) +#endif + +#ifndef PAGESIZE +# define PAGESIZE sysconf (_SC_PAGESIZE) +#endif enum { FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ }; -#include "libeio/eio.c" - typedef eio_req *aio_req; typedef eio_req *aio_req_ornot; +typedef eio_wd aio_wd; static SV *on_next_submit; static int next_pri = EIO_PRI_DEFAULT; @@ -180,7 +356,7 @@ } /* must be called at most once */ -static SV *req_sv (aio_req req, const char *klass) +static SV *req_sv (aio_req req, HV *stash) { if (!req->self) { @@ -188,21 +364,40 @@ sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); } - return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); + return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); +} + +static SV * +newSVaio_wd (aio_wd wd) +{ + return sv_bless (newRV_noinc (newSViv ((long)wd)), aio_wd_stash); } static aio_req SvAIO_REQ (SV *sv) { MAGIC *mg; - if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) - croak ("object of class " AIO_REQ_KLASS " expected"); + if (!SvROK (sv) + || (SvSTASH (SvRV (sv)) != aio_grp_stash + && SvSTASH (SvRV (sv)) != aio_req_stash + && !sv_derived_from (sv, "IO::AIO::REQ"))) + croak ("object of class IO::AIO::REQ expected"); mg = mg_find (SvRV (sv), PERL_MAGIC_ext); return mg ? (aio_req)mg->mg_ptr : 0; } +static aio_wd SvAIO_WD (SV *sv) +{ + if (!SvROK (sv) + || SvSTASH (SvRV (sv)) != aio_wd_stash + || SvTYPE (SvRV (sv)) != SVt_PVMG) + croak ("IO::AIO: expected a working directory object as returned by aio_wd"); + + return (aio_wd)(long)SvIVX (SvRV (sv)); +} + static void aio_grp_feed (aio_req grp) { if (grp->sv2 && SvOK (grp->sv2)) @@ -212,7 +407,7 @@ ENTER; SAVETMPS; PUSHMARK (SP); - XPUSHs (req_sv (grp, AIO_GRP_KLASS)); + XPUSHs (req_sv (grp, aio_grp_stash)); PUTBACK; call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); SPAGAIN; @@ -240,20 +435,39 @@ static int req_invoke (eio_req *req) { - dSP; - if (req->flags & FLAG_SV2_RO_OFF) SvREADONLY_off (req->sv2); if (!EIO_CANCELLED (req) && req->callback) { + dSP; + static SV *sv_result_cache; /* caches the result integer SV */ + SV *sv_result; + ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, 1); + /* do not recreate the result IV from scratch each time */ + if (expect_true (sv_result_cache)) + { + sv_result = sv_result_cache; sv_result_cache = 0; + SvIV_set (sv_result, req->result); + SvIOK_only (sv_result); + } + else + { + sv_result = newSViv (req->result); + SvREADONLY_on (sv_result); + } + switch (req->type) { + case EIO_WD_OPEN: + PUSHs (sv_2mortal (newSVaio_wd (req->wd))); + break; + case EIO_READDIR: { SV *rv = &PL_sv_undef; @@ -328,7 +542,7 @@ int symlen; symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); - gv_init (gv, stash, sym, symlen, 0); + gv_init (gv, aio_stash, sym, symlen, 0); symlen = snprintf ( sym, @@ -346,6 +560,38 @@ } break; + case EIO_STATVFS: + case EIO_FSTATVFS: + { + SV *rv = &PL_sv_undef; + +#ifndef _WIN32 + if (req->result >= 0) + { + EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); + HV *hv = newHV (); + + rv = sv_2mortal (newRV_noinc ((SV *)hv)); + + hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0); + hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0); + hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0); + hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0); + hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0); + hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0); + hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0); + hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); + hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); + hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); + hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); + } +#endif + + PUSHs (rv); + } + + break; + case EIO_GROUP: req->int1 = 2; /* mark group as finished */ @@ -365,6 +611,7 @@ break; case EIO_READLINK: + case EIO_REALPATH: if (req->result > 0) PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result))); break; @@ -374,8 +621,9 @@ case EIO_FSTAT: PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; PL_laststatval = req->result; + /* if compilation fails here then perl's Stat_t is not struct _stati64 */ PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); - PUSHs (sv_2mortal (newSViv (req->result))); + PUSHs (sv_result); break; case EIO_READ: @@ -384,17 +632,17 @@ *SvEND (req->sv2) = 0; SvPOK_only (req->sv2); SvSETMAGIC (req->sv2); - PUSHs (sv_2mortal (newSViv (req->result))); + PUSHs (sv_result); } break; - case EIO_DUP2: + case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), so fudge result value */ if (req->result > 0) - req->result = 0; + SvIV_set (sv_result, 0); /* FALLTHROUGH */ default: - PUSHs (sv_2mortal (newSViv (req->result))); + PUSHs (sv_result); break; } @@ -404,6 +652,11 @@ call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); SPAGAIN; + if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache)) + SvREFCNT_dec (sv_result); + else + sv_result_cache = sv_result; + FREETMPS; LEAVE; @@ -423,6 +676,8 @@ SvREFCNT_dec (req->sv1); SvREFCNT_dec (req->sv2); + SvREFCNT_dec (req->sv3); + SvREFCNT_dec (req->sv4); SvREFCNT_dec (req->callback); Safefree (req); @@ -430,8 +685,6 @@ static void req_cancel_subs (aio_req grp) { - aio_req sub; - if (grp->type != EIO_GROUP) return; @@ -441,8 +694,7 @@ eio_grp_cancel (grp); } -static void -create_respipe (void) +static void create_respipe (void) { if (s_epipe_renew (&respipe)) croak ("IO::AIO: unable to initialize result pipe"); @@ -483,13 +735,60 @@ } } -static void atfork_child (void) +static void ecb_cold +reinit (void) { create_respipe (); + + if (eio_init (want_poll, done_poll) < 0) + croak ("IO::AIO: unable to initialise eio library"); } -static SV * -get_cb (SV *cb_sv) +/*****************************************************************************/ + +#if !_POSIX_MAPPED_FILES +# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) +# define munmap(addr,length) (errno = ENOSYS, -1) +#endif + +#if !_POSIX_MEMORY_PROTECTION +# define mprotect(addr,len,prot) (errno = ENOSYS, -1) +# define PROT_NONE 0 +# define PROT_WRITE 0 +# define MAP_PRIVATE 0 +# define MAP_SHARED 0 +# define MAP_FIXED 0 +#endif + +#define MMAP_MAGIC PERL_MAGIC_ext + +static int mmap_free (pTHX_ SV *sv, MAGIC *mg) +{ + int old_errno = errno; + munmap (mg->mg_ptr, (size_t)mg->mg_obj); + errno = old_errno; + + mg->mg_obj = 0; /* just in case */ + + SvREADONLY_off (sv); + + if (SvPVX (sv) != mg->mg_ptr) + croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); + + SvCUR_set (sv, 0); + SvPVX (sv) = 0; + SvOK_off (sv); + + return 0; +} + +static MGVTBL mmap_vtbl = { + 0, 0, 0, 0, mmap_free +}; + +/*****************************************************************************/ + +static SV * get_cb (SV *cb_sv) { SvGETMAGIC (cb_sv); return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; @@ -516,7 +815,63 @@ SPAGAIN; \ \ if (GIMME_V != G_VOID) \ - XPUSHs (req_sv (req, AIO_REQ_KLASS)); + XPUSHs (req_sv (req, aio_req_stash)); + +static void +req_set_path (aio_req req, SV *path, SV **wdsv, SV **pathsv, eio_wd *wd, void **ptr) +{ + if (SvROK (path)) + { + AV *av = (AV *)SvRV (path); + SV *wdob; + + if (SvTYPE (av) != SVt_PVAV || AvFILLp (av) != 1) + croak ("IO::AIO: pathname arguments must be specified as strings or [wd, path] arrayrefs"); + + path = AvARRAY (av)[1]; + wdob = AvARRAY (av)[0]; + + if (SvOK (wdob)) + { + *wd = SvAIO_WD (wdob); + *wdsv = SvREFCNT_inc_NN (SvRV (wdob)); + } + else + *wd = EIO_INVALID_WD; + } + + *pathsv = newSVsv (path); + *ptr = SvPVbyte_nolen (*pathsv); +} + +static void +req_set_path1 (aio_req req, SV *path) +{ + req_set_path (req, path, &req->sv1, &req->sv3, &req->wd, &req->ptr1); +} + +static void +req_set_fh_or_path (aio_req req, int type_path, int type_fh, SV *fh_or_path) +{ + SV *rv = SvROK (fh_or_path) ? SvRV (fh_or_path) : fh_or_path; + + switch (SvTYPE (rv)) + { + case SVt_PVIO: + case SVt_PVLV: + case SVt_PVGV: + req->type = type_fh; + req->sv1 = newSVsv (fh_or_path); + req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); + break; + + default: + req->type = type_path; + req_set_path1 (req, fh_or_path); + break; + } + +} MODULE = IO::AIO PACKAGE = IO::AIO @@ -528,28 +883,103 @@ const char *name; IV iv; } *civ, const_iv[] = { -# define const_iv(name, value) { # name, (IV) value }, +# define const_niv(name, value) { # name, (IV) value }, +# define const_iv(name) { # name, (IV) name }, # define const_eio(name) { # name, (IV) EIO_ ## name }, - const_iv (EXDEV , EXDEV) - const_iv (ENOSYS , ENOSYS) - const_iv (O_RDONLY, O_RDONLY) - const_iv (O_WRONLY, O_WRONLY) - const_iv (O_CREAT , O_CREAT) - const_iv (O_TRUNC , O_TRUNC) -#ifndef _WIN32 - const_iv (S_IFIFO , S_IFIFO) -#endif - const_iv (FADV_NORMAL , POSIX_FADV_NORMAL) - const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) - const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) - const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) - const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) - const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) + const_iv (EXDEV) + const_iv (ENOSYS) + const_iv (O_RDONLY) + const_iv (O_WRONLY) + const_iv (O_RDWR) + const_iv (O_CREAT) + const_iv (O_TRUNC) + const_iv (O_EXCL) + const_iv (O_APPEND) + + const_iv (O_ASYNC) + const_iv (O_DIRECT) + const_iv (O_NOATIME) + + const_iv (O_CLOEXEC) + const_iv (O_NOCTTY) + const_iv (O_NOFOLLOW) + const_iv (O_NONBLOCK) + const_iv (O_EXEC) + const_iv (O_SEARCH) + const_iv (O_DIRECTORY) + const_iv (O_DSYNC) + const_iv (O_RSYNC) + const_iv (O_SYNC) + const_iv (O_TTY_INIT) + + const_iv (S_IFIFO) + const_iv (S_IFCHR) + const_iv (S_IFBLK) + const_iv (S_IFLNK) + const_iv (S_IFREG) + const_iv (S_IFDIR) + const_iv (S_IFWHT) + const_iv (S_IFSOCK) + const_iv (S_IFMT) + + const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) + const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) + const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) + const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE) + const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) + const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) + + const_niv (MADV_NORMAL , POSIX_MADV_NORMAL) + const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL) + const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) + const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) + const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) + + const_iv (ST_RDONLY) + const_iv (ST_NOSUID) + const_iv (ST_NODEV) + const_iv (ST_NOEXEC) + const_iv (ST_SYNCHRONOUS) + const_iv (ST_MANDLOCK) + const_iv (ST_WRITE) + const_iv (ST_APPEND) + const_iv (ST_IMMUTABLE) + const_iv (ST_NOATIME) + const_iv (ST_NODIRATIME) + const_iv (ST_RELATIME) + + const_iv (PROT_NONE) + const_iv (PROT_EXEC) + const_iv (PROT_READ) + const_iv (PROT_WRITE) + + /*const_iv (MAP_FIXED)*/ + const_iv (MAP_PRIVATE) + const_iv (MAP_SHARED) + const_iv (MAP_ANONYMOUS) + + /* linuxish */ + const_iv (MAP_HUGETLB) + const_iv (MAP_LOCKED) + const_iv (MAP_NORESERVE) + const_iv (MAP_POPULATE) + const_iv (MAP_NONBLOCK) + + const_eio (MCL_FUTURE) + const_eio (MCL_CURRENT) + + const_eio (MS_ASYNC) + const_eio (MS_INVALIDATE) + const_eio (MS_SYNC) + + const_eio (MT_MODIFY) const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) const_eio (SYNC_FILE_RANGE_WRITE) const_eio (SYNC_FILE_RANGE_WAIT_AFTER) + const_eio (FALLOC_FL_KEEP_SIZE) + const_eio (READDIR_DENTS) const_eio (READDIR_DIRS_FIRST) const_eio (READDIR_STAT_ORDER) @@ -566,22 +996,25 @@ const_eio (DT_WHT) }; - stash = gv_stashpv ("IO::AIO", 1); - - for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) - newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); + aio_stash = gv_stashpv ("IO::AIO" , 1); + aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); + aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); + aio_wd_stash = gv_stashpv ("IO::AIO::WD" , 1); - create_respipe (); + for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) + newCONSTSUB (aio_stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); - if (eio_init (want_poll, done_poll) < 0) - croak ("IO::AIO: unable to initialise eio library"); + newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); - /* atfork child called in fifo order, so before eio's handler */ - X_THREAD_ATFORK (0, 0, atfork_child); + reinit (); } void -max_poll_reqs (int nreqs) +reinit () + PROTOTYPE: + +void +max_poll_reqs (unsigned int nreqs) PROTOTYPE: $ CODE: eio_set_max_poll_reqs (nreqs); @@ -593,39 +1026,55 @@ eio_set_max_poll_time (nseconds); void -min_parallel (int nthreads) +min_parallel (unsigned int nthreads) PROTOTYPE: $ CODE: eio_set_min_parallel (nthreads); void -max_parallel (int nthreads) +max_parallel (unsigned int nthreads) PROTOTYPE: $ CODE: eio_set_max_parallel (nthreads); void -max_idle (int nthreads) +max_idle (unsigned int nthreads) PROTOTYPE: $ CODE: eio_set_max_idle (nthreads); void -max_outstanding (int maxreqs) +idle_timeout (unsigned int seconds) + PROTOTYPE: $ + CODE: + eio_set_idle_timeout (seconds); + +void +max_outstanding (unsigned int maxreqs) PROTOTYPE: $ CODE: max_outstanding = maxreqs; void +aio_wd (SV8 *pathname, SV *callback=&PL_sv_undef) + PPCODE: +{ + dREQ; + + req->type = EIO_WD_OPEN; + req_set_path1 (req, pathname); + + REQ_SEND; +} + +void aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$;$ PPCODE: { dREQ; req->type = EIO_OPEN; - req->sv1 = newSVsv (pathname); - req->ptr1 = SvPVbyte_nolen (req->sv1); + req_set_path1 (req, pathname); req->int1 = flags; req->int2 = mode; @@ -634,10 +1083,10 @@ void aio_fsync (SV *fh, SV *callback=&PL_sv_undef) - PROTOTYPE: $;$ ALIAS: aio_fsync = EIO_FSYNC aio_fdatasync = EIO_FDATASYNC + aio_syncfs = EIO_SYNCFS PPCODE: { int fd = s_fileno_croak (fh, 0); @@ -652,7 +1101,6 @@ void aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$$;$ PPCODE: { int fd = s_fileno_croak (fh, 0); @@ -669,28 +1117,50 @@ } void +aio_fallocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); + dREQ; + + req->type = EIO_FALLOCATE; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->int2 = mode; + req->offs = offset; + req->size = len; + + REQ_SEND (req); +} + +void aio_close (SV *fh, SV *callback=&PL_sv_undef) - PROTOTYPE: $;$ PPCODE: { - static int close_pipe = -1; /* dummy fd to close fds via dup2 */ + static int close_fd = -1; /* dummy fd to close fds via dup2 */ int fd = s_fileno_croak (fh, 0); dREQ; - if (close_pipe < 0) + if (expect_false (close_fd < 0)) { int pipefd [2]; - if (pipe (pipefd) < 0 - || close (pipefd [1]) < 0 - || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0) + if ( +#ifdef _WIN32 + _pipe (pipefd, 1, _O_BINARY) < 0 +#else + pipe (pipefd) < 0 + || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0 +#endif + || close (pipefd [1]) < 0 + ) abort (); /*D*/ - close_pipe = pipefd [0]; + close_fd = pipefd [0]; } req->type = EIO_DUP2; - req->int1 = close_pipe; + req->int1 = close_fd; req->sv2 = newSVsv (fh); req->int2 = fd; @@ -702,7 +1172,6 @@ ALIAS: aio_read = EIO_READ aio_write = EIO_WRITE - PROTOTYPE: $$$$$;$ PPCODE: { STRLEN svlen; @@ -752,23 +1221,22 @@ } void -aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) - PROTOTYPE: $$;$ +aio_readlink (SV8 *pathname, SV *callback=&PL_sv_undef) + ALIAS: + aio_readlink = EIO_READLINK + aio_realpath = EIO_REALPATH PPCODE: { - SV *data; dREQ; - req->type = EIO_READLINK; - req->sv1 = newSVsv (path); - req->ptr1 = SvPVbyte_nolen (req->sv1); + req->type = ix; + req_set_path1 (req, pathname); REQ_SEND; } void aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$$;$ PPCODE: { int ifd = s_fileno_croak (in_fh , 0); @@ -788,7 +1256,6 @@ void aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$;$ PPCODE: { int fd = s_fileno_croak (fh, 0); @@ -806,28 +1273,34 @@ void aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) ALIAS: - aio_stat = EIO_STAT - aio_lstat = EIO_LSTAT + aio_stat = EIO_STAT + aio_lstat = EIO_LSTAT + aio_statvfs = EIO_STATVFS PPCODE: { dREQ; req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (req->sv1)) - { - req->type = ix; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FSTAT; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } - + req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path); REQ_SEND; } +UV +major (UV dev) + ALIAS: + minor = 1 + CODE: + RETVAL = ix ? major (dev) : minor (dev); + OUTPUT: + RETVAL + +UV +makedev (UV maj, UV min) + CODE: + RETVAL = makedev (maj, min); + OUTPUT: + RETVAL + void aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) PPCODE: @@ -836,18 +1309,7 @@ req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; - req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (req->sv1)) - { - req->type = EIO_UTIME; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FUTIME; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } + req_set_fh_or_path (req, EIO_UTIME, EIO_FUTIME, fh_or_path); REQ_SEND; } @@ -858,19 +1320,8 @@ { dREQ; - req->sv1 = newSVsv (fh_or_path); req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; - - if (SvPOK (req->sv1)) - { - req->type = EIO_TRUNCATE; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FTRUNCATE; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } + req_set_fh_or_path (req, EIO_TRUNCATE, EIO_FTRUNCATE, fh_or_path); REQ_SEND; } @@ -885,18 +1336,7 @@ dREQ; req->int2 = mode; - req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (req->sv1)) - { - req->type = ix; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FCHMOD; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } + req_set_fh_or_path (req, EIO_CHMOD, EIO_FCHMOD, fh_or_path); REQ_SEND; } @@ -909,18 +1349,7 @@ req->int2 = SvOK (uid) ? SvIV (uid) : -1; req->int3 = SvOK (gid) ? SvIV (gid) : -1; - req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (req->sv1)) - { - req->type = EIO_CHOWN; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FCHOWN; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } + req_set_fh_or_path (req, EIO_CHOWN, EIO_FCHOWN, fh_or_path); REQ_SEND; } @@ -953,8 +1382,7 @@ dREQ; req->type = ix; - req->sv1 = newSVsv (pathname); - req->ptr1 = SvPVbyte_nolen (req->sv1); + req_set_path1 (req, pathname); REQ_SEND; } @@ -968,12 +1396,12 @@ PPCODE: { dREQ; + eio_wd wd2; req->type = ix; - req->sv1 = newSVsv (oldpath); - req->ptr1 = SvPVbyte_nolen (req->sv1); - req->sv2 = newSVsv (newpath); - req->ptr2 = SvPVbyte_nolen (req->sv2); + req_set_path1 (req, oldpath); + req_set_path (req, newpath, &req->sv2, &req->sv4, &wd2, &req->ptr2); + req->int3 = (long)wd2; REQ_SEND; } @@ -985,15 +1413,88 @@ dREQ; req->type = EIO_MKNOD; - req->sv1 = newSVsv (pathname); - req->ptr1 = SvPVbyte_nolen (req->sv1); req->int2 = (mode_t)mode; req->offs = dev; + req_set_path1 (req, pathname); REQ_SEND; } void +aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef) + ALIAS: + aio_mtouch = EIO_MTOUCH + aio_msync = EIO_MSYNC + PPCODE: +{ + STRLEN svlen; + char *svptr = SvPVbyte (data, svlen); + UV len = SvUV (length); + + if (offset < 0) + offset += svlen; + + if (offset < 0 || offset > svlen) + croak ("offset outside of scalar"); + + if (!SvOK (length) || len + offset > svlen) + len = svlen - offset; + + { + dREQ; + + req->type = ix; + req->sv2 = SvREFCNT_inc (data); + req->ptr2 = (char *)svptr + offset; + req->size = len; + req->int1 = flags; + + REQ_SEND; + } +} + +void +aio_mlock (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, SV *callback=&PL_sv_undef) + PPCODE: +{ + STRLEN svlen; + char *svptr = SvPVbyte (data, svlen); + UV len = SvUV (length); + + if (offset < 0) + offset += svlen; + + if (offset < 0 || offset > svlen) + croak ("offset outside of scalar"); + + if (!SvOK (length) || len + offset > svlen) + len = svlen - offset; + + { + dREQ; + + req->type = EIO_MLOCK; + req->sv2 = SvREFCNT_inc (data); + req->ptr2 = (char *)svptr + offset; + req->size = len; + + REQ_SEND; + } +} + +void +aio_mlockall (IV flags, SV *callback=&PL_sv_undef) + PPCODE: +{ + dREQ; + + req->type = EIO_MLOCKALL; + req->int1 = flags; + + REQ_SEND; +} + +void aio_busy (double delay, SV *callback=&PL_sv_undef) PPCODE: { @@ -1007,7 +1508,6 @@ void aio_group (SV *callback=&PL_sv_undef) - PROTOTYPE: ;$ PPCODE: { dREQ; @@ -1015,7 +1515,7 @@ req->type = EIO_GROUP; req_submit (req); - XPUSHs (req_sv (req, AIO_GRP_KLASS)); + XPUSHs (req_sv (req, aio_grp_stash)); } void @@ -1034,7 +1534,6 @@ int aioreq_pri (int pri = 0) - PROTOTYPE: ;$ CODE: RETVAL = next_pri; if (items > 0) @@ -1056,7 +1555,6 @@ void flush () - PROTOTYPE: CODE: while (eio_nreqs ()) { @@ -1065,8 +1563,7 @@ } int -poll() - PROTOTYPE: +poll () CODE: poll_wait (); RETVAL = poll_cb (); @@ -1074,15 +1571,14 @@ RETVAL int -poll_fileno() - PROTOTYPE: +poll_fileno () CODE: RETVAL = s_epipe_fd (&respipe); OUTPUT: RETVAL int -poll_cb(...) +poll_cb (...) PROTOTYPE: CODE: RETVAL = poll_cb (); @@ -1090,38 +1586,33 @@ RETVAL void -poll_wait() - PROTOTYPE: +poll_wait () CODE: poll_wait (); int -nreqs() - PROTOTYPE: +nreqs () CODE: RETVAL = eio_nreqs (); OUTPUT: RETVAL int -nready() - PROTOTYPE: +nready () CODE: RETVAL = eio_nready (); OUTPUT: RETVAL int -npending() - PROTOTYPE: +npending () CODE: RETVAL = eio_npending (); OUTPUT: RETVAL int -nthreads() - PROTOTYPE: +nthreads () CODE: RETVAL = eio_nthreads (); OUTPUT: @@ -1129,21 +1620,125 @@ int fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) - PROTOTYPE: $$$$ CODE: -#if _XOPEN_SOURCE >= 600 && !NO_FADVISE RETVAL = posix_fadvise (fh, offset, length, advice); -#else - RETVAL = errno = ENOSYS; -#endif OUTPUT: RETVAL -ssize_t +IV sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) - PROTOTYPE: $$$$ CODE: - eio_sendfile_sync (ofh, ifh, offset, count); + RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); + OUTPUT: + RETVAL + +void +mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0) + PPCODE: + sv_unmagic (scalar, MMAP_MAGIC); +{ + int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; + void *addr = (void *)mmap (0, length, prot, flags, fd, offset); + if (addr == (void *)-1) + XSRETURN_NO; + + sv_force_normal (scalar); + + /* we store the length in mg_obj, as namlen is I32 :/ */ + sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) + ->mg_obj = (SV *)length; + + SvUPGRADE (scalar, SVt_PV); /* nop... */ + + if (!(prot & PROT_WRITE)) + SvREADONLY_on (scalar); + + if (SvLEN (scalar)) + Safefree (SvPVX (scalar)); + + SvPVX (scalar) = (char *)addr; + SvCUR_set (scalar, length); + SvLEN_set (scalar, 0); + SvPOK_only (scalar); + + XSRETURN_YES; +} + +void +munmap (SV *scalar) + CODE: + sv_unmagic (scalar, MMAP_MAGIC); + +int +madvise (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) + ALIAS: + mprotect = 1 + CODE: +{ + STRLEN svlen; + void *addr = SvPVbyte (scalar, svlen); + size_t len = SvUV (length); + + if (offset < 0) + offset += svlen; + + if (offset < 0 || offset > svlen) + croak ("offset outside of scalar"); + + if (!SvOK (length) || len + offset > svlen) + len = svlen - offset; + + addr = (void *)(((intptr_t)addr) + offset); + eio_page_align (&addr, &len); + + switch (ix) + { + case 0: RETVAL = posix_madvise (addr, len, advice_or_prot); break; + case 1: RETVAL = mprotect (addr, len, advice_or_prot); break; + } +} + OUTPUT: + RETVAL + +int +munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef) + CODE: +{ + STRLEN svlen; + void *addr = SvPVbyte (scalar, svlen); + size_t len = SvUV (length); + + if (offset < 0) + offset += svlen; + + if (offset < 0 || offset > svlen) + croak ("offset outside of scalar"); + + if (!SvOK (length) || len + offset > svlen) + len = svlen - offset; + + addr = (void *)(((intptr_t)addr) + offset); + eio_page_align (&addr, &len); +#if _POSIX_MEMLOCK_RANGE + RETVAL = munlock (addr, len); +#else + RETVAL = ((errno = ENOSYS), -1); +#endif +} + OUTPUT: + RETVAL + +int +munlockall () + CODE: +#if _POSIX_MEMLOCK + munlockall (); +#else + RETVAL = -1; + errno = ENOSYS; +#endif + OUTPUT: + RETVAL void _on_next_submit (SV *cb) CODE: @@ -1152,6 +1747,30 @@ PROTOTYPES: DISABLE +MODULE = IO::AIO PACKAGE = IO::AIO::WD + +BOOT: +{ + newCONSTSUB (aio_stash, "CWD" , newSVaio_wd (EIO_CWD )); + newCONSTSUB (aio_stash, "INVALID_WD", newSVaio_wd (EIO_INVALID_WD)); +} + +void +DESTROY (SV *self) + CODE: +{ + aio_wd wd = SvAIO_WD (self); +#if HAVE_AT + SV *callback = &PL_sv_undef; + dREQ; /* clobbers next_pri :/ */ + req->type = EIO_WD_CLOSE; + req->wd = wd; + REQ_SEND; +#else + eio_wd_close_sync (wd); +#endif +} + MODULE = IO::AIO PACKAGE = IO::AIO::REQ void