--- IO-AIO/AIO.xs 2005/08/17 03:16:56 1.25 +++ IO-AIO/AIO.xs 2005/08/17 03:52:20 1.26 @@ -70,6 +70,23 @@ 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->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) + Safefree (req->statdata); + + if (req->callback) + SvREFCNT_dec (req->callback); + + Safefree (req); +} + static void poll_wait () { @@ -120,19 +137,11 @@ 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; @@ -161,20 +170,17 @@ SPAGAIN; } - do_croak = SvTRUE (ERRSV); - LEAVE; - - if (req->callback) - SvREFCNT_dec (req->callback); + do_croak = SvTRUE (ERRSV); + errno = errorno; count++; } prv = req; req = req->next; - Safefree (prv); + free_req (prv); if (do_croak) croak (0); @@ -236,7 +242,7 @@ 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); @@ -265,52 +271,62 @@ } } -static int fork_started; - -static void atfork_prepare (void) +static void create_pipe () { - int nstarted; - - for (;;) { - while (nreqs) - { - poll_wait (); - poll_cb (); - } - - nstarted = started; - max_parallel (0); - - pthread_mutex_lock (&reqlock); + if (pipe (respipe)) + croak ("unable to initialize result pipe"); - if (!nreqs && !started) - break; + if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) + croak ("cannot set result pipe to nonblocking mode"); - pthread_mutex_unlock (&reqlock); - - min_parallel (fork_started); - } + if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) + croak ("cannot set result pipe to nonblocking mode"); +} +static void atfork_prepare (void) +{ pthread_mutex_lock (&frklock); - fork_started = nstarted; + pthread_mutex_lock (&reqlock); pthread_mutex_lock (&reslock); - - assert (!started); - assert (!nreqs); - assert (!reqs && !reqe); - assert (!ress && !rese); } static void atfork_parent (void) { pthread_mutex_unlock (&reslock); - pthread_mutex_unlock (&frklock); pthread_mutex_unlock (&reqlock); - - min_parallel (fork_started); + pthread_mutex_unlock (&frklock); } -#define atfork_child atfork_parent +static void atfork_child (void) +{ + int restart = started; + started = 0; + + while (reqs) + { + free_req (reqs); + reqs = reqs->next; + } + + reqs = reqe = 0; + + while (ress) + { + free_req (ress); + ress = ress->next; + } + + ress = rese = 0; + + close (respipe [0]); + close (respipe [1]); + + create_pipe (); + + atfork_parent (); + + min_parallel (restart); +} /*****************************************************************************/ /* work around various missing functions */ @@ -490,15 +506,7 @@ 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); }