--- libeio/eio.c 2008/05/11 19:11:05 1.6 +++ libeio/eio.c 2008/05/13 19:34:11 1.13 @@ -10,7 +10,7 @@ #include #include #include -#include +#include #ifndef EIO_FINISH # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 @@ -44,10 +44,6 @@ #endif -# ifndef EIO_STRUCT_STAT -# define EIO_STRUCT_STAT struct stat -# endif - #if HAVE_SENDFILE # if __linux # include @@ -76,19 +72,16 @@ #define dBUF \ char *eio_buf; \ - X_LOCK (wrklock); \ + X_LOCK (etplock); \ self->dbuf = eio_buf = malloc (EIO_BUFSIZE); \ - X_UNLOCK (wrklock); \ + X_UNLOCK (etplock); \ + errno = ENOMEM; \ if (!eio_buf) \ return -1; #define EIO_TICKS ((1000000 + 1023) >> 10) -static void (*want_poll_cb) (void); -static void (*done_poll_cb) (void); - -static unsigned int max_poll_time = 0; -static unsigned int max_poll_reqs = 0; +/*****************************************************************************/ /* calculcate time difference in ~1/EIO_TICKS of a second */ static int tvdiff (struct timeval *tv1, struct timeval *tv2) @@ -97,24 +90,41 @@ + ((tv2->tv_usec - tv1->tv_usec) >> 10); } -static unsigned int started, idle, wanted; +static unsigned int started, idle, wanted = 4; -/* worker threads management */ -static mutex_t wrklock = X_MUTEX_INIT; +typedef struct etp_pool +{ + void (*want_poll_cb) (void); + void (*done_poll_cb) (void); + + unsigned int max_poll_time; + unsigned int max_poll_reqs; +} etp_pool; + +static volatile unsigned int nreqs, nready, npending; +static volatile unsigned int max_idle = 4; + +static mutex_t etplock = X_MUTEX_INIT; +static mutex_t reslock = X_MUTEX_INIT; +static mutex_t reqlock = X_MUTEX_INIT; +static cond_t reqwait = X_COND_INIT; -typedef struct worker { - /* locked by wrklock */ +typedef struct worker +{ + /* locked by etplock */ struct worker *prev, *next; thread_t tid; - /* locked by reslock, reqlock or wrklock */ + /* locked by reslock, reqlock or etplock */ eio_req *req; /* currently processed request */ void *dbuf; DIR *dirp; } worker; -static worker wrk_first = { &wrk_first, &wrk_first, 0 }; +static worker wrk_first = { &wrk_first, &wrk_first, 0 }; /* NOT etp */ + +/* worker threads management */ static void worker_clear (worker *wrk) { @@ -139,13 +149,6 @@ free (wrk); } -static volatile unsigned int nreqs, nready, npending; -static volatile unsigned int max_idle = 4; - -static mutex_t reslock = X_MUTEX_INIT; -static mutex_t reqlock = X_MUTEX_INIT; -static cond_t reqwait = X_COND_INIT; - unsigned int eio_nreqs (void) { return nreqs; @@ -238,6 +241,85 @@ abort (); } +static void etp_atfork_prepare (void) +{ + X_LOCK (etplock); + X_LOCK (reqlock); + X_LOCK (reslock); +#if !HAVE_PREADWRITE + X_LOCK (preadwritelock); +#endif +#if !HAVE_READDIR_R + X_LOCK (readdirlock); +#endif +} + +static void etp_atfork_parent (void) +{ +#if !HAVE_READDIR_R + X_UNLOCK (readdirlock); +#endif +#if !HAVE_PREADWRITE + X_UNLOCK (preadwritelock); +#endif + X_UNLOCK (reslock); + X_UNLOCK (reqlock); + X_UNLOCK (etplock); +} + +static void etp_atfork_child (void) +{ + eio_req *prv; + + while (prv = reqq_shift (&req_queue)) + eio_destroy (prv); + + while (prv = reqq_shift (&res_queue)) + eio_destroy (prv); + + while (wrk_first.next != &wrk_first) + { + worker *wrk = wrk_first.next; + + if (wrk->req) + eio_destroy (wrk->req); + + worker_clear (wrk); + worker_free (wrk); + } + + started = 0; + idle = 0; + nreqs = 0; + nready = 0; + npending = 0; + + etp_atfork_parent (); +} + +static void +etp_once_init (void) +{ + X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); +} + +static int +etp_init (etp_pool *etp, void (*want_poll)(void), void (*done_poll)(void)) +{ + static pthread_once_t doinit = PTHREAD_ONCE_INIT; + + pthread_once (&doinit, etp_once_init); + + memset (etp, 0, sizeof *etp); + + etp->want_poll_cb = want_poll; + etp->done_poll_cb = done_poll; +} + +static etp_pool etp; + +/*****************************************************************************/ + static void grp_try_feed (eio_req *grp) { while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) @@ -314,9 +396,9 @@ void eio_cancel (eio_req *req) { - X_LOCK (wrklock); + X_LOCK (etplock); req->flags |= EIO_FLAG_CANCELLED; - X_UNLOCK (wrklock); + X_UNLOCK (etplock); eio_grp_cancel (req); } @@ -327,10 +409,10 @@ { worker *wrk = calloc (1, sizeof (worker)); - if (!wrk) - croak ("unable to allocate worker thread data"); + /*TODO*/ + assert (("unable to allocate worker thread data", wrk)); - X_LOCK (wrklock); + X_LOCK (etplock); if (thread_create (&wrk->tid, eio_proc, (void *)wrk)) { @@ -343,7 +425,7 @@ else free (wrk); - X_UNLOCK (wrklock); + X_UNLOCK (etplock); } static void maybe_start_thread (void) @@ -360,6 +442,11 @@ void eio_submit (eio_req *req) { + req->pri += EIO_PRI_BIAS; + + if (req->pri < EIO_PRI_MIN + EIO_PRI_BIAS) req->pri = EIO_PRI_MIN + EIO_PRI_BIAS; + if (req->pri > EIO_PRI_MAX + EIO_PRI_BIAS) req->pri = EIO_PRI_MAX + EIO_PRI_BIAS; + ++nreqs; X_LOCK (reqlock); @@ -383,22 +470,22 @@ X_COND_SIGNAL (reqwait); X_UNLOCK (reqlock); - X_LOCK (wrklock); + X_LOCK (etplock); --started; - X_UNLOCK (wrklock); + X_UNLOCK (etplock); } void eio_set_max_poll_time (double nseconds) { if (WORDACCESS_UNSAFE) X_LOCK (reqlock); - max_poll_time = nseconds; + etp.max_poll_time = nseconds; if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); } void eio_set_max_poll_reqs (unsigned int maxreqs) { if (WORDACCESS_UNSAFE) X_LOCK (reqlock); - max_poll_reqs = maxreqs; + etp.max_poll_reqs = maxreqs; if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); } @@ -426,11 +513,11 @@ int eio_poll (void) { - int maxreqs = max_poll_reqs; + int maxreqs = etp.max_poll_reqs; struct timeval tv_start, tv_now; eio_req *req; - if (max_poll_time) + if (etp.max_poll_time) gettimeofday (&tv_start, 0); for (;;) @@ -444,8 +531,8 @@ { --npending; - if (!res_queue.size && done_poll_cb) - done_poll_cb (); + if (!res_queue.size && etp.done_poll_cb) + etp.done_poll_cb (); } X_UNLOCK (reslock); @@ -470,11 +557,11 @@ if (maxreqs && !--maxreqs) break; - if (max_poll_time) + if (etp.max_poll_time) { gettimeofday (&tv_now, 0); - if (tvdiff (&tv_start, &tv_now) >= max_poll_time) + if (tvdiff (&tv_start, &tv_now) >= etp.max_poll_time) break; } } @@ -487,8 +574,8 @@ /* work around various missing functions */ #if !HAVE_PREADWRITE -# define pread aio_pread -# define pwrite aio_pwrite +# define pread eio__pread +# define pwrite eio__pwrite /* * make our pread/pwrite safe against themselves, but not against @@ -497,7 +584,8 @@ */ static mutex_t preadwritelock = X_MUTEX_INIT; -static ssize_t pread (int fd, void *buf, size_t count, off_t offset) +static ssize_t +eio__pread (int fd, void *buf, size_t count, off_t offset) { ssize_t res; off_t ooffset; @@ -512,7 +600,8 @@ return res; } -static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) +static ssize_t +eio__pwrite (int fd, void *buf, size_t count, off_t offset) { ssize_t res; off_t ooffset; @@ -530,10 +619,11 @@ #ifndef HAVE_FUTIMES -# define utimes(path,times) aio_utimes (path, times) -# define futimes(fd,times) aio_futimes (fd, times) +# define utimes(path,times) eio__utimes (path, times) +# define futimes(fd,times) eio__futimes (fd, times) -static int aio_utimes (const char *filename, const struct timeval times[2]) +static int +eio__utimes (const char *filename, const struct timeval times[2]) { if (times) { @@ -548,7 +638,7 @@ return utime (filename, 0); } -static int aio_futimes (int fd, const struct timeval tv[2]) +static int eio__futimes (int fd, const struct timeval tv[2]) { errno = ENOSYS; return -1; @@ -561,9 +651,10 @@ #endif #if !HAVE_READAHEAD -# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) +# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) -static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) +static ssize_t +eio__readahead (int fd, off_t offset, size_t count, worker *self) { size_t todo = count; dBUF; @@ -584,11 +675,12 @@ #endif #if !HAVE_READDIR_R -# define readdir_r aio_readdir_r +# define readdir_r eio__readdir_r static mutex_t readdirlock = X_MUTEX_INIT; -static int readdir_r (DIR *dirp, EIO_STRUCT_DIRENT *ent, EIO_STRUCT_DIRENT **res) +static int +eio__readdir_r (DIR *dirp, EIO_STRUCT_DIRENT *ent, EIO_STRUCT_DIRENT **res) { EIO_STRUCT_DIRENT *e; int errorno; @@ -614,7 +706,8 @@ #endif /* sendfile always needs emulation */ -static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self) +static ssize_t +eio__sendfile (int ofd, int ifd, off_t offset, size_t count, worker *self) { ssize_t res; @@ -707,7 +800,8 @@ } /* read a full directory */ -static void scandir_ (eio_req *req, worker *self) +static void +eio__scandir (eio_req *req, worker *self) { DIR *dirp; union @@ -721,12 +815,12 @@ int memofs = 0; int res = 0; - X_LOCK (wrklock); + X_LOCK (etplock); self->dirp = dirp = opendir (req->ptr1); self->dbuf = u = malloc (sizeof (*u)); req->flags |= EIO_FLAG_PTR2_FREE; req->ptr2 = names = malloc (memlen); - X_UNLOCK (wrklock); + X_UNLOCK (etplock); if (dirp && u && names) for (;;) @@ -748,9 +842,9 @@ while (memofs + len > memlen) { memlen *= 2; - X_LOCK (wrklock); + X_LOCK (etplock); req->ptr2 = names = realloc (names, memlen); - X_UNLOCK (wrklock); + X_UNLOCK (etplock); if (!names) break; @@ -770,15 +864,18 @@ /*****************************************************************************/ #define ALLOC(len) \ - X_LOCK (wrklock); \ - req->flags |= EIO_FLAG_PTR2_FREE; \ - X_UNLOCK (wrklock); \ - req->ptr2 = malloc (len); \ if (!req->ptr2) \ { \ - errno = ENOMEM; \ - req->result = -1; \ - break; \ + X_LOCK (etplock); \ + req->flags |= EIO_FLAG_PTR2_FREE; \ + X_UNLOCK (etplock); \ + req->ptr2 = malloc (len); \ + if (!req->ptr2) \ + { \ + errno = ENOMEM; \ + req->result = -1; \ + break; \ + } \ } X_THREAD_PROC (eio_proc) @@ -811,9 +908,9 @@ { --idle; X_UNLOCK (reqlock); - X_LOCK (wrklock); + X_LOCK (etplock); --started; - X_UNLOCK (wrklock); + X_UNLOCK (etplock); goto quit; } @@ -834,15 +931,16 @@ if (!EIO_CANCELLED (req)) switch (req->type) { - case EIO_READ: req->result = req->offs >= 0 + case EIO_READ: ALLOC (req->size); + req->result = req->offs >= 0 ? pread (req->int1, req->ptr2, req->size, req->offs) : read (req->int1, req->ptr2, req->size); break; case EIO_WRITE: req->result = req->offs >= 0 ? pwrite (req->int1, req->ptr2, req->size, req->offs) : write (req->int1, req->ptr2, req->size); break; - case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; - case EIO_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break; + case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; + case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size, self); break; case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); req->result = stat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; @@ -868,6 +966,7 @@ case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; + case EIO_READLINK: ALLOC (NAME_MAX); req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; @@ -875,7 +974,7 @@ case EIO_FSYNC: req->result = fsync (req->int1); break; case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; - case EIO_READDIR: scandir_ (req, self); break; + case EIO_READDIR: eio__scandir (req, self); break; case EIO_BUSY: #ifdef _WIN32 @@ -918,6 +1017,7 @@ case EIO_GROUP: case EIO_NOP: + req->result = 0; break; case EIO_QUIT: @@ -934,8 +1034,8 @@ ++npending; - if (!reqq_push (&res_queue, req) && want_poll_cb) - want_poll_cb (); + if (!reqq_push (&res_queue, req) && etp.want_poll_cb) + etp.want_poll_cb (); self->req = 0; worker_clear (self); @@ -944,88 +1044,18 @@ } quit: - X_LOCK (wrklock); + X_LOCK (etplock); worker_free (self); - X_UNLOCK (wrklock); + X_UNLOCK (etplock); return 0; } /*****************************************************************************/ -static void eio_atfork_prepare (void) -{ - X_LOCK (wrklock); - X_LOCK (reqlock); - X_LOCK (reslock); -#if !HAVE_PREADWRITE - X_LOCK (preadwritelock); -#endif -#if !HAVE_READDIR_R - X_LOCK (readdirlock); -#endif -} - -static void eio_atfork_parent (void) -{ -#if !HAVE_READDIR_R - X_UNLOCK (readdirlock); -#endif -#if !HAVE_PREADWRITE - X_UNLOCK (preadwritelock); -#endif - X_UNLOCK (reslock); - X_UNLOCK (reqlock); - X_UNLOCK (wrklock); -} - -static void eio_atfork_child (void) -{ - eio_req *prv; - - while (prv = reqq_shift (&req_queue)) - eio_destroy (prv); - - while (prv = reqq_shift (&res_queue)) - eio_destroy (prv); - - while (wrk_first.next != &wrk_first) - { - worker *wrk = wrk_first.next; - - if (wrk->req) - eio_destroy (wrk->req); - - worker_clear (wrk); - worker_free (wrk); - } - - started = 0; - idle = 0; - nreqs = 0; - nready = 0; - npending = 0; - - eio_atfork_parent (); -} - int eio_init (void (*want_poll)(void), void (*done_poll)(void)) { - want_poll_cb = want_poll; - done_poll_cb = done_poll; - -#ifdef _WIN32 - X_MUTEX_CHECK (wrklock); - X_MUTEX_CHECK (reslock); - X_MUTEX_CHECK (reqlock); - X_MUTEX_CHECK (reqwait); - X_MUTEX_CHECK (preadwritelock); - X_MUTEX_CHECK (readdirlock); - - X_COND_CHECK (reqwait); -#endif - - X_THREAD_ATFORK (eio_atfork_prepare, eio_atfork_parent, eio_atfork_child); + etp_init (&etp, want_poll, done_poll); } static void eio_api_destroy (eio_req *req) @@ -1040,528 +1070,222 @@ if (!req) \ return 0; \ \ - req->type = EIO_ ## rtype; \ - req->pri = EIO_DEFAULT_PRI + EIO_PRI_BIAS; \ - req->finish = cb; \ + req->type = rtype; \ + req->pri = pri; \ + req->finish = cb; \ + req->data = data; \ req->destroy = eio_api_destroy; #define SEND eio_submit (req); return req -#define PATH (void)0 - -eio_req *eio_fsync (int fd, eio_cb cb) -{ - REQ (FSYNC); req->int1 = fd; SEND; -} - -eio_req *eio_fdatasync (int fd, eio_cb cb) -{ - REQ (FDATASYNC); req->int1 = fd; SEND; -} - -eio_req *eio_readahead (int fd, off_t offset, size_t length, eio_cb cb) -{ - REQ (READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; -} - -eio_req *eio_read (int fd, off_t offset, size_t length, void *data, eio_cb cb) -{ - REQ (READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = data; SEND; -} - -eio_req *eio_write (int fd, off_t offset, size_t length, void *data, eio_cb cb) -{ - REQ (WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = data; SEND; -} - -eio_req *eio_fstat (int fd, eio_cb cb) -{ - REQ (FSTAT); req->int1 = fd; SEND; -} - -eio_req *eio_futime (int fd, double atime, double mtime, eio_cb cb) -{ - REQ (FUTIME); req->int1 = fd; req->nv1 = atime; req->nv2 = mtime; SEND; -} +#define PATH \ + req->flags |= EIO_FLAG_PTR1_FREE; \ + req->ptr1 = strdup (path); \ + if (!req->ptr1) \ + { \ + eio_api_destroy (req); \ + return 0; \ + } -eio_req *eio_ftruncate (int fd, off_t offset, eio_cb cb) -{ - REQ (FTRUNCATE); req->int1 = fd; req->offs = offset; SEND; -} +#ifndef EIO_NO_WRAPPERS -eio_req *eio_fchmod (int fd, mode_t mode, eio_cb cb) +eio_req *eio_nop (int pri, eio_cb cb, void *data) { - REQ (FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; + REQ (EIO_NOP); SEND; } -eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, eio_cb cb) +eio_req *eio_busy (double delay, int pri, eio_cb cb, void *data) { - REQ (FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; + REQ (EIO_BUSY); req->nv1 = delay; SEND; } -eio_req *eio_dup2 (int fd, int fd2, eio_cb cb) +eio_req *eio_sync (int pri, eio_cb cb, void *data) { - REQ (DUP2); req->int1 = fd; req->int2 = fd2; SEND; + REQ (EIO_SYNC); SEND; } -eio_req *eio_sendfile (int out_fd, int in_fd, off_t in_offset, size_t length, eio_cb cb) +eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data) { - REQ (SENDFILE); req->int1 = out_fd; req->int2 = in_fd; req->offs = in_offset; req->size = length; SEND; + REQ (EIO_FSYNC); req->int1 = fd; SEND; } -eio_req *eio_open (const char *path, int flags, mode_t mode, eio_cb cb) +eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) { - REQ (OPEN); PATH; req->int1 = flags; req->int2 = (long)mode; SEND; + REQ (EIO_FDATASYNC); req->int1 = fd; SEND; } -eio_req *eio_readlink (const char *path, eio_cb cb) +eio_req *eio_close (int fd, int pri, eio_cb cb, void *data) { - REQ (READLINK); PATH; SEND; + REQ (EIO_CLOSE); req->int1 = fd; SEND; } -eio_req *eio_stat (const char *path, eio_cb cb) +eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) { - REQ (STAT); PATH; SEND; + REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; } -eio_req *eio_lstat (const char *path, eio_cb cb) +eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) { - REQ (LSTAT); PATH; SEND; + REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; } -eio_req *eio_utime (const char *path, double atime, double mtime, eio_cb cb) +eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) { - REQ (UTIME); PATH; req->nv1 = atime; req->nv2 = mtime; SEND; + REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; } -eio_req *eio_truncate (const char *path, off_t offset, eio_cb cb) +eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) { - REQ (TRUNCATE); PATH; req->offs = offset; SEND; + REQ (EIO_FSTAT); req->int1 = fd; SEND; } -eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, eio_cb cb) +eio_req *eio_futime (int fd, double atime, double mtime, int pri, eio_cb cb, void *data) { - REQ (CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; + REQ (EIO_FUTIME); req->int1 = fd; req->nv1 = atime; req->nv2 = mtime; SEND; } -eio_req *eio_chmod (const char *path, mode_t mode, eio_cb cb) +eio_req *eio_ftruncate (int fd, off_t offset, int pri, eio_cb cb, void *data) { - REQ (CHMOD); PATH; req->int2 = (long)mode; SEND; + REQ (EIO_FTRUNCATE); req->int1 = fd; req->offs = offset; SEND; } -eio_req *eio_mkdir (const char *path, mode_t mode, eio_cb cb) +eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data) { - REQ (MKDIR); PATH; req->int2 = (long)mode; SEND; + REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; } -eio_req *eio_unlink (const char *path, eio_cb cb) +eio_req *eio_fchown (int fd, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) { - REQ (UNLINK); PATH; SEND; + REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; } -eio_req *eio_rmdir (const char *path, eio_cb cb) +eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data) { - REQ (RMDIR); PATH; SEND; + REQ (EIO_DUP2); req->int1 = fd; req->int2 = fd2; SEND; } -eio_req *eio_readdir (const char *path, eio_cb cb) +eio_req *eio_sendfile (int out_fd, int in_fd, off_t in_offset, size_t length, int pri, eio_cb cb, void *data) { - REQ (READDIR); PATH; SEND; + REQ (EIO_SENDFILE); req->int1 = out_fd; req->int2 = in_fd; req->offs = in_offset; req->size = length; SEND; } -eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, eio_cb cb) +eio_req *eio_open (const char *path, int flags, mode_t mode, int pri, eio_cb cb, void *data) { - REQ (MKNOD); PATH; req->int2 = (long)mode; req->int2 = (long)dev; SEND; + REQ (EIO_OPEN); PATH; req->int1 = flags; req->int2 = (long)mode; SEND; } -eio_req *eio_busy (double delay, eio_cb cb) +eio_req *eio_utime (const char *path, double atime, double mtime, int pri, eio_cb cb, void *data) { - REQ (BUSY); req->nv1 = delay; SEND; + REQ (EIO_UTIME); PATH; req->nv1 = atime; req->nv2 = mtime; SEND; } -eio_req *eio_nop (eio_cb cb) +eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data) { - REQ (NOP); SEND; + REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND; } -#undef REQ -#undef PATH -#undef SEND - -#if 0 - -void -aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$;$ - PPCODE: +eio_req *eio_chown (const char *path, uid_t uid, gid_t gid, int pri, eio_cb cb, void *data) { - dREQ; - - req->type = EIO_OPEN; - req->sv1 = newSVsv (pathname); - req->ptr1 = SvPVbyte_nolen (req->sv1); - req->int1 = flags; - req->int2 = mode; - - EIO_SEND; + REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; } -void -aio_fsync (SV *fh, SV *callback=&PL_sv_undef) - PROTOTYPE: $;$ - ALIAS: - aio_fsync = EIO_FSYNC - aio_fdatasync = EIO_FDATASYNC - PPCODE: +eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data) { - dREQ; - - req->type = ix; - req->sv1 = newSVsv (fh); - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); - - EIO_SEND (req); + REQ (EIO_CHMOD); PATH; req->int2 = (long)mode; SEND; } -void -aio_close (SV *fh, SV *callback=&PL_sv_undef) - PROTOTYPE: $;$ - PPCODE: +eio_req *eio_mkdir (const char *path, mode_t mode, int pri, eio_cb cb, void *data) { - dREQ; - - req->type = EIO_CLOSE; - req->sv1 = newSVsv (fh); - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); - - EIO_SEND (req); + REQ (EIO_MKDIR); PATH; req->int2 = (long)mode; SEND; } -void -aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) - ALIAS: - aio_read = EIO_READ - aio_write = EIO_WRITE - PROTOTYPE: $$$$$;$ - PPCODE: +static eio_req * +eio__1path (int type, const char *path, int pri, eio_cb cb, void *data) { - STRLEN svlen; - char *svptr = SvPVbyte (data, svlen); - UV len = SvUV (length); - - SvUPGRADE (data, SVt_PV); - SvPOK_on (data); - - if (dataoffset < 0) - dataoffset += svlen; - - if (dataoffset < 0 || dataoffset > svlen) - croak ("dataoffset outside of data scalar"); - - if (ix == EIO_WRITE) - { - /* write: check length and adjust. */ - if (!SvOK (length) || len + dataoffset > svlen) - len = svlen - dataoffset; - } - else - { - /* read: grow scalar as necessary */ - svptr = SvGROW (data, len + dataoffset + 1); - } - - if (len < 0) - croak ("length must not be negative"); - - { - dREQ; - - req->type = ix; - req->sv1 = newSVsv (fh); - req->int1 = PerlIO_fileno (ix == EIO_READ ? IoIFP (sv_2io (fh)) - : IoOFP (sv_2io (fh))); - req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; - req->size = len; - req->sv2 = SvREFCNT_inc (data); - req->ptr2 = (char *)svptr + dataoffset; - req->stroffset = dataoffset; - - if (!SvREADONLY (data)) - { - SvREADONLY_on (data); - req->flags |= FLAG_SV2_RO_OFF; - } - - EIO_SEND; - } + REQ (type); PATH; SEND; } -void -aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) - PROTOTYPE: $$;$ - PPCODE: +eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) { - SV *data; - dREQ; - - data = newSV (NAME_MAX); - SvPOK_on (data); - - req->type = EIO_READLINK; - req->sv1 = newSVsv (path); - req->ptr1 = SvPVbyte_nolen (req->sv1); - req->sv2 = data; - req->ptr2 = SvPVbyte_nolen (data); - - EIO_SEND; + return eio__1path (EIO_READLINK, path, pri, cb, data); } -void -aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$$;$ - PPCODE: +eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) { - dREQ; - - req->type = EIO_SENDFILE; - req->sv1 = newSVsv (out_fh); - req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); - req->sv2 = newSVsv (in_fh); - req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); - req->offs = SvVAL64 (in_offset); - req->size = length; - - EIO_SEND; + return eio__1path (EIO_STAT, path, pri, cb, data); } -void -aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) - PROTOTYPE: $$$;$ - PPCODE: +eio_req *eio_lstat (const char *path, int pri, eio_cb cb, void *data) { - dREQ; - - req->type = EIO_READAHEAD; - req->sv1 = newSVsv (fh); - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); - req->offs = SvVAL64 (offset); - req->size = length; - - EIO_SEND; + return eio__1path (EIO_LSTAT, path, pri, cb, data); } -void -aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) - ALIAS: - aio_stat = EIO_STAT - aio_lstat = EIO_LSTAT - PPCODE: +eio_req *eio_unlink (const char *path, int pri, eio_cb cb, void *data) { - dREQ; - - req->ptr2 = malloc (sizeof (EIO_STRUCT_STAT)); - if (!req->ptr2) - { - req_destroy (req); - croak ("out of memory during aio_stat statdata allocation"); - } - - req->flags |= FLAG_PTR2_FREE; - req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (fh_or_path)) - { - req->type = ix; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FSTAT; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } - - EIO_SEND; + return eio__1path (EIO_UNLINK, path, pri, cb, data); } -void -aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) - PPCODE: +eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data) { - dREQ; - - req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; - req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; - req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (fh_or_path)) - { - req->type = EIO_UTIME; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FUTIME; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } - - EIO_SEND; + return eio__1path (EIO_RMDIR, path, pri, cb, data); } -void -aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) - PPCODE: +eio_req *eio_readdir (const char *path, int pri, eio_cb cb, void *data) { - dREQ; - - req->sv1 = newSVsv (fh_or_path); - req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; - - if (SvPOK (fh_or_path)) - { - req->type = EIO_TRUNCATE; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FTRUNCATE; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } - - EIO_SEND; + return eio__1path (EIO_READDIR, path, pri, cb, data); } -void -aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) - ALIAS: - aio_chmod = EIO_CHMOD - aio_fchmod = EIO_FCHMOD - aio_mkdir = EIO_MKDIR - PPCODE: +eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) { - dREQ; - - req->type = type; - req->int2 = mode; - req->sv1 = newSVsv (fh_or_path); - - if (ix == EIO_FCHMOD) - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - else - req->ptr1 = SvPVbyte_nolen (req->sv1); - - EIO_SEND; + REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int2 = (long)dev; SEND; } -void -aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef) - PPCODE: +static eio_req * +eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) { - dREQ; + REQ (type); PATH; - req->int2 = SvOK (uid) ? SvIV (uid) : -1; - req->int3 = SvOK (gid) ? SvIV (gid) : -1; - req->sv1 = newSVsv (fh_or_path); - - if (SvPOK (fh_or_path)) - { - req->type = EIO_CHOWN; - req->ptr1 = SvPVbyte_nolen (req->sv1); - } - else - { - req->type = EIO_FCHOWN; - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); - } + req->flags |= EIO_FLAG_PTR2_FREE; + req->ptr2 = strdup (new_path); + if (!req->ptr2) + { + eio_api_destroy (req); + return 0; + } - EIO_SEND; + SEND; } -void -aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) - ALIAS: - aio_unlink = EIO_UNLINK - aio_rmdir = EIO_RMDIR - aio_readdir = EIO_READDIR - PPCODE: +eio_req *eio_link (const char *path, const char *new_path, int pri, eio_cb cb, void *data) { - dREQ; - - req->type = ix; - req->sv1 = newSVsv (pathname); - req->ptr1 = SvPVbyte_nolen (req->sv1); - - EIO_SEND; + return eio__2path (EIO_LINK, path, new_path, pri, cb, data); } -void -aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef) - ALIAS: - aio_link = EIO_LINK - aio_symlink = EIO_SYMLINK - aio_rename = EIO_RENAME - PPCODE: +eio_req *eio_symlink (const char *path, const char *new_path, int pri, eio_cb cb, void *data) { - dREQ; - - req->type = ix; - req->sv1 = newSVsv (oldpath); - req->ptr1 = SvPVbyte_nolen (req->sv1); - req->sv2 = newSVsv (newpath); - req->ptr2 = SvPVbyte_nolen (req->sv2); - - EIO_SEND; + return eio__2path (EIO_SYMLINK, path, new_path, pri, cb, data); } -void -aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef) - PPCODE: +eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) { - dREQ; - - req->type = EIO_MKNOD; - req->sv1 = newSVsv (pathname); - req->ptr1 = SvPVbyte_nolen (req->sv1); - req->int2 = (mode_t)mode; - req->offs = dev; - - EIO_SEND; + return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); } -void -aio_busy (double delay, SV *callback=&PL_sv_undef) - PPCODE: -{ - dREQ; - - req->type = EIO_BUSY; - req->nv1 = delay < 0. ? 0. : delay; - - EIO_SEND; -} +#endif -void -aio_group (SV *callback=&PL_sv_undef) - PROTOTYPE: ;$ - PPCODE: +eio_req *eio_grp (eio_cb cb, void *data) { - dREQ; + const int pri = EIO_PRI_MAX; - req->type = EIO_GROUP; - - req_send (req); - XPUSHs (req_sv (req, AIO_GRP_KLASS)); + REQ (EIO_GROUP); SEND; } -void -aio_nop (SV *callback=&PL_sv_undef) - ALIAS: - aio_nop = EIO_NOP - aio_sync = EIO_SYNC - PPCODE: -{ - dREQ; +#undef REQ +#undef PATH +#undef SEND -#endif +/*****************************************************************************/ +/* grp functions */ void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) { @@ -1594,4 +1318,18 @@ grp->grp_first = req; } +/*****************************************************************************/ +/* misc garbage */ + +ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) +{ + worker wrk; + + wrk.dbuf = 0; + + eio__sendfile (ofd, ifd, offset, count, &wrk); + + if (wrk.dbuf) + free (wrk.dbuf); +}