--- IO-AIO/AIO.xs 2009/07/14 00:13:36 1.151 +++ IO-AIO/AIO.xs 2010/01/10 23:05:12 1.162 @@ -13,10 +13,20 @@ #include #include #include +#include #include #include #include +#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES +# include +#endif + +#if !_POSIX_MAPPED_FILES +# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) +# define munmap(addr,length) (errno = ENOSYS, -1) +#endif + /* perl namespace pollution */ #undef VERSION @@ -101,13 +111,11 @@ /*****************************************************************************/ -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; #define EIO_REQ_MEMBERS \ SV *callback; \ @@ -123,27 +131,87 @@ # 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 +#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 MCL_CURRENT +# define MCL_CURRENT 0 +#endif +#ifndef MCL_FUTURE +# define MCL_FUTURE 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 PAGESIZE +# define PAGESIZE sysconf (_SC_PAGESIZE) +#endif + static int req_invoke (eio_req *req); #define EIO_FINISH(req) req_invoke (req) static void req_destroy (eio_req *grp); @@ -162,7 +230,7 @@ static int next_pri = EIO_PRI_DEFAULT; static int max_outstanding; -static int respipe_osf [2], respipe [2] = { -1, -1 }; +static s_epipe respipe; static void req_destroy (aio_req req); static void req_cancel (aio_req req); @@ -170,19 +238,17 @@ static void want_poll (void) { /* write a dummy byte to the pipe so fh becomes ready */ - respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); + s_epipe_signal (&respipe); } static void done_poll (void) { /* read any signals sent by the worker threads */ - char buf [4]; - while (respipe_read (respipe [0], buf, 4) == 4) - ; + s_epipe_drain (&respipe); } /* 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) { @@ -190,15 +256,18 @@ 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 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); @@ -214,7 +283,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; @@ -330,7 +399,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, @@ -348,6 +417,36 @@ } break; + case EIO_STATVFS: + case EIO_FSTATVFS: + { + SV *rv = &PL_sv_undef; + + 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); + } + + PUSHs (rv); + } + + break; + case EIO_GROUP: req->int1 = 2; /* mark group as finished */ @@ -443,54 +542,15 @@ eio_grp_cancel (grp); } -#ifdef USE_SOCKETS_AS_HANDLES -# define TO_SOCKET(x) (win32_get_osfhandle (x)) -#else -# define TO_SOCKET(x) (x) -#endif - static void create_respipe (void) { - int old_readfd = respipe [0]; - - if (respipe [1] >= 0) - respipe_close (TO_SOCKET (respipe [1])); - -#ifdef _WIN32 - if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) -#else - if (pipe (respipe)) -#endif - croak ("unable to initialize result pipe"); - - if (old_readfd >= 0) - { - if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) - croak ("unable to initialize result pipe(2)"); - - respipe_close (respipe [0]); - respipe [0] = old_readfd; - } - -#ifdef _WIN32 - int arg = 1; - if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) - || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) -#else - if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) - || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) -#endif - croak ("unable to initialize result pipe(3)"); - - respipe_osf [0] = TO_SOCKET (respipe [0]); - respipe_osf [1] = TO_SOCKET (respipe [1]); + if (s_epipe_renew (&respipe)) + croak ("IO::AIO: unable to initialize result pipe"); } static void poll_wait (void) { - fd_set rfd; - while (eio_nreqs ()) { int size; @@ -504,10 +564,7 @@ etp_maybe_start_thread (); - FD_ZERO (&rfd); - FD_SET (respipe [0], &rfd); - - PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); + s_epipe_wait (&respipe); } } @@ -532,22 +589,39 @@ create_respipe (); } -static SV * -get_cb (SV *cb_sv) +/*****************************************************************************/ + +#define MMAP_MAGIC PERL_MAGIC_ext + +static int +mmap_free (pTHX_ SV *sv, MAGIC *mg) { - HV *st; - GV *gvp; - CV *cv; + int old_errno = errno; + munmap (mg->mg_ptr, (size_t)mg->mg_obj); + errno = old_errno; - if (!SvOK (cb_sv)) - return 0; + mg->mg_obj = 0; /* just in case */ - cv = sv_2cv (cb_sv, &st, &gvp, 0); + SvREADONLY_off (sv); + SvCUR_set (sv, 0); + SvLEN_set (sv, 0); + SvPVX (sv) = 0; + SvOK_off (sv); - if (!cv) - croak ("IO::AIO callback must be undef or a CODE reference"); + return 0; +} + +static MGVTBL mmap_vtbl = { + 0, 0, 0, 0, mmap_free +}; - return (SV *)cv; +/*****************************************************************************/ + +static SV * +get_cb (SV *cb_sv) +{ + SvGETMAGIC (cb_sv); + return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; } #define dREQ \ @@ -571,7 +645,7 @@ SPAGAIN; \ \ if (GIMME_V != G_VOID) \ - XPUSHs (req_sv (req, AIO_REQ_KLASS)); + XPUSHs (req_sv (req, aio_req_stash)); MODULE = IO::AIO PACKAGE = IO::AIO @@ -583,23 +657,63 @@ 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) + const_iv (EXDEV) + const_iv (ENOSYS) + const_iv (O_RDONLY) + const_iv (O_WRONLY) + const_iv (O_CREAT) + const_iv (O_TRUNC) #ifndef _WIN32 - const_iv (S_IFIFO , S_IFIFO) + const_iv (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_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_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_EXEC) + const_iv (PROT_NONE) + 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_iv (MCL_FUTURE) + const_iv (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) @@ -621,10 +735,14 @@ const_eio (DT_WHT) }; - stash = gv_stashpv ("IO::AIO", 1); + 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); for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) - newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); + newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); + + newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); create_respipe (); @@ -861,8 +979,9 @@ 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; @@ -876,7 +995,7 @@ } else { - req->type = EIO_FSTAT; + req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT; req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); } @@ -1049,6 +1168,40 @@ } 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 + PROTOTYPE: $$$$;$ + PPCODE: +{ + STRLEN svlen; + UV len = SvUV (length); + char *svptr = SvPVbyte (data, svlen); + + 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->size = len; + req->sv2 = SvREFCNT_inc (data); + req->ptr2 = (char *)svptr + offset; + req->int1 = flags; + + REQ_SEND; + } +} + +void aio_busy (double delay, SV *callback=&PL_sv_undef) PPCODE: { @@ -1070,7 +1223,7 @@ req->type = EIO_GROUP; req_submit (req); - XPUSHs (req_sv (req, AIO_GRP_KLASS)); + XPUSHs (req_sv (req, aio_grp_stash)); } void @@ -1132,7 +1285,7 @@ poll_fileno() PROTOTYPE: CODE: - RETVAL = respipe [0]; + RETVAL = s_epipe_fd (&respipe); OUTPUT: RETVAL @@ -1189,7 +1342,7 @@ #if _XOPEN_SOURCE >= 600 && !NO_FADVISE RETVAL = posix_fadvise (fh, offset, length, advice); #else - RETVAL = errno = ENOSYS; + RETVAL = errno = ENOSYS; /* yes, this is actually correct */ #endif OUTPUT: RETVAL @@ -1198,7 +1351,72 @@ 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) + PROTOTYPE: $$$$$;$ + 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; + + /* 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); + + SvPVX (scalar) = (char *)addr; + SvCUR_set (scalar, length); + SvLEN_set (scalar, 0); + SvPOK_only (scalar); + + XSRETURN_YES; +} + +void +munmap (SV *scalar) + PROTOTYPE: $ + CODE: + sv_unmagic (scalar, MMAP_MAGIC); + +int +mlockall (int flags) + PROTOTYPE: $ + CODE: +#if _POSIX_MEMLOCK +#if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 + extern int mallopt (int, int); + mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ +#endif + mlockall (flags); +#else + RETVAL = -1; + errno = ENOSYS; +#endif + OUTPUT: + RETVAL + +int +munlockall () + PROTOTYPE: + CODE: +#if _POSIX_MEMLOCK + munlockall (); +#else + RETVAL = -1; + errno = ENOSYS; +#endif + OUTPUT: + RETVAL void _on_next_submit (SV *cb) CODE: @@ -1223,7 +1441,7 @@ if (items > 1) { - SV *cb_cv = get_cb (callback); + SV *cb_cv =get_cb (callback); SvREFCNT_dec (req->callback); req->callback = SvREFCNT_inc (cb_cv);