#if __linux # define _GNU_SOURCE #endif #define _REENTRANT 1 #include #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "autoconf/config.h" #include #include #include #include #include #include #include #include #include #include #include #include #if HAVE_SENDFILE # if __linux # include # elif __freebsd # include # include # elif __hpux # include # elif __solaris /* not yet */ # include # else # error sendfile support requested but not available # endif #endif /* used for struct dirent, AIX doesn't provide it */ #ifndef NAME_MAX # define NAME_MAX 4096 #endif #if __ia64 # define STACKSIZE 65536 #else # define STACKSIZE 8192 #endif enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_READAHEAD, REQ_SENDFILE, REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_FSYNC, REQ_FDATASYNC, REQ_UNLINK, REQ_RMDIR, REQ_RENAME, REQ_READDIR, REQ_LINK, REQ_SYMLINK, REQ_GROUP, REQ_NOP, REQ_SLEEP, }; #define AIO_REQ_KLASS "IO::AIO::REQ" #define AIO_GRP_KLASS "IO::AIO::GRP" typedef struct aio_cb { struct aio_cb *volatile next; SV *data, *callback; SV *fh, *fh2; void *dataptr, *data2ptr; Stat_t *statdata; off_t offset; size_t length; ssize_t result; STRLEN dataoffset; int type; int fd, fd2; int errorno; mode_t mode; /* open */ 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 { FLAG_CANCELLED = 0x01, }; 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; static int respipe [2]; #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP #else # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER #endif static pthread_mutex_t reslock = AIO_MUTEX_INIT; static pthread_mutex_t reqlock = AIO_MUTEX_INIT; static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; static volatile aio_req reqs, reqe; /* queue start, queue end */ static volatile aio_req ress, rese; /* queue start, queue end */ static void req_invoke (aio_req req); static void req_free (aio_req req); /* must be called at most once */ static SV *req_sv (aio_req req, const char *klass) { if (!req->self) { req->self = (SV *)newHV (); sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); } return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); } static aio_req SvAIO_REQ (SV *sv) { MAGIC *mg; if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) croak ("object of class " AIO_REQ_KLASS " expected"); mg = mg_find (SvRV (sv), PERL_MAGIC_ext); return mg ? (aio_req)mg->mg_ptr : 0; } static void aio_grp_feed (aio_req grp) { while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) { int old_len = grp->length; if (grp->fh2 && SvOK (grp->fh2)) { dSP; ENTER; SAVETMPS; PUSHMARK (SP); XPUSHs (req_sv (grp, AIO_GRP_KLASS)); PUTBACK; call_sv (grp->fh2, G_VOID | G_EVAL); SPAGAIN; FREETMPS; LEAVE; } /* stop if no progress has been made */ if (old_len == grp->length) { SvREFCNT_dec (grp->fh2); grp->fh2 = 0; break; } } } static void aio_grp_dec (aio_req grp) { --grp->length; /* call feeder, if applicable */ aio_grp_feed (grp); /* finish, if done */ if (!grp->length && grp->fd) { req_invoke (grp); req_free (grp); } } static void poll_wait () { fd_set rfd; while (nreqs) { aio_req req; pthread_mutex_lock (&reslock); req = ress; pthread_mutex_unlock (&reslock); if (req) 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; int errorno = errno; if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) return; errno = req->errorno; ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, 1); switch (req->type) { case REQ_READDIR: { SV *rv = &PL_sv_undef; if (req->result >= 0) { char *buf = req->data2ptr; AV *av = newAV (); while (req->result) { SV *sv = newSVpv (buf, 0); av_push (av, sv); buf += SvCUR (sv) + 1; req->result--; } rv = sv_2mortal (newRV_noinc ((SV *)av)); } PUSHs (rv); } break; case REQ_OPEN: { /* convert fd to fh */ SV *fh; PUSHs (sv_2mortal (newSViv (req->result))); PUTBACK; call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); SPAGAIN; fh = SvREFCNT_inc (POPs); PUSHMARK (SP); XPUSHs (sv_2mortal (fh)); } break; case REQ_GROUP: req->fd = 2; /* mark group as finished */ if (req->data) { int i; AV *av = (AV *)req->data; EXTEND (SP, AvFILL (av) + 1); for (i = 0; i <= AvFILL (av); ++i) PUSHs (*av_fetch (av, i, 0)); } break; case REQ_NOP: case REQ_SLEEP: break; default: PUSHs (sv_2mortal (newSViv (req->result))); break; } PUTBACK; call_sv (req->callback, G_VOID | G_EVAL); SPAGAIN; FREETMPS; LEAVE; errno = errorno; if (SvTRUE (ERRSV)) { req_free (req); croak (0); } } static void req_free (aio_req req) { if (req->grp) { aio_req grp = req->grp; /* unlink request */ if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; if (grp->grp_first == req) grp->grp_first = req->grp_next; aio_grp_dec (grp); } if (req->self) { sv_unmagic (req->self, PERL_MAGIC_ext); SvREFCNT_dec (req->self); } SvREFCNT_dec (req->data); SvREFCNT_dec (req->fh); SvREFCNT_dec (req->fh2); SvREFCNT_dec (req->callback); Safefree (req->statdata); if (req->type == REQ_READDIR && req->result >= 0) free (req->data2ptr); Safefree (req); } static void req_cancel (aio_req req) { req->flags |= FLAG_CANCELLED; if (req->type == REQ_GROUP) { aio_req sub; for (sub = req->grp_first; sub; sub = sub->grp_next) req_cancel (sub); } } static int poll_cb () { dSP; int count = 0; int do_croak = 0; aio_req req; for (;;) { pthread_mutex_lock (&reslock); req = ress; if (req) { ress = req->next; if (!ress) { /* read any signals sent by the worker threads */ char buf [32]; while (read (respipe [0], buf, 32) == 32) ; rese = 0; } } pthread_mutex_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); } return count; } static void *aio_proc(void *arg); static void start_thread (void) { sigset_t fullsigset, oldsigset; pthread_t tid; pthread_attr_t attr; pthread_attr_init (&attr); pthread_attr_setstacksize (&attr, STACKSIZE); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); sigfillset (&fullsigset); sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); if (pthread_create (&tid, &attr, aio_proc, 0) == 0) started++; sigprocmask (SIG_SETMASK, &oldsigset, 0); } static void req_send (aio_req req) { while (started < wanted && nreqs >= started) start_thread (); ++nreqs; pthread_mutex_lock (&reqlock); req->next = 0; if (reqe) { reqe->next = req; reqe = req; } else reqe = reqs = req; pthread_cond_signal (&reqwait); pthread_mutex_unlock (&reqlock); if (nreqs > max_outstanding) for (;;) { poll_cb (); if (nreqs <= max_outstanding) break; poll_wait (); } } static void end_thread (void) { aio_req req; Newz (0, req, 1, aio_cb); req->type = REQ_QUIT; req_send (req); } static void min_parallel (int nthreads) { if (wanted < nthreads) wanted = nthreads; } static void max_parallel (int nthreads) { int cur = started; if (wanted > nthreads) wanted = nthreads; while (cur > wanted) { end_thread (); cur--; } while (started > wanted) { poll_wait (); poll_cb (); } } static void create_pipe () { 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"); } /*****************************************************************************/ /* work around various missing functions */ #if !HAVE_PREADWRITE # define pread aio_pread # define pwrite aio_pwrite /* * make our pread/pwrite safe against themselves, but not against * normal read/write by using a mutex. slows down execution a lot, * but that's your problem, not mine. */ static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; static ssize_t pread (int fd, void *buf, size_t count, off_t offset) { ssize_t res; off_t ooffset; pthread_mutex_lock (&preadwritelock); ooffset = lseek (fd, 0, SEEK_CUR); lseek (fd, offset, SEEK_SET); res = read (fd, buf, count); lseek (fd, ooffset, SEEK_SET); pthread_mutex_unlock (&preadwritelock); return res; } static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) { ssize_t res; off_t ooffset; pthread_mutex_lock (&preadwritelock); ooffset = lseek (fd, 0, SEEK_CUR); lseek (fd, offset, SEEK_SET); res = write (fd, buf, count); lseek (fd, offset, SEEK_SET); pthread_mutex_unlock (&preadwritelock); return res; } #endif #if !HAVE_FDATASYNC # define fdatasync fsync #endif #if !HAVE_READAHEAD # define readahead aio_readahead static ssize_t readahead (int fd, off_t offset, size_t count) { char readahead_buf[4096]; while (count > 0) { size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); pread (fd, readahead_buf, len, offset); offset += len; count -= len; } errno = 0; } #endif #if !HAVE_READDIR_R # define readdir_r aio_readdir_r static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) { struct dirent *e; int errorno; pthread_mutex_lock (&readdirlock); e = readdir (dirp); errorno = errno; if (e) { *res = ent; strcpy (ent->d_name, e->d_name); } else *res = 0; pthread_mutex_unlock (&readdirlock); errno = errorno; return e ? 0 : -1; } #endif /* sendfile always needs emulation */ static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) { ssize_t res; if (!count) return 0; #if HAVE_SENDFILE # if __linux res = sendfile (ofd, ifd, &offset, count); # elif __freebsd /* * Of course, the freebsd sendfile is a dire hack with no thoughts * wasted on making it similar to other I/O functions. */ { off_t sbytes; res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); if (res < 0 && sbytes) /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ res = sbytes; } # elif __hpux res = sendfile (ofd, ifd, offset, count, 0, 0); # elif __solaris { struct sendfilevec vec; size_t sbytes; vec.sfv_fd = ifd; vec.sfv_flag = 0; vec.sfv_off = offset; vec.sfv_len = count; res = sendfilev (ofd, &vec, 1, &sbytes); if (res < 0 && sbytes) res = sbytes; } # endif #else res = -1; errno = ENOSYS; #endif if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK #if __solaris || errno == EAFNOSUPPORT || errno == EPROTOTYPE #endif ) ) { /* emulate sendfile. this is a major pain in the ass */ char buf[4096]; res = 0; while (count) { ssize_t cnt; cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); if (cnt <= 0) { if (cnt && !res) res = -1; break; } cnt = write (ofd, buf, cnt); if (cnt <= 0) { if (cnt && !res) res = -1; break; } offset += cnt; res += cnt; count -= cnt; } } return res; } /* read a full directory */ static int scandir_ (const char *path, void **namesp) { DIR *dirp = opendir (path); union { struct dirent d; char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; } u; struct dirent *entp; char *name, *names; int memlen = 4096; int memofs = 0; int res = 0; int errorno; if (!dirp) return -1; names = malloc (memlen); for (;;) { errno = 0, readdir_r (dirp, &u.d, &entp); if (!entp) break; name = entp->d_name; if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) { int len = strlen (name) + 1; res++; while (memofs + len > memlen) { memlen *= 2; names = realloc (names, memlen); if (!names) break; } memcpy (names + memofs, name, len); memofs += len; } } errorno = errno; closedir (dirp); if (errorno) { free (names); errno = errorno; res = -1; } *namesp = (void *)names; return res; } /*****************************************************************************/ static void *aio_proc (void *thr_arg) { aio_req req; int type; do { pthread_mutex_lock (&reqlock); for (;;) { req = reqs; if (reqs) { reqs = reqs->next; if (!reqs) reqe = 0; } if (req) break; pthread_cond_wait (&reqwait, &reqlock); } pthread_mutex_unlock (&reqlock); errno = 0; /* strictly unnecessary */ type = req->type; /* remember type for QUIT check */ if (!(req->flags & FLAG_CANCELLED)) 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; case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; case REQ_CLOSE: req->result = close (req->fd); break; case REQ_UNLINK: req->result = unlink (req->dataptr); break; case REQ_RMDIR: req->result = rmdir (req->dataptr); break; case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; case REQ_FSYNC: req->result = fsync (req->fd); break; case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; case REQ_SLEEP: { struct timeval tv; tv.tv_sec = req->fd; tv.tv_usec = req->fd2; req->result = select (0, 0, 0, 0, &tv); } case REQ_GROUP: case REQ_NOP: case REQ_QUIT: break; default: req->result = ENOSYS; break; } req->errorno = errno; pthread_mutex_lock (&reslock); req->next = 0; if (rese) { rese->next = req; rese = req; } else { rese = ress = req; /* write a dummy byte to the pipe so fh becomes ready */ write (respipe [1], &respipe, 1); } pthread_mutex_unlock (&reslock); } while (type != REQ_QUIT); return 0; } /*****************************************************************************/ static void atfork_prepare (void) { pthread_mutex_lock (&reqlock); pthread_mutex_lock (&reslock); #if !HAVE_PREADWRITE pthread_mutex_lock (&preadwritelock); #endif #if !HAVE_READDIR_R pthread_mutex_lock (&readdirlock); #endif } static void atfork_parent (void) { #if !HAVE_READDIR_R pthread_mutex_unlock (&readdirlock); #endif #if !HAVE_PREADWRITE pthread_mutex_unlock (&preadwritelock); #endif pthread_mutex_unlock (&reslock); pthread_mutex_unlock (&reqlock); } static void atfork_child (void) { aio_req prv; started = 0; while (reqs) { prv = reqs; reqs = prv->next; req_free (prv); } reqs = reqe = 0; while (ress) { prv = ress; ress = prv->next; req_free (prv); } ress = rese = 0; close (respipe [0]); close (respipe [1]); create_pipe (); atfork_parent (); } #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"); \ \ Newz (0, req, 1, aio_cb); \ if (!req) \ croak ("out of memory during aio_req allocation"); \ \ req->callback = newSVsv (callback); \ req->pri = req_pri #define REQ_SEND \ req_send (req); \ \ if (GIMME_V != G_VOID) \ XPUSHs (req_sv (req, AIO_REQ_KLASS)); MODULE = IO::AIO PACKAGE = IO::AIO PROTOTYPES: ENABLE BOOT: { HV *stash = gv_stashpv ("IO::AIO", 1); newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); create_pipe (); pthread_atfork (atfork_prepare, atfork_parent, atfork_child); } void min_parallel (nthreads) int nthreads PROTOTYPE: $ void max_parallel (nthreads) int nthreads PROTOTYPE: $ int max_outstanding (nreqs) int nreqs PROTOTYPE: $ CODE: RETVAL = max_outstanding; max_outstanding = nreqs; void aio_open (pathname,flags,mode,callback=&PL_sv_undef) SV * pathname int flags int mode SV * callback PROTOTYPE: $$$;$ PPCODE: { dREQ; req->type = REQ_OPEN; req->data = newSVsv (pathname); req->dataptr = SvPVbyte_nolen (req->data); req->fd = flags; req->mode = mode; REQ_SEND; } void aio_close (fh,callback=&PL_sv_undef) SV * fh SV * callback PROTOTYPE: $;$ ALIAS: aio_close = REQ_CLOSE aio_fsync = REQ_FSYNC aio_fdatasync = REQ_FDATASYNC PPCODE: { dREQ; req->type = ix; req->fh = newSVsv (fh); req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); REQ_SEND (req); } void aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) SV * fh UV offset UV length SV * data UV dataoffset SV * callback ALIAS: aio_read = REQ_READ aio_write = REQ_WRITE PROTOTYPE: $$$$$;$ PPCODE: { aio_req req; STRLEN svlen; char *svptr = SvPVbyte (data, svlen); SvUPGRADE (data, SVt_PV); SvPOK_on (data); if (dataoffset < 0) dataoffset += svlen; if (dataoffset < 0 || dataoffset > svlen) croak ("data offset outside of string"); if (ix == REQ_WRITE) { /* write: check length and adjust. */ if (length < 0 || length + dataoffset > svlen) length = svlen - dataoffset; } else { /* read: grow scalar as necessary */ svptr = SvGROW (data, length + dataoffset); } if (length < 0) croak ("length must not be negative"); { dREQ; req->type = ix; req->fh = newSVsv (fh); req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) : IoOFP (sv_2io (fh))); req->offset = offset; req->length = length; req->data = SvREFCNT_inc (data); req->dataptr = (char *)svptr + dataoffset; if (!SvREADONLY (data)) { SvREADONLY_on (data); req->data2ptr = (void *)data; } REQ_SEND; } } void aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) SV * out_fh SV * in_fh UV in_offset UV length SV * callback PROTOTYPE: $$$$;$ PPCODE: { dREQ; req->type = REQ_SENDFILE; req->fh = newSVsv (out_fh); req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); req->fh2 = newSVsv (in_fh); req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); req->offset = in_offset; req->length = length; REQ_SEND; } void aio_readahead (fh,offset,length,callback=&PL_sv_undef) SV * fh UV offset IV length SV * callback PROTOTYPE: $$$;$ PPCODE: { dREQ; req->type = REQ_READAHEAD; req->fh = newSVsv (fh); req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); req->offset = offset; req->length = length; REQ_SEND; } void aio_stat (fh_or_path,callback=&PL_sv_undef) SV * fh_or_path SV * callback ALIAS: aio_stat = REQ_STAT aio_lstat = REQ_LSTAT PPCODE: { dREQ; New (0, req->statdata, 1, Stat_t); if (!req->statdata) { req_free (req); croak ("out of memory during aio_req->statdata allocation"); } if (SvPOK (fh_or_path)) { req->type = ix; req->data = newSVsv (fh_or_path); req->dataptr = SvPVbyte_nolen (req->data); } else { req->type = REQ_FSTAT; req->fh = newSVsv (fh_or_path); req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); } REQ_SEND; } void aio_unlink (pathname,callback=&PL_sv_undef) SV * pathname SV * callback ALIAS: aio_unlink = REQ_UNLINK aio_rmdir = REQ_RMDIR aio_readdir = REQ_READDIR PPCODE: { dREQ; req->type = ix; req->data = newSVsv (pathname); req->dataptr = SvPVbyte_nolen (req->data); REQ_SEND; } void aio_link (oldpath,newpath,callback=&PL_sv_undef) SV * oldpath SV * newpath SV * callback ALIAS: aio_link = REQ_LINK aio_symlink = REQ_SYMLINK aio_rename = REQ_RENAME PPCODE: { dREQ; req->type = ix; req->fh = newSVsv (oldpath); req->data2ptr = SvPVbyte_nolen (req->fh); req->data = newSVsv (newpath); req->dataptr = SvPVbyte_nolen (req->data); REQ_SEND; } void aio_sleep (delay,callback=&PL_sv_undef) double delay SV * callback PPCODE: { dREQ; req->type = REQ_SLEEP; req->fd = delay < 0. ? 0 : delay; req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); REQ_SEND; } void aio_group (callback=&PL_sv_undef) SV * callback PROTOTYPE: ;$ PPCODE: { dREQ; req->type = REQ_GROUP; req_send (req); XPUSHs (req_sv (req, AIO_GRP_KLASS)); } void aio_nop (callback=&PL_sv_undef) SV * callback PPCODE: { dREQ; req->type = REQ_NOP; 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: CODE: while (nreqs) { poll_wait (); poll_cb (); } void poll() PROTOTYPE: CODE: if (nreqs) { poll_wait (); poll_cb (); } int poll_fileno() PROTOTYPE: CODE: RETVAL = respipe [0]; OUTPUT: RETVAL int poll_cb(...) PROTOTYPE: CODE: RETVAL = poll_cb (); OUTPUT: RETVAL void poll_wait() PROTOTYPE: CODE: if (nreqs) poll_wait (); int nreqs() PROTOTYPE: CODE: RETVAL = nreqs; OUTPUT: RETVAL PROTOTYPES: DISABLE MODULE = IO::AIO PACKAGE = IO::AIO::REQ void cancel (aio_req_ornot req) PROTOTYPE: CODE: req_cancel (req); void cb (aio_req_ornot req, SV *callback=&PL_sv_undef) CODE: SvREFCNT_dec (req->callback); req->callback = newSVsv (callback); MODULE = IO::AIO PACKAGE = IO::AIO::GRP void add (aio_req grp, ...) PPCODE: { int i; aio_req req; if (grp->fd == 2) croak ("cannot add requests to IO::AIO::GRP after the group finished"); for (i = 1; i < items; ++i ) { if (GIMME_V != G_VOID) XPUSHs (sv_2mortal (newSVsv (ST (i)))); req = SvAIO_REQ (ST (i)); if (req) { ++grp->length; req->grp = grp; req->grp_prev = 0; req->grp_next = grp->grp_first; if (grp->grp_first) grp->grp_first->grp_prev = req; grp->grp_first = req; } } } void result (aio_req grp, ...) CODE: { int i; AV *av = newAV (); for (i = 1; i < items; ++i ) av_push (av, newSVsv (ST (i))); SvREFCNT_dec (grp->data); grp->data = (SV *)av; } void feed_limit (aio_req grp, int limit) CODE: grp->fd2 = limit; aio_grp_feed (grp); void feed (aio_req grp, SV *callback=&PL_sv_undef) CODE: { SvREFCNT_dec (grp->fh2); grp->fh2 = newSVsv (callback); if (grp->fd2 <= 0) grp->fd2 = 2; aio_grp_feed (grp); }