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/.
38 #endif /* HAVE_UNISTD_H */
43 #endif /* G_OS_WIN32 */
47 #endif /* G_OS_WIN32 */
49 typedef struct _GRealTimer GRealTimer;
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 (GRealTimer, 1);
80 GETTIME (timer->start);
82 return ((GTimer*) timer);
86 g_timer_destroy (GTimer *timer)
88 g_return_if_fail (timer != NULL);
94 g_timer_start (GTimer *timer)
98 g_return_if_fail (timer != NULL);
100 rtimer = (GRealTimer*) timer;
101 rtimer->active = TRUE;
103 GETTIME (rtimer->start);
107 g_timer_stop (GTimer *timer)
111 g_return_if_fail (timer != NULL);
113 rtimer = (GRealTimer*) timer;
114 rtimer->active = FALSE;
116 GETTIME(rtimer->end);
120 g_timer_reset (GTimer *timer)
124 g_return_if_fail (timer != NULL);
126 rtimer = (GRealTimer*) timer;
128 GETTIME (rtimer->start);
132 g_timer_elapsed (GTimer *timer,
133 gulong *microseconds)
138 struct timeval elapsed;
139 #endif /* G_OS_WIN32 */
141 g_return_val_if_fail (timer != NULL, 0);
143 rtimer = (GRealTimer*) timer;
147 rtimer->end = GetTickCount ();
149 /* Check for wraparound, which happens every 49.7 days. */
150 if (rtimer->end < rtimer->start)
151 total = (UINT_MAX - (rtimer->start - rtimer->end)) / 1000.0;
153 total = (rtimer->end - rtimer->start) / 1000.0;
157 if (rtimer->end < rtimer->start)
159 ((UINT_MAX - (rtimer->start - rtimer->end)) % 1000) * 1000;
162 ((rtimer->end - rtimer->start) % 1000) * 1000;
164 #else /* !G_OS_WIN32 */
166 gettimeofday (&rtimer->end, NULL);
168 if (rtimer->start.tv_usec > rtimer->end.tv_usec)
170 rtimer->end.tv_usec += G_USEC_PER_SEC;
171 rtimer->end.tv_sec--;
174 elapsed.tv_usec = rtimer->end.tv_usec - rtimer->start.tv_usec;
175 elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec;
177 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
185 else if (microseconds)
186 *microseconds = elapsed.tv_usec;
188 #endif /* !G_OS_WIN32 */
194 g_usleep (gulong microseconds)
197 Sleep (microseconds / 1000);
198 #else /* !G_OS_WIN32 */
199 # ifdef HAVE_NANOSLEEP
200 struct timespec request, remaining;
201 request.tv_sec = microseconds / G_USEC_PER_SEC;
202 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
203 while (nanosleep (&request, &remaining) == EINTR)
205 # else /* !HAVE_NANOSLEEP */
206 if (g_thread_supported ())
208 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
209 static GCond* cond = NULL;
212 g_get_current_time (&end_time);
213 if (microseconds > G_MAXLONG)
215 microseconds -= G_MAXLONG;
216 g_time_val_add (&end_time, G_MAXLONG);
218 g_time_val_add (&end_time, microseconds);
220 g_static_mutex_lock (&mutex);
223 cond = g_cond_new ();
225 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
229 g_static_mutex_unlock (&mutex);
234 tv.tv_sec = microseconds / G_USEC_PER_SEC;
235 tv.tv_usec = microseconds % G_USEC_PER_SEC;
236 select(0, NULL, NULL, NULL, &tv);
238 # endif /* !HAVE_NANOSLEEP */
239 #endif /* !G_OS_WIN32 */
245 * @microseconds: number of microseconds to add to @time
247 * Adds the given number of microseconds to @time. @microseconds can
248 * also be negative to decrease the value of @time.
251 g_time_val_add (GTimeVal *time, glong microseconds)
253 g_return_if_fail (time->tv_usec >= 0 && time->tv_usec < G_USEC_PER_SEC);
255 if (microseconds >= 0)
257 time->tv_usec += microseconds % G_USEC_PER_SEC;
258 time->tv_sec += microseconds / G_USEC_PER_SEC;
259 if (time->tv_usec >= G_USEC_PER_SEC)
261 time->tv_usec -= G_USEC_PER_SEC;
268 time->tv_usec -= microseconds % G_USEC_PER_SEC;
269 time->tv_sec -= microseconds / G_USEC_PER_SEC;
270 if (time->tv_usec < 0)
272 time->tv_usec += G_USEC_PER_SEC;