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 */
57 #else /* !G_OS_WIN32 */
60 #endif /* !G_OS_WIN32 */
67 GetSystemTimeAsFileTime ((FILETIME *)&v)
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)
182 struct timeval elapsed;
183 #endif /* G_OS_WIN32 */
185 g_return_val_if_fail (timer != NULL, 0);
189 GETTIME (timer->end);
191 elapsed = timer->end - timer->start;
193 total = elapsed / 1e7;
196 *microseconds = (elapsed / 10) % 1000000;
197 #else /* !G_OS_WIN32 */
199 gettimeofday (&timer->end, NULL);
201 if (timer->start.tv_usec > timer->end.tv_usec)
203 timer->end.tv_usec += G_USEC_PER_SEC;
207 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
208 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
210 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
218 else if (microseconds)
219 *microseconds = elapsed.tv_usec;
221 #endif /* !G_OS_WIN32 */
227 g_usleep (gulong microseconds)
230 Sleep (microseconds / 1000);
231 #else /* !G_OS_WIN32 */
232 # ifdef HAVE_NANOSLEEP
233 struct timespec request, remaining;
234 request.tv_sec = microseconds / G_USEC_PER_SEC;
235 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
236 while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
238 # else /* !HAVE_NANOSLEEP */
239 if (g_thread_supported ())
241 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
242 static GCond* cond = NULL;
245 g_get_current_time (&end_time);
246 if (microseconds > G_MAXLONG)
248 microseconds -= G_MAXLONG;
249 g_time_val_add (&end_time, G_MAXLONG);
251 g_time_val_add (&end_time, microseconds);
253 g_static_mutex_lock (&mutex);
256 cond = g_cond_new ();
258 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
262 g_static_mutex_unlock (&mutex);
267 tv.tv_sec = microseconds / G_USEC_PER_SEC;
268 tv.tv_usec = microseconds % G_USEC_PER_SEC;
269 select(0, NULL, NULL, NULL, &tv);
271 # endif /* !HAVE_NANOSLEEP */
272 #endif /* !G_OS_WIN32 */
277 * @time_: a #GTimeVal
278 * @microseconds: number of microseconds to add to @time
280 * Adds the given number of microseconds to @time_. @microseconds can
281 * also be negative to decrease the value of @time_.
284 g_time_val_add (GTimeVal *time_, glong microseconds)
286 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
288 if (microseconds >= 0)
290 time_->tv_usec += microseconds % G_USEC_PER_SEC;
291 time_->tv_sec += microseconds / G_USEC_PER_SEC;
292 if (time_->tv_usec >= G_USEC_PER_SEC)
294 time_->tv_usec -= G_USEC_PER_SEC;
301 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
302 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
303 if (time_->tv_usec < 0)
305 time_->tv_usec += G_USEC_PER_SEC;
311 #define __G_TIMER_C__
312 #include "galiasdef.c"