--- IO-AIO/AIO.xs 2005/07/10 17:07:44 1.1 +++ IO-AIO/AIO.xs 2005/07/31 19:08:39 1.20 @@ -1,30 +1,30 @@ -#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 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)) +#if __ia64 +# define STACKSIZE 65536 #else -# define STACKSIZE ( 512 * sizeof (long)) +# define STACKSIZE 4096 #endif enum { @@ -36,7 +36,7 @@ }; typedef struct aio_cb { - struct aio_cb *next; + struct aio_cb *volatile next; int type; @@ -46,7 +46,7 @@ ssize_t result; mode_t mode; /* open */ int errorno; - SV *data, *callback; + SV *data, *callback, *fh; void *dataptr; STRLEN dataoffset; @@ -56,10 +56,125 @@ typedef aio_cb *aio_req; static int started; -static int nreqs; -static int reqpipe[2], respipe[2]; +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 +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; + aio_req req, prv; + + pthread_mutex_lock (&reslock); + + { + /* read any signals sent by the worker threads */ + char buf [32]; + while (read (respipe [0], buf, 32) > 0) + ; + } + + req = ress; + ress = rese = 0; + + pthread_mutex_unlock (&reslock); + + while (req) + { + 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->data) + SvREFCNT_dec (req->data); + + if (req->fh) + SvREFCNT_dec (req->fh); + + 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); + + Safefree (req->statdata); + } + + ENTER; + PUSHMARK (SP); + 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; -static aio_req qs, qe; /* queue start, queue end */ + fh = SvREFCNT_inc (POPs); + + PUSHMARK (SP); + XPUSHs (sv_2mortal (fh)); + } + + if (SvOK (req->callback)) + { + PUTBACK; + call_sv (req->callback, G_VOID | G_EVAL); + SPAGAIN; + } + + LEAVE; + + if (req->callback) + SvREFCNT_dec (req->callback); + + errno = errorno; + count++; + } + + prv = req; + req = req->next; + Safefree (prv); + + /* TODO: croak on errors? */ + } + + return count; +} static void *aio_proc(void *arg); @@ -84,31 +199,30 @@ } 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) { 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); - send_reqs (); + while (nreqs > max_outstanding) + { + poll_wait (); + poll_cb (); + } } static void @@ -121,140 +235,115 @@ send_req (req); } -static void -read_write (pTHX_ - int dowrite, int fd, off_t offset, size_t length, - SV *data, STRLEN dataoffset, SV *callback) -{ - aio_req req; - STRLEN svlen; - char *svptr = SvPV (data, svlen); +/* work around various missing functions */ - SvUPGRADE (data, SVt_PV); - SvPOK_on (data); +#if !HAVE_PREADWRITE +# define pread aio_pread +# define pwrite aio_pwrite - if (dataoffset < 0) - dataoffset += svlen; +/* + * 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; - if (dataoffset < 0 || dataoffset > svlen) - croak ("data offset outside of string"); - - if (dowrite) - { - /* 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); - } +static ssize_t +pread (int fd, void *buf, size_t count, off_t offset) +{ + ssize_t res; + off_t ooffset; - if (length < 0) - croak ("length must not be negative"); + 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); - Newz (0, req, 1, aio_cb); + return res; +} - if (!req) - croak ("out of memory during aio_req allocation"); +static ssize_t +pwrite (int fd, void *buf, size_t count, off_t offset) +{ + ssize_t res; + off_t ooffset; - 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); + 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); - send_req (req); + return res; } +#endif -static void -poll_wait () +#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) { - fd_set rfd; - FD_ZERO(&rfd); - FD_SET(respipe[0], &rfd); + while (count > 0) + { + size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); - select (respipe[0] + 1, &rfd, 0, 0, 0); + pread (fd, readahead_buf, len, offset); + offset += len; + count -= len; + } + + errno = 0; } +#endif -static int -poll_cb (pTHX) +static void * +aio_proc (void *thr_arg) { - dSP; - int count = 0; aio_req req; + int type; - while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) + do { - nreqs--; + pthread_mutex_lock (&reqlock); - if (req->type == REQ_QUIT) - started--; - else + for (;;) { - int errorno = errno; - errno = req->errorno; - - if (req->type == REQ_READ) - SvCUR_set (req->data, req->dataoffset - + req->result > 0 ? req->result : 0); + req = reqs; - if (req->data) - SvREFCNT_dec (req->data); - - if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) + if (reqs) { - PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; - PL_laststatval = req->result; - PL_statcache = *(req->statdata); - - Safefree (req->statdata); + reqs = reqs->next; + if (!reqs) reqe = 0; } - PUSHMARK (SP); - XPUSHs (sv_2mortal (newSViv (req->result))); - PUTBACK; - call_sv (req->callback, G_VOID); - SPAGAIN; - - if (req->callback) - SvREFCNT_dec (req->callback); + if (req) + break; - errno = errorno; - count++; + pthread_cond_wait (&reqwait, &reqlock); } - Safefree (req); - } - - if (qs) - send_reqs (); - - return count; -} - -static void * -aio_proc (void *thr_arg) -{ - aio_req req; - - /* then loop */ - while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) - { + 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_STAT: req->result = stat (req->dataptr, req->statdata); break; case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; @@ -264,12 +353,11 @@ case REQ_CLOSE: req->result = close (req->fd); break; case REQ_UNLINK: req->result = unlink (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: - write (respipe[1], (void *)&req, sizeof (req)); - return 0; + break; default: req->result = ENOSYS; @@ -277,23 +365,44 @@ } 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 +PROTOTYPES: ENABLE + BOOT: { - if (pipe (reqpipe) || pipe (respipe)) - croak ("unable to initialize request or result pipe"); + if (pipe (respipe)) + croak ("unable to initialize result pipe"); - if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK)) + if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) croak ("cannot set result pipe to nonblocking mode"); - if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) + if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) croak ("cannot set result pipe to nonblocking mode"); } @@ -321,17 +430,25 @@ while (started > nthreads) { poll_wait (); - poll_cb (aTHX); + poll_cb (); } } +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; @@ -352,10 +469,10 @@ } 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 @@ -370,43 +487,80 @@ croak ("out of memory during aio_req allocation"); req->type = ix; - req->fd = PerlIO_fileno (fh); + req->fh = newSVsv (fh); + req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); req->callback = SvREFCNT_inc (callback); 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 + IV length + SV * data + IV 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 = SvPV (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"); + + Newz (0, req, 1, aio_cb); + + if (!req) + croak ("out of memory during aio_req allocation"); + + 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->callback = SvREFCNT_inc (callback); + + send_req (req); +} void -aio_readahead(fh,offset,length,callback) - InputStream fh - UV offset - IV length - SV * callback - PROTOTYPE: $$$$ +aio_readahead(fh,offset,length,callback=&PL_sv_undef) + SV * fh + UV offset + IV length + SV * callback + PROTOTYPE: $$$;$ CODE: { aio_req req; @@ -420,7 +574,8 @@ croak ("out of memory during aio_req allocation"); 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); @@ -429,12 +584,12 @@ } 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; @@ -451,13 +606,14 @@ 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); } else { req->type = REQ_FSTAT; + req->fh = newSVsv (fh_or_path); req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); } @@ -467,10 +623,9 @@ } void -aio_unlink(pathname,callback) +aio_unlink(pathname,callback=&PL_sv_undef) SV * pathname SV * callback - PROTOTYPE: $$ CODE: { aio_req req; @@ -488,11 +643,31 @@ 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 @@ -500,7 +675,7 @@ poll_cb(...) PROTOTYPE: CODE: - RETVAL = poll_cb (aTHX); + RETVAL = poll_cb (); OUTPUT: RETVAL @@ -508,7 +683,8 @@ poll_wait() PROTOTYPE: CODE: - poll_wait (); + if (nreqs) + poll_wait (); int nreqs()