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/.
33 #include "glibconfig.h"
37 #endif /* HAVE_UNISTD_H */
43 #endif /* G_OS_WIN32 */
47 #endif /* G_OS_WIN32 */
57 #else /* !G_OS_WIN32 */
60 #endif /* !G_OS_WIN32 */
68 #else /* !G_OS_WIN32 */
70 gettimeofday (&v, NULL)
71 #endif /* !G_OS_WIN32 */
78 timer = g_new (GTimer, 1);
81 GETTIME (timer->start);
87 g_timer_destroy (GTimer *timer)
89 g_return_if_fail (timer != NULL);
95 g_timer_start (GTimer *timer)
97 g_return_if_fail (timer != NULL);
101 GETTIME (timer->start);
105 g_timer_stop (GTimer *timer)
107 g_return_if_fail (timer != NULL);
109 timer->active = FALSE;
115 g_timer_reset (GTimer *timer)
117 g_return_if_fail (timer != NULL);
119 GETTIME (timer->start);
123 g_timer_continue (GTimer *timer)
128 struct timeval elapsed;
129 #endif /* G_OS_WIN32 */
131 g_return_if_fail (timer != NULL);
132 g_return_if_fail (timer->active == FALSE);
134 /* Get elapsed time and reset timer start time
135 * to the current time minus the previously
141 elapsed = timer->end - timer->start;
143 GETTIME (timer->start);
145 timer->start -= elapsed;
147 #else /* !G_OS_WIN32 */
149 if (timer->start.tv_usec > timer->end.tv_usec)
151 timer->end.tv_usec += G_USEC_PER_SEC;
155 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
156 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
158 GETTIME (timer->start);
160 if (timer->start.tv_usec < elapsed.tv_usec)
162 timer->start.tv_usec += G_USEC_PER_SEC;
163 timer->start.tv_sec--;
166 timer->start.tv_usec -= elapsed.tv_usec;
167 timer->start.tv_sec -= elapsed.tv_sec;
169 #endif /* !G_OS_WIN32 */
171 timer->active = TRUE;
175 g_timer_elapsed (GTimer *timer,
176 gulong *microseconds)
180 struct timeval elapsed;
181 #endif /* G_OS_WIN32 */
183 g_return_val_if_fail (timer != NULL, 0);
187 timer->end = GetTickCount ();
189 /* Check for wraparound, which happens every 49.7 days. */
190 if (timer->end < timer->start)
191 total = (UINT_MAX - (timer->start - timer->end - 1)) / 1000.0;
193 total = (timer->end - timer->start) / 1000.0;
197 if (timer->end < timer->start)
199 ((UINT_MAX - (timer->start - timer->end - 1)) % 1000) * 1000;
202 ((timer->end - timer->start) % 1000) * 1000;
204 #else /* !G_OS_WIN32 */
206 gettimeofday (&timer->end, NULL);
208 if (timer->start.tv_usec > timer->end.tv_usec)
210 timer->end.tv_usec += G_USEC_PER_SEC;
214 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
215 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
217 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
225 else if (microseconds)
226 *microseconds = elapsed.tv_usec;
228 #endif /* !G_OS_WIN32 */
234 g_usleep (gulong microseconds)
237 Sleep (microseconds / 1000);
238 #else /* !G_OS_WIN32 */
239 # ifdef HAVE_NANOSLEEP
240 struct timespec request, remaining;
241 request.tv_sec = microseconds / G_USEC_PER_SEC;
242 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
243 while (nanosleep (&request, &remaining) == EINTR)
245 # else /* !HAVE_NANOSLEEP */
246 if (g_thread_supported ())
248 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
249 static GCond* cond = NULL;
252 g_get_current_time (&end_time);
253 if (microseconds > G_MAXLONG)
255 microseconds -= G_MAXLONG;
256 g_time_val_add (&end_time, G_MAXLONG);
258 g_time_val_add (&end_time, microseconds);
260 g_static_mutex_lock (&mutex);
263 cond = g_cond_new ();
265 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
269 g_static_mutex_unlock (&mutex);
274 tv.tv_sec = microseconds / G_USEC_PER_SEC;
275 tv.tv_usec = microseconds % G_USEC_PER_SEC;
276 select(0, NULL, NULL, NULL, &tv);
278 # endif /* !HAVE_NANOSLEEP */
279 #endif /* !G_OS_WIN32 */
284 * @time_: a #GTimeVal
285 * @microseconds: number of microseconds to add to @time
287 * Adds the given number of microseconds to @time_. @microseconds can
288 * also be negative to decrease the value of @time_.
291 g_time_val_add (GTimeVal *time_, glong microseconds)
293 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
295 if (microseconds >= 0)
297 time_->tv_usec += microseconds % G_USEC_PER_SEC;
298 time_->tv_sec += microseconds / G_USEC_PER_SEC;
299 if (time_->tv_usec >= G_USEC_PER_SEC)
301 time_->tv_usec -= G_USEC_PER_SEC;
308 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
309 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
310 if (time_->tv_usec < 0)
312 time_->tv_usec += G_USEC_PER_SEC;