Home | History | Annotate | Download | only in util
      1 /**************************************************************************
      2  *
      3  * Copyright 2008-2010 VMware, Inc.
      4  * All Rights Reserved.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the
      8  * "Software"), to deal in the Software without restriction, including
      9  * without limitation the rights to use, copy, modify, merge, publish,
     10  * distribute, sub license, and/or sell copies of the Software, and to
     11  * permit persons to whom the Software is furnished to do so, subject to
     12  * the following conditions:
     13  *
     14  * The above copyright notice and this permission notice (including the
     15  * next paragraph) shall be included in all copies or substantial portions
     16  * of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
     21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
     22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     25  *
     26  **************************************************************************/
     27 
     28 /**
     29  * @file
     30  * OS independent time-manipulation functions.
     31  *
     32  * @author Jose Fonseca <jfonseca (at) vmware.com>
     33  */
     34 
     35 #include "os_time.h"
     36 
     37 /* TODO: fix this dependency */
     38 #include "gallium/include/pipe/p_config.h"
     39 
     40 #include "util/u_atomic.h"
     41 
     42 #if defined(PIPE_OS_UNIX)
     43 #  include <unistd.h> /* usleep */
     44 #  include <time.h> /* timeval */
     45 #  include <sys/time.h> /* timeval */
     46 #  include <sched.h> /* sched_yield */
     47 #  include <errno.h>
     48 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
     49 #  include <windows.h>
     50 #else
     51 #  error Unsupported OS
     52 #endif
     53 
     54 
     55 int64_t
     56 os_time_get_nano(void)
     57 {
     58 #if defined(PIPE_OS_LINUX)
     59 
     60    struct timespec tv;
     61    clock_gettime(CLOCK_MONOTONIC, &tv);
     62    return tv.tv_nsec + tv.tv_sec*INT64_C(1000000000);
     63 
     64 #elif defined(PIPE_OS_UNIX)
     65 
     66    struct timeval tv;
     67    gettimeofday(&tv, NULL);
     68    return tv.tv_usec*INT64_C(1000) + tv.tv_sec*INT64_C(1000000000);
     69 
     70 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
     71 
     72    static LARGE_INTEGER frequency;
     73    LARGE_INTEGER counter;
     74    int64_t secs, nanosecs;
     75    if(!frequency.QuadPart)
     76       QueryPerformanceFrequency(&frequency);
     77    QueryPerformanceCounter(&counter);
     78    /* Compute seconds and nanoseconds parts separately to
     79     * reduce severity of precision loss.
     80     */
     81    secs = counter.QuadPart / frequency.QuadPart;
     82    nanosecs = (counter.QuadPart % frequency.QuadPart) * INT64_C(1000000000)
     83       / frequency.QuadPart;
     84    return secs*INT64_C(1000000000) + nanosecs;
     85 
     86 #else
     87 
     88 #error Unsupported OS
     89 
     90 #endif
     91 }
     92 
     93 
     94 
     95 void
     96 os_time_sleep(int64_t usecs)
     97 {
     98 #if defined(PIPE_OS_LINUX)
     99    struct timespec time;
    100    time.tv_sec = usecs / 1000000;
    101    time.tv_nsec = (usecs % 1000000) * 1000;
    102    while (clock_nanosleep(CLOCK_MONOTONIC, 0, &time, &time) == EINTR);
    103 
    104 #elif defined(PIPE_OS_UNIX)
    105    usleep(usecs);
    106 
    107 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
    108    DWORD dwMilliseconds = (DWORD) ((usecs + 999) / 1000);
    109    /* Avoid Sleep(O) as that would cause to sleep for an undetermined duration */
    110    if (dwMilliseconds) {
    111       Sleep(dwMilliseconds);
    112    }
    113 #else
    114 #  error Unsupported OS
    115 #endif
    116 }
    117 
    118 
    119 
    120 int64_t
    121 os_time_get_absolute_timeout(uint64_t timeout)
    122 {
    123    int64_t time, abs_timeout;
    124 
    125    /* Also check for the type upper bound. */
    126    if (timeout == OS_TIMEOUT_INFINITE || timeout > INT64_MAX)
    127       return OS_TIMEOUT_INFINITE;
    128 
    129    time = os_time_get_nano();
    130    abs_timeout = time + (int64_t)timeout;
    131 
    132    /* Check for overflow. */
    133    if (abs_timeout < time)
    134       return OS_TIMEOUT_INFINITE;
    135 
    136    return abs_timeout;
    137 }
    138 
    139 
    140 bool
    141 os_wait_until_zero(volatile int *var, uint64_t timeout)
    142 {
    143    if (!p_atomic_read(var))
    144       return true;
    145 
    146    if (!timeout)
    147       return false;
    148 
    149    if (timeout == OS_TIMEOUT_INFINITE) {
    150       while (p_atomic_read(var)) {
    151 #if defined(PIPE_OS_UNIX)
    152          sched_yield();
    153 #endif
    154       }
    155       return true;
    156    }
    157    else {
    158       int64_t start_time = os_time_get_nano();
    159       int64_t end_time = start_time + timeout;
    160 
    161       while (p_atomic_read(var)) {
    162          if (os_time_timeout(start_time, end_time, os_time_get_nano()))
    163             return false;
    164 
    165 #if defined(PIPE_OS_UNIX)
    166          sched_yield();
    167 #endif
    168       }
    169       return true;
    170    }
    171 }
    172 
    173 
    174 bool
    175 os_wait_until_zero_abs_timeout(volatile int *var, int64_t timeout)
    176 {
    177    if (!p_atomic_read(var))
    178       return true;
    179 
    180    if (timeout == OS_TIMEOUT_INFINITE)
    181       return os_wait_until_zero(var, OS_TIMEOUT_INFINITE);
    182 
    183    while (p_atomic_read(var)) {
    184       if (os_time_get_nano() >= timeout)
    185          return false;
    186 
    187 #if defined(PIPE_OS_UNIX)
    188       sched_yield();
    189 #endif
    190    }
    191    return true;
    192 }
    193