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> 407ec681f3Smrg#include <time.h> 4101e04c3fSmrg 4201e04c3fSmrg#ifdef __cplusplus 4301e04c3fSmrgextern "C" { 4401e04c3fSmrg#endif 4501e04c3fSmrg 4601e04c3fSmrg/* must be equal to PIPE_TIMEOUT_INFINITE */ 4701e04c3fSmrg#define OS_TIMEOUT_INFINITE 0xffffffffffffffffull 4801e04c3fSmrg 4901e04c3fSmrg/* 5001e04c3fSmrg * Get the current time in nanoseconds from an unknown base. 5101e04c3fSmrg */ 5201e04c3fSmrgint64_t 5301e04c3fSmrgos_time_get_nano(void); 5401e04c3fSmrg 5501e04c3fSmrg 5601e04c3fSmrg/* 5701e04c3fSmrg * Get the current time in microseconds from an unknown base. 5801e04c3fSmrg */ 5901e04c3fSmrgstatic inline int64_t 6001e04c3fSmrgos_time_get(void) 6101e04c3fSmrg{ 6201e04c3fSmrg return os_time_get_nano() / 1000; 6301e04c3fSmrg} 6401e04c3fSmrg 6501e04c3fSmrg 667ec681f3Smrgstatic inline struct tm * 677ec681f3Smrgos_localtime(const time_t *timer, struct tm *buf) 687ec681f3Smrg{ 697ec681f3Smrg#ifdef _WIN32 707ec681f3Smrg return localtime_s(buf, timer) ? NULL : buf; 717ec681f3Smrg#else 727ec681f3Smrg return localtime_r(timer, buf); 737ec681f3Smrg#endif 747ec681f3Smrg} 757ec681f3Smrg 767ec681f3Smrg 7701e04c3fSmrg/* 7801e04c3fSmrg * Sleep. 7901e04c3fSmrg */ 8001e04c3fSmrgvoid 8101e04c3fSmrgos_time_sleep(int64_t usecs); 8201e04c3fSmrg 8301e04c3fSmrg 8401e04c3fSmrg/* 8501e04c3fSmrg * Helper function for detecting time outs, taking in account overflow. 8601e04c3fSmrg * 8701e04c3fSmrg * Returns true if the current time has elapsed beyond the specified interval. 8801e04c3fSmrg */ 8901e04c3fSmrgstatic inline bool 9001e04c3fSmrgos_time_timeout(int64_t start, 9101e04c3fSmrg int64_t end, 9201e04c3fSmrg int64_t curr) 9301e04c3fSmrg{ 9401e04c3fSmrg if (start <= end) 9501e04c3fSmrg return !(start <= curr && curr < end); 9601e04c3fSmrg else 9701e04c3fSmrg return !((start <= curr) || (curr < end)); 9801e04c3fSmrg} 9901e04c3fSmrg 10001e04c3fSmrg 10101e04c3fSmrg/** 10201e04c3fSmrg * Convert a relative timeout in nanoseconds into an absolute timeout, 10301e04c3fSmrg * in other words, it returns current time + timeout. 10401e04c3fSmrg * os_time_get_nano() must be monotonic. 10501e04c3fSmrg * OS_TIMEOUT_INFINITE is passed through unchanged. If the calculation 10601e04c3fSmrg * overflows, OS_TIMEOUT_INFINITE is returned. 10701e04c3fSmrg */ 10801e04c3fSmrgint64_t 10901e04c3fSmrgos_time_get_absolute_timeout(uint64_t timeout); 11001e04c3fSmrg 11101e04c3fSmrg 11201e04c3fSmrg/** 11301e04c3fSmrg * Wait until the variable at the given memory location is zero. 11401e04c3fSmrg * 11501e04c3fSmrg * \param var variable 11601e04c3fSmrg * \param timeout timeout in ns, can be anything from 0 (no wait) to 11701e04c3fSmrg * OS_TIMEOUT_INFINITE (wait forever) 11801e04c3fSmrg * \return true if the variable is zero 11901e04c3fSmrg */ 12001e04c3fSmrgbool 12101e04c3fSmrgos_wait_until_zero(volatile int *var, uint64_t timeout); 12201e04c3fSmrg 12301e04c3fSmrg 12401e04c3fSmrg/** 12501e04c3fSmrg * Wait until the variable at the given memory location is zero. 12601e04c3fSmrg * The timeout is the absolute time when the waiting should stop. If it is 12701e04c3fSmrg * less than or equal to the current time, it only returns the status and 12801e04c3fSmrg * doesn't wait. OS_TIMEOUT_INFINITE waits forever. This requires that 12901e04c3fSmrg * os_time_get_nano is monotonic. 13001e04c3fSmrg * 13101e04c3fSmrg * \param var variable 13201e04c3fSmrg * \param timeout the time in ns when the waiting should stop 13301e04c3fSmrg * \return true if the variable is zero 13401e04c3fSmrg */ 13501e04c3fSmrgbool 13601e04c3fSmrgos_wait_until_zero_abs_timeout(volatile int *var, int64_t timeout); 13701e04c3fSmrg 13801e04c3fSmrg#ifdef __cplusplus 13901e04c3fSmrg} 14001e04c3fSmrg#endif 14101e04c3fSmrg 14201e04c3fSmrg#endif /* _OS_TIME_H_ */ 143