1 /************************************************************************** 2 * 3 * Copyright 2008-2010 VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 /** 29 * @file 30 * OS independent time-manipulation functions. 31 * 32 * @author Jose Fonseca <jfonseca (at) vmware.com> 33 */ 34 35 36 #include "pipe/p_defines.h" 37 #include "util/u_atomic.h" 38 39 #if defined(PIPE_OS_UNIX) 40 # include <time.h> /* timeval */ 41 # include <sys/time.h> /* timeval */ 42 # include <sched.h> /* sched_yield */ 43 # include <errno.h> 44 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) 45 # include <windows.h> 46 #else 47 # error Unsupported OS 48 #endif 49 50 #include "os_time.h" 51 52 53 int64_t 54 os_time_get_nano(void) 55 { 56 #if defined(PIPE_OS_LINUX) 57 58 struct timespec tv; 59 clock_gettime(CLOCK_MONOTONIC, &tv); 60 return tv.tv_nsec + tv.tv_sec*INT64_C(1000000000); 61 62 #elif defined(PIPE_OS_UNIX) 63 64 struct timeval tv; 65 gettimeofday(&tv, NULL); 66 return tv.tv_usec*INT64_C(1000) + tv.tv_sec*INT64_C(1000000000); 67 68 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) 69 70 static LARGE_INTEGER frequency; 71 LARGE_INTEGER counter; 72 if(!frequency.QuadPart) 73 QueryPerformanceFrequency(&frequency); 74 QueryPerformanceCounter(&counter); 75 return counter.QuadPart*INT64_C(1000000000)/frequency.QuadPart; 76 77 #else 78 79 #error Unsupported OS 80 81 #endif 82 } 83 84 85 86 void 87 os_time_sleep(int64_t usecs) 88 { 89 #if defined(PIPE_OS_LINUX) 90 struct timespec time; 91 time.tv_sec = usecs / 1000000; 92 time.tv_nsec = (usecs % 1000000) * 1000; 93 while (clock_nanosleep(CLOCK_MONOTONIC, 0, &time, &time) == EINTR); 94 95 #elif defined(PIPE_OS_UNIX) 96 usleep(usecs); 97 98 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) 99 DWORD dwMilliseconds = (DWORD) ((usecs + 999) / 1000); 100 /* Avoid Sleep(O) as that would cause to sleep for an undetermined duration */ 101 if (dwMilliseconds) { 102 Sleep(dwMilliseconds); 103 } 104 #else 105 # error Unsupported OS 106 #endif 107 } 108 109 110 111 int64_t 112 os_time_get_absolute_timeout(uint64_t timeout) 113 { 114 int64_t time, abs_timeout; 115 116 /* Also check for the type upper bound. */ 117 if (timeout == PIPE_TIMEOUT_INFINITE || timeout > INT64_MAX) 118 return PIPE_TIMEOUT_INFINITE; 119 120 time = os_time_get_nano(); 121 abs_timeout = time + (int64_t)timeout; 122 123 /* Check for overflow. */ 124 if (abs_timeout < time) 125 return PIPE_TIMEOUT_INFINITE; 126 127 return abs_timeout; 128 } 129 130 131 bool 132 os_wait_until_zero(volatile int *var, uint64_t timeout) 133 { 134 if (!p_atomic_read(var)) 135 return true; 136 137 if (!timeout) 138 return false; 139 140 if (timeout == PIPE_TIMEOUT_INFINITE) { 141 while (p_atomic_read(var)) { 142 #if defined(PIPE_OS_UNIX) 143 sched_yield(); 144 #endif 145 } 146 return true; 147 } 148 else { 149 int64_t start_time = os_time_get_nano(); 150 int64_t end_time = start_time + timeout; 151 152 while (p_atomic_read(var)) { 153 if (os_time_timeout(start_time, end_time, os_time_get_nano())) 154 return false; 155 156 #if defined(PIPE_OS_UNIX) 157 sched_yield(); 158 #endif 159 } 160 return true; 161 } 162 } 163 164 165 bool 166 os_wait_until_zero_abs_timeout(volatile int *var, int64_t timeout) 167 { 168 if (!p_atomic_read(var)) 169 return true; 170 171 if (timeout == PIPE_TIMEOUT_INFINITE) 172 return os_wait_until_zero(var, PIPE_TIMEOUT_INFINITE); 173 174 while (p_atomic_read(var)) { 175 if (os_time_get_nano() >= timeout) 176 return false; 177 178 #if defined(PIPE_OS_UNIX) 179 sched_yield(); 180 #endif 181 } 182 return true; 183 } 184