fio: add nanosleep() to Windows
authorBruce Cran <bruce@cran.org.uk>
Wed, 15 Aug 2012 18:24:24 +0000 (20:24 +0200)
committerJens Axboe <axboe@kernel.dk>
Wed, 15 Aug 2012 18:24:24 +0000 (20:24 +0200)
I've attached a patch which adds an implementation of nanosleep() to
Windows, fixing rate-limiting.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
os/windows/posix.c

index 41bcb2d4e50a8fd3d0f4db3b93d3c0dff5b163f7..27260ce6714a378c08444a6309dfc925392c8859 100755 (executable)
@@ -20,6 +20,9 @@
 
 #include "../os-windows.h"
 
 
 #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;
 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)
 {
 
 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)
 }
 
 DIR *opendir(const char *dirname)