--- libeio/eio.c 2011/07/05 16:57:41 1.79 +++ libeio/eio.c 2011/07/14 18:30:10 1.86 @@ -56,17 +56,23 @@ #include #include #include -#include #include #include #include +#include /* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ -/* intptr_t only comes form stdint.h, says idiot openbsd coder */ +/* intptr_t only comes from stdint.h, says idiot openbsd coder */ #if HAVE_STDINT_H # include #endif +#ifndef ECANCELED +# define ECANCELED EDOM +#endif + +static void eio_destroy (eio_req *req); + #ifndef EIO_FINISH # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 #endif @@ -79,43 +85,83 @@ # define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0) #endif +#ifndef EIO_FD_TO_WIN32_HANDLE +# define EIO_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd) +#endif +#ifndef EIO_WIN32_HANDLE_TO_FD +# define EIO_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0) +#endif + +#define EIO_ERRNO(errval,retval) ((errno = errval), retval) + +#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1) + #ifdef _WIN32 - /*doh*/ + #define PAGESIZE 4096 /* GetSystemInfo? */ + + #define stat(path,buf) _stati64 (path,buf) + #define lstat(path,buf) stat (path,buf) + #define fstat(fd,buf) _fstati64 (path,buf) + #define fsync(fd) (FlushFileBuffers (EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1)) + #define mkdir(path,mode) _mkdir (path) + #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) + + #define chown(path,uid,gid) EIO_ENOSYS () + #define fchown(fd,uid,gid) EIO_ENOSYS () + #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ + #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ + #define mknod(path,mode,dev) EIO_ENOSYS () + #define sync() EIO_ENOSYS () + + /* we could even stat and see if it exists */ + static int + symlink (const char *old, const char *neu) + { + if (CreateSymbolicLink (neu, old, 1)) + return 0; + + if (CreateSymbolicLink (neu, old, 0)) + return 0; + + return EIO_ERRNO (ENOENT, -1); + } + #else -# include -# include -# include -# include -# include -# include - -#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES -# include -#endif - -/* 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 + #include + #include + #include + #include + #include + #include + #include -#ifdef _D_EXACT_NAMLEN -# undef D_NAMLEN -# define D_NAMLEN(de) _D_EXACT_NAMLEN (de) -#endif + #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES + #include + #endif -# ifdef _DIRENT_HAVE_D_TYPE -# define D_TYPE(de) (de)->d_type -# endif + /* 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 -# ifndef EIO_STRUCT_DIRENT -# define EIO_STRUCT_DIRENT struct dirent -# 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 #endif @@ -377,6 +423,9 @@ static void ecb_cold etp_thread_init (void) { +#if !HAVE_PREADWRITE + X_MUTEX_CREATE (preadwritelock); +#endif X_MUTEX_CREATE (wrklock); X_MUTEX_CREATE (reslock); X_MUTEX_CREATE (reqlock); @@ -386,23 +435,11 @@ static void ecb_cold etp_atfork_prepare (void) { - X_LOCK (wrklock); - X_LOCK (reqlock); - X_LOCK (reslock); -#if !HAVE_PREADWRITE - X_LOCK (preadwritelock); -#endif } static void ecb_cold etp_atfork_parent (void) { -#if !HAVE_PREADWRITE - X_UNLOCK (preadwritelock); -#endif - X_UNLOCK (reslock); - X_UNLOCK (reqlock); - X_UNLOCK (wrklock); } static void ecb_cold @@ -585,9 +622,7 @@ static void etp_cancel (ETP_REQ *req) { - X_LOCK (wrklock); - req->flags |= EIO_FLAG_CANCELLED; - X_UNLOCK (wrklock); + req->cancelled = 1; eio_grp_cancel (req); } @@ -713,7 +748,7 @@ return 0; } -void +static void eio_destroy (eio_req *req) { if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); @@ -946,6 +981,17 @@ return fdatasync (fd); } +static int +eio__fallocate (int fd, int mode, off_t offset, size_t len) +{ +#if HAVE_FALLOCATE + return fallocate (fd, mode, offset, len); +#else + errno = ENOSYS; + return -1; +#endif +} + #if !HAVE_READAHEAD # undef readahead # define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) @@ -983,6 +1029,9 @@ for (;;) { +#ifdef __APPLE__ +# undef HAVE_SENDFILE /* broken, as everything on os x */ +#endif #if HAVE_SENDFILE # if __linux off_t soffset = offset; @@ -1008,7 +1057,7 @@ if (sbytes) res = sbytes; -# elif defined (__APPLE__) && 0 /* broken, as everything on os x */ +# elif defined (__APPLE__) off_t sbytes = count; res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); @@ -1035,7 +1084,7 @@ # endif -#elif defined (_WIN32) +#elif defined (_WIN32) && 0 /* does not work, just for documentation of what would need to be done */ /* actually, cannot be done like this, as TransmitFile changes the file offset, */ /* libeio guarantees that the file offset does not change, and windows */ @@ -1150,7 +1199,7 @@ } #if !_POSIX_MEMLOCK -# define eio__mlockall(a) ((errno = ENOSYS), -1) +# define eio__mlockall(a) eio_nosyscall() #else static int @@ -1174,7 +1223,7 @@ #endif #if !_POSIX_MEMLOCK_RANGE -# define eio__mlock(a,b) ((errno = ENOSYS), -1) +# define eio__mlock(a,b) EIO_ENOSYS () #else static int @@ -1188,7 +1237,7 @@ #endif #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) -# define eio__msync(a,b,c) ((errno = ENOSYS), -1) +# define eio__msync(a,b,c) EIO_ENOSYS () #else static int @@ -1787,12 +1836,15 @@ ETP_REQ *req; struct timespec ts; etp_worker *self = (etp_worker *)thr_arg; + int timeout; - /* try to distribute timeouts somewhat randomly */ + /* try to distribute timeouts somewhat evenly */ ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); for (;;) { + ts.tv_sec = 0; + X_LOCK (reqlock); for (;;) @@ -1802,23 +1854,28 @@ if (req) break; + if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ + { + X_UNLOCK (reqlock); + X_LOCK (wrklock); + --started; + X_UNLOCK (wrklock); + goto quit; + } + ++idle; - ts.tv_sec = time (0) + idle_timeout; - if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) + if (idle <= max_idle) + /* we are allowed to idle, so do so without any timeout */ + X_COND_WAIT (reqwait, reqlock); + else { - if (idle > max_idle) - { - --idle; - X_UNLOCK (reqlock); - X_LOCK (wrklock); - --started; - X_UNLOCK (wrklock); - goto quit; - } + /* initialise timeout once */ + if (!ts.tv_sec) + ts.tv_sec = time (0) + idle_timeout; - /* we are allowed to idle, so do so without any timeout */ - X_COND_WAIT (reqwait, reqlock); + if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) + ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ } --idle; @@ -1831,8 +1888,7 @@ if (req->type < 0) goto quit; - if (!EIO_CANCELLED (req)) - ETP_EXECUTE (self, req); + ETP_EXECUTE (self, req); X_LOCK (reslock); @@ -1896,6 +1952,13 @@ static void eio_execute (etp_worker *self, eio_req *req) { + if (ecb_expect_false (EIO_CANCELLED (req))) + { + req->result = -1; + req->errorno = ECANCELED; + return; + } + switch (req->type) { case EIO_READ: ALLOC (req->size); @@ -1952,6 +2015,7 @@ case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; case EIO_MLOCKALL: req->result = eio__mlockall (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_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; case EIO_READDIR: eio__scandir (req, self); break; @@ -2061,6 +2125,11 @@ REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; } +eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND; +} + eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) { REQ (EIO_FDATASYNC); req->int1 = fd; SEND;