--- IO-AIO/AIO.xs 2005/07/12 11:02:54 1.9 +++ IO-AIO/AIO.xs 2005/07/20 21:57:04 1.12 @@ -1,16 +1,21 @@ +#define _XOPEN_SOURCE 500 + #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #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 ;) */ @@ -51,7 +56,7 @@ 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]; @@ -65,14 +70,14 @@ static void poll_wait () { - if (!nreqs) - return; - - fd_set rfd; - FD_ZERO(&rfd); - FD_SET(respipe [0], &rfd); + if (nreqs && !ress) + { + fd_set rfd; + FD_ZERO(&rfd); + FD_SET(respipe [0], &rfd); - select (respipe [0] + 1, &rfd, 0, 0, 0); + select (respipe [0] + 1, &rfd, 0, 0, 0); + } } static int @@ -80,32 +85,24 @@ { 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; + req = ress; + ress = rese = 0; - 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) @@ -163,7 +160,11 @@ count++; } - Safefree (req); + prv = req; + req = req->next; + Safefree (prv); + + /* TODO: croak on errors? */ } return count; @@ -310,8 +311,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