util,gallivm: Explicitly enable/disable fma attribute.
[mesa.git] / src / gallium / auxiliary / os / 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
36 #include "pipe/p_defines.h"
37 #include "util/u_atomic.h"
38
39 #if defined(PIPE_OS_UNIX)
40 # include <time.h> /* timeval */
41 # include <sys/time.h> /* timeval */
42 # include <sched.h> /* sched_yield */
43 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
44 # include <windows.h>
45 #else
46 # error Unsupported OS
47 #endif
48
49 #include "os_time.h"
50
51
52 int64_t
53 os_time_get_nano(void)
54 {
55 #if defined(PIPE_OS_LINUX)
56
57 struct timespec tv;
58 clock_gettime(CLOCK_MONOTONIC, &tv);
59 return tv.tv_nsec + tv.tv_sec*INT64_C(1000000000);
60
61 #elif defined(PIPE_OS_UNIX)
62
63 struct timeval tv;
64 gettimeofday(&tv, NULL);
65 return tv.tv_usec*INT64_C(1000) + tv.tv_sec*INT64_C(1000000000);
66
67 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
68
69 static LARGE_INTEGER frequency;
70 LARGE_INTEGER counter;
71 if(!frequency.QuadPart)
72 QueryPerformanceFrequency(&frequency);
73 QueryPerformanceCounter(&counter);
74 return counter.QuadPart*INT64_C(1000000000)/frequency.QuadPart;
75
76 #else
77
78 #error Unsupported OS
79
80 #endif
81 }
82
83
84 #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
85
86 void
87 os_time_sleep(int64_t usecs)
88 {
89 DWORD dwMilliseconds = (DWORD) ((usecs + 999) / 1000);
90 /* Avoid Sleep(O) as that would cause to sleep for an undetermined duration */
91 if (dwMilliseconds) {
92 Sleep(dwMilliseconds);
93 }
94 }
95
96 #endif
97
98
99 int64_t
100 os_time_get_absolute_timeout(uint64_t timeout)
101 {
102 int64_t time, abs_timeout;
103
104 /* Also check for the type upper bound. */
105 if (timeout == PIPE_TIMEOUT_INFINITE || timeout > INT64_MAX)
106 return PIPE_TIMEOUT_INFINITE;
107
108 time = os_time_get_nano();
109 abs_timeout = time + (int64_t)timeout;
110
111 /* Check for overflow. */
112 if (abs_timeout < time)
113 return PIPE_TIMEOUT_INFINITE;
114
115 return abs_timeout;
116 }
117
118
119 bool
120 os_wait_until_zero(volatile int *var, uint64_t timeout)
121 {
122 if (!p_atomic_read(var))
123 return true;
124
125 if (!timeout)
126 return false;
127
128 if (timeout == PIPE_TIMEOUT_INFINITE) {
129 while (p_atomic_read(var)) {
130 #if defined(PIPE_OS_UNIX)
131 sched_yield();
132 #endif
133 }
134 return true;
135 }
136 else {
137 int64_t start_time = os_time_get_nano();
138 int64_t end_time = start_time + timeout;
139
140 while (p_atomic_read(var)) {
141 if (os_time_timeout(start_time, end_time, os_time_get_nano()))
142 return false;
143
144 #if defined(PIPE_OS_UNIX)
145 sched_yield();
146 #endif
147 }
148 return true;
149 }
150 }
151
152
153 bool
154 os_wait_until_zero_abs_timeout(volatile int *var, int64_t timeout)
155 {
156 if (!p_atomic_read(var))
157 return true;
158
159 if (timeout == PIPE_TIMEOUT_INFINITE)
160 return os_wait_until_zero(var, PIPE_TIMEOUT_INFINITE);
161
162 while (p_atomic_read(var)) {
163 if (os_time_get_nano() >= timeout)
164 return false;
165
166 #if defined(PIPE_OS_UNIX)
167 sched_yield();
168 #endif
169 }
170 return true;
171 }