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/.
32 #include "glibconfig.h"
36 #endif /* HAVE_UNISTD_H */
42 #endif /* G_OS_WIN32 */
46 #endif /* G_OS_WIN32 */
56 #else /* !G_OS_WIN32 */
59 #endif /* !G_OS_WIN32 */
67 #else /* !G_OS_WIN32 */
69 gettimeofday (&v, NULL)
70 #endif /* !G_OS_WIN32 */
77 timer = g_new (GTimer, 1);
80 GETTIME (timer->start);
86 g_timer_destroy (GTimer *timer)
88 g_return_if_fail (timer != NULL);
94 g_timer_start (GTimer *timer)
96 g_return_if_fail (timer != NULL);
100 GETTIME (timer->start);
104 g_timer_stop (GTimer *timer)
106 g_return_if_fail (timer != NULL);
108 timer->active = FALSE;
114 g_timer_reset (GTimer *timer)
116 g_return_if_fail (timer != NULL);
118 GETTIME (timer->start);
122 g_timer_continue (GTimer *timer)
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
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;
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;
174 g_timer_elapsed (GTimer *timer,
175 gulong *microseconds)
179 struct timeval elapsed;
180 #endif /* G_OS_WIN32 */
182 g_return_val_if_fail (timer != NULL, 0);
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;
192 total = (timer->end - timer->start) / 1000.0;
196 if (timer->end < timer->start)
198 ((UINT_MAX - (timer->start - timer->end - 1)) % 1000) * 1000;
201 ((timer->end - timer->start) % 1000) * 1000;
203 #else /* !G_OS_WIN32 */
205 gettimeofday (&timer->end, NULL);
207 if (timer->start.tv_usec > timer->end.tv_usec)
209 timer->end.tv_usec += G_USEC_PER_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);
224 else if (microseconds)
225 *microseconds = elapsed.tv_usec;
227 #endif /* !G_OS_WIN32 */
233 g_usleep (gulong microseconds)
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)
244 # else /* !HAVE_NANOSLEEP */
245 if (g_thread_supported ())
247 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
248 static GCond* cond = NULL;
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);
262 cond = g_cond_new ();
264 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
268 g_static_mutex_unlock (&mutex);
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 */
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_.
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;
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;