1b8e80941Smrg/************************************************************************** 2b8e80941Smrg * 3b8e80941Smrg * Copyright 2008-2010 VMware, Inc. 4b8e80941Smrg * All Rights Reserved. 5b8e80941Smrg * 6b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 7b8e80941Smrg * copy of this software and associated documentation files (the 8b8e80941Smrg * "Software"), to deal in the Software without restriction, including 9b8e80941Smrg * without limitation the rights to use, copy, modify, merge, publish, 10b8e80941Smrg * distribute, sub license, and/or sell copies of the Software, and to 11b8e80941Smrg * permit persons to whom the Software is furnished to do so, subject to 12b8e80941Smrg * the following conditions: 13b8e80941Smrg * 14b8e80941Smrg * The above copyright notice and this permission notice (including the 15b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions 16b8e80941Smrg * of the Software. 17b8e80941Smrg * 18b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19b8e80941Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20b8e80941Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21b8e80941Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22b8e80941Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23b8e80941Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24b8e80941Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25b8e80941Smrg * 26b8e80941Smrg **************************************************************************/ 27b8e80941Smrg 28b8e80941Smrg/** 29b8e80941Smrg * @file 30b8e80941Smrg * OS independent time-manipulation functions. 31b8e80941Smrg * 32b8e80941Smrg * @author Jose Fonseca <jfonseca@vmware.com> 33b8e80941Smrg */ 34b8e80941Smrg 35b8e80941Smrg#ifndef _OS_TIME_H_ 36b8e80941Smrg#define _OS_TIME_H_ 37b8e80941Smrg 38b8e80941Smrg#include <stdbool.h> 39b8e80941Smrg#include <stdint.h> 40b8e80941Smrg 41b8e80941Smrg#ifdef __cplusplus 42b8e80941Smrgextern "C" { 43b8e80941Smrg#endif 44b8e80941Smrg 45b8e80941Smrg/* must be equal to PIPE_TIMEOUT_INFINITE */ 46b8e80941Smrg#define OS_TIMEOUT_INFINITE 0xffffffffffffffffull 47b8e80941Smrg 48b8e80941Smrg/* 49b8e80941Smrg * Get the current time in nanoseconds from an unknown base. 50b8e80941Smrg */ 51b8e80941Smrgint64_t 52b8e80941Smrgos_time_get_nano(void); 53b8e80941Smrg 54b8e80941Smrg 55b8e80941Smrg/* 56b8e80941Smrg * Get the current time in microseconds from an unknown base. 57b8e80941Smrg */ 58b8e80941Smrgstatic inline int64_t 59b8e80941Smrgos_time_get(void) 60b8e80941Smrg{ 61b8e80941Smrg return os_time_get_nano() / 1000; 62b8e80941Smrg} 63b8e80941Smrg 64b8e80941Smrg 65b8e80941Smrg/* 66b8e80941Smrg * Sleep. 67b8e80941Smrg */ 68b8e80941Smrgvoid 69b8e80941Smrgos_time_sleep(int64_t usecs); 70b8e80941Smrg 71b8e80941Smrg 72b8e80941Smrg/* 73b8e80941Smrg * Helper function for detecting time outs, taking in account overflow. 74b8e80941Smrg * 75b8e80941Smrg * Returns true if the current time has elapsed beyond the specified interval. 76b8e80941Smrg */ 77b8e80941Smrgstatic inline bool 78b8e80941Smrgos_time_timeout(int64_t start, 79b8e80941Smrg int64_t end, 80b8e80941Smrg int64_t curr) 81b8e80941Smrg{ 82b8e80941Smrg if (start <= end) 83b8e80941Smrg return !(start <= curr && curr < end); 84b8e80941Smrg else 85b8e80941Smrg return !((start <= curr) || (curr < end)); 86b8e80941Smrg} 87b8e80941Smrg 88b8e80941Smrg 89b8e80941Smrg/** 90b8e80941Smrg * Convert a relative timeout in nanoseconds into an absolute timeout, 91b8e80941Smrg * in other words, it returns current time + timeout. 92b8e80941Smrg * os_time_get_nano() must be monotonic. 93b8e80941Smrg * OS_TIMEOUT_INFINITE is passed through unchanged. If the calculation 94b8e80941Smrg * overflows, OS_TIMEOUT_INFINITE is returned. 95b8e80941Smrg */ 96b8e80941Smrgint64_t 97b8e80941Smrgos_time_get_absolute_timeout(uint64_t timeout); 98b8e80941Smrg 99b8e80941Smrg 100b8e80941Smrg/** 101b8e80941Smrg * Wait until the variable at the given memory location is zero. 102b8e80941Smrg * 103b8e80941Smrg * \param var variable 104b8e80941Smrg * \param timeout timeout in ns, can be anything from 0 (no wait) to 105b8e80941Smrg * OS_TIMEOUT_INFINITE (wait forever) 106b8e80941Smrg * \return true if the variable is zero 107b8e80941Smrg */ 108b8e80941Smrgbool 109b8e80941Smrgos_wait_until_zero(volatile int *var, uint64_t timeout); 110b8e80941Smrg 111b8e80941Smrg 112b8e80941Smrg/** 113b8e80941Smrg * Wait until the variable at the given memory location is zero. 114b8e80941Smrg * The timeout is the absolute time when the waiting should stop. If it is 115b8e80941Smrg * less than or equal to the current time, it only returns the status and 116b8e80941Smrg * doesn't wait. OS_TIMEOUT_INFINITE waits forever. This requires that 117b8e80941Smrg * os_time_get_nano is monotonic. 118b8e80941Smrg * 119b8e80941Smrg * \param var variable 120b8e80941Smrg * \param timeout the time in ns when the waiting should stop 121b8e80941Smrg * \return true if the variable is zero 122b8e80941Smrg */ 123b8e80941Smrgbool 124b8e80941Smrgos_wait_until_zero_abs_timeout(volatile int *var, int64_t timeout); 125b8e80941Smrg 126b8e80941Smrg#ifdef __cplusplus 127b8e80941Smrg} 128b8e80941Smrg#endif 129b8e80941Smrg 130b8e80941Smrg#endif /* _OS_TIME_H_ */ 131