--- IO-AIO/AIO.xs 2006/10/23 18:48:08 1.58 +++ IO-AIO/AIO.xs 2006/10/23 20:05:27 1.60 @@ -68,10 +68,6 @@ { struct aio_cb *volatile next; - struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; - - SV *self; /* the perl counterpart of this request, if any */ - SV *data, *callback; SV *fh, *fh2; void *dataptr, *data2ptr; @@ -80,13 +76,17 @@ size_t length; ssize_t result; + STRLEN dataoffset; int type; int fd, fd2; int errorno; - STRLEN dataoffset; mode_t mode; /* open */ - unsigned char pri; + unsigned char flags; + unsigned char pri; + + SV *self; /* the perl counterpart of this request, if any */ + struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; } aio_cb; enum { @@ -96,6 +96,16 @@ typedef aio_cb *aio_req; typedef aio_cb *aio_req_ornot; +enum { + PRI_MIN = 4, + PRI_MAX = -4, + + DEFAULT_PRI = 0, + PRI_BIAS = PRI_MIN, +}; + +static int next_pri = DEFAULT_PRI + PRI_BIAS; + static int started, wanted; static volatile int nreqs; static int max_outstanding = 1<<30; @@ -183,8 +193,16 @@ static void poll_wait () { - if (nreqs && !ress) + while (nreqs) { + aio_req req; + pthread_mutex_lock (&reslock); + req = ress; + pthread_mutex_unlock (&reslock); + + if (req) + return; + fd_set rfd; FD_ZERO(&rfd); FD_SET(respipe [0], &rfd); @@ -792,9 +810,10 @@ pthread_mutex_unlock (&reqlock); errno = 0; /* strictly unnecessary */ + type = req->type; /* remember type for QUIT check */ if (!(req->flags & FLAG_CANCELLED)) - switch (type = req->type) /* remember type for QUIT check */ + switch (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; @@ -923,6 +942,8 @@ #define dREQ \ aio_req req; \ + int req_pri = next_pri; \ + next_pri = DEFAULT_PRI + PRI_BIAS; \ \ if (SvOK (callback) && !SvROK (callback)) \ croak ("callback must be undef or of reference type"); \ @@ -931,7 +952,8 @@ if (!req) \ croak ("out of memory during aio_req allocation"); \ \ - req->callback = newSVsv (callback) + req->callback = newSVsv (callback); \ + req->pri = req_pri #define REQ_SEND \ req_send (req); \ @@ -1216,8 +1238,10 @@ PPCODE: { dREQ; + req->type = REQ_GROUP; req_send (req); + XPUSHs (req_sv (req, AIO_GRP_KLASS)); } @@ -1233,6 +1257,17 @@ REQ_SEND; } +#if 0 + +void +aio_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; + +#endif + void flush () PROTOTYPE: @@ -1295,7 +1330,7 @@ req_cancel (req); void -cb (aio_req req, SV *callback=&PL_sv_undef) +cb (aio_req_ornot req, SV *callback=&PL_sv_undef) CODE: SvREFCNT_dec (req->callback); req->callback = newSVsv (callback);