--- IO-AIO/AIO.xs 2010/11/01 22:03:43 1.174 +++ IO-AIO/AIO.xs 2011/07/18 01:27:03 1.189 @@ -13,73 +13,67 @@ #include #include #include -#include #include #include #include -#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES +#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 */ @@ -120,11 +114,64 @@ #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 @@ -142,13 +189,12 @@ # define POSIX_FADV_DONTNEED 0 #endif -#if _XOPEN_SOURCE < 600 || NO_FADVISE +#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 -# define NO_MADVISE 1 #endif #ifndef POSIX_MADV_SEQUENTIAL # define POSIX_MADV_SEQUENTIAL 0 @@ -163,7 +209,7 @@ # define POSIX_MADV_DONTNEED 0 #endif -#if _XOPEN_SOURCE < 600 || NO_MADVISE +#if !HAVE_POSIX_MADVISE # define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */ #endif @@ -180,6 +226,12 @@ # 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 @@ -211,6 +263,31 @@ # 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 @@ -234,21 +311,24 @@ # 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 -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) - 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; @@ -356,6 +436,7 @@ { sv_result = sv_result_cache; sv_result_cache = 0; SvIV_set (sv_result, req->result); + SvIOK_only (sv_result); } else { @@ -462,6 +543,7 @@ { SV *rv = &PL_sv_undef; +#ifndef _WIN32 if (req->result >= 0) { EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); @@ -481,6 +563,7 @@ 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); } @@ -506,6 +589,7 @@ break; case EIO_READLINK: + case EIO_REALPATH: if (req->result > 0) PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result))); break; @@ -515,6 +599,7 @@ 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_result); break; @@ -529,7 +614,7 @@ } break; - case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), su fudge result value */ + case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), so fudge result value */ if (req->result > 0) SvIV_set (sv_result, 0); /* FALLTHROUGH */ @@ -576,8 +661,6 @@ static void req_cancel_subs (aio_req grp) { - aio_req sub; - if (grp->type != EIO_GROUP) return; @@ -587,8 +670,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"); @@ -629,9 +711,13 @@ } } -static void atfork_child (void) +static void ecb_cold +atfork_child (void) { create_respipe (); + + if (eio_init (want_poll, done_poll) < 0) + croak ("IO::AIO: unable to initialise eio library"); } /*****************************************************************************/ @@ -639,6 +725,9 @@ #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 @@ -649,8 +738,7 @@ #define MMAP_MAGIC PERL_MAGIC_ext -static int -mmap_free (pTHX_ SV *sv, MAGIC *mg) +static int mmap_free (pTHX_ SV *sv, MAGIC *mg) { int old_errno = errno; munmap (mg->mg_ptr, (size_t)mg->mg_obj); @@ -676,8 +764,7 @@ /*****************************************************************************/ -static SV * -get_cb (SV *cb_sv) +static SV * get_cb (SV *cb_sv) { SvGETMAGIC (cb_sv); return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; @@ -728,9 +815,33 @@ const_iv (O_TRUNC) const_iv (O_EXCL) const_iv (O_APPEND) -#ifndef _WIN32 + + 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) -#endif + 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) @@ -787,6 +898,8 @@ 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) @@ -812,17 +925,12 @@ newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); - create_respipe (); - - if (eio_init (want_poll, done_poll) < 0) - croak ("IO::AIO: unable to initialise eio library"); - - /* atfork child called in fifo order, so before eio's handler */ X_THREAD_ATFORK (0, 0, atfork_child); + atfork_child (); /* calls create_respipe and eio_init */ } void -max_poll_reqs (int nreqs) +max_poll_reqs (unsigned int nreqs) PROTOTYPE: $ CODE: eio_set_max_poll_reqs (nreqs); @@ -834,25 +942,31 @@ 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; @@ -907,27 +1021,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) 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; @@ -989,12 +1126,14 @@ void aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) + ALIAS: + aio_readlink = EIO_READLINK + aio_realpath = EIO_REALPATH PPCODE: { - SV *data; dREQ; - req->type = EIO_READLINK; + req->type = ix; req->sv1 = newSVsv (path); req->ptr1 = SvPVbyte_nolen (req->sv1); @@ -1062,6 +1201,22 @@ 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: @@ -1526,7 +1681,7 @@ addr = (void *)(((intptr_t)addr) + offset); eio_page_align (&addr, &len); -#if _POSIX_MEMLOCK +#if _POSIX_MEMLOCK_RANGE RETVAL = munlock (addr, len); #else RETVAL = ((errno = ENOSYS), -1);