--- IO-AIO/AIO.xs 2005/07/10 20:07:11 1.3 +++ IO-AIO/AIO.xs 2005/07/22 08:25:22 1.13 @@ -1,4 +1,4 @@ -#define PERL_NO_GET_CONTEXT +#define _XOPEN_SOURCE 500 #include "EXTERN.h" #include "perl.h" @@ -6,25 +6,25 @@ #include #include + #include #include #include #include -#include +#if __linux +#include +#endif #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 { @@ -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,7 +56,8 @@ 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]; static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; @@ -66,152 +67,42 @@ static volatile aio_req reqs, reqe; /* queue start, queue end */ static volatile aio_req ress, rese; /* queue start, queue end */ -static void *aio_proc(void *arg); - -static void -start_thread (void) -{ - sigset_t fullsigset, oldsigset; - pthread_t tid; - pthread_attr_t attr; - - pthread_attr_init (&attr); - pthread_attr_setstacksize (&attr, STACKSIZE); - pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); - - sigfillset (&fullsigset); - sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); - - if (pthread_create (&tid, &attr, aio_proc, 0) == 0) - started++; - - sigprocmask (SIG_SETMASK, &oldsigset, 0); -} - static void -send_req (aio_req req) +poll_wait () { - nreqs++; - - pthread_mutex_lock (&reqlock); - - req->next = 0; - - if (reqe) + if (nreqs && !ress) { - reqe->next = req; - reqe = req; - } - else - reqe = reqs = req; - - pthread_cond_signal (&reqwait); - pthread_mutex_unlock (&reqlock); -} - -static void -end_thread (void) -{ - aio_req req; - New (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) -{ - aio_req req; - STRLEN svlen; - char *svptr = SvPV (data, svlen); - - SvUPGRADE (data, SVt_PV); - SvPOK_on (data); + fd_set rfd; + FD_ZERO(&rfd); + FD_SET(respipe [0], &rfd); - if (dataoffset < 0) - dataoffset += svlen; - - 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); + select (respipe [0] + 1, &rfd, 0, 0, 0); } - - 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 = 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); - - send_req (req); -} - -static void -poll_wait () -{ - if (!nreqs) - return; - - fd_set rfd; - FD_ZERO(&rfd); - FD_SET(respipe [0], &rfd); - - select (respipe [0] + 1, &rfd, 0, 0, 0); } static int -poll_cb (pTHX) +poll_cb () { dSP; int count = 0; - aio_req req; - + aio_req req, prv; + + pthread_mutex_lock (&reslock); + { - /* read and signals sent by the worker threads */ + /* read any signals sent by the worker threads */ char buf [32]; while (read (respipe [0], buf, 32) > 0) ; } - for (;;) - { - pthread_mutex_lock (&reslock); + req = ress; + ress = rese = 0; - req = ress; - - if (ress) - { - ress = ress->next; - if (!ress) rese = 0; - } - - pthread_mutex_unlock (&reslock); - - if (!req) - break; + pthread_mutex_unlock (&reslock); + while (req) + { nreqs--; if (req->type == REQ_QUIT) @@ -228,6 +119,9 @@ 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; @@ -237,6 +131,7 @@ Safefree (req->statdata); } + ENTER; PUSHMARK (SP); XPUSHs (sv_2mortal (newSViv (req->result))); @@ -249,15 +144,20 @@ call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); SPAGAIN; - fh = POPs; + fh = SvREFCNT_inc (POPs); PUSHMARK (SP); - XPUSHs (fh); + XPUSHs (sv_2mortal (fh)); } - PUTBACK; - call_sv (req->callback, G_VOID | G_EVAL); - SPAGAIN; + if (SvOK (req->callback)) + { + PUTBACK; + call_sv (req->callback, G_VOID | G_EVAL); + SPAGAIN; + } + + LEAVE; if (req->callback) SvREFCNT_dec (req->callback); @@ -266,12 +166,75 @@ count++; } - Safefree (req); + prv = req; + req = req->next; + Safefree (prv); + + /* TODO: croak on errors? */ } return count; } +static void *aio_proc(void *arg); + +static void +start_thread (void) +{ + sigset_t fullsigset, oldsigset; + pthread_t tid; + pthread_attr_t attr; + + pthread_attr_init (&attr); + pthread_attr_setstacksize (&attr, STACKSIZE); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + + sigfillset (&fullsigset); + sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); + + if (pthread_create (&tid, &attr, aio_proc, 0) == 0) + started++; + + sigprocmask (SIG_SETMASK, &oldsigset, 0); +} + +static void +send_req (aio_req req) +{ + nreqs++; + + pthread_mutex_lock (&reqlock); + + req->next = 0; + + if (reqe) + { + reqe->next = req; + reqe = req; + } + else + reqe = reqs = req; + + pthread_cond_signal (&reqwait); + pthread_mutex_unlock (&reqlock); + + while (nreqs > max_outstanding) + { + poll_wait (); + poll_cb (); + } +} + +static void +end_thread (void) +{ + aio_req req; + New (0, req, 1, aio_cb); + req->type = REQ_QUIT; + + send_req (req); +} + static void * aio_proc (void *thr_arg) { @@ -306,8 +269,8 @@ 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; + 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 @@ -361,6 +324,8 @@ MODULE = IO::AIO PACKAGE = IO::AIO +PROTOTYPES: ENABLE + BOOT: { if (pipe (respipe)) @@ -397,17 +362,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; @@ -428,10 +401,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 @@ -446,43 +419,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; @@ -496,7 +506,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); @@ -505,12 +516,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; @@ -527,13 +538,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))); } @@ -543,10 +555,9 @@ } void -aio_unlink(pathname,callback) +aio_unlink(pathname,callback=&PL_sv_undef) SV * pathname SV * callback - PROTOTYPE: $$ CODE: { aio_req req; @@ -564,6 +575,26 @@ 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: @@ -576,7 +607,7 @@ poll_cb(...) PROTOTYPE: CODE: - RETVAL = poll_cb (aTHX); + RETVAL = poll_cb (); OUTPUT: RETVAL