--- IO-AIO/AIO.xs 2005/07/10 18:16:49 1.2 +++ IO-AIO/AIO.xs 2006/06/24 16:27:02 1.40 @@ -1,53 +1,76 @@ -#define PERL_NO_GET_CONTEXT +#define _REENTRANT 1 +#include #include "EXTERN.h" #include "perl.h" #include "XSUB.h" +#include "autoconf/config.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 +#endif -typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ -typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ -typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ - -#if __i386 || __amd64 -# define STACKSIZE ( 256 * sizeof (long)) -#elif __ia64 -# define STACKSIZE (8192 * sizeof (long)) +/* used for struct dirent, AIX doesn't provide it */ +#ifndef NAME_MAX +# define NAME_MAX 4096 +#endif + +#if __ia64 +# define STACKSIZE 65536 #else -# define STACKSIZE ( 512 * sizeof (long)) +# define STACKSIZE 8192 #endif enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_READAHEAD, - REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, + REQ_SENDFILE, + REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_FSYNC, REQ_FDATASYNC, + REQ_UNLINK, REQ_RMDIR, REQ_RENAME, + REQ_READDIR, + REQ_LINK, REQ_SYMLINK, }; typedef struct aio_cb { - struct aio_cb *next; + struct aio_cb *volatile next; int type; - int fd; + /* should receive a cleanup, with unions */ + int fd, fd2; off_t offset; size_t length; ssize_t result; mode_t mode; /* open */ int errorno; SV *data, *callback; - void *dataptr; + SV *fh, *fh2; + void *dataptr, *data2ptr; STRLEN dataoffset; Stat_t *statdata; @@ -55,11 +78,179 @@ typedef aio_cb *aio_req; -static int started; -static int nreqs; -static int reqpipe[2], respipe[2]; +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; + +static volatile aio_req reqs, reqe; /* queue start, queue end */ +static volatile aio_req ress, rese; /* queue start, queue end */ + +static void free_req (aio_req req) +{ + if (req->data) + SvREFCNT_dec (req->data); + + if (req->fh) + SvREFCNT_dec (req->fh); + + if (req->fh2) + SvREFCNT_dec (req->fh2); + + if (req->statdata) + Safefree (req->statdata); + + if (req->callback) + SvREFCNT_dec (req->callback); + + if (req->type == REQ_READDIR && req->result >= 0) + free (req->data2ptr); + + Safefree (req); +} + +static void +poll_wait () +{ + if (nreqs && !ress) + { + fd_set rfd; + FD_ZERO(&rfd); + FD_SET(respipe [0], &rfd); + + select (respipe [0] + 1, &rfd, 0, 0, 0); + } +} + +static int +poll_cb () +{ + dSP; + int count = 0; + int do_croak = 0; + aio_req req; + + for (;;) + { + pthread_mutex_lock (&reslock); + req = ress; + + if (req) + { + ress = req->next; + + if (!ress) + { + /* read any signals sent by the worker threads */ + char buf [32]; + while (read (respipe [0], buf, 32) == 32) + ; + + rese = 0; + } + } + + pthread_mutex_unlock (&reslock); + + if (!req) + break; + + nreqs--; + + if (req->type == REQ_QUIT) + started--; + else + { + int errorno = errno; + errno = req->errorno; + + if (req->type == REQ_READ) + SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); + + if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) + SvREADONLY_off (req->data); + + if (req->statdata) + { + PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; + PL_laststatval = req->result; + PL_statcache = *(req->statdata); + } + + ENTER; + PUSHMARK (SP); + + if (req->type == REQ_READDIR) + { + SV *rv = &PL_sv_undef; + + if (req->result >= 0) + { + char *buf = req->data2ptr; + AV *av = newAV (); + + while (req->result) + { + SV *sv = newSVpv (buf, 0); + + av_push (av, sv); + buf += SvCUR (sv) + 1; + req->result--; + } -static aio_req qs, qe; /* queue start, queue end */ + rv = sv_2mortal (newRV_noinc ((SV *)av)); + } + + XPUSHs (rv); + } + else + { + XPUSHs (sv_2mortal (newSViv (req->result))); + + if (req->type == REQ_OPEN) + { + /* convert fd to fh */ + SV *fh; + + PUTBACK; + call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); + SPAGAIN; + + fh = SvREFCNT_inc (POPs); + + PUSHMARK (SP); + XPUSHs (sv_2mortal (fh)); + } + } + + if (SvOK (req->callback)) + { + PUTBACK; + call_sv (req->callback, G_VOID | G_EVAL); + SPAGAIN; + + if (SvTRUE (ERRSV)) + { + free_req (req); + croak (0); + } + } + + LEAVE; + + errno = errorno; + count++; + } + + free_req (req); + } + + return count; +} static void *aio_proc(void *arg); @@ -84,193 +275,389 @@ } static void -send_reqs (void) -{ - /* this write is atomic */ - while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs) - { - qs = qs->next; - if (!qs) qe = 0; - } -} - -static void send_req (aio_req req) { + while (started < wanted && nreqs >= started) + start_thread (); + nreqs++; + + pthread_mutex_lock (&reqlock); + req->next = 0; - if (qe) + if (reqe) { - qe->next = req; - qe = req; + reqe->next = req; + reqe = req; } else - qe = qs = req; + reqe = reqs = req; + + pthread_cond_signal (&reqwait); + pthread_mutex_unlock (&reqlock); + + if (nreqs > max_outstanding) + for (;;) + { + poll_cb (); - send_reqs (); + if (nreqs <= max_outstanding) + break; + + poll_wait (); + } } static void end_thread (void) { aio_req req; - New (0, req, 1, aio_cb); + Newz (0, req, 1, aio_cb); req->type = REQ_QUIT; send_req (req); } -static void -read_write (pTHX_ - int dowrite, int fd, off_t offset, size_t length, - SV *data, STRLEN dataoffset, SV *callback) +static void min_parallel (int nthreads) { - aio_req req; - STRLEN svlen; - char *svptr = SvPV (data, svlen); - - SvUPGRADE (data, SVt_PV); - SvPOK_on (data); + if (wanted < nthreads) + wanted = nthreads; +} - if (dataoffset < 0) - dataoffset += svlen; +static void max_parallel (int nthreads) +{ + int cur = started; - if (dataoffset < 0 || dataoffset > svlen) - croak ("data offset outside of string"); + if (wanted > nthreads) + wanted = nthreads; - if (dowrite) + while (cur > wanted) { - /* write: check length and adjust. */ - if (length < 0 || length + dataoffset > svlen) - length = svlen - dataoffset; + end_thread (); + cur--; } - else + + while (started > wanted) { - /* read: grow scalar as necessary */ - svptr = SvGROW (data, length + dataoffset); + poll_wait (); + poll_cb (); } +} - if (length < 0) - croak ("length must not be negative"); +static void create_pipe () +{ + if (pipe (respipe)) + croak ("unable to initialize result pipe"); - Newz (0, req, 1, aio_cb); + if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) + croak ("cannot set result pipe to nonblocking mode"); - if (!req) - croak ("out of memory during aio_req allocation"); + if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) + croak ("cannot set result pipe to nonblocking mode"); +} - req->type = dowrite ? REQ_WRITE : REQ_READ; - req->fd = fd; - req->offset = offset; - req->length = length; - req->data = SvREFCNT_inc (data); - req->dataptr = (char *)svptr + dataoffset; - req->callback = SvREFCNT_inc (callback); +/*****************************************************************************/ +/* work around various missing functions */ - send_req (req); +#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) +{ + ssize_t res; + off_t ooffset; + + 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); + + return res; } -static void -poll_wait () +static ssize_t +pwrite (int fd, void *buf, size_t count, off_t offset) { - fd_set rfd; - FD_ZERO(&rfd); - FD_SET(respipe[0], &rfd); + 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; +} +#endif + +#if !HAVE_FDATASYNC +# define fdatasync fsync +#endif + +#if !HAVE_READAHEAD +# define readahead aio_readahead + +static ssize_t +readahead (int fd, off_t offset, size_t count) +{ + char readahead_buf[4096]; + + while (count > 0) + { + size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); + + pread (fd, readahead_buf, len, offset); + offset += len; + count -= len; + } - select (respipe[0] + 1, &rfd, 0, 0, 0); + errno = 0; } +#endif + +#if !HAVE_READDIR_R +# define readdir_r aio_readdir_r +static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; + static int -poll_cb (pTHX) +readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) { - dSP; - int count = 0; - aio_req req; + struct dirent *e; + int errorno; - while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) + pthread_mutex_lock (&readdirlock); + + e = readdir (dirp); + errorno = errno; + + if (e) { - nreqs--; + *res = ent; + strcpy (ent->d_name, e->d_name); + } + else + *res = 0; - if (req->type == REQ_QUIT) - started--; - else - { - int errorno = errno; - errno = req->errorno; + pthread_mutex_unlock (&readdirlock); - if (req->type == REQ_READ) - SvCUR_set (req->data, req->dataoffset - + req->result > 0 ? req->result : 0); + errno = errorno; + return e ? 0 : -1; +} +#endif - if (req->data) - SvREFCNT_dec (req->data); +/* sendfile always needs emulation */ +static ssize_t +sendfile_ (int ofd, int ifd, off_t offset, size_t count) +{ + ssize_t res; - if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) - { - PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; - PL_laststatval = req->result; - PL_statcache = *(req->statdata); + if (!count) + return 0; - Safefree (req->statdata); +#if HAVE_SENDFILE +# if __linux + res = sendfile (ofd, ifd, &offset, count); + +# 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; +#endif + + if (res < 0 + && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK +#if __solaris + || errno == EAFNOSUPPORT || errno == EPROTOTYPE +#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); + + if (cnt <= 0) + { + if (cnt && !res) res = -1; + break; } - PUSHMARK (SP); - XPUSHs (sv_2mortal (newSViv (req->result))); + cnt = write (ofd, buf, cnt); - if (req->type == REQ_OPEN) + if (cnt <= 0) { - /* convert fd to fh */ - SV *fh; + if (cnt && !res) res = -1; + break; + } - PUTBACK; - call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); - SPAGAIN; + offset += cnt; + res += cnt; + count -= cnt; + } + } + + return res; +} - fh = POPs; +/* read a full directory */ +static int +scandir_ (const char *path, void **namesp) +{ + 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; - PUSHMARK (SP); - XPUSHs (fh); - } + if (!dirp) + return -1; - PUTBACK; - call_sv (req->callback, G_VOID | G_EVAL); - SPAGAIN; - - if (req->callback) - SvREFCNT_dec (req->callback); + names = malloc (memlen); - errno = errorno; - count++; - } + for (;;) + { + errno = 0, readdir_r (dirp, &u.d, &entp); + + if (!entp) + break; - Safefree (req); + name = entp->d_name; + + if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) + { + int len = strlen (name) + 1; + + res++; + + while (memofs + len > memlen) + { + memlen *= 2; + names = realloc (names, memlen); + if (!names) + break; + } + + memcpy (names + memofs, name, len); + memofs += len; + } } - if (qs) - send_reqs (); + errorno = errno; + closedir (dirp); - return count; + if (errorno) + { + free (names); + errno = errorno; + res = -1; + } + + *namesp = (void *)names; + return res; } +/*****************************************************************************/ + static void * aio_proc (void *thr_arg) { aio_req req; + int type; - /* then loop */ - while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) + do { + pthread_mutex_lock (&reqlock); + + for (;;) + { + req = reqs; + + if (reqs) + { + reqs = reqs->next; + if (!reqs) reqe = 0; + } + + if (req) + break; + + pthread_cond_wait (&reqwait, &reqlock); + } + + pthread_mutex_unlock (&reqlock); + errno = 0; /* strictly unnecessary */ - switch (req->type) + type = req->type; + + switch (type) { - case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; - case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; -#if SYS_readahead + 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; + case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; -#else - case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; -#endif + case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; @@ -279,13 +666,17 @@ 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_FSYNC: req->result = fsync (req->fd); 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; case REQ_QUIT: - write (respipe[1], (void *)&req, sizeof (req)); - return 0; + break; default: req->result = ENOSYS; @@ -293,222 +684,375 @@ } req->errorno = errno; - write (respipe[1], (void *)&req, sizeof (req)); + + pthread_mutex_lock (&reslock); + + req->next = 0; + + if (rese) + { + rese->next = req; + rese = req; + } + else + { + rese = ress = req; + + /* write a dummy byte to the pipe so fh becomes ready */ + write (respipe [1], &respipe, 1); + } + + pthread_mutex_unlock (&reslock); } + while (type != REQ_QUIT); return 0; } -MODULE = IO::AIO PACKAGE = IO::AIO +/*****************************************************************************/ -BOOT: +static void atfork_prepare (void) { - if (pipe (reqpipe) || pipe (respipe)) - croak ("unable to initialize request or result pipe"); + 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 +} - if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK)) - croak ("cannot set result pipe to nonblocking mode"); +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); +} + +static void atfork_child (void) +{ + aio_req prv; - if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) - croak ("cannot set result pipe to nonblocking mode"); + started = 0; + + while (reqs) + { + prv = reqs; + reqs = prv->next; + free_req (prv); + } + + reqs = reqe = 0; + + while (ress) + { + prv = ress; + ress = prv->next; + free_req (prv); + } + + ress = rese = 0; + + close (respipe [0]); + close (respipe [1]); + create_pipe (); + + atfork_parent (); +} + +#define dREQ \ + aio_req req; \ + \ + if (SvOK (callback) && !SvROK (callback)) \ + croak ("clalback 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); + +MODULE = IO::AIO PACKAGE = IO::AIO + +PROTOTYPES: ENABLE + +BOOT: +{ + create_pipe (); + pthread_atfork (atfork_prepare, atfork_parent, atfork_child); } void -min_parallel(nthreads) +min_parallel (nthreads) int nthreads PROTOTYPE: $ - CODE: - while (nthreads > started) - start_thread (); void -max_parallel(nthreads) +max_parallel (nthreads) int nthreads PROTOTYPE: $ - CODE: -{ - int cur = started; - while (cur > nthreads) - { - end_thread (); - cur--; - } - while (started > nthreads) - { - poll_wait (); - poll_cb (aTHX); - } -} +int +max_outstanding (nreqs) + int nreqs + PROTOTYPE: $ + CODE: + RETVAL = max_outstanding; + max_outstanding = nreqs; void -aio_open(pathname,flags,mode,callback) +aio_open (pathname,flags,mode,callback=&PL_sv_undef) SV * pathname int flags int mode SV * callback - PROTOTYPE: $$$$ + PROTOTYPE: $$$;$ CODE: { - aio_req req; - - Newz (0, req, 1, aio_cb); - - if (!req) - croak ("out of memory during aio_req allocation"); + dREQ; req->type = REQ_OPEN; req->data = newSVsv (pathname); - req->dataptr = SvPV_nolen (req->data); + req->dataptr = SvPVbyte_nolen (req->data); req->fd = flags; req->mode = mode; - req->callback = SvREFCNT_inc (callback); send_req (req); } void -aio_close(fh,callback) - InputStream fh - SV * callback - PROTOTYPE: $$ +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 CODE: { - aio_req req; - - Newz (0, req, 1, aio_cb); - - if (!req) - croak ("out of memory during aio_req allocation"); + dREQ; req->type = ix; - req->fd = PerlIO_fileno (fh); - req->callback = SvREFCNT_inc (callback); + req->fh = newSVsv (fh); + req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); send_req (req); } void -aio_read(fh,offset,length,data,dataoffset,callback) - InputStream fh - UV offset - IV length - SV * data - IV dataoffset - SV * callback - PROTOTYPE: $$$$$$ +aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) + SV * fh + UV offset + UV length + SV * data + UV dataoffset + SV * callback + ALIAS: + aio_read = REQ_READ + aio_write = REQ_WRITE + PROTOTYPE: $$$$$;$ CODE: - read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); +{ + aio_req req; + STRLEN svlen; + char *svptr = SvPVbyte (data, svlen); -void -aio_write(fh,offset,length,data,dataoffset,callback) - OutputStream fh - UV offset - IV length - SV * data - IV dataoffset - SV * callback - PROTOTYPE: $$$$$$ - CODE: - read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); + SvUPGRADE (data, SVt_PV); + SvPOK_on (data); + + if (dataoffset < 0) + dataoffset += svlen; + + if (dataoffset < 0 || dataoffset > svlen) + croak ("data offset outside of string"); + + if (ix == REQ_WRITE) + { + /* write: check length and adjust. */ + if (length < 0 || length + dataoffset > svlen) + length = svlen - dataoffset; + } + else + { + /* read: grow scalar as necessary */ + svptr = SvGROW (data, length + dataoffset); + } + + 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; + + if (!SvREADONLY (data)) + { + SvREADONLY_on (data); + req->data2ptr = (void *)data; + } + + send_req (req); + } +} void -aio_readahead(fh,offset,length,callback) - InputStream fh - UV offset - IV length - SV * callback - PROTOTYPE: $$$$ +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: $$$$;$ CODE: { - aio_req req; + dREQ; - if (length < 0) - croak ("length must not be negative"); + 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; - Newz (0, req, 1, aio_cb); + send_req (req); +} - if (!req) - croak ("out of memory during aio_req allocation"); +void +aio_readahead (fh,offset,length,callback=&PL_sv_undef) + SV * fh + UV offset + IV length + SV * callback + PROTOTYPE: $$$;$ + CODE: +{ + dREQ; req->type = REQ_READAHEAD; - req->fd = PerlIO_fileno (fh); + req->fh = newSVsv (fh); + req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); req->offset = offset; req->length = length; - req->callback = SvREFCNT_inc (callback); send_req (req); } void -aio_stat(fh_or_path,callback) +aio_stat (fh_or_path,callback=&PL_sv_undef) SV * fh_or_path SV * callback - PROTOTYPE: $$ ALIAS: - aio_lstat = 1 + aio_stat = REQ_STAT + aio_lstat = REQ_LSTAT CODE: { - aio_req req; - - Newz (0, req, 1, aio_cb); - - if (!req) - croak ("out of memory during aio_req allocation"); + dREQ; New (0, req->statdata, 1, Stat_t); - if (!req->statdata) - croak ("out of memory during aio_req->statdata allocation"); + { + free_req (req); + croak ("out of memory during aio_req->statdata allocation"); + } if (SvPOK (fh_or_path)) { - req->type = ix ? REQ_LSTAT : REQ_STAT; + req->type = ix; req->data = newSVsv (fh_or_path); - req->dataptr = SvPV_nolen (req->data); + 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->callback = SvREFCNT_inc (callback); - send_req (req); } void -aio_unlink(pathname,callback) +aio_unlink (pathname,callback=&PL_sv_undef) SV * pathname SV * callback - PROTOTYPE: $$ + ALIAS: + aio_unlink = REQ_UNLINK + aio_rmdir = REQ_RMDIR + aio_readdir = REQ_READDIR CODE: { - aio_req req; + dREQ; - Newz (0, req, 1, aio_cb); + req->type = ix; + req->data = newSVsv (pathname); + req->dataptr = SvPVbyte_nolen (req->data); - if (!req) - croak ("out of memory during aio_req allocation"); + send_req (req); +} + +void +aio_link (oldpath,newpath,callback=&PL_sv_undef) + SV * oldpath + SV * newpath + SV * callback + ALIAS: + aio_link = REQ_LINK + aio_symlink = REQ_SYMLINK + aio_rename = REQ_RENAME + CODE: +{ + dREQ; - req->type = REQ_UNLINK; - req->data = newSVsv (pathname); - req->dataptr = SvPV_nolen (req->data); - req->callback = SvREFCNT_inc (callback); + req->type = ix; + req->fh = newSVsv (oldpath); + req->data2ptr = SvPVbyte_nolen (req->fh); + req->data = newSVsv (newpath); + req->dataptr = SvPVbyte_nolen (req->data); send_req (req); } +void +flush () + PROTOTYPE: + CODE: + while (nreqs) + { + poll_wait (); + poll_cb (); + } + +void +poll() + PROTOTYPE: + CODE: + if (nreqs) + { + poll_wait (); + poll_cb (); + } + int poll_fileno() PROTOTYPE: CODE: - RETVAL = respipe[0]; + RETVAL = respipe [0]; OUTPUT: RETVAL @@ -516,7 +1060,7 @@ poll_cb(...) PROTOTYPE: CODE: - RETVAL = poll_cb (aTHX); + RETVAL = poll_cb (); OUTPUT: RETVAL @@ -524,7 +1068,8 @@ poll_wait() PROTOTYPE: CODE: - poll_wait (); + if (nreqs) + poll_wait (); int nreqs()