#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include #include #include #define STACKSIZE 128 /* yeah */ #define REQ_QUIT 0 #define REQ_READ 1 #define REQ_WRITE 2 typedef struct { char stack[STACKSIZE]; } aio_thread; typedef struct { int type; aio_thread *thread; /* read/write */ int fd; off_t offset; size_t length; ssize_t result; int errorno; SV *data; void *dataptr; STRLEN dataoffset; } aio_cb; typedef aio_cb *aio_req; static int started; static int reqpipe[2], respipe[2]; static int aio_proc(void *arg); static void start_thread(void) { aio_thread *thr; New (0, thr, 1, aio_thread); if (clone (aio_proc, &(thr->stack[STACKSIZE]), CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, thr) >= 0) started++; else Safefree (thr); } static void end_thread(void) { aio_req req = 0; write (reqpipe[1], &req, sizeof (aio_req)); started--; } static void set_errno(int errorno) { errno = errorno; } #undef errno #include static int aio_proc(void *thr_arg) { aio_thread *thr = thr_arg; int sig; int errno; aio_req req; /* we rely on gcc's ability to create closures. */ _syscall3(int,lseek,int,fd,off_t,offset,int,whence); _syscall3(int,read,int,fd,char *,buf,off_t,count); _syscall3(int,write,int,fd,char *,buf,off_t,count); /* first get rid of any signals */ for (sig = 1; sig < _NSIG; sig++) signal (sig, SIG_IGN); signal (SIGTERM, SIG_DFL); /* then loop */ while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) { req->thread = thr; if (req->type == REQ_READ || req->type == REQ_WRITE) { errno = 0; if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) { if (req->type == REQ_READ) req->result = read (req->fd, req->dataptr, req->length); else req->result = write(req->fd, req->dataptr, req->length); } req->errorno = errno; } else { write (respipe[1], (void *)&req, sizeof (req)); break; } write (respipe[1], (void *)&req, sizeof (req)); } return 0; } MODULE = Linux::AIO PACKAGE = Linux::AIO BOOT: { if (pipe (reqpipe) || pipe (respipe)) croak ("unable to initialize request or result pipe"); } void min_parallel(nthreads) int nthreads CODE: while (nthreads > started) start_thread (); void max_parallel(nthreads) int nthreads CODE: while (started > nthreads) end_thread (); void read(fh,offset,length,data,dataoffset,callback) ALIAS: write = 1 CODE: