--- IO-AIO/AIO.xs 2005/07/13 00:13:09 1.10 +++ IO-AIO/AIO.xs 2005/08/17 04:47:02 1.27 @@ -1,8 +1,11 @@ +#define _REENTRANT 1 +#include + #include "EXTERN.h" #include "perl.h" #include "XSUB.h" -#define _XOPEN_SOURCE 500 +#include "autoconf/config.h" #include #include @@ -11,9 +14,6 @@ #include #include #include -#if __linux -#include -#endif #include @@ -31,8 +31,10 @@ REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_READAHEAD, - REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, + REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_FSYNC, REQ_FDATASYNC, + REQ_UNLINK, REQ_RMDIR, + REQ_SYMLINK, }; typedef struct aio_cb { @@ -46,8 +48,8 @@ ssize_t result; mode_t mode; /* open */ int errorno; - SV *data, *callback; - void *dataptr; + SV *data, *callback, *fh; + void *dataptr, *data2ptr; STRLEN dataoffset; Stat_t *statdata; @@ -56,7 +58,7 @@ typedef aio_cb *aio_req; static int started; -static int nreqs; +static volatile int nreqs; static int max_outstanding = 1<<30; static int respipe [2]; @@ -67,17 +69,34 @@ 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->statdata) + Safefree (req->statdata); + + if (req->callback) + SvREFCNT_dec (req->callback); + + Safefree (req); +} + static void poll_wait () { - if (!nreqs) - return; - - fd_set rfd; - FD_ZERO(&rfd); - FD_SET(respipe [0], &rfd); + if (nreqs && !ress) + { + fd_set rfd; + FD_ZERO(&rfd); + FD_SET(respipe [0], &rfd); - select (respipe [0] + 1, &rfd, 0, 0, 0); + select (respipe [0] + 1, &rfd, 0, 0, 0); + } } static int @@ -85,25 +104,27 @@ { dSP; int count = 0; + int do_croak = 0; aio_req req; - - { - /* read and signals sent by the worker threads */ - char buf [32]; - while (read (respipe [0], buf, 32) > 0) - ; - } for (;;) { pthread_mutex_lock (&reslock); - req = ress; - if (ress) + if (req) { - ress = ress->next; - if (!ress) rese = 0; + ress = req->next; + + if (!ress) + { + rese = 0; + + /* read any signals sent by the worker threads */ + char buf [32]; + while (read (respipe [0], buf, 32) == 32) + ; + } } pthread_mutex_unlock (&reslock); @@ -121,21 +142,16 @@ errno = req->errorno; if (req->type == REQ_READ) - SvCUR_set (req->data, req->dataoffset - + req->result > 0 ? req->result : 0); - - if (req->data) - SvREFCNT_dec (req->data); + SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); - if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) + if (req->statdata) { PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; PL_laststatval = req->result; PL_statcache = *(req->statdata); - - Safefree (req->statdata); } + ENTER; PUSHMARK (SP); XPUSHs (sv_2mortal (newSViv (req->result))); @@ -148,10 +164,10 @@ call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); SPAGAIN; - fh = POPs; + fh = SvREFCNT_inc (POPs); PUSHMARK (SP); - XPUSHs (fh); + XPUSHs (sv_2mortal (fh)); } if (SvOK (req->callback)) @@ -159,16 +175,21 @@ PUTBACK; call_sv (req->callback, G_VOID | G_EVAL); SPAGAIN; + + if (SvTRUE (ERRSV)) + { + free_req (req); + croak (0); + } } - - if (req->callback) - SvREFCNT_dec (req->callback); + + LEAVE; errno = errorno; count++; } - Safefree (req); + free_req (req); } return count; @@ -216,70 +237,179 @@ pthread_cond_signal (&reqwait); pthread_mutex_unlock (&reqlock); - while (nreqs > max_outstanding) - { - poll_wait (); - poll_cb (); - } + if (nreqs > max_outstanding) + for (;;) + { + poll_cb (); + + 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 (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 (dataoffset < 0) - dataoffset += svlen; + while (nthreads > started) + start_thread (); +} - if (dataoffset < 0 || dataoffset > svlen) - croak ("data offset outside of string"); +static void max_parallel (int nthreads) +{ + int cur = started; - if (dowrite) - { - /* write: check length and adjust. */ - if (length < 0 || length + dataoffset > svlen) - length = svlen - dataoffset; + while (cur > nthreads) + { + end_thread (); + cur--; } - else + + while (started > nthreads) { - /* 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 (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) + croak ("cannot set result pipe to nonblocking mode"); +} - if (!req) - croak ("out of memory during aio_req allocation"); +static void atfork_prepare (void) +{ + for (;;) { - 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); + for (;;) + { + poll_cb (); - send_req (req); + if (!nreqs) + break; + + poll_wait (); + } + + pthread_mutex_lock (&reqlock); + + if (!nreqs) + break; + + pthread_mutex_unlock (&reqlock); + } + + pthread_mutex_lock (&reslock); + + assert (!nreqs && !reqs && !ress); +} + +static void atfork_parent (void) +{ + pthread_mutex_unlock (&reslock); + pthread_mutex_unlock (&reqlock); +} + +static void atfork_child (void) +{ + int restart = started; + started = 0; + + atfork_parent (); + + min_parallel (restart); +} + +/*****************************************************************************/ +/* 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 iolock = 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 (&iolock); + ooffset = lseek (fd, 0, SEEK_CUR); + lseek (fd, offset, SEEK_SET); + res = read (fd, buf, count); + lseek (fd, ooffset, SEEK_SET); + pthread_mutex_unlock (&iolock); + + return res; +} + +static ssize_t +pwrite (int fd, void *buf, size_t count, off_t offset) +{ + ssize_t res; + off_t ooffset; + + pthread_mutex_lock (&iolock); + ooffset = lseek (fd, 0, SEEK_CUR); + lseek (fd, offset, SEEK_SET); + res = write (fd, buf, count); + lseek (fd, offset, SEEK_SET); + pthread_mutex_unlock (&iolock); + + return res; +} +#endif + +#if !HAVE_FDATASYNC +# define fdatasync fsync +#endif + +#if !HAVE_READAHEAD +# define readahead aio_readahead + +static char readahead_buf[4096]; + +static ssize_t +readahead (int fd, off_t offset, size_t count) +{ + while (count > 0) + { + size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); + + pread (fd, readahead_buf, len, offset); + offset += len; + count -= len; + } + + errno = 0; } +#endif + +/*****************************************************************************/ static void * aio_proc (void *thr_arg) @@ -317,11 +447,8 @@ { 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; -#if SYS_readahead + 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_STAT: req->result = stat (req->dataptr, req->statdata); break; case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; @@ -330,9 +457,11 @@ 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_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_QUIT: break; @@ -368,49 +497,37 @@ return 0; } +#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: { - if (pipe (respipe)) - croak ("unable to initialize result pipe"); - - if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) - croak ("cannot set result pipe to nonblocking mode"); - - if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) - croak ("cannot set result pipe to nonblocking mode"); + create_pipe (); + pthread_atfork (atfork_prepare, atfork_parent, atfork_child); } void min_parallel(nthreads) int nthreads PROTOTYPE: $ - CODE: - while (nthreads > started) - start_thread (); void max_parallel(nthreads) int nthreads PROTOTYPE: $ - CODE: -{ - int cur = started; - while (cur > nthreads) - { - end_thread (); - cur--; - } - - while (started > nthreads) - { - poll_wait (); - poll_cb (); - } -} int max_outstanding(nreqs) @@ -429,27 +546,21 @@ 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=&PL_sv_undef) - InputStream fh - SV * callback + SV * fh + SV * callback PROTOTYPE: $;$ ALIAS: aio_close = REQ_CLOSE @@ -457,68 +568,89 @@ 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=&PL_sv_undef) - InputStream fh - UV offset - IV length - SV * data - IV dataoffset - SV * callback - PROTOTYPE: $$$$$;$ - CODE: - read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); - -void -aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) - OutputStream fh - UV offset - IV length - SV * data - IV dataoffset - SV * callback + SV * fh + UV offset + IV length + SV * data + IV dataoffset + SV * callback + ALIAS: + aio_read = REQ_READ + aio_write = REQ_WRITE PROTOTYPE: $$$$$;$ CODE: - read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); - -void -aio_readahead(fh,offset,length,callback=&PL_sv_undef) - InputStream fh - UV offset - IV length - SV * callback - PROTOTYPE: $$$;$ - CODE: { aio_req req; + STRLEN svlen; + char *svptr = SvPVbyte (data, svlen); + + 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"); - Newz (0, req, 1, aio_cb); + { + 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 (!req) - croak ("out of memory during aio_req allocation"); + send_req (req); + } +} + +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); } @@ -532,32 +664,28 @@ 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->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); } @@ -565,19 +693,34 @@ aio_unlink(pathname,callback=&PL_sv_undef) SV * pathname SV * callback + ALIAS: + aio_unlink = REQ_UNLINK + aio_rmdir = REQ_RMDIR 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_symlink(oldpath,newpath,callback=&PL_sv_undef) + SV * oldpath + SV * newpath + SV * callback + CODE: +{ + dREQ; - req->type = REQ_UNLINK; - req->data = newSVsv (pathname); - req->dataptr = SvPV_nolen (req->data); - req->callback = SvREFCNT_inc (callback); + req->type = REQ_SYMLINK; + req->fh = newSVsv (oldpath); + req->data2ptr = SvPVbyte_nolen (req->fh); + req->data = newSVsv (newpath); + req->dataptr = SvPVbyte_nolen (req->data); send_req (req); }