--- libeio/eio.c 2010/02/23 14:32:45 1.54 +++ libeio/eio.c 2010/10/30 14:36:53 1.58 @@ -76,12 +76,15 @@ # include "config.h" # include # include -# include # include # include # include # include +#if _POSIX_MEMLOCK || _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 */ @@ -224,10 +227,10 @@ static volatile unsigned int npending; /* reqlock */ static volatile unsigned int max_idle = 4; -static mutex_t wrklock = X_MUTEX_INIT; -static mutex_t reslock = X_MUTEX_INIT; -static mutex_t reqlock = X_MUTEX_INIT; -static cond_t reqwait = X_COND_INIT; +static xmutex_t wrklock = X_MUTEX_INIT; +static xmutex_t reslock = X_MUTEX_INIT; +static xmutex_t reqlock = X_MUTEX_INIT; +static xcond_t reqwait = X_COND_INIT; #if !HAVE_PREADWRITE /* @@ -235,7 +238,7 @@ * 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 xmutex_t preadwritelock = X_MUTEX_INIT; #endif typedef struct etp_worker @@ -243,7 +246,7 @@ /* locked by wrklock */ struct etp_worker *prev, *next; - thread_t tid; + xthread_t tid; /* locked by reslock, reqlock or wrklock */ ETP_REQ *req; /* currently processed request */ @@ -1394,6 +1397,69 @@ } } +#ifdef PAGESIZE +# define eio_pagesize() PAGESIZE +#else +static intptr_t +eio_pagesize (void) +{ + static intptr_t page; + + if (!page) + page = sysconf (_SC_PAGESIZE); + + return page; +} +#endif + +static void +eio_page_align (void **addr, size_t *length) +{ + intptr_t mask = eio_pagesize () - 1; + + /* round down addr */ + intptr_t adj = mask & (intptr_t)*addr; + + *addr = (void *)((intptr_t)*addr - adj); + *length += adj; + + /* round up length */ + *length = (*length + mask) & ~mask; +} + +#if !_POSIX_MEMLOCK +# define eio__mlock(a,b) ((errno = ENOSYS), -1) +# define eio__mlockall(a) ((errno = ENOSYS), -1) +#else + +static int +eio__mlock (void *addr, size_t length) +{ + eio_page_align (&addr, &length); + + return mlock (addr, length); +} + +static int +eio__mlockall (int flags) +{ + #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 + extern int mallopt (int, int); + mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ + #endif + + if (EIO_MCL_CURRENT != MCL_CURRENT + || EIO_MCL_FUTURE != MCL_FUTURE) + { + flags = 0 + | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) + | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); + } + + return mlockall (flags); +} +#endif + #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) # define eio__msync(a,b,c) ((errno = ENOSYS), -1) #else @@ -1401,6 +1467,8 @@ int eio__msync (void *mem, size_t len, int flags) { + eio_page_align (&mem, &len); + if (EIO_MS_ASYNC != MS_SYNC || EIO_MS_INVALIDATE != MS_INVALIDATE || EIO_MS_SYNC != MS_SYNC) @@ -1419,25 +1487,19 @@ 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 + eio_page_align (&mem, &len); - /* 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); + { + intptr_t addr = (intptr_t)mem; + intptr_t end = addr + len; + intptr_t page = eio_pagesize (); + + 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; } @@ -1570,8 +1632,6 @@ static void eio_execute (etp_worker *self, eio_req *req) { - errno = 0; - switch (req->type) { case EIO_READ: ALLOC (req->size); @@ -1623,19 +1683,21 @@ 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_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_READDIR: eio__scandir (req, self); break; case EIO_BUSY: #ifdef _WIN32 - Sleep (req->nv1 * 1000.); + Sleep (req->nv1 * 1e3); #else { struct timeval tv; tv.tv_sec = req->nv1; - tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; + tv.tv_usec = (req->nv1 - tv.tv_sec) * 1e6; req->result = select (0, 0, 0, 0, &tv); } @@ -1660,7 +1722,6 @@ else times = 0; - req->result = req->type == EIO_FUTIME ? futimes (req->int1, times) : utimes (req->ptr1, times); @@ -1679,6 +1740,7 @@ break; default: + errno = ENOSYS; req->result = -1; break; } @@ -1718,6 +1780,16 @@ REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; } +eio_req *eio_mlock (void *addr, size_t length, int pri, eio_cb cb, void *data) +{ + REQ (EIO_MLOCK); req->ptr2 = addr; req->size = length; SEND; +} + +eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_MLOCKALL); 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;