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