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@vmware.com>
33 */
34
35 #include "os_time.h"
36 #include "detect_os.h"
37
38 #include "c11/time.h"
39
40 #include "util/u_atomic.h"
41
42 #if DETECT_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 DETECT_OS_WINDOWS
49 # include <windows.h>
50 #else
51 # error Unsupported OS
52 #endif
53
54
55 int64_t
os_time_get_nano(void)56 os_time_get_nano(void)
57 {
58 struct timespec ts;
59 timespec_get(&ts, TIME_MONOTONIC);
60 return ts.tv_nsec + ts.tv_sec*INT64_C(1000000000);
61 }
62
63
64
65 void
os_time_sleep(int64_t usecs)66 os_time_sleep(int64_t usecs)
67 {
68 #if DETECT_OS_LINUX
69 struct timespec time;
70 time.tv_sec = usecs / 1000000;
71 time.tv_nsec = (usecs % 1000000) * 1000;
72 while (clock_nanosleep(CLOCK_MONOTONIC, 0, &time, &time) == EINTR);
73
74 #elif DETECT_OS_UNIX
75 usleep(usecs);
76
77 #elif DETECT_OS_WINDOWS
78 DWORD dwMilliseconds = (DWORD) ((usecs + 999) / 1000);
79 /* Avoid Sleep(O) as that would cause to sleep for an undetermined duration */
80 if (dwMilliseconds) {
81 Sleep(dwMilliseconds);
82 }
83 #else
84 # error Unsupported OS
85 #endif
86 }
87
88
89
90 int64_t
os_time_get_absolute_timeout(uint64_t timeout)91 os_time_get_absolute_timeout(uint64_t timeout)
92 {
93 int64_t time, abs_timeout;
94
95 /* Also check for the type upper bound. */
96 if (timeout == OS_TIMEOUT_INFINITE || timeout > INT64_MAX)
97 return OS_TIMEOUT_INFINITE;
98
99 time = os_time_get_nano();
100 abs_timeout = time + (int64_t)timeout;
101
102 /* Check for overflow. */
103 if (abs_timeout < time)
104 return OS_TIMEOUT_INFINITE;
105
106 return abs_timeout;
107 }
108
109
110 bool
os_wait_until_zero(volatile int * var,uint64_t timeout)111 os_wait_until_zero(volatile int *var, uint64_t timeout)
112 {
113 if (!p_atomic_read(var))
114 return true;
115
116 if (!timeout)
117 return false;
118
119 if (timeout == OS_TIMEOUT_INFINITE) {
120 while (p_atomic_read(var)) {
121 #if DETECT_OS_UNIX
122 sched_yield();
123 #endif
124 }
125 return true;
126 }
127 else {
128 int64_t start_time = os_time_get_nano();
129 int64_t end_time = start_time + timeout;
130
131 while (p_atomic_read(var)) {
132 if (os_time_timeout(start_time, end_time, os_time_get_nano()))
133 return false;
134
135 #if DETECT_OS_UNIX
136 sched_yield();
137 #endif
138 }
139 return true;
140 }
141 }
142
143
144 bool
os_wait_until_zero_abs_timeout(volatile int * var,int64_t timeout)145 os_wait_until_zero_abs_timeout(volatile int *var, int64_t timeout)
146 {
147 if (!p_atomic_read(var))
148 return true;
149
150 if (timeout == OS_TIMEOUT_INFINITE)
151 return os_wait_until_zero(var, OS_TIMEOUT_INFINITE);
152
153 while (p_atomic_read(var)) {
154 if (os_time_get_nano() >= timeout)
155 return false;
156
157 #if DETECT_OS_UNIX
158 sched_yield();
159 #endif
160 }
161 return true;
162 }
163