--- IO-AIO/AIO.xs 2006/10/23 14:49:51 1.56 +++ IO-AIO/AIO.xs 2015/06/25 15:20:11 1.234 @@ -1,1070 +1,1295 @@ -#define _REENTRANT 1 +#include "libeio/xthread.h" + #include #include "EXTERN.h" #include "perl.h" #include "XSUB.h" -#include "autoconf/config.h" - -#include +#include "schmorp.h" #include +#include #include -#include -#include #include #include #include -#include #include -#include #include -#if HAVE_SENDFILE -# if __linux -# include -# elif __freebsd -# include -# include -# elif __hpux -# include -# elif __solaris /* not yet */ -# include -# else -# error sendfile support requested but not available -# endif +#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES +# include #endif -/* used for struct dirent, AIX doesn't provide it */ -#ifndef NAME_MAX -# define NAME_MAX 4096 +/* the incompetent fool that created musl keeps __linux__, refuses + * to implement any linux standard apis, and also has no way to test + * for his broken iplementation. on't complain if this fails for you. + */ +#if __linux__ && (defined __GLIBC__ || defined __UCLIBC__) +# include +# ifdef FS_IOC_FIEMAP +# include +# include +# define HAVE_FIEMAP 1 +# endif #endif -#if __ia64 -# define STACKSIZE 65536 -#else -# define STACKSIZE 8192 -#endif +/* perl namespace pollution */ +#undef VERSION -enum { - REQ_QUIT, - REQ_OPEN, REQ_CLOSE, - REQ_READ, REQ_WRITE, REQ_READAHEAD, - REQ_SENDFILE, - REQ_STAT, REQ_LSTAT, REQ_FSTAT, - REQ_FSYNC, REQ_FDATASYNC, - REQ_UNLINK, REQ_RMDIR, REQ_RENAME, - REQ_READDIR, - REQ_LINK, REQ_SYMLINK, - REQ_GROUP, REQ_NOP, - REQ_SLEEP, -}; +/* perl stupidly overrides readdir and maybe others */ +/* with thread-unsafe versions, imagine that :( */ +#undef readdir +#undef opendir +#undef closedir + +#ifdef _WIN32 + + // 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 -#define AIO_REQ_KLASS "IO::AIO::REQ" -#define AIO_GRP_KLASS "IO::AIO::GRP" + #define EIO_STRUCT_STAT struct _stati64 + #define EIO_STRUCT_STATI64 -typedef struct aio_cb -{ - struct aio_cb *volatile next; +#else - struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; + #include + #include + #include + #include + #include - SV *self; /* the perl counterpart of this request, if any */ - - SV *data, *callback; - SV *fh, *fh2; - void *dataptr, *data2ptr; - Stat_t *statdata; - off_t offset; - size_t length; - ssize_t result; - - int type; - int fd, fd2; - int errorno; - STRLEN dataoffset; - mode_t mode; /* open */ - unsigned char cancelled; -} aio_cb; - -typedef aio_cb *aio_req; -typedef aio_cb *aio_req_ornot; - -static int started, wanted; -static volatile int nreqs; -static int max_outstanding = 1<<30; -static int respipe [2]; - -static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; + #define EIO_STRUCT_STAT Stat_t -static volatile aio_req reqs, reqe; /* queue start, queue end */ -static volatile aio_req ress, rese; /* queue start, queue end */ +#endif -static void req_invoke (aio_req req); -static void req_free (aio_req req); +/*****************************************************************************/ -/* must be called at most once */ -static SV *req_sv (aio_req req, const char *klass) -{ - if (!req->self) - { - req->self = (SV *)newHV (); - sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); - } +#if __GNUC__ >= 3 +# define expect(expr,value) __builtin_expect ((expr),(value)) +#else +# define expect(expr,value) (expr) +#endif - return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); -} +#define expect_false(expr) expect ((expr) != 0, 0) +#define expect_true(expr) expect ((expr) != 0, 1) -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"); +typedef SV SV8; /* byte-sv, used for argument-checking */ +typedef int aio_rfd; /* read file desriptor */ +typedef int aio_wfd; /* write file descriptor */ - mg = mg_find (SvRV (sv), PERL_MAGIC_ext); +static HV *aio_stash, *aio_req_stash, *aio_grp_stash, *aio_wd_stash; - return mg ? (aio_req)mg->mg_ptr : 0; -} +#define EIO_REQ_MEMBERS \ + SV *callback; \ + SV *sv1, *sv2; \ + SV *sv3, *sv4; \ + STRLEN stroffset; \ + SV *self; -static void aio_grp_feed (aio_req grp) -{ - while (grp->length < grp->fd2) - { - int old_len = grp->length; +#define EIO_NO_WRAPPERS 1 - if (grp->fh2 && SvOK (grp->fh2)) - { - dSP; +#include "libeio/config.h" +#include "libeio/eio.h" - ENTER; - SAVETMPS; - PUSHMARK (SP); - XPUSHs (req_sv (grp, AIO_GRP_KLASS)); - PUTBACK; - call_sv (grp->fh2, G_VOID | G_EVAL); - SPAGAIN; - FREETMPS; - LEAVE; - } +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) - /* stop if no progress has been made */ - if (old_len == grp->length) - { - SvREFCNT_dec (grp->fh2); - grp->fh2 = 0; - break; - } - } -} +#include "libeio/eio.c" -static void aio_grp_dec (aio_req grp) -{ - --grp->length; +#if !HAVE_POSIX_FADVISE +# define posix_fadvise(a,b,c,d) errno = ENOSYS /* also return ENOSYS */ +#endif - /* call feeder, if applicable */ - aio_grp_feed (grp); +#if !HAVE_POSIX_MADVISE +# define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */ +#endif - /* finish, if done */ - if (!grp->length && grp->fd) - { - req_invoke (grp); - req_free (grp); - } -} +#ifndef MAP_ANONYMOUS +# ifdef MAP_ANON +# define MAP_ANONYMOUS MAP_ANON +# else +# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ +# endif +#endif -static void poll_wait () -{ - if (nreqs && !ress) - { - fd_set rfd; - FD_ZERO(&rfd); - FD_SET(respipe [0], &rfd); +/* defines all sorts of constants to 0 unless they are already defined */ +#include "def0.h" - select (respipe [0] + 1, &rfd, 0, 0, 0); - } -} +#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 + +#if PAGESIZE <= 0 +# define PAGESIZE sysconf (_SC_PAGESIZE) +#endif + +/*****************************************************************************/ -static void req_invoke (aio_req req) +static void +fiemap (eio_req *req) { - dSP; - int errorno = errno; + req->result = -1; - if (req->cancelled || !SvOK (req->callback)) +#if HAVE_FIEMAP + /* assume some c99 */ + struct fiemap *fiemap = 0; + size_t end_offset; + int count = req->int3; + + req->flags |= EIO_FLAG_PTR1_FREE; + + /* heuristic: start with 512 bytes (8 extents), and if that isn't enough, */ + /* increase in 3.5kb steps */ + if (count < 0) + count = 8; + + fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); + errno = ENOMEM; + if (!fiemap) return; - errno = req->errorno; + req->ptr1 = fiemap; - ENTER; - SAVETMPS; - PUSHMARK (SP); - EXTEND (SP, 1); - - switch (req->type) - { - case REQ_READDIR: - { - SV *rv = &PL_sv_undef; + fiemap->fm_start = req->offs; + fiemap->fm_length = req->size; + fiemap->fm_flags = req->int2; + fiemap->fm_extent_count = count; - if (req->result >= 0) - { - char *buf = req->data2ptr; - AV *av = newAV (); + if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap) < 0) + return; - while (req->result) - { - SV *sv = newSVpv (buf, 0); + if (req->int3 >= 0 /* not autosizing */ + || !fiemap->fm_mapped_extents /* no more extents */ + || fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST /* hit eof */) + goto done; + + /* else we have to loop - + * it would be tempting (actually I tried that first) to just query the + * number of extents needed, but linux often feels like not returning all + * extents, without telling us it left any out. this complicates + * this quite a bit. + */ - av_push (av, sv); - buf += SvCUR (sv) + 1; - req->result--; - } + end_offset = fiemap->fm_length + (fiemap->fm_length == FIEMAP_MAX_OFFSET ? 0 : fiemap->fm_start); - rv = sv_2mortal (newRV_noinc ((SV *)av)); - } + for (;;) + { + /* we go in 54 extent steps - 3kb, in the hope that this fits nicely on the eio stack (normally 16+ kb) */ + char scratch[3072]; + struct fiemap *incmap = (struct fiemap *)scratch; + + incmap->fm_start = fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_logical + + fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_length; + incmap->fm_length = fiemap->fm_length - (incmap->fm_start - fiemap->fm_start); + incmap->fm_flags = fiemap->fm_flags; + incmap->fm_extent_count = (sizeof (scratch) - sizeof (struct fiemap)) / sizeof (struct fiemap_extent); + + if (ioctl (req->int1, FS_IOC_FIEMAP, incmap) < 0) + return; + + if (!incmap->fm_mapped_extents) + goto done; + + count = fiemap->fm_mapped_extents + incmap->fm_mapped_extents; + fiemap = realloc (fiemap, sizeof (*fiemap) + sizeof (struct fiemap_extent) * count); + errno = ENOMEM; + if (!fiemap) + return; - PUSHs (rv); - } - break; + req->ptr1 = fiemap; - case REQ_OPEN: + for (count = 0; count < incmap->fm_mapped_extents; ++count) { - /* convert fd to fh */ - SV *fh; + struct fiemap_extent *e = incmap->fm_extents + count; + + if (e->fe_logical + e->fe_length >= end_offset) + goto done; - PUSHs (sv_2mortal (newSViv (req->result))); - PUTBACK; - call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); - SPAGAIN; + fiemap->fm_extents [fiemap->fm_mapped_extents++] = *e; - fh = SvREFCNT_inc (POPs); + if (e->fe_flags & FIEMAP_EXTENT_LAST) + goto done; - PUSHMARK (SP); - XPUSHs (sv_2mortal (fh)); } - break; + } - case REQ_GROUP: - req->fd = 2; /* mark group as finished */ +done: + req->result = 0; - if (req->data) - { - int i; - AV *av = (AV *)req->data; +#else + errno = ENOSYS; +#endif +} - EXTEND (SP, AvFILL (av) + 1); - for (i = 0; i <= AvFILL (av); ++i) - PUSHs (*av_fetch (av, i, 0)); - } - break; +/*****************************************************************************/ - case REQ_NOP: - case REQ_SLEEP: - break; +enum { + FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ +}; - default: - PUSHs (sv_2mortal (newSViv (req->result))); - break; - } +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; +static int max_outstanding; - PUTBACK; - call_sv (req->callback, G_VOID | G_EVAL); - SPAGAIN; +static s_epipe respipe; - FREETMPS; - LEAVE; +static void req_destroy (aio_req req); +static void req_cancel (aio_req req); - errno = errorno; +static void +want_poll (void) +{ + /* write a dummy byte to the pipe so fh becomes ready */ + s_epipe_signal (&respipe); +} - if (SvTRUE (ERRSV)) - { - req_free (req); - croak (0); - } +static void +done_poll (void) +{ + /* read any signals sent by the worker threads */ + s_epipe_drain (&respipe); } -static void req_free (aio_req req) +/* must be called at most once */ +static SV * +req_sv (aio_req req, HV *stash) { - if (req->grp) + if (!req->self) { - aio_req grp = req->grp; + req->self = (SV *)newHV (); + sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); + } - /* unlink request */ - if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; - if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; + return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); +} - if (grp->grp_first == req) - grp->grp_first = req->grp_next; +static SV * +newSVaio_wd (aio_wd wd) +{ + return sv_bless (newRV_noinc (newSViv ((intptr_t)wd)), aio_wd_stash); +} - aio_grp_dec (grp); - } +static aio_req +SvAIO_REQ (SV *sv) +{ + MAGIC *mg; - if (req->self) - { - sv_unmagic (req->self, PERL_MAGIC_ext); - SvREFCNT_dec (req->self); - } + if (!SvROK (sv) + /* for speed reasons, we do not verify that SvROK actually has a stash ptr */ + || (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"); - SvREFCNT_dec (req->data); - SvREFCNT_dec (req->fh); - SvREFCNT_dec (req->fh2); - SvREFCNT_dec (req->callback); - Safefree (req->statdata); + mg = mg_find (SvRV (sv), PERL_MAGIC_ext); + + return mg ? (aio_req)mg->mg_ptr : 0; +} - if (req->type == REQ_READDIR && req->result >= 0) - free (req->data2ptr); +static aio_wd +SvAIO_WD (SV *sv) +{ + if (!SvROK (sv) + || SvTYPE (SvRV (sv)) != SVt_PVMG + || SvSTASH (SvRV (sv)) != aio_wd_stash) + croak ("IO::AIO: expected a working directory object as returned by aio_wd"); - Safefree (req); + return (aio_wd)(long)SvIVX (SvRV (sv)); } -static void req_cancel (aio_req req) +static SV * +newmortalFH (int fd, int flags) { - req->cancelled = 1; + if (fd < 0) + return &PL_sv_undef; - if (req->type == REQ_GROUP) + GV *gv = (GV *)sv_newmortal (); + char sym[64]; + int symlen; + + symlen = snprintf (sym, sizeof (sym), "fd#%d", fd); + gv_init (gv, aio_stash, sym, symlen, 0); + + symlen = snprintf ( + sym, + sizeof (sym), + "%s&=%d", + flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", + fd + ); + + return do_open (gv, sym, symlen, 0, 0, 0, 0) + ? (SV *)gv : &PL_sv_undef; +} + +static void +aio_grp_feed (aio_req grp) +{ + if (grp->sv2 && SvOK (grp->sv2)) { - aio_req sub; + dSP; - for (sub = req->grp_first; sub; sub = sub->grp_next) - req_cancel (sub); + ENTER; + SAVETMPS; + PUSHMARK (SP); + XPUSHs (req_sv (grp, aio_grp_stash)); + PUTBACK; + call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); + SPAGAIN; + FREETMPS; + LEAVE; } } -static int poll_cb () +static void +req_submit (eio_req *req) { - dSP; - int count = 0; - int do_croak = 0; - aio_req req; + eio_submit (req); - for (;;) + if (expect_false (on_next_submit)) { - pthread_mutex_lock (&reslock); - req = ress; - - if (req) - { - ress = req->next; + dSP; + SV *cb = sv_2mortal (on_next_submit); - if (!ress) - { - /* read any signals sent by the worker threads */ - char buf [32]; - while (read (respipe [0], buf, 32) == 32) - ; + on_next_submit = 0; - rese = 0; - } - } + PUSHMARK (SP); + PUTBACK; + call_sv (cb, G_DISCARD | G_EVAL); + } +} - pthread_mutex_unlock (&reslock); +static int +req_invoke (eio_req *req) +{ + if (req->flags & FLAG_SV2_RO_OFF) + SvREADONLY_off (req->sv2); - if (!req) - break; + if (!EIO_CANCELLED (req) && req->callback) + { + dSP; + static SV *sv_result_cache; /* caches the result integer SV */ + SV *sv_result; - --nreqs; + ENTER; + SAVETMPS; + PUSHMARK (SP); + EXTEND (SP, 1); - if (req->type == REQ_QUIT) - started--; - else if (req->type == REQ_GROUP && req->length) + /* do not recreate the result IV from scratch each time */ + if (expect_true (sv_result_cache)) { - req->fd = 1; /* mark request as delayed */ - continue; + sv_result = sv_result_cache; sv_result_cache = 0; + SvIV_set (sv_result, req->result); + SvIOK_only (sv_result); } else { - if (req->type == REQ_READ) - SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); + sv_result = newSViv (req->result); + SvREADONLY_on (sv_result); + } - if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) - SvREADONLY_off (req->data); + switch (req->type) + { + case EIO_WD_OPEN: + PUSHs (req->result ? &PL_sv_undef : sv_2mortal (newSVaio_wd (req->wd))); + break; - if (req->statdata) + case EIO_READDIR: { - PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; - PL_laststatval = req->result; - PL_statcache = *(req->statdata); - } + SV *rv = &PL_sv_undef; - req_invoke (req); + if (req->result >= 0) + { + int i; + char *names = (char *)req->ptr2; + eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */ + AV *av = newAV (); + + av_extend (av, req->result - 1); + + for (i = 0; i < req->result; ++i) + { + if (req->int1 & EIO_READDIR_DENTS) + { + SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen); + + if (req->int1 & EIO_READDIR_CUSTOM2) + { + static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */ + AV *avent = newAV (); + + av_extend (avent, 2); + + if (!sv_type [ent->type]) + { + sv_type [ent->type] = newSViv (ent->type); + SvREADONLY_on (sv_type [ent->type]); + } + + av_store (avent, 0, namesv); + av_store (avent, 1, SvREFCNT_inc (sv_type [ent->type])); + av_store (avent, 2, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode)); + + av_store (av, i, newRV_noinc ((SV *)avent)); + } + else + av_store (av, i, namesv); + + ++ent; + } + else + { + SV *name = newSVpv (names, 0); + av_store (av, i, name); + names += SvCUR (name) + 1; + } + } - count++; - } + rv = sv_2mortal (newRV_noinc ((SV *)av)); + } - req_free (req); - } + PUSHs (rv); - return count; -} + if (req->int1 & EIO_READDIR_CUSTOM1) + XPUSHs (sv_2mortal (newSViv (req->int1 & ~(EIO_READDIR_CUSTOM1 | EIO_READDIR_CUSTOM2)))); + } + break; -static void *aio_proc(void *arg); + case EIO_OPEN: + PUSHs (newmortalFH (req->result, req->int1 & (O_RDONLY | O_WRONLY | O_RDWR))); + break; -static void start_thread (void) -{ - sigset_t fullsigset, oldsigset; - pthread_t tid; - pthread_attr_t attr; + 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 (); + /* POSIX requires fsid to be unsigned long, but AIX in its infinite wisdom + * chooses to make it a struct. + */ + unsigned long fsid = 0; + memcpy (&fsid, &f->f_fsid, sizeof (unsigned long) < sizeof (f->f_fsid) ? sizeof (unsigned long) : sizeof (f->f_fsid)); + + 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 (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 - pthread_attr_init (&attr); - pthread_attr_setstacksize (&attr, STACKSIZE); - pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + PUSHs (rv); + } - sigfillset (&fullsigset); - sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); + break; - if (pthread_create (&tid, &attr, aio_proc, 0) == 0) - started++; + case EIO_GROUP: + req->int1 = 2; /* mark group as finished */ - sigprocmask (SIG_SETMASK, &oldsigset, 0); -} + if (req->sv1) + { + int i; + AV *av = (AV *)req->sv1; -static void req_send (aio_req req) -{ - while (started < wanted && nreqs >= started) - start_thread (); + EXTEND (SP, AvFILL (av) + 1); + for (i = 0; i <= AvFILL (av); ++i) + PUSHs (*av_fetch (av, i, 0)); + } + break; - ++nreqs; + case EIO_NOP: + case EIO_BUSY: + break; - pthread_mutex_lock (&reqlock); + case EIO_READLINK: + case EIO_REALPATH: + if (req->result > 0) + PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result))); + break; - req->next = 0; + case EIO_STAT: + case EIO_LSTAT: + case EIO_FSTAT: + PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; + + if (!(PL_laststatval = req->result)) + /* if compilation fails here then perl's Stat_t is not struct _stati64 */ + PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); - if (reqe) - { - reqe->next = req; - reqe = req; - } - else - reqe = reqs = req; + PUSHs (sv_result); + break; - pthread_cond_signal (&reqwait); - pthread_mutex_unlock (&reqlock); + case EIO_SEEK: + PUSHs (req->result ? sv_result : sv_2mortal (newSVval64 (req->offs))); + break; - if (nreqs > max_outstanding) - for (;;) - { - poll_cb (); + case EIO_READ: + { + SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); + *SvEND (req->sv2) = 0; + SvPOK_only (req->sv2); + SvSETMAGIC (req->sv2); + PUSHs (sv_result); + } + break; - if (nreqs <= max_outstanding) - break; + case EIO_CUSTOM: + if (req->feed == fiemap) + { +#if HAVE_FIEMAP + if (!req->result) + { + struct fiemap *fiemap = (struct fiemap *)req->ptr1; + + if (fiemap->fm_extent_count) + { + AV *av = newAV (); + int i; + + while (fiemap->fm_mapped_extents) + { + struct fiemap_extent *extent = &fiemap->fm_extents [--fiemap->fm_mapped_extents]; + AV *ext_av = newAV (); + + av_store (ext_av, 3, newSVuv (extent->fe_flags)); + av_store (ext_av, 2, newSVval64 (extent->fe_length)); + av_store (ext_av, 1, newSVval64 (extent->fe_physical)); + av_store (ext_av, 0, newSVval64 (extent->fe_logical)); + + av_store (av, fiemap->fm_mapped_extents, newRV_noinc ((SV *)ext_av)); + } + + PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); + } + else + { + SvIV_set (sv_result, fiemap->fm_mapped_extents); + PUSHs (sv_result); + } + } +#endif + } + else + PUSHs (sv_result); + break; - poll_wait (); - } -} + case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), so fudge result value */ + if (req->result > 0) + SvIV_set (sv_result, 0); + /* FALLTHROUGH */ -static void end_thread (void) -{ - aio_req req; - Newz (0, req, 1, aio_cb); - req->type = REQ_QUIT; + default: + PUSHs (sv_result); + break; + } - req_send (req); -} + errno = req->errorno; -static void min_parallel (int nthreads) -{ - if (wanted < nthreads) - wanted = nthreads; -} + PUTBACK; + call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); + SPAGAIN; -static void max_parallel (int nthreads) -{ - int cur = started; + if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache)) + SvREFCNT_dec (sv_result); + else + sv_result_cache = sv_result; - if (wanted > nthreads) - wanted = nthreads; + FREETMPS; + LEAVE; - while (cur > wanted) - { - end_thread (); - cur--; + PUTBACK; } - while (started > wanted) - { - poll_wait (); - poll_cb (); - } + return !!SvTRUE (ERRSV); } -static void create_pipe () +static void +req_destroy (aio_req req) { - if (pipe (respipe)) - croak ("unable to initialize result pipe"); + if (req->self) + { + sv_unmagic (req->self, PERL_MAGIC_ext); + SvREFCNT_dec (req->self); + } - if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) - croak ("cannot set result pipe to nonblocking mode"); + SvREFCNT_dec (req->sv1); + SvREFCNT_dec (req->sv2); + SvREFCNT_dec (req->sv3); + SvREFCNT_dec (req->sv4); + SvREFCNT_dec (req->callback); - if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) - croak ("cannot set result pipe to nonblocking mode"); + free (req); } -/*****************************************************************************/ -/* work around various missing functions */ - -#if !HAVE_PREADWRITE -# define pread aio_pread -# define pwrite aio_pwrite - -/* - * make our pread/pwrite safe against themselves, but not against - * normal read/write by using a mutex. slows down execution a lot, - * but that's your problem, not mine. - */ -static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; - -static ssize_t pread (int fd, void *buf, size_t count, off_t offset) +static void +req_cancel_subs (aio_req grp) { - ssize_t res; - off_t ooffset; + if (grp->type != EIO_GROUP) + return; - pthread_mutex_lock (&preadwritelock); - ooffset = lseek (fd, 0, SEEK_CUR); - lseek (fd, offset, SEEK_SET); - res = read (fd, buf, count); - lseek (fd, ooffset, SEEK_SET); - pthread_mutex_unlock (&preadwritelock); + SvREFCNT_dec (grp->sv2); + grp->sv2 = 0; - return res; + eio_grp_cancel (grp); } -static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) +static void ecb_cold +create_respipe (void) { - ssize_t res; - off_t ooffset; - - pthread_mutex_lock (&preadwritelock); - ooffset = lseek (fd, 0, SEEK_CUR); - lseek (fd, offset, SEEK_SET); - res = write (fd, buf, count); - lseek (fd, offset, SEEK_SET); - pthread_mutex_unlock (&preadwritelock); - - return res; + if (s_epipe_renew (&respipe)) + croak ("IO::AIO: unable to initialize result pipe"); } -#endif -#if !HAVE_FDATASYNC -# define fdatasync fsync -#endif +static void +poll_wait (void) +{ + while (eio_nreqs ()) + { + int size; -#if !HAVE_READAHEAD -# define readahead aio_readahead + X_LOCK (reslock); + size = res_queue.size; + X_UNLOCK (reslock); -static ssize_t readahead (int fd, off_t offset, size_t count) -{ - char readahead_buf[4096]; + if (size) + return; - while (count > 0) - { - size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); + etp_maybe_start_thread (); - pread (fd, readahead_buf, len, offset); - offset += len; - count -= len; + s_epipe_wait (&respipe); } - - errno = 0; } -#endif - -#if !HAVE_READDIR_R -# define readdir_r aio_readdir_r -static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; - -static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) +static int +poll_cb (void) { - struct dirent *e; - int errorno; + for (;;) + { + int res = eio_poll (); - pthread_mutex_lock (&readdirlock); + if (res > 0) + croak (0); - e = readdir (dirp); - errorno = errno; + if (!max_outstanding || max_outstanding > eio_nreqs ()) + return res; - if (e) - { - *res = ent; - strcpy (ent->d_name, e->d_name); + poll_wait (); } - else - *res = 0; - - pthread_mutex_unlock (&readdirlock); - - errno = errorno; - return e ? 0 : -1; } -#endif -/* sendfile always needs emulation */ -static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) +static void ecb_cold +reinit (void) { - ssize_t res; + create_respipe (); - if (!count) - return 0; - -#if HAVE_SENDFILE -# if __linux - res = sendfile (ofd, ifd, &offset, count); + if (eio_init (want_poll, done_poll) < 0) + croak ("IO::AIO: unable to initialise eio library"); +} -# elif __freebsd - /* - * Of course, the freebsd sendfile is a dire hack with no thoughts - * wasted on making it similar to other I/O functions. - */ - { - off_t sbytes; - res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); - - if (res < 0 && sbytes) - /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ - res = sbytes; - } - -# elif __hpux - res = sendfile (ofd, ifd, offset, count, 0, 0); - -# elif __solaris - { - struct sendfilevec vec; - size_t sbytes; - - vec.sfv_fd = ifd; - vec.sfv_flag = 0; - vec.sfv_off = offset; - vec.sfv_len = count; - - res = sendfilev (ofd, &vec, 1, &sbytes); - - if (res < 0 && sbytes) - res = sbytes; - } +/*****************************************************************************/ -# endif -#else - res = -1; - errno = ENOSYS; +#if !_POSIX_MAPPED_FILES +# define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS () +# define munmap(addr,length) EIO_ENOSYS () #endif - if (res < 0 - && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK -#if __solaris - || errno == EAFNOSUPPORT || errno == EPROTOTYPE +#if !_POSIX_MEMORY_PROTECTION +# define mprotect(addr,len,prot) EIO_ENOSYS () +# define PROT_NONE 0 +# define PROT_WRITE 0 +# define MAP_PRIVATE 0 +# define MAP_SHARED 0 +# define MAP_FIXED 0 #endif - ) - ) - { - /* emulate sendfile. this is a major pain in the ass */ - char buf[4096]; - res = 0; - while (count) - { - ssize_t cnt; - - cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); +#define MMAP_MAGIC PERL_MAGIC_ext - if (cnt <= 0) - { - if (cnt && !res) res = -1; - break; - } +static int ecb_cold +mmap_free (pTHX_ SV *sv, MAGIC *mg) +{ + int old_errno = errno; + munmap (mg->mg_ptr, (size_t)mg->mg_obj); + errno = old_errno; - cnt = write (ofd, buf, cnt); + mg->mg_obj = 0; /* just in case */ - if (cnt <= 0) - { - if (cnt && !res) res = -1; - break; - } + SvREADONLY_off (sv); - offset += cnt; - res += cnt; - count -= cnt; - } - } + if (SvPVX (sv) != mg->mg_ptr) + croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); - return res; + SvCUR_set (sv, 0); + SvPVX (sv) = 0; + SvOK_off (sv); + + return 0; } -/* read a full directory */ -static int scandir_ (const char *path, void **namesp) +static MGVTBL mmap_vtbl = { + 0, 0, 0, 0, mmap_free +}; + +/*****************************************************************************/ + +static SV * +get_cb (SV *cb_sv) { - DIR *dirp = opendir (path); - union - { - struct dirent d; - char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; - } u; - struct dirent *entp; - char *name, *names; - int memlen = 4096; - int memofs = 0; - int res = 0; - int errorno; + SvGETMAGIC (cb_sv); + return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; +} - if (!dirp) - return -1; +static aio_req ecb_noinline +dreq (SV *callback) +{ + SV *cb_cv; + aio_req req; + int req_pri = next_pri; + next_pri = EIO_PRI_DEFAULT; - names = malloc (memlen); + cb_cv = get_cb (callback); - for (;;) - { - errno = 0, readdir_r (dirp, &u.d, &entp); + req = calloc (sizeof (*req), 1); + if (!req) + croak ("out of memory during eio_req allocation"); - if (!entp) - break; + req->callback = SvREFCNT_inc (cb_cv); + req->pri = req_pri; - name = entp->d_name; + return req; +} - if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) - { - int len = strlen (name) + 1; +#define dREQ \ + aio_req req = dreq (callback); \ + +#define REQ_SEND \ + PUTBACK; \ + req_submit (req); \ + SPAGAIN; \ + \ + if (GIMME_V != G_VOID) \ + XPUSHs (req_sv (req, aio_req_stash)); + +ecb_inline void +req_set_path (aio_req req, SV *path, SV **wdsv, SV **pathsv, eio_wd *wd, void **ptr) +{ + if (expect_false (SvROK (path))) + { + SV *rv = SvRV (path); + SV *wdob; - res++; + if (SvTYPE (rv) == SVt_PVAV && AvFILLp (rv) == 1) + { + path = AvARRAY (rv)[1]; + wdob = AvARRAY (rv)[0]; - while (memofs + len > memlen) + if (SvOK (wdob)) { - memlen *= 2; - names = realloc (names, memlen); - if (!names) - break; + *wd = SvAIO_WD (wdob); + *wdsv = SvREFCNT_inc_NN (SvRV (wdob)); } - - memcpy (names + memofs, name, len); - memofs += len; + else + *wd = EIO_INVALID_WD; } + else if (SvTYPE (rv) == SVt_PVMG && SvSTASH (rv) == aio_wd_stash) + { + *wd = (aio_wd)(long)SvIVX (rv); + *wdsv = SvREFCNT_inc_NN (rv); + *ptr = "."; + return; /* path set to "." */ + } + else + croak ("IO::AIO: pathname arguments must be specified as a string, an IO::AIO::WD object or a [IO::AIO::WD, path] pair"); } - errorno = errno; - closedir (dirp); - - if (errorno) - { - free (names); - errno = errorno; - res = -1; - } - - *namesp = (void *)names; - return res; + *pathsv = newSVsv (path); + *ptr = SvPVbyte_nolen (*pathsv); } -/*****************************************************************************/ +static void ecb_noinline +req_set_path1 (aio_req req, SV *path) +{ + req_set_path (req, path, &req->sv1, &req->sv3, &req->wd, &req->ptr1); +} -static void *aio_proc (void *thr_arg) +static void ecb_noinline +req_set_fh_or_path (aio_req req, int type_path, int type_fh, SV *fh_or_path) { - aio_req req; - int type; + SV *rv = SvROK (fh_or_path) ? SvRV (fh_or_path) : fh_or_path; - do + switch (SvTYPE (rv)) { - pthread_mutex_lock (&reqlock); - - for (;;) - { - req = reqs; + 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; - if (reqs) - { - reqs = reqs->next; - if (!reqs) reqe = 0; - } + default: + req->type = type_path; + req_set_path1 (req, fh_or_path); + break; + } +} - if (req) - break; +XS(boot_IO__AIO) ecb_cold; - pthread_cond_wait (&reqwait, &reqlock); - } +MODULE = IO::AIO PACKAGE = IO::AIO - pthread_mutex_unlock (&reqlock); - - errno = 0; /* strictly unnecessary */ +PROTOTYPES: ENABLE - if (!req->cancelled) - switch (type = req->type) /* remember type for QUIT check */ - { - case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; - case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; +BOOT: +{ + static const struct { + const char *name; + IV iv; + } *civ, const_iv[] = { +# define const_niv(name, value) { # name, (IV) value }, +# define const_iv(name) { # name, (IV) name }, +# define const_eio(name) { # name, (IV) EIO_ ## name }, + + /* you have to re-run ./gendef0 after adding/Removing any constants here */ + + const_iv (ENOSYS) + const_iv (EXDEV) + const_iv (EBADR) + + 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_PATH) + const_iv (O_TMPFILE) + 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_iv (FIEMAP_FLAG_SYNC) + const_iv (FIEMAP_FLAG_XATTR) + const_iv (FIEMAP_FLAGS_COMPAT) + const_iv (FIEMAP_EXTENT_LAST) + const_iv (FIEMAP_EXTENT_UNKNOWN) + const_iv (FIEMAP_EXTENT_DELALLOC) + const_iv (FIEMAP_EXTENT_ENCODED) + const_iv (FIEMAP_EXTENT_DATA_ENCRYPTED) + const_iv (FIEMAP_EXTENT_NOT_ALIGNED) + const_iv (FIEMAP_EXTENT_DATA_INLINE) + const_iv (FIEMAP_EXTENT_DATA_TAIL) + const_iv (FIEMAP_EXTENT_UNWRITTEN) + const_iv (FIEMAP_EXTENT_MERGED) + const_iv (FIEMAP_EXTENT_SHARED) + + const_iv (SPLICE_F_MOVE) + const_iv (SPLICE_F_NONBLOCK) + const_iv (SPLICE_F_MORE) + const_iv (SPLICE_F_GIFT) + + const_iv (SEEK_DATA) + const_iv (SEEK_HOLE) + + /* libeio constants */ + const_eio (SEEK_SET) + const_eio (SEEK_CUR) + const_eio (SEEK_END) + + 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 (FALLOC_FL_PUNCH_HOLE) + const_eio (FALLOC_FL_COLLAPSE_RANGE) + const_eio (FALLOC_FL_ZERO_RANGE) + + const_eio (READDIR_DENTS) + const_eio (READDIR_DIRS_FIRST) + const_eio (READDIR_STAT_ORDER) + const_eio (READDIR_FOUND_UNKNOWN) + + const_eio (DT_UNKNOWN) + const_eio (DT_FIFO) + const_eio (DT_CHR) + const_eio (DT_DIR) + const_eio (DT_BLK) + const_eio (DT_REG) + const_eio (DT_LNK) + const_eio (DT_SOCK) + const_eio (DT_WHT) + }; + + 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); + + 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)); - case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; - case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; + newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); - case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; - case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; - case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; - - case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; - case REQ_CLOSE: req->result = close (req->fd); break; - case REQ_UNLINK: req->result = unlink (req->dataptr); break; - case REQ_RMDIR: req->result = rmdir (req->dataptr); break; - case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; - case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; - case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; - - case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; - case REQ_FSYNC: req->result = fsync (req->fd); break; - case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; + reinit (); +} - case REQ_SLEEP: - { - struct timeval tv; +void +reinit () + PROTOTYPE: - tv.tv_sec = req->fd; - tv.tv_usec = req->fd2; +void +max_poll_reqs (unsigned int nreqs) + PROTOTYPE: $ + CODE: + eio_set_max_poll_reqs (nreqs); - req->result = select (0, 0, 0, 0, &tv); - } +void +max_poll_time (double nseconds) + PROTOTYPE: $ + CODE: + eio_set_max_poll_time (nseconds); - case REQ_GROUP: - case REQ_NOP: - case REQ_QUIT: - break; - - default: - req->result = ENOSYS; - break; - } +void +min_parallel (unsigned int nthreads) + PROTOTYPE: $ + CODE: + eio_set_min_parallel (nthreads); - req->errorno = errno; +void +max_parallel (unsigned int nthreads) + PROTOTYPE: $ + CODE: + eio_set_max_parallel (nthreads); - pthread_mutex_lock (&reslock); +void +max_idle (unsigned int nthreads) + PROTOTYPE: $ + CODE: + eio_set_max_idle (nthreads); - req->next = 0; +void +idle_timeout (unsigned int seconds) + PROTOTYPE: $ + CODE: + eio_set_idle_timeout (seconds); - if (rese) - { - rese->next = req; - rese = req; - } - else - { - rese = ress = req; +void +max_outstanding (unsigned int maxreqs) + PROTOTYPE: $ + CODE: + max_outstanding = maxreqs; - /* write a dummy byte to the pipe so fh becomes ready */ - write (respipe [1], &respipe, 1); - } +void +aio_wd (SV8 *pathname, SV *callback = &PL_sv_undef) + PPCODE: +{ + dREQ; - pthread_mutex_unlock (&reslock); - } - while (type != REQ_QUIT); + req->type = EIO_WD_OPEN; + req_set_path1 (req, pathname); - return 0; + REQ_SEND; } -/*****************************************************************************/ - -static void atfork_prepare (void) +void +aio_open (SV8 *pathname, int flags, int mode, SV *callback = &PL_sv_undef) + PPCODE: { - pthread_mutex_lock (&reqlock); - pthread_mutex_lock (&reslock); -#if !HAVE_PREADWRITE - pthread_mutex_lock (&preadwritelock); -#endif -#if !HAVE_READDIR_R - pthread_mutex_lock (&readdirlock); -#endif -} + dREQ; -static void atfork_parent (void) -{ -#if !HAVE_READDIR_R - pthread_mutex_unlock (&readdirlock); -#endif -#if !HAVE_PREADWRITE - pthread_mutex_unlock (&preadwritelock); -#endif - pthread_mutex_unlock (&reslock); - pthread_mutex_unlock (&reqlock); + req->type = EIO_OPEN; + req_set_path1 (req, pathname); + req->int1 = flags; + req->int2 = mode; + + REQ_SEND; } -static void atfork_child (void) +void +aio_fsync (SV *fh, SV *callback = &PL_sv_undef) + ALIAS: + aio_fsync = EIO_FSYNC + aio_fdatasync = EIO_FDATASYNC + aio_syncfs = EIO_SYNCFS + PPCODE: { - aio_req prv; - - started = 0; - - while (reqs) - { - prv = reqs; - reqs = prv->next; - req_free (prv); - } - - reqs = reqe = 0; - - while (ress) - { - prv = ress; - ress = prv->next; - req_free (prv); - } - - ress = rese = 0; + int fd = s_fileno_croak (fh, 0); + dREQ; - close (respipe [0]); - close (respipe [1]); - create_pipe (); + req->type = ix; + req->sv1 = newSVsv (fh); + req->int1 = fd; - atfork_parent (); + REQ_SEND; } -#define dREQ \ - aio_req req; \ - \ - if (SvOK (callback) && !SvROK (callback)) \ - croak ("callback must be undef or of reference type"); \ - \ - Newz (0, req, 1, aio_cb); \ - if (!req) \ - croak ("out of memory during aio_req allocation"); \ - \ - req->callback = newSVsv (callback) - -#define REQ_SEND \ - req_send (req); \ - \ - if (GIMME_V != G_VOID) \ - XPUSHs (req_sv (req, AIO_REQ_KLASS)); - -MODULE = IO::AIO PACKAGE = IO::AIO - -PROTOTYPES: ENABLE - -BOOT: +void +aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback = &PL_sv_undef) + PPCODE: { - HV *stash = gv_stashpv ("IO::AIO", 1); - newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); - newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); - newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); + int fd = s_fileno_croak (fh, 0); + dREQ; + + req->type = EIO_SYNC_FILE_RANGE; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->offs = offset; + req->size = nbytes; + req->int2 = flags; - create_pipe (); - pthread_atfork (atfork_prepare, atfork_parent, atfork_child); + REQ_SEND; } void -min_parallel (nthreads) - int nthreads - PROTOTYPE: $ +aio_allocate (SV *fh, int mode, off_t offset, size_t len, SV *callback = &PL_sv_undef) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); + dREQ; -void -max_parallel (nthreads) - int nthreads - PROTOTYPE: $ + req->type = EIO_FALLOCATE; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->int2 = mode; + req->offs = offset; + req->size = len; -int -max_outstanding (nreqs) - int nreqs - PROTOTYPE: $ - CODE: - RETVAL = max_outstanding; - max_outstanding = nreqs; + REQ_SEND; +} void -aio_open (pathname,flags,mode,callback=&PL_sv_undef) - SV * pathname - int flags - int mode - SV * callback - PROTOTYPE: $$$;$ +aio_close (SV *fh, SV *callback = &PL_sv_undef) PPCODE: { + static int close_fd = -1; /* dummy fd to close fds via dup2 */ + int fd = s_fileno_croak (fh, 0); dREQ; - req->type = REQ_OPEN; - req->data = newSVsv (pathname); - req->dataptr = SvPVbyte_nolen (req->data); - req->fd = flags; - req->mode = mode; + if (expect_false (close_fd < 0)) + { + int pipefd [2]; + + 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_fd = pipefd [0]; + } + + req->type = EIO_DUP2; + req->int1 = close_fd; + req->sv2 = newSVsv (fh); + req->int2 = fd; REQ_SEND; } void -aio_close (fh,callback=&PL_sv_undef) - SV * fh - SV * callback - PROTOTYPE: $;$ - ALIAS: - aio_close = REQ_CLOSE - aio_fsync = REQ_FSYNC - aio_fdatasync = REQ_FDATASYNC - PPCODE: +aio_seek (SV *fh, SV *offset, int whence, SV *callback = &PL_sv_undef) + PPCODE: { + int fd = s_fileno_croak (fh, 0); dREQ; - req->type = ix; - req->fh = newSVsv (fh); - req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); + req->type = EIO_SEEK; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->offs = SvVAL64 (offset); + req->int2 = whence; - REQ_SEND (req); + REQ_SEND; } void -aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) - SV * fh - UV offset - UV length - SV * data - UV dataoffset - SV * callback +aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback = &PL_sv_undef) ALIAS: - aio_read = REQ_READ - aio_write = REQ_WRITE - PROTOTYPE: $$$$$;$ + aio_read = EIO_READ + aio_write = EIO_WRITE PPCODE: { - aio_req req; STRLEN svlen; + int fd = s_fileno_croak (fh, ix == EIO_WRITE); char *svptr = SvPVbyte (data, svlen); - - SvUPGRADE (data, SVt_PV); - SvPOK_on (data); + UV len = SvUV (length); if (dataoffset < 0) dataoffset += svlen; if (dataoffset < 0 || dataoffset > svlen) - croak ("data offset outside of string"); + croak ("dataoffset outside of data scalar"); - if (ix == REQ_WRITE) + if (ix == EIO_WRITE) { /* write: check length and adjust. */ - if (length < 0 || length + dataoffset > svlen) - length = svlen - dataoffset; + if (!SvOK (length) || len + dataoffset > svlen) + len = svlen - dataoffset; } else { - /* read: grow scalar as necessary */ - svptr = SvGROW (data, length + dataoffset); + /* read: check type and grow scalar as necessary */ + if (!SvPOK (data) || SvLEN (data) >= SvCUR (data)) + svptr = sv_grow (data, len + dataoffset + 1); + else if (SvCUR (data) < len + dataoffset) + croak ("length + dataoffset outside of scalar, and cannot grow"); } - if (length < 0) - croak ("length must not be negative"); - { dREQ; req->type = ix; - req->fh = newSVsv (fh); - req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) - : IoOFP (sv_2io (fh))); - req->offset = offset; - req->length = length; - req->data = SvREFCNT_inc (data); - req->dataptr = (char *)svptr + dataoffset; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; + req->size = len; + req->sv2 = SvREFCNT_inc (data); + req->ptr2 = (char *)svptr + dataoffset; + req->stroffset = dataoffset; if (!SvREADONLY (data)) { SvREADONLY_on (data); - req->data2ptr = (void *)data; + req->flags |= FLAG_SV2_RO_OFF; } REQ_SEND; @@ -1072,192 +1297,400 @@ } void -aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) - SV * out_fh - SV * in_fh - UV in_offset - UV length - SV * callback - PROTOTYPE: $$$$;$ +aio_readlink (SV8 *pathname, SV *callback = &PL_sv_undef) + ALIAS: + aio_readlink = EIO_READLINK + aio_realpath = EIO_REALPATH + PPCODE: +{ + dREQ; + + 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) PPCODE: { + int ifd = s_fileno_croak (in_fh , 0); + int ofd = s_fileno_croak (out_fh, 1); dREQ; - req->type = REQ_SENDFILE; - req->fh = newSVsv (out_fh); - req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); - req->fh2 = newSVsv (in_fh); - req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); - req->offset = in_offset; - req->length = length; + req->type = EIO_SENDFILE; + req->sv1 = newSVsv (out_fh); + req->int1 = ofd; + req->sv2 = newSVsv (in_fh); + req->int2 = ifd; + req->offs = in_offset; + req->size = length; REQ_SEND; } void -aio_readahead (fh,offset,length,callback=&PL_sv_undef) - SV * fh - UV offset - IV length - SV * callback - PROTOTYPE: $$$;$ +aio_readahead (SV *fh, off_t offset, size_t length, SV *callback = &PL_sv_undef) PPCODE: { + int fd = s_fileno_croak (fh, 0); dREQ; - req->type = REQ_READAHEAD; - req->fh = newSVsv (fh); - req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); - req->offset = offset; - req->length = length; + req->type = EIO_READAHEAD; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->offs = offset; + req->size = length; REQ_SEND; } void -aio_stat (fh_or_path,callback=&PL_sv_undef) - SV * fh_or_path - SV * callback +aio_stat (SV8 *fh_or_path, SV *callback = &PL_sv_undef) ALIAS: - aio_stat = REQ_STAT - aio_lstat = REQ_LSTAT + aio_stat = EIO_STAT + aio_lstat = EIO_LSTAT + aio_statvfs = EIO_STATVFS PPCODE: { dREQ; - New (0, req->statdata, 1, Stat_t); - if (!req->statdata) - { - req_free (req); - croak ("out of memory during aio_req->statdata allocation"); - } + req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path); - if (SvPOK (fh_or_path)) - { - req->type = ix; - req->data = newSVsv (fh_or_path); - req->dataptr = SvPVbyte_nolen (req->data); - } - else - { - req->type = REQ_FSTAT; - req->fh = newSVsv (fh_or_path); - req->fd = PerlIO_fileno (IoIFP (sv_2io (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: +{ + dREQ; + + req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; + req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; + req_set_fh_or_path (req, EIO_UTIME, EIO_FUTIME, fh_or_path); + + REQ_SEND; +} + +void +aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback = &PL_sv_undef) + PPCODE: +{ + dREQ; + + req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; + req_set_fh_or_path (req, EIO_TRUNCATE, EIO_FTRUNCATE, fh_or_path); REQ_SEND; } void -aio_unlink (pathname,callback=&PL_sv_undef) - SV * pathname - SV * callback +aio_chmod (SV8 *fh_or_path, int mode, SV *callback = &PL_sv_undef) + PPCODE: +{ + dREQ; + + req->int2 = mode; + req_set_fh_or_path (req, EIO_CHMOD, EIO_FCHMOD, fh_or_path); + + REQ_SEND; +} + +void +aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback = &PL_sv_undef) + PPCODE: +{ + dREQ; + + req->int2 = SvOK (uid) ? SvIV (uid) : -1; + req->int3 = SvOK (gid) ? SvIV (gid) : -1; + req_set_fh_or_path (req, EIO_CHOWN, EIO_FCHOWN, fh_or_path); + + REQ_SEND; +} + +void +aio_readdirx (SV8 *pathname, IV flags, SV *callback = &PL_sv_undef) + PPCODE: +{ + dREQ; + + req->type = EIO_READDIR; + req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1; + + if (flags & EIO_READDIR_DENTS) + req->int1 |= EIO_READDIR_CUSTOM2; + + req_set_path1 (req, pathname); + + REQ_SEND; +} + +void +aio_mkdir (SV8 *pathname, int mode, SV *callback = &PL_sv_undef) + PPCODE: +{ + dREQ; + + req->type = EIO_MKDIR; + req->int2 = mode; + req_set_path1 (req, pathname); + + REQ_SEND; +} + +void +aio_unlink (SV8 *pathname, SV *callback = &PL_sv_undef) ALIAS: - aio_unlink = REQ_UNLINK - aio_rmdir = REQ_RMDIR - aio_readdir = REQ_READDIR + aio_unlink = EIO_UNLINK + aio_rmdir = EIO_RMDIR + aio_readdir = EIO_READDIR PPCODE: { dREQ; req->type = ix; - req->data = newSVsv (pathname); - req->dataptr = SvPVbyte_nolen (req->data); - + req_set_path1 (req, pathname); + REQ_SEND; } void -aio_link (oldpath,newpath,callback=&PL_sv_undef) - SV * oldpath - SV * newpath - SV * callback +aio_link (SV8 *oldpath, SV8 *newpath, SV *callback = &PL_sv_undef) ALIAS: - aio_link = REQ_LINK - aio_symlink = REQ_SYMLINK - aio_rename = REQ_RENAME + aio_link = EIO_LINK + aio_symlink = EIO_SYMLINK + aio_rename = EIO_RENAME PPCODE: { + eio_wd wd2 = 0; dREQ; req->type = ix; - req->fh = newSVsv (oldpath); - req->data2ptr = SvPVbyte_nolen (req->fh); - req->data = newSVsv (newpath); - req->dataptr = SvPVbyte_nolen (req->data); + req_set_path1 (req, oldpath); + req_set_path (req, newpath, &req->sv2, &req->sv4, &wd2, &req->ptr2); + req->int3 = (long)wd2; REQ_SEND; } void -aio_sleep (delay,callback=&PL_sv_undef) - double delay - SV * callback +aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback = &PL_sv_undef) + PPCODE: +{ + dREQ; + + req->type = EIO_MKNOD; + 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_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback = &PL_sv_undef) + PPCODE: +{ + int fd = s_fileno_croak (fh, 0); + dREQ; + + req->type = EIO_CUSTOM; + req->sv1 = newSVsv (fh); + req->int1 = fd; + + req->feed = fiemap; +#if HAVE_FIEMAP + /* keep our fingers crossed that the next two types are 64 bit */ + req->offs = start; + req->size = SvOK (length) ? SvVAL64 (length) : ~0ULL; + req->int2 = flags; + req->int3 = SvOK (count) ? SvIV (count) : -1; +#endif + + REQ_SEND; +} + +void +aio_busy (double delay, SV *callback = &PL_sv_undef) PPCODE: { dREQ; - req->type = REQ_SLEEP; - req->fd = delay < 0. ? 0 : delay; - req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); + req->type = EIO_BUSY; + req->nv1 = delay < 0. ? 0. : delay; REQ_SEND; } void -aio_group (callback=&PL_sv_undef) - SV * callback - PROTOTYPE: ;$ +aio_group (SV *callback = &PL_sv_undef) PPCODE: { dREQ; - req->type = REQ_GROUP; - req_send (req); - XPUSHs (req_sv (req, AIO_GRP_KLASS)); + + req->type = EIO_GROUP; + + PUTBACK; + req_submit (req); + SPAGAIN; + + XPUSHs (req_sv (req, aio_grp_stash)); } void -aio_nop (callback=&PL_sv_undef) - SV * callback +aio_nop (SV *callback = &PL_sv_undef) + ALIAS: + aio_nop = EIO_NOP + aio_sync = EIO_SYNC PPCODE: { dREQ; - req->type = REQ_NOP; + req->type = ix; REQ_SEND; } +int +aioreq_pri (int pri = NO_INIT) + CODE: + RETVAL = next_pri; + if (items > 0) + { + if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN; + if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX; + next_pri = pri; + } + OUTPUT: + RETVAL + void -flush () - PROTOTYPE: +aioreq_nice (int nice = 0) CODE: - while (nreqs) - { - poll_wait (); - poll_cb (); - } + nice = next_pri - nice; + if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN; + if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX; + next_pri = nice; void -poll() - PROTOTYPE: +flush () CODE: - if (nreqs) + while (eio_nreqs ()) { poll_wait (); poll_cb (); } int -poll_fileno() - PROTOTYPE: +poll () + CODE: + poll_wait (); + RETVAL = poll_cb (); + OUTPUT: + RETVAL + +int +poll_fileno () CODE: - RETVAL = respipe [0]; + RETVAL = s_epipe_fd (&respipe); OUTPUT: RETVAL int -poll_cb(...) +poll_cb (...) PROTOTYPE: CODE: RETVAL = poll_cb (); @@ -1265,35 +1698,286 @@ RETVAL void -poll_wait() - PROTOTYPE: +poll_wait () CODE: - if (nreqs) - poll_wait (); + poll_wait (); int -nreqs() - PROTOTYPE: +nreqs () + CODE: + RETVAL = eio_nreqs (); + OUTPUT: + RETVAL + +int +nready () CODE: - RETVAL = nreqs; + RETVAL = eio_nready (); OUTPUT: RETVAL +int +npending () + CODE: + RETVAL = eio_npending (); + OUTPUT: + RETVAL + +int +nthreads () + CODE: + RETVAL = eio_nthreads (); + OUTPUT: + RETVAL + +int +fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) + CODE: + RETVAL = posix_fadvise (fh, offset, length, advice); + OUTPUT: + RETVAL + +IV +sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) + CODE: + RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); + OUTPUT: + RETVAL + +void +mmap (SV *scalar, STRLEN length, int prot, int flags, SV *fh = &PL_sv_undef, 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, STRLEN offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) + ALIAS: + mprotect = 1 + CODE: +{ + STRLEN svlen; + void *addr = SvPVbyte (scalar, svlen); + STRLEN 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, STRLEN 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 = EIO_ENOSYS (); +#endif +} + OUTPUT: + RETVAL + +int +munlockall () + CODE: +#if _POSIX_MEMLOCK + munlockall (); +#else + RETVAL = EIO_ENOSYS (); +#endif + OUTPUT: + RETVAL + +int +splice (aio_rfd rfh, SV *off_in, aio_wfd wfh, SV *off_out, size_t length, unsigned int flags) + CODE: +{ +#if HAVE_LINUX_SPLICE + loff_t off_in_, off_out_; + RETVAL = splice ( + rfh, SvOK (off_in ) ? (off_in_ = SvVAL64 (off_in )), &off_in_ : 0, + wfh, SvOK (off_out) ? (off_out_ = SvVAL64 (off_out)), &off_out_ : 0, + length, flags + ); +#else + RETVAL = EIO_ENOSYS (); +#endif +} + OUTPUT: + RETVAL + +int +tee (aio_rfd rfh, aio_wfd wfh, size_t length, unsigned int flags) + CODE: +#if HAVE_LINUX_SPLICE + RETVAL = tee (rfh, wfh, length, flags); +#else + RETVAL = EIO_ENOSYS (); +#endif + OUTPUT: + RETVAL + +int +pipesize (aio_rfd rfh, int new_size = -1) + PROTOTYPE: $;$ + CODE: +#if defined(F_SETPIPE_SZ) && defined(F_GETPIPE_SZ) + if (new_size >= 0) + RETVAL = fcntl (rfh, F_SETPIPE_SZ, new_size); + else + RETVAL = fcntl (rfh, F_GETPIPE_SZ); +#else + errno = ENOSYS; + RETVAL = -1; +#endif + OUTPUT: + RETVAL + +void +pipe2 (int flags = 0) + PROTOTYPE: ;$ + PPCODE: +{ + int fd[2]; + int res; + + if (flags) +#if HAVE_PIPE2 + res = pipe2 (fd, flags); +#else + res = (errno = ENOSYS, -1); +#endif + else + res = pipe (fd); + + if (!res) + { + EXTEND (SP, 2); + PUSHs (newmortalFH (fd[0], O_RDONLY)); + PUSHs (newmortalFH (fd[1], O_WRONLY)); + } +} + +void _on_next_submit (SV *cb) + CODE: + SvREFCNT_dec (on_next_submit); + on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; + 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 :/ */ + next_pri = req->pri; /* restore next_pri */ + req->pri = EIO_PRI_MAX; /* better use max. priority to conserve fds */ + 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 cancel (aio_req_ornot req) - PROTOTYPE: CODE: - req_cancel (req); + eio_cancel (req); void -cb (aio_req req, SV *callback=&PL_sv_undef) - CODE: - SvREFCNT_dec (req->callback); - req->callback = newSVsv (callback); +cb (aio_req_ornot req, SV *callback = NO_INIT) + PPCODE: +{ + if (GIMME_V != G_VOID) + XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); + + if (items > 1) + { + SV *cb_cv = get_cb (callback); + + SvREFCNT_dec (req->callback); + req->callback = SvREFCNT_inc (cb_cv); + } +} MODULE = IO::AIO PACKAGE = IO::AIO::GRP @@ -1302,64 +1986,69 @@ PPCODE: { int i; - aio_req req; - if (grp->fd == 2) + if (grp->int1 == 2) croak ("cannot add requests to IO::AIO::GRP after the group finished"); for (i = 1; i < items; ++i ) { + aio_req req; + if (GIMME_V != G_VOID) XPUSHs (sv_2mortal (newSVsv (ST (i)))); req = SvAIO_REQ (ST (i)); if (req) - { - ++grp->length; - req->grp = grp; - - req->grp_prev = 0; - req->grp_next = grp->grp_first; - - if (grp->grp_first) - grp->grp_first->grp_prev = req; - - grp->grp_first = req; - } + eio_grp_add (grp, req); } } void +cancel_subs (aio_req_ornot req) + CODE: + req_cancel_subs (req); + +void result (aio_req grp, ...) CODE: { int i; - AV *av = newAV (); + AV *av; + + grp->errorno = errno; + + av = newAV (); + av_extend (av, items - 1); for (i = 1; i < items; ++i ) av_push (av, newSVsv (ST (i))); - SvREFCNT_dec (grp->data); - grp->data = (SV *)av; + SvREFCNT_dec (grp->sv1); + grp->sv1 = (SV *)av; } void -feed_limit (aio_req grp, int limit) +errno (aio_req grp, int errorno = errno) + CODE: + grp->errorno = errorno; + +void +limit (aio_req grp, int limit) CODE: - grp->fd2 = limit; - aio_grp_feed (grp); + eio_grp_limit (grp, limit); void -feed (aio_req grp, SV *callback=&PL_sv_undef) +feed (aio_req grp, SV *callback = &PL_sv_undef) CODE: { - SvREFCNT_dec (grp->fh2); - grp->fh2 = newSVsv (callback); + SvREFCNT_dec (grp->sv2); + grp->sv2 = newSVsv (callback); + grp->feed = aio_grp_feed; - if (grp->fd2 <= 0) - grp->fd2 = 2; + if (grp->int2 <= 0) + grp->int2 = 2; - aio_grp_feed (grp); + eio_grp_limit (grp, grp->int2); }