--- IO-AIO/AIO.xs 2009/06/12 16:48:08 1.144 +++ IO-AIO/AIO.xs 2009/07/15 01:36:04 1.153 @@ -6,6 +6,8 @@ #include "perl.h" #include "XSUB.h" +#include "schmorp.h" + #include #include #include @@ -77,9 +79,13 @@ /* use NV for 32 bit perls as it allows larger offsets */ #if IVSIZE >= 8 +# define VAL64 IV # define SvVAL64 SvIV +# define newSVval64 newSViv #else +# define VAL64 NV # define SvVAL64 SvNV +# define newSVval64 newSVnv #endif /*****************************************************************************/ @@ -97,6 +103,8 @@ 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" @@ -111,6 +119,31 @@ #include "libeio/eio.h" +#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); @@ -129,7 +162,8 @@ static int next_pri = EIO_PRI_DEFAULT; static int max_outstanding; -static int respipe_osf [2], respipe [2] = { -1, -1 }; +static int respipe_osf [2]; +static s_epipe respipe; static void req_destroy (aio_req req); static void req_cancel (aio_req req); @@ -137,15 +171,13 @@ 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 */ @@ -230,7 +262,8 @@ if (req->result >= 0) { int i; - char *buf = req->ptr2; + char *names = (char *)req->ptr2; + eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */ AV *av = newAV (); av_extend (av, req->result - 1); @@ -239,8 +272,7 @@ { if (req->int1 & EIO_READDIR_DENTS) { - eio_dirent *ent = (eio_dirent *)buf; - SV *namesv = newSVpvn (ent->name, ent->namelen); + SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen); if (req->int1 & EIO_READDIR_CUSTOM2) { @@ -264,13 +296,13 @@ else av_store (av, i, namesv); - buf += sizeof (eio_dirent); + ++ent; } else { - SV *name = newSVpv (buf, 0); + SV *name = newSVpv (names, 0); av_store (av, i, name); - buf += SvCUR (name) + 1; + names += SvCUR (name) + 1; } } @@ -296,15 +328,15 @@ char sym [64]; int symlen; - symlen = snprintf (sym, sizeof (sym), "fd#%u", (unsigned int)req->result); + symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); gv_init (gv, stash, sym, symlen, 0); symlen = snprintf ( sym, sizeof (sym), - "%s&=%u", + "%s&=%d", flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", - (unsigned int)req->result + (int)req->result ); if (do_open (gv, sym, symlen, 0, 0, 0, 0)) @@ -410,54 +442,18 @@ 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 + if (s_epipe_renew (&respipe)) 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]); + respipe_osf [0] = S_TO_SOCKET (respipe.fd [0]); + respipe_osf [1] = S_TO_SOCKET (respipe.fd [1]); } static void poll_wait (void) { - fd_set rfd; - while (eio_nreqs ()) { int size; @@ -471,10 +467,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); } } @@ -502,19 +495,8 @@ static SV * get_cb (SV *cb_sv) { - HV *st; - GV *gvp; - CV *cv; - - if (!SvOK (cb_sv)) - return 0; - - cv = sv_2cv (cb_sv, &st, &gvp, 0); - - if (!cv) - croak ("IO::AIO callback must be undef or a CODE reference"); - - return (SV *)cv; + SvGETMAGIC (cb_sv); + return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; } #define dREQ \ @@ -540,25 +522,12 @@ if (GIMME_V != G_VOID) \ XPUSHs (req_sv (req, AIO_REQ_KLASS)); -static int -extract_fd (SV *fh, int wr) -{ - int fd = PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh))); - - if (fd < 0) - croak ("illegal fh argument, either not an OS file or read/write mode mismatch"); - - return fd; -} - MODULE = IO::AIO PACKAGE = IO::AIO PROTOTYPES: ENABLE BOOT: { - stash = gv_stashpv ("IO::AIO", 1); - static const struct { const char *name; IV iv; @@ -574,6 +543,13 @@ #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_eio (SYNC_FILE_RANGE_WAIT_BEFORE) const_eio (SYNC_FILE_RANGE_WRITE) const_eio (SYNC_FILE_RANGE_WAIT_AFTER) @@ -594,6 +570,8 @@ 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)); @@ -666,7 +644,7 @@ aio_fdatasync = EIO_FDATASYNC PPCODE: { - int fd = extract_fd (fh, 0); + int fd = s_fileno_croak (fh, 0); dREQ; req->type = ix; @@ -677,18 +655,18 @@ } void -aio_sync_file_range (SV *fh, SV *offset, SV *nbytes, IV flags, SV *callback=&PL_sv_undef) +aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) PROTOTYPE: $$$$;$ PPCODE: { - int fd = extract_fd (fh, 0); + int fd = s_fileno_croak (fh, 0); dREQ; req->type = EIO_SYNC_FILE_RANGE; req->sv1 = newSVsv (fh); req->int1 = fd; - req->offs = SvVAL64 (offset); - req->size = SvVAL64 (nbytes); + req->offs = offset; + req->size = nbytes; req->int2 = flags; REQ_SEND (req); @@ -700,7 +678,7 @@ PPCODE: { static int close_pipe = -1; /* dummy fd to close fds via dup2 */ - int fd = extract_fd (fh, 0); + int fd = s_fileno_croak (fh, 0); dREQ; if (close_pipe < 0) @@ -732,7 +710,7 @@ PPCODE: { STRLEN svlen; - int fd = extract_fd (fh, ix == EIO_WRITE); + int fd = s_fileno_croak (fh, ix == EIO_WRITE); char *svptr = SvPVbyte (data, svlen); UV len = SvUV (length); @@ -793,12 +771,12 @@ } void -aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) +aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) PROTOTYPE: $$$$;$ PPCODE: { - int ifd = extract_fd (in_fh , 0); - int ofd = extract_fd (out_fh, 0); + int ifd = s_fileno_croak (in_fh , 0); + int ofd = s_fileno_croak (out_fh, 1); dREQ; req->type = EIO_SENDFILE; @@ -806,24 +784,24 @@ req->int1 = ofd; req->sv2 = newSVsv (in_fh); req->int2 = ifd; - req->offs = SvVAL64 (in_offset); + req->offs = in_offset; req->size = length; REQ_SEND; } void -aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) +aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) PROTOTYPE: $$$;$ PPCODE: { - int fd = extract_fd (fh, 0); + int fd = s_fileno_croak (fh, 0); dREQ; req->type = EIO_READAHEAD; req->sv1 = newSVsv (fh); req->int1 = fd; - req->offs = SvVAL64 (offset); + req->offs = offset; req->size = length; REQ_SEND; @@ -1103,7 +1081,7 @@ poll_fileno() PROTOTYPE: CODE: - RETVAL = respipe [0]; + RETVAL = s_epipe_fd (&respipe); OUTPUT: RETVAL @@ -1153,6 +1131,24 @@ OUTPUT: RETVAL +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 +sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) + PROTOTYPE: $$$$ + CODE: + eio_sendfile_sync (ofh, ifh, offset, count); + void _on_next_submit (SV *cb) CODE: SvREFCNT_dec (on_next_submit); @@ -1176,7 +1172,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);