os_time.h revision 01e04c3f
101e04c3fSmrg/************************************************************************** 201e04c3fSmrg * 301e04c3fSmrg * Copyright 2008-2010 VMware, Inc. 401e04c3fSmrg * All Rights Reserved. 501e04c3fSmrg * 601e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 701e04c3fSmrg * copy of this software and associated documentation files (the 801e04c3fSmrg * "Software"), to deal in the Software without restriction, including 901e04c3fSmrg * without limitation the rights to use, copy, modify, merge, publish, 1001e04c3fSmrg * distribute, sub license, and/or sell copies of the Software, and to 1101e04c3fSmrg * permit persons to whom the Software is furnished to do so, subject to 1201e04c3fSmrg * the following conditions: 1301e04c3fSmrg * 1401e04c3fSmrg * The above copyright notice and this permission notice (including the 1501e04c3fSmrg * next paragraph) shall be included in all copies or substantial portions 1601e04c3fSmrg * of the Software. 1701e04c3fSmrg * 1801e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 1901e04c3fSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2001e04c3fSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 2101e04c3fSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 2201e04c3fSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 2301e04c3fSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 2401e04c3fSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2501e04c3fSmrg * 2601e04c3fSmrg **************************************************************************/ 2701e04c3fSmrg 2801e04c3fSmrg/** 2901e04c3fSmrg * @file 3001e04c3fSmrg * OS independent time-manipulation functions. 3101e04c3fSmrg * 3201e04c3fSmrg * @author Jose Fonseca <jfonseca@vmware.com> 3301e04c3fSmrg */ 3401e04c3fSmrg 3501e04c3fSmrg#ifndef _OS_TIME_H_ 3601e04c3fSmrg#define _OS_TIME_H_ 3701e04c3fSmrg 3801e04c3fSmrg#include <stdbool.h> 3901e04c3fSmrg#include <stdint.h> 4001e04c3fSmrg 4101e04c3fSmrg#ifdef __cplusplus 4201e04c3fSmrgextern "C" { 4301e04c3fSmrg#endif 4401e04c3fSmrg 4501e04c3fSmrg/* must be equal to PIPE_TIMEOUT_INFINITE */ 4601e04c3fSmrg#define OS_TIMEOUT_INFINITE 0xffffffffffffffffull 4701e04c3fSmrg 4801e04c3fSmrg/* 4901e04c3fSmrg * Get the current time in nanoseconds from an unknown base. 5001e04c3fSmrg */ 5101e04c3fSmrgint64_t 5201e04c3fSmrgos_time_get_nano(void); 5301e04c3fSmrg 5401e04c3fSmrg 5501e04c3fSmrg/* 5601e04c3fSmrg * Get the current time in microseconds from an unknown base. 5701e04c3fSmrg */ 5801e04c3fSmrgstatic inline int64_t 5901e04c3fSmrgos_time_get(void) 6001e04c3fSmrg{ 6101e04c3fSmrg return os_time_get_nano() / 1000; 6201e04c3fSmrg} 6301e04c3fSmrg 6401e04c3fSmrg 6501e04c3fSmrg/* 6601e04c3fSmrg * Sleep. 6701e04c3fSmrg */ 6801e04c3fSmrgvoid 6901e04c3fSmrgos_time_sleep(int64_t usecs); 7001e04c3fSmrg 7101e04c3fSmrg 7201e04c3fSmrg/* 7301e04c3fSmrg * Helper function for detecting time outs, taking in account overflow. 7401e04c3fSmrg * 7501e04c3fSmrg * Returns true if the current time has elapsed beyond the specified interval. 7601e04c3fSmrg */ 7701e04c3fSmrgstatic inline bool 7801e04c3fSmrgos_time_timeout(int64_t start, 7901e04c3fSmrg int64_t end, 8001e04c3fSmrg int64_t curr) 8101e04c3fSmrg{ 8201e04c3fSmrg if (start <= end) 8301e04c3fSmrg return !(start <= curr && curr < end); 8401e04c3fSmrg else 8501e04c3fSmrg return !((start <= curr) || (curr < end)); 8601e04c3fSmrg} 8701e04c3fSmrg 8801e04c3fSmrg 8901e04c3fSmrg/** 9001e04c3fSmrg * Convert a relative timeout in nanoseconds into an absolute timeout, 9101e04c3fSmrg * in other words, it returns current time + timeout. 9201e04c3fSmrg * os_time_get_nano() must be monotonic. 9301e04c3fSmrg * OS_TIMEOUT_INFINITE is passed through unchanged. If the calculation 9401e04c3fSmrg * overflows, OS_TIMEOUT_INFINITE is returned. 9501e04c3fSmrg */ 9601e04c3fSmrgint64_t 9701e04c3fSmrgos_time_get_absolute_timeout(uint64_t timeout); 9801e04c3fSmrg 9901e04c3fSmrg 10001e04c3fSmrg/** 10101e04c3fSmrg * Wait until the variable at the given memory location is zero. 10201e04c3fSmrg * 10301e04c3fSmrg * \param var variable 10401e04c3fSmrg * \param timeout timeout in ns, can be anything from 0 (no wait) to 10501e04c3fSmrg * OS_TIMEOUT_INFINITE (wait forever) 10601e04c3fSmrg * \return true if the variable is zero 10701e04c3fSmrg */ 10801e04c3fSmrgbool 10901e04c3fSmrgos_wait_until_zero(volatile int *var, uint64_t timeout); 11001e04c3fSmrg 11101e04c3fSmrg 11201e04c3fSmrg/** 11301e04c3fSmrg * Wait until the variable at the given memory location is zero. 11401e04c3fSmrg * The timeout is the absolute time when the waiting should stop. If it is 11501e04c3fSmrg * less than or equal to the current time, it only returns the status and 11601e04c3fSmrg * doesn't wait. OS_TIMEOUT_INFINITE waits forever. This requires that 11701e04c3fSmrg * os_time_get_nano is monotonic. 11801e04c3fSmrg * 11901e04c3fSmrg * \param var variable 12001e04c3fSmrg * \param timeout the time in ns when the waiting should stop 12101e04c3fSmrg * \return true if the variable is zero 12201e04c3fSmrg */ 12301e04c3fSmrgbool 12401e04c3fSmrgos_wait_until_zero_abs_timeout(volatile int *var, int64_t timeout); 12501e04c3fSmrg 12601e04c3fSmrg#ifdef __cplusplus 12701e04c3fSmrg} 12801e04c3fSmrg#endif 12901e04c3fSmrg 13001e04c3fSmrg#endif /* _OS_TIME_H_ */ 131