--- IO-AIO/AIO.xs 2006/10/26 13:25:40 1.77 +++ IO-AIO/AIO.xs 2006/10/26 16:28:33 1.79 @@ -63,6 +63,18 @@ # define STACKSIZE 16384 #endif +/* wether word reads are potentially non-atomic. + * this is conservatice, likely most arches this runs + * on have atomic word read/writes. + */ +#ifndef WORDREAD_UNSAFE +# if __i386 || __x86_64 +# define WORDREAD_UNSAFE 0 +# else +# define WORDREAD_UNSAFE 1 +# endif +#endif + /* buffer size for various temporary buffers */ #define AIO_BUFSIZE 65536 @@ -134,8 +146,9 @@ static int next_pri = DEFAULT_PRI + PRI_BIAS; -static int started, wanted; -static volatile int nreqs; +static unsigned int started, wanted; +static volatile unsigned int nreqs, nready, npending; +static volatile unsigned int max_outstanding = 0xffffffff; static int respipe [2]; #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) @@ -327,13 +340,9 @@ while (nreqs) { int size; -#if !(__i386 || __x86_64) /* safe without sempahore on these archs */ - LOCK (reslock); -#endif + if (WORDREAD_UNSAFE) LOCK (reslock); size = res_queue.size; -#if !(__i386 || __x86_64) /* safe without sempahore on these archs */ - UNLOCK (reslock); -#endif + if (WORDREAD_UNSAFE) UNLOCK (reslock); if (size) return; @@ -351,8 +360,6 @@ if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) { - errno = req->errorno; - ENTER; SAVETMPS; PUSHMARK (SP); @@ -427,6 +434,7 @@ break; } + errno = req->errorno; PUTBACK; call_sv (req->callback, G_VOID | G_EVAL); @@ -505,57 +513,69 @@ int do_croak = 0; aio_req req; - while (max <= 0 || count < max) + for (;;) { - LOCK (reslock); - req = reqq_shift (&res_queue); - - if (req) + while (max <= 0 || count < max) { - if (!res_queue.size) - { - /* read any signals sent by the worker threads */ - char buf [32]; - while (read (respipe [0], buf, 32) == 32) - ; - } - } + LOCK (reslock); + req = reqq_shift (&res_queue); - UNLOCK (reslock); + if (req) + { + --npending; - if (!req) - break; + if (!res_queue.size) + { + /* read any signals sent by the worker threads */ + char buf [32]; + while (read (respipe [0], buf, 32) == 32) + ; + } + } - --nreqs; + UNLOCK (reslock); - if (req->type == REQ_QUIT) - started--; - else if (req->type == REQ_GROUP && req->length) - { - req->fd = 1; /* mark request as delayed */ - continue; - } - else - { - if (req->type == REQ_READ) - SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); + if (!req) + break; - if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) - SvREADONLY_off (req->data); + --nreqs; - if (req->statdata) + if (req->type == REQ_QUIT) + --started; + else if (req->type == REQ_GROUP && req->length) { - PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; - PL_laststatval = req->result; - PL_statcache = *(req->statdata); + req->fd = 1; /* mark request as delayed */ + continue; } + else + { + 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); - req_invoke (req); + if (req->statdata) + { + PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; + PL_laststatval = req->result; + PL_statcache = *(req->statdata); + } - count++; + req_invoke (req); + + count++; + } + + req_free (req); } - req_free (req); + if (nreqs <= max_outstanding) + break; + + poll_wait (); + + max = 0; } return count; @@ -588,7 +608,7 @@ wrk->next = wrk_first.next; wrk_first.next->prev = wrk; wrk_first.next = wrk; - started++; + ++started; } else free (wrk); @@ -605,6 +625,7 @@ ++nreqs; LOCK (reqlock); + ++nready; reqq_push (&req_queue, req); pthread_cond_signal (&reqwait); UNLOCK (reqlock); @@ -936,6 +957,8 @@ pthread_cond_wait (&reqwait, &reqlock); } + --nready; + UNLOCK (reqlock); errno = 0; /* strictly unnecessary */ @@ -990,6 +1013,8 @@ LOCK (reslock); + ++npending; + if (!reqq_push (&res_queue, req)) /* write a dummy byte to the pipe so fh becomes ready */ write (respipe [1], &respipe, 1); @@ -1104,14 +1129,21 @@ } void -min_parallel (nthreads) - int nthreads +min_parallel (int nthreads) PROTOTYPE: $ void -max_parallel (nthreads) - int nthreads +max_parallel (int nthreads) + PROTOTYPE: $ + +int +max_outstanding (int maxreqs) PROTOTYPE: $ + CODE: + RETVAL = max_outstanding; + max_outstanding = maxreqs; + OUTPUT: + RETVAL void aio_open (pathname,flags,mode,callback=&PL_sv_undef) @@ -1376,20 +1408,27 @@ REQ_SEND; } -void -aioreq_pri (int pri = DEFAULT_PRI) - CODE: - if (pri < PRI_MIN) pri = PRI_MIN; - if (pri > PRI_MAX) pri = PRI_MAX; - next_pri = pri + PRI_BIAS; +int +aioreq_pri (int pri = 0) + PROTOTYPE: ;$ + CODE: + RETVAL = next_pri - PRI_BIAS; + if (items > 0) + { + if (pri < PRI_MIN) pri = PRI_MIN; + if (pri > PRI_MAX) pri = PRI_MAX; + next_pri = pri + PRI_BIAS; + } + OUTPUT: + RETVAL void aioreq_nice (int nice = 0) - CODE: - nice = next_pri - nice; - if (nice < PRI_MIN) nice = PRI_MIN; - if (nice > PRI_MAX) nice = PRI_MAX; - next_pri = nice + PRI_BIAS; + CODE: + nice = next_pri - nice; + if (nice < PRI_MIN) nice = PRI_MIN; + if (nice > PRI_MAX) nice = PRI_MAX; + next_pri = nice + PRI_BIAS; void flush () @@ -1450,6 +1489,26 @@ OUTPUT: RETVAL +int +nready() + PROTOTYPE: + CODE: + if (WORDREAD_UNSAFE) LOCK (reqlock); + RETVAL = nready; + if (WORDREAD_UNSAFE) UNLOCK (reqlock); + OUTPUT: + RETVAL + +int +npending() + PROTOTYPE: + CODE: + if (WORDREAD_UNSAFE) LOCK (reslock); + RETVAL = npending; + if (WORDREAD_UNSAFE) UNLOCK (reslock); + OUTPUT: + RETVAL + PROTOTYPES: DISABLE MODULE = IO::AIO PACKAGE = IO::AIO::REQ @@ -1510,7 +1569,11 @@ CODE: { int i; - AV *av = newAV (); + AV *av; + + grp->errorno = errno; + + av = newAV (); for (i = 1; i < items; ++i ) av_push (av, newSVsv (ST (i))); @@ -1520,6 +1583,11 @@ } void +errno (aio_req grp, int errorno = errno) + CODE: + grp->errorno = errorno; + +void limit (aio_req grp, int limit) CODE: grp->fd2 = limit;