--- libeio/eio.c 2008/05/17 12:17:25 1.14 +++ libeio/eio.c 2010/01/02 14:24:32 1.49 @@ -1,7 +1,7 @@ /* * libeio implementation * - * Copyright (c) 2007,2008 Marc Alexander Lehmann + * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann * All rights reserved. * * Redistribution and use in source and binary forms, with or without modifica- @@ -38,6 +38,10 @@ */ #include "eio.h" + +#ifdef EIO_STACKSIZE +# define XTHREAD_STACKSIZE EIO_STACKSIZE +#endif #include "xthread.h" #include @@ -66,17 +70,35 @@ #ifdef _WIN32 /*doh*/ - #else # include "config.h" # include # include +# include # include # include # include # include +/* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ +# if __freebsd || defined __NetBSD__ || defined __OpenBSD__ +# define _DIRENT_HAVE_D_TYPE /* sigh */ +# define D_INO(de) (de)->d_fileno +# define D_NAMLEN(de) (de)->d_namlen +# elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 +# define D_INO(de) (de)->d_ino +# endif + +#ifdef _D_EXACT_NAMLEN +# undef D_NAMLEN +# define D_NAMLEN(de) _D_EXACT_NAMLEN (de) +#endif + +# ifdef _DIRENT_HAVE_D_TYPE +# define D_TYPE(de) (de)->d_type +# endif + # ifndef EIO_STRUCT_DIRENT # define EIO_STRUCT_DIRENT struct dirent # endif @@ -86,18 +108,28 @@ #if HAVE_SENDFILE # if __linux # include -# elif __freebsd +# elif __freebsd || defined __APPLE__ # include # include # elif __hpux # include -# elif __solaris /* not yet */ +# elif __solaris # include # else # error sendfile support requested but not available # endif #endif +#ifndef D_TYPE +# define D_TYPE(de) 0 +#endif +#ifndef D_INO +# define D_INO(de) 0 +#endif +#ifndef D_NAMLEN +# define D_NAMLEN(de) strlen ((de)->d_name) +#endif + /* number of seconds after which an idle threads exit */ #define IDLE_TIMEOUT 10 @@ -122,13 +154,28 @@ /*****************************************************************************/ +#if __GNUC__ >= 3 +# define expect(expr,value) __builtin_expect ((expr),(value)) +#else +# define expect(expr,value) (expr) +#endif + +#define expect_false(expr) expect ((expr) != 0, 0) +#define expect_true(expr) expect ((expr) != 0, 1) + +/*****************************************************************************/ + #define ETP_PRI_MIN EIO_PRI_MIN #define ETP_PRI_MAX EIO_PRI_MAX +struct etp_worker; + #define ETP_REQ eio_req #define ETP_DESTROY(req) eio_destroy (req) static int eio_finish (eio_req *req); #define ETP_FINISH(req) eio_finish (req) +static void eio_execute (struct etp_worker *self, eio_req *req); +#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) #define ETP_WORKER_CLEAR(req) \ if (wrk->dbuf) \ @@ -142,6 +189,7 @@ closedir (wrk->dirp); \ wrk->dirp = 0; \ } + #define ETP_WORKER_COMMON \ void *dbuf; \ DIR *dirp; @@ -150,7 +198,7 @@ #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) -/* calculcate time difference in ~1/EIO_TICKS of a second */ +/* calculate time difference in ~1/EIO_TICKS of a second */ static int tvdiff (struct timeval *tv1, struct timeval *tv2) { return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS @@ -175,6 +223,15 @@ static mutex_t reqlock = X_MUTEX_INIT; static cond_t reqwait = X_COND_INIT; +#if !HAVE_PREADWRITE +/* + * make our pread/pwrite emulation 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 mutex_t preadwritelock = X_MUTEX_INIT; +#endif + typedef struct etp_worker { /* locked by wrklock */ @@ -312,16 +369,10 @@ #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 @@ -334,10 +385,10 @@ { ETP_REQ *prv; - while (prv = reqq_shift (&req_queue)) + while ((prv = reqq_shift (&req_queue))) ETP_DESTROY (prv); - while (prv = reqq_shift (&res_queue)) + while ((prv = reqq_shift (&res_queue))) ETP_DESTROY (prv); while (wrk_first.next != &wrk_first) @@ -375,6 +426,8 @@ want_poll_cb = want_poll; done_poll_cb = done_poll; + + return 0; } X_THREAD_PROC (etp_proc); @@ -404,11 +457,11 @@ static void etp_maybe_start_thread (void) { - if (etp_nthreads () >= wanted) + if (expect_true (etp_nthreads () >= wanted)) return; /* todo: maybe use idle here, but might be less exact */ - if (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()) + if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) return; etp_start_thread (); @@ -471,7 +524,7 @@ --nreqs; X_UNLOCK (reqlock); - if (req->type == EIO_GROUP && req->size) + if (expect_false (req->type == EIO_GROUP && req->size)) { req->int1 = 1; /* mark request as delayed */ continue; @@ -479,11 +532,11 @@ else { int res = ETP_FINISH (req); - if (res) + if (expect_false (res)) return res; } - if (maxreqs && !--maxreqs) + if (expect_false (maxreqs && !--maxreqs)) break; if (maxtime) @@ -512,23 +565,42 @@ { req->pri -= ETP_PRI_MIN; - if (req->pri < ETP_PRI_MIN - ETP_PRI_MIN) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; - if (req->pri > ETP_PRI_MAX - ETP_PRI_MIN) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; + if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; + if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; - X_LOCK (reqlock); - ++nreqs; - ++nready; - reqq_push (&req_queue, req); - X_COND_SIGNAL (reqwait); - X_UNLOCK (reqlock); + if (expect_false (req->type == EIO_GROUP)) + { + /* I hope this is worth it :/ */ + X_LOCK (reqlock); + ++nreqs; + X_UNLOCK (reqlock); + + X_LOCK (reslock); + + ++npending; + + if (!reqq_push (&res_queue, req) && want_poll_cb) + want_poll_cb (); + + X_UNLOCK (reslock); + } + else + { + X_LOCK (reqlock); + ++nreqs; + ++nready; + reqq_push (&req_queue, req); + X_COND_SIGNAL (reqwait); + X_UNLOCK (reqlock); - etp_maybe_start_thread (); + etp_maybe_start_thread (); + } } static void etp_set_max_poll_time (double nseconds) { if (WORDACCESS_UNSAFE) X_LOCK (reslock); - max_poll_time = nseconds; + max_poll_time = nseconds * EIO_TICKS; if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); } @@ -567,12 +639,12 @@ { while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) { - int old_len = grp->size; + grp->flags &= ~EIO_FLAG_GROUPADD; EIO_FEED (grp); /* stop if no progress has been made */ - if (old_len == grp->size) + if (!(grp->flags & EIO_FLAG_GROUPADD)) { grp->feed = 0; break; @@ -699,16 +771,11 @@ /* work around various missing functions */ #if !HAVE_PREADWRITE +# undef pread +# undef pwrite # define pread eio__pread # define pwrite eio__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 mutex_t preadwritelock = X_MUTEX_INIT; - static ssize_t eio__pread (int fd, void *buf, size_t count, off_t offset) { @@ -735,7 +802,7 @@ ooffset = lseek (fd, 0, SEEK_CUR); lseek (fd, offset, SEEK_SET); res = write (fd, buf, count); - lseek (fd, offset, SEEK_SET); + lseek (fd, ooffset, SEEK_SET); X_UNLOCK (preadwritelock); return res; @@ -744,6 +811,8 @@ #ifndef HAVE_FUTIMES +# undef utimes +# undef futimes # define utimes(path,times) eio__utimes (path, times) # define futimes(fd,times) eio__futimes (fd, times) @@ -772,14 +841,44 @@ #endif #if !HAVE_FDATASYNC -# define fdatasync fsync +# undef fdatasync +# define fdatasync(fd) fsync (fd) #endif +/* sync_file_range always needs emulation */ +int +eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) +{ +#if HAVE_SYNC_FILE_RANGE + int res; + + if (EIO_SYNC_FILE_RANGE_WAIT_BEFORE != SYNC_FILE_RANGE_WAIT_BEFORE + || EIO_SYNC_FILE_RANGE_WRITE != SYNC_FILE_RANGE_WRITE + || EIO_SYNC_FILE_RANGE_WAIT_AFTER != SYNC_FILE_RANGE_WAIT_AFTER) + { + flags = 0 + | (flags & EIO_SYNC_FILE_RANGE_WAIT_BEFORE ? SYNC_FILE_RANGE_WAIT_BEFORE : 0) + | (flags & EIO_SYNC_FILE_RANGE_WRITE ? SYNC_FILE_RANGE_WRITE : 0) + | (flags & EIO_SYNC_FILE_RANGE_WAIT_AFTER ? SYNC_FILE_RANGE_WAIT_AFTER : 0); + } + + res = sync_file_range (fd, offset, nbytes, flags); + + if (!res || errno != ENOSYS) + return res; +#endif + + /* even though we could play tricks with the flags, it's better to always + * call fdatasync, as that matches the expectation of its users best */ + return fdatasync (fd); +} + #if !HAVE_READAHEAD +# undef readahead # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) static ssize_t -eio__readahead (int fd, off_t offset, size_t count, worker *self) +eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) { size_t todo = count; dBUF; @@ -799,37 +898,6 @@ #endif -#if !HAVE_READDIR_R -# define readdir_r eio__readdir_r - -static mutex_t readdirlock = X_MUTEX_INIT; - -static int -eio__readdir_r (DIR *dirp, EIO_STRUCT_DIRENT *ent, EIO_STRUCT_DIRENT **res) -{ - EIO_STRUCT_DIRENT *e; - int errorno; - - X_LOCK (readdirlock); - - e = readdir (dirp); - errorno = errno; - - if (e) - { - *res = ent; - strcpy (ent->d_name, e->d_name); - } - else - *res = 0; - - X_UNLOCK (readdirlock); - - errno = errorno; - return e ? 0 : -1; -} -#endif - /* sendfile always needs emulation */ static ssize_t eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) @@ -857,6 +925,16 @@ res = sbytes; } +# elif defined (__APPLE__) + + { + off_t sbytes = count; + res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); + + if (res < 0 && errno == EAGAIN && sbytes) + res = sbytes; + } + # elif __hpux res = sendfile (ofd, ifd, offset, count, 0, 0); @@ -877,6 +955,16 @@ } # endif + +#elif defined (_WIN32) + + /* does not work, just for documentation of what would need to be done */ + { + HANDLE h = TO_SOCKET (ifd); + SetFilePointer (h, offset, 0, FILE_BEGIN); + res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); + } + #else res = -1; errno = ENOSYS; @@ -884,6 +972,11 @@ if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK + /* BSDs */ +#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ + || errno == ENOTSUP +#endif + || errno == EOPNOTSUPP /* BSDs */ #if __solaris || errno == EAFNOSUPPORT || errno == EPROTOTYPE #endif @@ -924,66 +1017,414 @@ return res; } +static signed char +eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) +{ + return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */ + : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; +} + +#define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 + +#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ +#define EIO_SORT_FAST 60 /* when to only use insertion sort */ + +static void +eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) +{ + unsigned char bits [9 + sizeof (ino_t) * 8]; + unsigned char *bit = bits; + + assert (CHAR_BIT == 8); + assert (sizeof (eio_dirent) * 8 < 256); + assert (offsetof (eio_dirent, inode)); /* we use 0 as sentinel */ + assert (offsetof (eio_dirent, score)); /* we use 0 as sentinel */ + + if (size <= EIO_SORT_FAST) + return; + + /* first prepare an array of bits to test in our radix sort */ + /* try to take endianness into account, as well as differences in ino_t sizes */ + /* inode_bits must contain all inodes ORed together */ + /* which is used to skip bits that are 0 everywhere, which is very common */ + { + ino_t endianness; + int i, j; + + /* we store the byte offset of byte n into byte n of "endianness" */ + for (i = 0; i < sizeof (ino_t); ++i) + ((unsigned char *)&endianness)[i] = i; + + *bit++ = 0; + + for (i = 0; i < sizeof (ino_t); ++i) + { + /* shifting off the byte offsets out of "endianness" */ + int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8; + endianness >>= 8; + + for (j = 0; j < 8; ++j) + if (inode_bits & (((ino_t)1) << (i * 8 + j))) + *bit++ = offs + j; + } + + for (j = 0; j < 8; ++j) + if (score_bits & (1 << j)) + *bit++ = offsetof (eio_dirent, score) * 8 + j; + } + + /* now actually do the sorting (a variant of MSD radix sort) */ + { + eio_dirent *base_stk [9 + sizeof (ino_t) * 8], *base; + eio_dirent *end_stk [9 + sizeof (ino_t) * 8], *end; + unsigned char *bit_stk [9 + sizeof (ino_t) * 8]; + int stk_idx = 0; + + base_stk [stk_idx] = dents; + end_stk [stk_idx] = dents + size; + bit_stk [stk_idx] = bit - 1; + + do + { + base = base_stk [stk_idx]; + end = end_stk [stk_idx]; + bit = bit_stk [stk_idx]; + + for (;;) + { + unsigned char O = *bit >> 3; + unsigned char M = 1 << (*bit & 7); + + eio_dirent *a = base; + eio_dirent *b = end; + + if (b - a < EIO_SORT_CUTOFF) + break; + + /* now bit-partition the array on the bit */ + /* this ugly asymmetric loop seems to perform much better than typical */ + /* partition algos found in the literature */ + do + if (!(((unsigned char *)a)[O] & M)) + ++a; + else if (!(((unsigned char *)--b)[O] & M)) + { + eio_dirent tmp = *a; *a = *b; *b = tmp; + ++a; + } + while (b > a); + + /* next bit, or stop, if no bits left in this path */ + if (!*--bit) + break; + + base_stk [stk_idx] = a; + end_stk [stk_idx] = end; + bit_stk [stk_idx] = bit; + ++stk_idx; + + end = a; + } + } + while (stk_idx--); + } +} + +static void +eio_dent_insertion_sort (eio_dirent *dents, int size) +{ + /* first move the smallest element to the front, to act as a sentinel */ + { + int i; + eio_dirent *min = dents; + + /* the radix pre-pass ensures that the minimum element is in the first EIO_SORT_CUTOFF + 1 elements */ + for (i = size > EIO_SORT_FAST ? EIO_SORT_CUTOFF + 1 : size; --i; ) + if (EIO_DENT_CMP (dents [i], <, *min)) + min = &dents [i]; + + /* swap elements 0 and j (minimum) */ + { + eio_dirent tmp = *dents; *dents = *min; *min = tmp; + } + } + + /* then do standard insertion sort, assuming that all elements are >= dents [0] */ + { + eio_dirent *i, *j; + + for (i = dents + 1; i < dents + size; ++i) + { + eio_dirent value = *i; + + for (j = i - 1; EIO_DENT_CMP (*j, >, value); --j) + j [1] = j [0]; + + j [1] = value; + } + } +} + +static void +eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, ino_t inode_bits) +{ + if (size <= 1) + return; /* our insertion sort relies on size > 0 */ + + /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */ + /* and stop sorting when the partitions are <= EIO_SORT_CUTOFF */ + eio_dent_radix_sort (dents, size, score_bits, inode_bits); + + /* use an insertion sort at the end, or for small arrays, */ + /* as insertion sort is more efficient for small partitions */ + eio_dent_insertion_sort (dents, size); +} + /* read a full directory */ static void eio__scandir (eio_req *req, etp_worker *self) { DIR *dirp; - union - { - EIO_STRUCT_DIRENT d; - char b [offsetof (EIO_STRUCT_DIRENT, d_name) + NAME_MAX + 1]; - } *u; EIO_STRUCT_DIRENT *entp; char *name, *names; - int memlen = 4096; - int memofs = 0; - int res = 0; + int namesalloc = 4096; + int namesoffs = 0; + int flags = req->int1; + eio_dirent *dents = 0; + int dentalloc = 128; + int dentoffs = 0; + ino_t inode_bits = 0; + + req->result = -1; + + if (!(flags & EIO_READDIR_DENTS)) + flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); X_LOCK (wrklock); + /* the corresponding closedir is in ETP_WORKER_CLEAR */ self->dirp = dirp = opendir (req->ptr1); - self->dbuf = u = malloc (sizeof (*u)); - req->flags |= EIO_FLAG_PTR2_FREE; - req->ptr2 = names = malloc (memlen); + req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; + req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; + req->ptr2 = names = malloc (namesalloc); X_UNLOCK (wrklock); - if (dirp && u && names) + if (dirp && names && (!flags || dents)) for (;;) { errno = 0; - readdir_r (dirp, &u->d, &entp); + entp = readdir (dirp); if (!entp) - break; + { + if (errno) + break; + + /* sort etc. */ + req->int1 = flags; + req->result = dentoffs; + + if (flags & EIO_READDIR_STAT_ORDER) + eio_dent_sort (dents, dentoffs, 0, inode_bits); /* sort by inode exclusively */ + else if (flags & EIO_READDIR_DIRS_FIRST) + if (flags & EIO_READDIR_FOUND_UNKNOWN) + eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ + else + { + /* in this case, all is known, and we just put dirs first and sort them */ + eio_dirent *oth = dents + dentoffs; + eio_dirent *dir = dents; + + /* now partition dirs to the front, and non-dirs to the back */ + /* by walking from both sides and swapping if necessary */ + /* also clear score, so it doesn't influence sorting */ + while (oth > dir) + { + if (dir->type == EIO_DT_DIR) + ++dir; + else if ((--oth)->type == EIO_DT_DIR) + { + eio_dirent tmp = *dir; *dir = *oth; *oth = tmp; + + ++dir; + } + } + + /* now sort the dirs only */ + eio_dent_sort (dents, dir - dents, 0, inode_bits); + } + + break; + } + /* now add the entry to our list(s) */ name = entp->d_name; + /* skip . and .. entries */ if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) { - int len = strlen (name) + 1; + int len = D_NAMLEN (entp) + 1; - res++; - - while (memofs + len > memlen) + while (expect_false (namesoffs + len > namesalloc)) { - memlen *= 2; + namesalloc *= 2; X_LOCK (wrklock); - req->ptr2 = names = realloc (names, memlen); + req->ptr2 = names = realloc (names, namesalloc); X_UNLOCK (wrklock); if (!names) break; } - memcpy (names + memofs, name, len); - memofs += len; + memcpy (names + namesoffs, name, len); + + if (dents) + { + struct eio_dirent *ent; + + if (expect_false (dentoffs == dentalloc)) + { + dentalloc *= 2; + X_LOCK (wrklock); + req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); + X_UNLOCK (wrklock); + + if (!dents) + break; + } + + ent = dents + dentoffs; + + ent->nameofs = namesoffs; /* rather dirtily we store the offset in the pointer */ + ent->namelen = len - 1; + ent->inode = D_INO (entp); + + inode_bits |= ent->inode; + + switch (D_TYPE (entp)) + { + default: + ent->type = EIO_DT_UNKNOWN; + flags |= EIO_READDIR_FOUND_UNKNOWN; + break; + + #ifdef DT_FIFO + case DT_FIFO: ent->type = EIO_DT_FIFO; break; + #endif + #ifdef DT_CHR + case DT_CHR: ent->type = EIO_DT_CHR; break; + #endif + #ifdef DT_MPC + case DT_MPC: ent->type = EIO_DT_MPC; break; + #endif + #ifdef DT_DIR + case DT_DIR: ent->type = EIO_DT_DIR; break; + #endif + #ifdef DT_NAM + case DT_NAM: ent->type = EIO_DT_NAM; break; + #endif + #ifdef DT_BLK + case DT_BLK: ent->type = EIO_DT_BLK; break; + #endif + #ifdef DT_MPB + case DT_MPB: ent->type = EIO_DT_MPB; break; + #endif + #ifdef DT_REG + case DT_REG: ent->type = EIO_DT_REG; break; + #endif + #ifdef DT_NWK + case DT_NWK: ent->type = EIO_DT_NWK; break; + #endif + #ifdef DT_CMP + case DT_CMP: ent->type = EIO_DT_CMP; break; + #endif + #ifdef DT_LNK + case DT_LNK: ent->type = EIO_DT_LNK; break; + #endif + #ifdef DT_SOCK + case DT_SOCK: ent->type = EIO_DT_SOCK; break; + #endif + #ifdef DT_DOOR + case DT_DOOR: ent->type = EIO_DT_DOOR; break; + #endif + #ifdef DT_WHT + case DT_WHT: ent->type = EIO_DT_WHT; break; + #endif + } + + ent->score = 7; + + if (flags & EIO_READDIR_DIRS_FIRST) + { + if (ent->type == EIO_DT_UNKNOWN) + { + if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ + ent->score = 1; + else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ + ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ + } + else if (ent->type == EIO_DT_DIR) + ent->score = 0; + } + } + + namesoffs += len; + ++dentoffs; + } + + if (EIO_CANCELLED (req)) + { + errno = ECANCELED; + break; } } +} - if (errno) - res = -1; - - req->result = res; +#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) +# define eio__msync(a,b,c) ((errno = ENOSYS), -1) +#else + +int +eio__msync (void *mem, size_t len, int flags) +{ + if (EIO_MS_ASYNC != MS_SYNC + || EIO_MS_INVALIDATE != MS_INVALIDATE + || EIO_MS_SYNC != MS_SYNC) + { + flags = 0 + | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0) + | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0) + | (flags & EIO_MS_SYNC ? MS_SYNC : 0); + } + + return msync (mem, len, flags); +} + +#endif + +int +eio__mtouch (void *mem, size_t len, int flags) +{ + intptr_t addr = (intptr_t)mem; + intptr_t end = addr + len; +#ifdef PAGESIZE + const intptr_t page = PAGESIZE; +#else + static intptr_t page; + + if (!page) + page = sysconf (_SC_PAGESIZE); +#endif + + /* round down to start of page, although this is probably useless */ + addr &= ~(page - 1); /* assume page size is always a power of two */ + + if (addr < end) + if (flags & EIO_MT_MODIFY) /* modify */ + do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); + else + do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); + + return 0; } /*****************************************************************************/ @@ -1049,109 +1490,11 @@ X_UNLOCK (reqlock); - errno = 0; /* strictly unnecessary */ + if (req->type < 0) + goto quit; if (!EIO_CANCELLED (req)) - switch (req->type) - { - 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 = 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; - case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); - req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; - case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); - req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break; - - case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; - case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; - case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break; - case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; - case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; - case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; - - case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break; - case EIO_CLOSE: req->result = close (req->int1); break; - case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; - case EIO_UNLINK: req->result = unlink (req->ptr1); break; - case EIO_RMDIR: req->result = rmdir (req->ptr1); break; - case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; - case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; - 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; - - case EIO_SYNC: req->result = 0; sync (); break; - case EIO_FSYNC: req->result = fsync (req->int1); break; - case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; - - case EIO_READDIR: eio__scandir (req, self); break; - - case EIO_BUSY: -#ifdef _WIN32 - Sleep (req->nv1 * 1000.); -#else - { - struct timeval tv; - - tv.tv_sec = req->nv1; - tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; - - req->result = select (0, 0, 0, 0, &tv); - } -#endif - break; - - case EIO_UTIME: - case EIO_FUTIME: - { - struct timeval tv[2]; - struct timeval *times; - - if (req->nv1 != -1. || req->nv2 != -1.) - { - tv[0].tv_sec = req->nv1; - tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.; - tv[1].tv_sec = req->nv2; - tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.; - - times = tv; - } - else - times = 0; - - - req->result = req->type == EIO_FUTIME - ? futimes (req->int1, times) - : utimes (req->ptr1, times); - } - - case EIO_GROUP: - case EIO_NOP: - req->result = 0; - break; - - case -1: - goto quit; - - default: - req->result = -1; - break; - } - - req->errorno = errno; + ETP_EXECUTE (self, req); X_LOCK (reslock); @@ -1178,7 +1521,7 @@ int eio_init (void (*want_poll)(void), void (*done_poll)(void)) { - etp_init (want_poll, done_poll); + return etp_init (want_poll, done_poll); } static void eio_api_destroy (eio_req *req) @@ -1210,6 +1553,119 @@ return 0; \ } +static void eio_execute (etp_worker *self, eio_req *req) +{ + errno = 0; + + switch (req->type) + { + 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 = 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; + case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); + req->result = lstat (req->ptr1, (EIO_STRUCT_STAT *)req->ptr2); break; + case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); + req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break; + + case EIO_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; + case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; + case EIO_CHMOD: req->result = chmod (req->ptr1, (mode_t)req->int2); break; + case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; + case EIO_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; + case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; + + case EIO_OPEN: req->result = open (req->ptr1, req->int1, (mode_t)req->int2); break; + case EIO_CLOSE: req->result = close (req->int1); break; + case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; + case EIO_UNLINK: req->result = unlink (req->ptr1); break; + case EIO_RMDIR: req->result = rmdir (req->ptr1); break; + case EIO_MKDIR: req->result = mkdir (req->ptr1, (mode_t)req->int2); break; + case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; + 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->int3); break; + + case EIO_READLINK: ALLOC (NAME_MAX); + req->result = readlink (req->ptr1, req->ptr2, NAME_MAX); break; + + case EIO_SYNC: req->result = 0; sync (); break; + case EIO_FSYNC: req->result = fsync (req->int1); break; + case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; + case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; + case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; + case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; + + case EIO_READDIR: eio__scandir (req, self); break; + + case EIO_BUSY: +#ifdef _WIN32 + Sleep (req->nv1 * 1000.); +#else + { + struct timeval tv; + + tv.tv_sec = req->nv1; + tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; + + req->result = select (0, 0, 0, 0, &tv); + } +#endif + break; + + case EIO_UTIME: + case EIO_FUTIME: + { + struct timeval tv[2]; + struct timeval *times; + + if (req->nv1 != -1. || req->nv2 != -1.) + { + tv[0].tv_sec = req->nv1; + tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.; + tv[1].tv_sec = req->nv2; + tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.; + + times = tv; + } + else + times = 0; + + + req->result = req->type == EIO_FUTIME + ? futimes (req->int1, times) + : utimes (req->ptr1, times); + } + break; + + case EIO_GROUP: + abort (); /* handled in eio_request */ + + case EIO_NOP: + req->result = 0; + break; + + case EIO_CUSTOM: + ((void (*)(eio_req *))req->feed) (req); + break; + + default: + req->result = -1; + break; + } + + req->errorno = errno; +} + #ifndef EIO_NO_WRAPPERS eio_req *eio_nop (int pri, eio_cb cb, void *data) @@ -1232,6 +1688,21 @@ REQ (EIO_FSYNC); req->int1 = fd; SEND; } +eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; +} + +eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; +} + +eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; +} + eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) { REQ (EIO_FDATASYNC); req->int1 = fd; SEND; @@ -1353,14 +1824,14 @@ return eio__1path (EIO_RMDIR, path, pri, cb, data); } -eio_req *eio_readdir (const char *path, int pri, eio_cb cb, void *data) +eio_req *eio_readdir (const char *path, int flags, int pri, eio_cb cb, void *data) { - return eio__1path (EIO_READDIR, path, pri, cb, data); + REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; } eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) { - REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int2 = (long)dev; SEND; + REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->int3 = (long)dev; SEND; } static eio_req * @@ -1394,6 +1865,11 @@ return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); } +eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) +{ + REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; +} + #endif eio_req *eio_grp (eio_cb cb, void *data) @@ -1429,6 +1905,8 @@ { assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); + grp->flags |= EIO_FLAG_GROUPADD; + ++grp->size; req->grp = grp; @@ -1447,12 +1925,15 @@ ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) { etp_worker wrk; + ssize_t ret; wrk.dbuf = 0; - eio__sendfile (ofd, ifd, offset, count, &wrk); + ret = eio__sendfile (ofd, ifd, offset, count, &wrk); if (wrk.dbuf) free (wrk.dbuf); + + return ret; }