os_time.c 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#include "os_time.h"
3601e04c3fSmrg
3701e04c3fSmrg/* TODO: fix this dependency */
3801e04c3fSmrg#include "gallium/include/pipe/p_config.h"
3901e04c3fSmrg
4001e04c3fSmrg#include "util/u_atomic.h"
4101e04c3fSmrg
4201e04c3fSmrg#if defined(PIPE_OS_UNIX)
4301e04c3fSmrg#  include <unistd.h> /* usleep */
4401e04c3fSmrg#  include <time.h> /* timeval */
4501e04c3fSmrg#  include <sys/time.h> /* timeval */
4601e04c3fSmrg#  include <sched.h> /* sched_yield */
4701e04c3fSmrg#  include <errno.h>
4801e04c3fSmrg#elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
4901e04c3fSmrg#  include <windows.h>
5001e04c3fSmrg#else
5101e04c3fSmrg#  error Unsupported OS
5201e04c3fSmrg#endif
5301e04c3fSmrg
5401e04c3fSmrg
5501e04c3fSmrgint64_t
5601e04c3fSmrgos_time_get_nano(void)
5701e04c3fSmrg{
5801e04c3fSmrg#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_BSD)
5901e04c3fSmrg
6001e04c3fSmrg   struct timespec tv;
6101e04c3fSmrg   clock_gettime(CLOCK_MONOTONIC, &tv);
6201e04c3fSmrg   return tv.tv_nsec + tv.tv_sec*INT64_C(1000000000);
6301e04c3fSmrg
6401e04c3fSmrg#elif defined(PIPE_OS_UNIX)
6501e04c3fSmrg
6601e04c3fSmrg   struct timeval tv;
6701e04c3fSmrg   gettimeofday(&tv, NULL);
6801e04c3fSmrg   return tv.tv_usec*INT64_C(1000) + tv.tv_sec*INT64_C(1000000000);
6901e04c3fSmrg
7001e04c3fSmrg#elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
7101e04c3fSmrg
7201e04c3fSmrg   static LARGE_INTEGER frequency;
7301e04c3fSmrg   LARGE_INTEGER counter;
7401e04c3fSmrg   int64_t secs, nanosecs;
7501e04c3fSmrg   if(!frequency.QuadPart)
7601e04c3fSmrg      QueryPerformanceFrequency(&frequency);
7701e04c3fSmrg   QueryPerformanceCounter(&counter);
7801e04c3fSmrg   /* Compute seconds and nanoseconds parts separately to
7901e04c3fSmrg    * reduce severity of precision loss.
8001e04c3fSmrg    */
8101e04c3fSmrg   secs = counter.QuadPart / frequency.QuadPart;
8201e04c3fSmrg   nanosecs = (counter.QuadPart % frequency.QuadPart) * INT64_C(1000000000)
8301e04c3fSmrg      / frequency.QuadPart;
8401e04c3fSmrg   return secs*INT64_C(1000000000) + nanosecs;
8501e04c3fSmrg
8601e04c3fSmrg#else
8701e04c3fSmrg
8801e04c3fSmrg#error Unsupported OS
8901e04c3fSmrg
9001e04c3fSmrg#endif
9101e04c3fSmrg}
9201e04c3fSmrg
9301e04c3fSmrg
9401e04c3fSmrg
9501e04c3fSmrgvoid
9601e04c3fSmrgos_time_sleep(int64_t usecs)
9701e04c3fSmrg{
9801e04c3fSmrg#if defined(PIPE_OS_LINUX)
9901e04c3fSmrg   struct timespec time;
10001e04c3fSmrg   time.tv_sec = usecs / 1000000;
10101e04c3fSmrg   time.tv_nsec = (usecs % 1000000) * 1000;
10201e04c3fSmrg   while (clock_nanosleep(CLOCK_MONOTONIC, 0, &time, &time) == EINTR);
10301e04c3fSmrg
10401e04c3fSmrg#elif defined(PIPE_OS_UNIX)
10501e04c3fSmrg   usleep(usecs);
10601e04c3fSmrg
10701e04c3fSmrg#elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
10801e04c3fSmrg   DWORD dwMilliseconds = (DWORD) ((usecs + 999) / 1000);
10901e04c3fSmrg   /* Avoid Sleep(O) as that would cause to sleep for an undetermined duration */
11001e04c3fSmrg   if (dwMilliseconds) {
11101e04c3fSmrg      Sleep(dwMilliseconds);
11201e04c3fSmrg   }
11301e04c3fSmrg#else
11401e04c3fSmrg#  error Unsupported OS
11501e04c3fSmrg#endif
11601e04c3fSmrg}
11701e04c3fSmrg
11801e04c3fSmrg
11901e04c3fSmrg
12001e04c3fSmrgint64_t
12101e04c3fSmrgos_time_get_absolute_timeout(uint64_t timeout)
12201e04c3fSmrg{
12301e04c3fSmrg   int64_t time, abs_timeout;
12401e04c3fSmrg
12501e04c3fSmrg   /* Also check for the type upper bound. */
12601e04c3fSmrg   if (timeout == OS_TIMEOUT_INFINITE || timeout > INT64_MAX)
12701e04c3fSmrg      return OS_TIMEOUT_INFINITE;
12801e04c3fSmrg
12901e04c3fSmrg   time = os_time_get_nano();
13001e04c3fSmrg   abs_timeout = time + (int64_t)timeout;
13101e04c3fSmrg
13201e04c3fSmrg   /* Check for overflow. */
13301e04c3fSmrg   if (abs_timeout < time)
13401e04c3fSmrg      return OS_TIMEOUT_INFINITE;
13501e04c3fSmrg
13601e04c3fSmrg   return abs_timeout;
13701e04c3fSmrg}
13801e04c3fSmrg
13901e04c3fSmrg
14001e04c3fSmrgbool
14101e04c3fSmrgos_wait_until_zero(volatile int *var, uint64_t timeout)
14201e04c3fSmrg{
14301e04c3fSmrg   if (!p_atomic_read(var))
14401e04c3fSmrg      return true;
14501e04c3fSmrg
14601e04c3fSmrg   if (!timeout)
14701e04c3fSmrg      return false;
14801e04c3fSmrg
14901e04c3fSmrg   if (timeout == OS_TIMEOUT_INFINITE) {
15001e04c3fSmrg      while (p_atomic_read(var)) {
15101e04c3fSmrg#if defined(PIPE_OS_UNIX)
15201e04c3fSmrg         sched_yield();
15301e04c3fSmrg#endif
15401e04c3fSmrg      }
15501e04c3fSmrg      return true;
15601e04c3fSmrg   }
15701e04c3fSmrg   else {
15801e04c3fSmrg      int64_t start_time = os_time_get_nano();
15901e04c3fSmrg      int64_t end_time = start_time + timeout;
16001e04c3fSmrg
16101e04c3fSmrg      while (p_atomic_read(var)) {
16201e04c3fSmrg         if (os_time_timeout(start_time, end_time, os_time_get_nano()))
16301e04c3fSmrg            return false;
16401e04c3fSmrg
16501e04c3fSmrg#if defined(PIPE_OS_UNIX)
16601e04c3fSmrg         sched_yield();
16701e04c3fSmrg#endif
16801e04c3fSmrg      }
16901e04c3fSmrg      return true;
17001e04c3fSmrg   }
17101e04c3fSmrg}
17201e04c3fSmrg
17301e04c3fSmrg
17401e04c3fSmrgbool
17501e04c3fSmrgos_wait_until_zero_abs_timeout(volatile int *var, int64_t timeout)
17601e04c3fSmrg{
17701e04c3fSmrg   if (!p_atomic_read(var))
17801e04c3fSmrg      return true;
17901e04c3fSmrg
18001e04c3fSmrg   if (timeout == OS_TIMEOUT_INFINITE)
18101e04c3fSmrg      return os_wait_until_zero(var, OS_TIMEOUT_INFINITE);
18201e04c3fSmrg
18301e04c3fSmrg   while (p_atomic_read(var)) {
18401e04c3fSmrg      if (os_time_get_nano() >= timeout)
18501e04c3fSmrg         return false;
18601e04c3fSmrg
18701e04c3fSmrg#if defined(PIPE_OS_UNIX)
18801e04c3fSmrg      sched_yield();
18901e04c3fSmrg#endif
19001e04c3fSmrg   }
19101e04c3fSmrg   return true;
19201e04c3fSmrg}
193