Add g_type_module_register_enum() and g_type_module_register_flags().
[glib.git] / glib / gtimer.c
blobb2f516bc4419e666641f29c64b4587d43199dd58
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 /*
28 * MT safe
31 #include "config.h"
32 #include "glibconfig.h"
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif /* HAVE_UNISTD_H */
38 #ifndef G_OS_WIN32
39 #include <sys/time.h>
40 #include <time.h>
41 #include <errno.h>
42 #endif /* G_OS_WIN32 */
44 #ifdef G_OS_WIN32
45 #include <windows.h>
46 #endif /* G_OS_WIN32 */
48 #include "glib.h"
51 struct _GTimer
53 #ifdef G_OS_WIN32
54 DWORD start;
55 DWORD end;
56 #else /* !G_OS_WIN32 */
57 struct timeval start;
58 struct timeval end;
59 #endif /* !G_OS_WIN32 */
61 guint active : 1;
64 #ifdef G_OS_WIN32
65 # define GETTIME(v) \
66 v = GetTickCount ()
67 #else /* !G_OS_WIN32 */
68 # define GETTIME(v) \
69 gettimeofday (&v, NULL)
70 #endif /* !G_OS_WIN32 */
72 GTimer*
73 g_timer_new (void)
75 GTimer *timer;
77 timer = g_new (GTimer, 1);
78 timer->active = TRUE;
80 GETTIME (timer->start);
82 return timer;
85 void
86 g_timer_destroy (GTimer *timer)
88 g_return_if_fail (timer != NULL);
90 g_free (timer);
93 void
94 g_timer_start (GTimer *timer)
96 g_return_if_fail (timer != NULL);
98 timer->active = TRUE;
100 GETTIME (timer->start);
103 void
104 g_timer_stop (GTimer *timer)
106 g_return_if_fail (timer != NULL);
108 timer->active = FALSE;
110 GETTIME(timer->end);
113 void
114 g_timer_reset (GTimer *timer)
116 g_return_if_fail (timer != NULL);
118 GETTIME (timer->start);
121 void
122 g_timer_continue (GTimer *timer)
124 #ifdef G_OS_WIN32
125 DWORD elapsed;
126 #else
127 struct timeval elapsed;
128 #endif /* G_OS_WIN32 */
130 g_return_if_fail (timer != NULL);
131 g_return_if_fail (timer->active == FALSE);
133 /* Get elapsed time and reset timer start time
134 * to the current time minus the previously
135 * elapsed interval.
138 #ifdef G_OS_WIN32
140 elapsed = timer->end - timer->start;
142 GETTIME (timer->start);
144 timer->start -= elapsed;
146 #else /* !G_OS_WIN32 */
148 if (timer->start.tv_usec > timer->end.tv_usec)
150 timer->end.tv_usec += G_USEC_PER_SEC;
151 timer->end.tv_sec--;
154 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
155 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
157 GETTIME (timer->start);
159 if (timer->start.tv_usec < elapsed.tv_usec)
161 timer->start.tv_usec += G_USEC_PER_SEC;
162 timer->start.tv_sec--;
165 timer->start.tv_usec -= elapsed.tv_usec;
166 timer->start.tv_sec -= elapsed.tv_sec;
168 #endif /* !G_OS_WIN32 */
170 timer->active = TRUE;
173 gdouble
174 g_timer_elapsed (GTimer *timer,
175 gulong *microseconds)
177 gdouble total;
178 #ifndef G_OS_WIN32
179 struct timeval elapsed;
180 #endif /* G_OS_WIN32 */
182 g_return_val_if_fail (timer != NULL, 0);
184 #ifdef G_OS_WIN32
185 if (timer->active)
186 timer->end = GetTickCount ();
188 /* Check for wraparound, which happens every 49.7 days. */
189 if (timer->end < timer->start)
190 total = (UINT_MAX - (timer->start - timer->end - 1)) / 1000.0;
191 else
192 total = (timer->end - timer->start) / 1000.0;
194 if (microseconds)
196 if (timer->end < timer->start)
197 *microseconds =
198 ((UINT_MAX - (timer->start - timer->end - 1)) % 1000) * 1000;
199 else
200 *microseconds =
201 ((timer->end - timer->start) % 1000) * 1000;
203 #else /* !G_OS_WIN32 */
204 if (timer->active)
205 gettimeofday (&timer->end, NULL);
207 if (timer->start.tv_usec > timer->end.tv_usec)
209 timer->end.tv_usec += G_USEC_PER_SEC;
210 timer->end.tv_sec--;
213 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
214 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
216 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
217 if (total < 0)
219 total = 0;
221 if (microseconds)
222 *microseconds = 0;
224 else if (microseconds)
225 *microseconds = elapsed.tv_usec;
227 #endif /* !G_OS_WIN32 */
229 return total;
232 void
233 g_usleep (gulong microseconds)
235 #ifdef G_OS_WIN32
236 Sleep (microseconds / 1000);
237 #else /* !G_OS_WIN32 */
238 # ifdef HAVE_NANOSLEEP
239 struct timespec request, remaining;
240 request.tv_sec = microseconds / G_USEC_PER_SEC;
241 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
242 while (nanosleep (&request, &remaining) == EINTR)
243 request = remaining;
244 # else /* !HAVE_NANOSLEEP */
245 if (g_thread_supported ())
247 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
248 static GCond* cond = NULL;
249 GTimeVal end_time;
251 g_get_current_time (&end_time);
252 if (microseconds > G_MAXLONG)
254 microseconds -= G_MAXLONG;
255 g_time_val_add (&end_time, G_MAXLONG);
257 g_time_val_add (&end_time, microseconds);
259 g_static_mutex_lock (&mutex);
261 if (!cond)
262 cond = g_cond_new ();
264 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
265 &end_time))
266 /* do nothing */;
268 g_static_mutex_unlock (&mutex);
270 else
272 struct timeval tv;
273 tv.tv_sec = microseconds / G_USEC_PER_SEC;
274 tv.tv_usec = microseconds % G_USEC_PER_SEC;
275 select(0, NULL, NULL, NULL, &tv);
277 # endif /* !HAVE_NANOSLEEP */
278 #endif /* !G_OS_WIN32 */
282 * g_time_val_add:
283 * @time_: a #GTimeVal
284 * @microseconds: number of microseconds to add to @time
286 * Adds the given number of microseconds to @time_. @microseconds can
287 * also be negative to decrease the value of @time_.
289 void
290 g_time_val_add (GTimeVal *time_, glong microseconds)
292 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
294 if (microseconds >= 0)
296 time_->tv_usec += microseconds % G_USEC_PER_SEC;
297 time_->tv_sec += microseconds / G_USEC_PER_SEC;
298 if (time_->tv_usec >= G_USEC_PER_SEC)
300 time_->tv_usec -= G_USEC_PER_SEC;
301 time_->tv_sec++;
304 else
306 microseconds *= -1;
307 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
308 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
309 if (time_->tv_usec < 0)
311 time_->tv_usec += G_USEC_PER_SEC;
312 time_->tv_sec--;