Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / gallium / auxiliary / util / u_time.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <jrfonseca@tungstengraphics.com>
33 */
34
35
36 #include "pipe/p_config.h"
37
38 #if defined(PIPE_OS_LINUX)
39 #include <sys/time.h>
40 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
41 #include <windows.h>
42 #include <winddi.h>
43 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
44 #include <windows.h>
45 extern VOID KeQuerySystemTime(PLARGE_INTEGER);
46 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
47 #include <windows.h>
48 #else
49 #error Unsupported OS
50 #endif
51
52 #include "util/u_time.h"
53
54
55 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) || defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
56
57 static int64_t frequency = 0;
58
59 static INLINE void
60 util_time_get_frequency(void)
61 {
62 if(!frequency) {
63 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
64 LONGLONG temp;
65 EngQueryPerformanceFrequency(&temp);
66 frequency = temp;
67 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
68 LARGE_INTEGER temp;
69 QueryPerformanceFrequency(&temp);
70 frequency = temp.QuadPart;
71 #endif
72 }
73 }
74 #endif
75
76
77 void
78 util_time_get(struct util_time *t)
79 {
80 #if defined(PIPE_OS_LINUX)
81 gettimeofday(&t->tv, NULL);
82 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
83 LONGLONG temp;
84 EngQueryPerformanceCounter(&temp);
85 t->counter = temp;
86 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
87 /* Updated every 10 miliseconds, measured in units of 100 nanoseconds.
88 * http://msdn.microsoft.com/en-us/library/ms801642.aspx */
89 LARGE_INTEGER temp;
90 KeQuerySystemTime(&temp);
91 t->counter = temp.QuadPart;
92 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
93 LARGE_INTEGER temp;
94 QueryPerformanceCounter(&temp);
95 t->counter = temp.QuadPart;
96 #endif
97 }
98
99
100 void
101 util_time_add(const struct util_time *t1,
102 int64_t usecs,
103 struct util_time *t2)
104 {
105 #if defined(PIPE_OS_LINUX)
106 t2->tv.tv_sec = t1->tv.tv_sec + usecs / 1000000;
107 t2->tv.tv_usec = t1->tv.tv_usec + usecs % 1000000;
108 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) || defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
109 util_time_get_frequency();
110 t2->counter = t1->counter + (usecs * frequency + INT64_C(999999))/INT64_C(1000000);
111 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
112 /* 1 tick = 100 nano seconds. */
113 t2->counter = t1->counter + usecs * 10;
114 #elif
115 LARGE_INTEGER temp;
116 LONGLONG freq;
117 freq = temp.QuadPart;
118 t2->counter = t1->counter + (usecs * freq)/1000000L;
119 #endif
120 }
121
122
123 int64_t
124 util_time_diff(const struct util_time *t1,
125 const struct util_time *t2)
126 {
127 #if defined(PIPE_OS_LINUX)
128 return (t2->tv.tv_usec - t1->tv.tv_usec) +
129 (t2->tv.tv_sec - t1->tv.tv_sec)*1000000;
130 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) || defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
131 util_time_get_frequency();
132 return (t2->counter - t1->counter)*INT64_C(1000000)/frequency;
133 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
134 return (t2->counter - t1->counter)/10;
135 #endif
136 }
137
138
139
140 uint64_t
141 util_time_micros( void )
142 {
143 struct util_time t1;
144
145 util_time_get(&t1);
146
147 #if defined(PIPE_OS_LINUX)
148 return t1.tv.tv_usec + t1.tv.tv_sec*1000000LL;
149 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) || defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
150 util_time_get_frequency();
151 return t1.counter*INT64_C(1000000)/frequency;
152 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
153 return t1.counter/10;
154 #endif
155 }
156
157
158
159 /**
160 * Compare two time values.
161 *
162 * Not publicly available because it does not take in account wrap-arounds.
163 * Use util_time_timeout instead.
164 */
165 static INLINE int
166 util_time_compare(const struct util_time *t1,
167 const struct util_time *t2)
168 {
169 #if defined(PIPE_OS_LINUX)
170 if (t1->tv.tv_sec < t2->tv.tv_sec)
171 return -1;
172 else if(t1->tv.tv_sec > t2->tv.tv_sec)
173 return 1;
174 else if (t1->tv.tv_usec < t2->tv.tv_usec)
175 return -1;
176 else if(t1->tv.tv_usec > t2->tv.tv_usec)
177 return 1;
178 else
179 return 0;
180 #elif defined(PIPE_OS_WINDOWS)
181 if (t1->counter < t2->counter)
182 return -1;
183 else if(t1->counter > t2->counter)
184 return 1;
185 else
186 return 0;
187 #endif
188 }
189
190
191 boolean
192 util_time_timeout(const struct util_time *start,
193 const struct util_time *end,
194 const struct util_time *curr)
195 {
196 if(util_time_compare(start, end) <= 0)
197 return !(util_time_compare(start, curr) <= 0 && util_time_compare(curr, end) < 0);
198 else
199 return !(util_time_compare(start, curr) <= 0 || util_time_compare(curr, end) < 0);
200 }
201
202
203 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
204 void util_time_sleep(unsigned usecs)
205 {
206 LONGLONG start, curr, end;
207
208 EngQueryPerformanceCounter(&start);
209
210 if(!frequency)
211 EngQueryPerformanceFrequency(&frequency);
212
213 end = start + (usecs * frequency + 999999LL)/1000000LL;
214
215 do {
216 EngQueryPerformanceCounter(&curr);
217 } while(start <= curr && curr < end ||
218 end < start && (curr < end || start <= curr));
219 }
220 #endif