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