util: move os_time.[ch] to src/util
[mesa.git] / src / util / os_time.c
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
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 <time.h> /* timeval */
44 # include <sys/time.h> /* timeval */
45 # include <sched.h> /* sched_yield */
46 # include <errno.h>
47 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
48 # include <windows.h>
49 #else
50 # error Unsupported OS
51 #endif
52
53
54 int64_t
55 os_time_get_nano(void)
56 {
57 #if defined(PIPE_OS_LINUX)
58
59 struct timespec tv;
60 clock_gettime(CLOCK_MONOTONIC, &tv);
61 return tv.tv_nsec + tv.tv_sec*INT64_C(1000000000);
62
63 #elif defined(PIPE_OS_UNIX)
64
65 struct timeval tv;
66 gettimeofday(&tv, NULL);
67 return tv.tv_usec*INT64_C(1000) + tv.tv_sec*INT64_C(1000000000);
68
69 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
70
71 static LARGE_INTEGER frequency;
72 LARGE_INTEGER counter;
73 int64_t secs, nanosecs;
74 if(!frequency.QuadPart)
75 QueryPerformanceFrequency(&frequency);
76 QueryPerformanceCounter(&counter);
77 /* Compute seconds and nanoseconds parts separately to
78 * reduce severity of precision loss.
79 */
80 secs = counter.QuadPart / frequency.QuadPart;
81 nanosecs = (counter.QuadPart % frequency.QuadPart) * INT64_C(1000000000)
82 / frequency.QuadPart;
83 return secs*INT64_C(1000000000) + nanosecs;
84
85 #else
86
87 #error Unsupported OS
88
89 #endif
90 }
91
92
93
94 void
95 os_time_sleep(int64_t usecs)
96 {
97 #if defined(PIPE_OS_LINUX)
98 struct timespec time;
99 time.tv_sec = usecs / 1000000;
100 time.tv_nsec = (usecs % 1000000) * 1000;
101 while (clock_nanosleep(CLOCK_MONOTONIC, 0, &time, &time) == EINTR);
102
103 #elif defined(PIPE_OS_UNIX)
104 usleep(usecs);
105
106 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
107 DWORD dwMilliseconds = (DWORD) ((usecs + 999) / 1000);
108 /* Avoid Sleep(O) as that would cause to sleep for an undetermined duration */
109 if (dwMilliseconds) {
110 Sleep(dwMilliseconds);
111 }
112 #else
113 # error Unsupported OS
114 #endif
115 }
116
117
118
119 int64_t
120 os_time_get_absolute_timeout(uint64_t timeout)
121 {
122 int64_t time, abs_timeout;
123
124 /* Also check for the type upper bound. */
125 if (timeout == OS_TIMEOUT_INFINITE || timeout > INT64_MAX)
126 return OS_TIMEOUT_INFINITE;
127
128 time = os_time_get_nano();
129 abs_timeout = time + (int64_t)timeout;
130
131 /* Check for overflow. */
132 if (abs_timeout < time)
133 return OS_TIMEOUT_INFINITE;
134
135 return abs_timeout;
136 }
137
138
139 bool
140 os_wait_until_zero(volatile int *var, uint64_t timeout)
141 {
142 if (!p_atomic_read(var))
143 return true;
144
145 if (!timeout)
146 return false;
147
148 if (timeout == OS_TIMEOUT_INFINITE) {
149 while (p_atomic_read(var)) {
150 #if defined(PIPE_OS_UNIX)
151 sched_yield();
152 #endif
153 }
154 return true;
155 }
156 else {
157 int64_t start_time = os_time_get_nano();
158 int64_t end_time = start_time + timeout;
159
160 while (p_atomic_read(var)) {
161 if (os_time_timeout(start_time, end_time, os_time_get_nano()))
162 return false;
163
164 #if defined(PIPE_OS_UNIX)
165 sched_yield();
166 #endif
167 }
168 return true;
169 }
170 }
171
172
173 bool
174 os_wait_until_zero_abs_timeout(volatile int *var, int64_t timeout)
175 {
176 if (!p_atomic_read(var))
177 return true;
178
179 if (timeout == OS_TIMEOUT_INFINITE)
180 return os_wait_until_zero(var, OS_TIMEOUT_INFINITE);
181
182 while (p_atomic_read(var)) {
183 if (os_time_get_nano() >= timeout)
184 return false;
185
186 #if defined(PIPE_OS_UNIX)
187 sched_yield();
188 #endif
189 }
190 return true;
191 }