--- IO-AIO/AIO.xs 2006/10/26 16:28:33 1.79 +++ IO-AIO/AIO.xs 2006/10/27 19:17:23 1.80 @@ -147,9 +147,6 @@ static int next_pri = DEFAULT_PRI + PRI_BIAS; 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) # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP @@ -200,10 +197,45 @@ free (wrk); } +static volatile unsigned int nreqs, nready, npending; +static volatile unsigned int max_outstanding = 0xffffffff; +static int respipe [2]; + static pthread_mutex_t reslock = AIO_MUTEX_INIT; static pthread_mutex_t reqlock = AIO_MUTEX_INIT; static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; +#if WORDREAD_UNSAFE + +static unsigned int get_nready () +{ + unsigned int retval; + + LOCK (reqlock); + retval = nready; + UNLOCK (reqlock); + + return retval; +} + +static unsigned int get_npending () +{ + unsigned int retval; + + LOCK (reslock); + retval = npending; + UNLOCK (reslock); + + return retval; +} + +#else + +# define get_nready() nready +# define get_npending() npending + +#endif + /* * a somewhat faster data structure might be nice, but * with 8 priorities this actually needs <20 insns @@ -333,27 +365,6 @@ } } -static void poll_wait () -{ - fd_set rfd; - - while (nreqs) - { - int size; - if (WORDREAD_UNSAFE) LOCK (reslock); - size = res_queue.size; - if (WORDREAD_UNSAFE) UNLOCK (reslock); - - if (size) - return; - - FD_ZERO(&rfd); - FD_SET(respipe [0], &rfd); - - select (respipe [0] + 1, &rfd, 0, 0, 0); - } -} - static void req_invoke (aio_req req) { dSP; @@ -506,81 +517,6 @@ req_cancel_subs (req); } -static int poll_cb (int max) -{ - dSP; - int count = 0; - int do_croak = 0; - aio_req req; - - for (;;) - { - while (max <= 0 || count < max) - { - LOCK (reslock); - req = reqq_shift (&res_queue); - - if (req) - { - --npending; - - if (!res_queue.size) - { - /* read any signals sent by the worker threads */ - char buf [32]; - while (read (respipe [0], buf, 32) == 32) - ; - } - } - - UNLOCK (reslock); - - if (!req) - break; - - --nreqs; - - 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->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); - } - - req_invoke (req); - - count++; - } - - req_free (req); - } - - if (nreqs <= max_outstanding) - break; - - poll_wait (); - - max = 0; - } - - return count; -} - static void *aio_proc(void *arg); static void start_thread (void) @@ -596,6 +532,9 @@ pthread_attr_init (&attr); pthread_attr_setstacksize (&attr, STACKSIZE); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); +#ifdef PTHREAD_SCOPE_PROCESS + pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); +#endif sigfillset (&fullsigset); @@ -617,11 +556,42 @@ UNLOCK (wrklock); } -static void req_send (aio_req req) +static void maybe_start_thread () { - while (started < wanted && nreqs >= started) - start_thread (); +#if 0 + static struct timeval last; + struct timeval diff, now; +#endif + if (started >= wanted) + return; + + if (nready <= nreqs - get_nready () - get_npending ()) + return; + +#if 0 + gettimeofday (&now, 0); + + diff.tv_sec = now.tv_sec - last.tv_sec; + diff.tv_usec = now.tv_usec - last.tv_usec; + + if (diff.tv_usec < 0) + { + --diff.tv_sec; + diff.tv_usec += 1000000; + } + + if (!diff.tv_sec && diff.tv_usec < 10000) + return; + + last = now; +#endif + + start_thread (); +} + +static void req_send (aio_req req) +{ ++nreqs; LOCK (reqlock); @@ -629,6 +599,8 @@ reqq_push (&req_queue, req); pthread_cond_signal (&reqwait); UNLOCK (reqlock); + + maybe_start_thread (); } static void end_thread (void) @@ -641,6 +613,10 @@ req->pri = PRI_MAX + PRI_BIAS; req_send (req); + + LOCK (wrklock); + --started; + UNLOCK (wrklock); } static void min_parallel (int nthreads) @@ -651,22 +627,109 @@ static void max_parallel (int nthreads) { - int cur = started; - if (wanted > nthreads) wanted = nthreads; - while (cur > wanted) + while (started > wanted) + end_thread (); +} + +static void poll_wait () +{ + fd_set rfd; + + while (nreqs) { - end_thread (); - cur--; + int size; + if (WORDREAD_UNSAFE) LOCK (reslock); + size = res_queue.size; + if (WORDREAD_UNSAFE) UNLOCK (reslock); + + if (size) + return; + + maybe_start_thread (); + + FD_ZERO(&rfd); + FD_SET(respipe [0], &rfd); + + select (respipe [0] + 1, &rfd, 0, 0, 0); } +} - while (started > wanted) +static int poll_cb (int max) +{ + dSP; + int count = 0; + int do_croak = 0; + aio_req req; + + for (;;) { + while (max <= 0 || count < max) + { + maybe_start_thread (); + + LOCK (reslock); + req = reqq_shift (&res_queue); + + if (req) + { + --npending; + + if (!res_queue.size) + { + /* read any signals sent by the worker threads */ + char buf [32]; + while (read (respipe [0], buf, 32) == 32) + ; + } + } + + UNLOCK (reslock); + + if (!req) + break; + + --nreqs; + + 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->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); + } + + req_invoke (req); + + count++; + } + + req_free (req); + } + + if (nreqs <= max_outstanding) + break; + poll_wait (); - poll_cb (0); + + max = 0; } + + return count; } static void create_pipe () @@ -940,10 +1003,9 @@ static void *aio_proc (void *thr_arg) { aio_req req; - int type; worker *self = (worker *)thr_arg; - do + for (;;) { LOCK (reqlock); @@ -962,10 +1024,9 @@ UNLOCK (reqlock); errno = 0; /* strictly unnecessary */ - type = req->type; /* remember type for QUIT check */ if (!(req->flags & FLAG_CANCELLED)) - switch (type) + switch (req->type) { 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; @@ -1001,9 +1062,15 @@ case REQ_GROUP: case REQ_NOP: - case REQ_QUIT: break; + case REQ_QUIT: + LOCK (wrklock); + worker_free (self); + --started; + UNLOCK (wrklock); + return 0; + default: req->result = ENOSYS; break; @@ -1024,13 +1091,6 @@ UNLOCK (reslock); } - while (type != REQ_QUIT); - - LOCK (wrklock); - worker_free (self); - UNLOCK (wrklock); - - return 0; } /*****************************************************************************/ @@ -1126,6 +1186,8 @@ create_pipe (); pthread_atfork (atfork_prepare, atfork_parent, atfork_child); + + start_thread (); } void @@ -1493,9 +1555,7 @@ nready() PROTOTYPE: CODE: - if (WORDREAD_UNSAFE) LOCK (reqlock); - RETVAL = nready; - if (WORDREAD_UNSAFE) UNLOCK (reqlock); + RETVAL = get_nready (); OUTPUT: RETVAL @@ -1503,9 +1563,7 @@ npending() PROTOTYPE: CODE: - if (WORDREAD_UNSAFE) LOCK (reslock); - RETVAL = npending; - if (WORDREAD_UNSAFE) UNLOCK (reslock); + RETVAL = get_npending (); OUTPUT: RETVAL