From: Bruce Cran Date: Wed, 15 Aug 2012 18:24:24 +0000 (+0200) Subject: fio: add nanosleep() to Windows X-Git-Tag: fio-2.0.9~6 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=21c753874722ceff6e78c9e57768932436bce35b;p=fio.git fio: add nanosleep() to Windows I've attached a patch which adds an implementation of nanosleep() to Windows, fixing rate-limiting. Signed-off-by: Jens Axboe --- diff --git a/os/windows/posix.c b/os/windows/posix.c index 41bcb2d4..27260ce6 100755 --- a/os/windows/posix.c +++ b/os/windows/posix.c @@ -20,6 +20,9 @@ #include "../os-windows.h" +extern unsigned long mtime_since_now(struct timeval *); +extern void fio_gettime(struct timeval *, void *); + long sysconf(int name) { long long val = -1; @@ -631,9 +634,30 @@ int poll(struct pollfd fds[], nfds_t nfds, int timeout) int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { - log_err("%s is not implemented\n", __func__); - errno = ENOSYS; - return -1; + struct timeval tv; + DWORD ms_remaining; + DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0); + + if (ms_total == 0) + ms_total = 1; + + ms_remaining = ms_total; + + /* Since Sleep() can sleep for less than the requested time, add a loop to + ensure we only return after the requested length of time has elapsed */ + do { + fio_gettime(&tv, NULL); + Sleep(ms_remaining); + ms_remaining = ms_total - mtime_since_now(&tv); + } while (ms_remaining > 0 && ms_remaining < ms_total); + + /* this implementation will never sleep for less than the requested time */ + if (rmtp != NULL) { + rmtp->tv_sec = 0; + rmtp->tv_nsec = 0; + } + + return 0; } DIR *opendir(const char *dirname)