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_elapsed (GTimer *timer,
123 gulong *microseconds)
127 struct timeval elapsed;
128 #endif /* G_OS_WIN32 */
130 g_return_val_if_fail (timer != NULL, 0);
134 timer->end = GetTickCount ();
136 /* Check for wraparound, which happens every 49.7 days. */
137 if (timer->end < timer->start)
138 total = (UINT_MAX - (timer->start - timer->end - 1)) / 1000.0;
140 total = (timer->end - timer->start) / 1000.0;
144 if (timer->end < timer->start)
146 ((UINT_MAX - (timer->start - timer->end - 1)) % 1000) * 1000;
149 ((timer->end - timer->start) % 1000) * 1000;
151 #else /* !G_OS_WIN32 */
153 gettimeofday (&timer->end, NULL);
155 if (timer->start.tv_usec > timer->end.tv_usec)
157 timer->end.tv_usec += G_USEC_PER_SEC;
161 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
162 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
164 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
172 else if (microseconds)
173 *microseconds = elapsed.tv_usec;
175 #endif /* !G_OS_WIN32 */
181 g_usleep (gulong microseconds)
184 Sleep (microseconds / 1000);
185 #else /* !G_OS_WIN32 */
186 # ifdef HAVE_NANOSLEEP
187 struct timespec request, remaining;
188 request.tv_sec = microseconds / G_USEC_PER_SEC;
189 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
190 while (nanosleep (&request, &remaining) == EINTR)
192 # else /* !HAVE_NANOSLEEP */
193 if (g_thread_supported ())
195 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
196 static GCond* cond = NULL;
199 g_get_current_time (&end_time);
200 if (microseconds > G_MAXLONG)
202 microseconds -= G_MAXLONG;
203 g_time_val_add (&end_time, G_MAXLONG);
205 g_time_val_add (&end_time, microseconds);
207 g_static_mutex_lock (&mutex);
210 cond = g_cond_new ();
212 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
216 g_static_mutex_unlock (&mutex);
221 tv.tv_sec = microseconds / G_USEC_PER_SEC;
222 tv.tv_usec = microseconds % G_USEC_PER_SEC;
223 select(0, NULL, NULL, NULL, &tv);
225 # endif /* !HAVE_NANOSLEEP */
226 #endif /* !G_OS_WIN32 */
231 * @time_: a #GTimeVal
232 * @microseconds: number of microseconds to add to @time
234 * Adds the given number of microseconds to @time_. @microseconds can
235 * also be negative to decrease the value of @time_.
238 g_time_val_add (GTimeVal *time_, glong microseconds)
240 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
242 if (microseconds >= 0)
244 time_->tv_usec += microseconds % G_USEC_PER_SEC;
245 time_->tv_sec += microseconds / G_USEC_PER_SEC;
246 if (time_->tv_usec >= G_USEC_PER_SEC)
248 time_->tv_usec -= G_USEC_PER_SEC;
255 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
256 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
257 if (time_->tv_usec < 0)
259 time_->tv_usec += G_USEC_PER_SEC;