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 */
54 #else /* !G_OS_WIN32 */
57 #endif /* !G_OS_WIN32 */
65 #else /* !G_OS_WIN32 */
67 gettimeofday (&v, NULL)
68 #endif /* !G_OS_WIN32 */
75 timer = g_new (GTimer, 1);
78 GETTIME (timer->start);
84 g_timer_destroy (GTimer *timer)
86 g_return_if_fail (timer != NULL);
92 g_timer_start (GTimer *timer)
94 g_return_if_fail (timer != NULL);
98 GETTIME (timer->start);
102 g_timer_stop (GTimer *timer)
104 g_return_if_fail (timer != NULL);
106 timer->active = FALSE;
112 g_timer_reset (GTimer *timer)
114 g_return_if_fail (timer != NULL);
116 GETTIME (timer->start);
120 g_timer_elapsed (GTimer *timer,
121 gulong *microseconds)
125 struct timeval elapsed;
126 #endif /* G_OS_WIN32 */
128 g_return_val_if_fail (timer != NULL, 0);
132 timer->end = GetTickCount ();
134 /* Check for wraparound, which happens every 49.7 days. */
135 if (timer->end < timer->start)
136 total = (UINT_MAX - (timer->start - timer->end)) / 1000.0;
138 total = (timer->end - timer->start) / 1000.0;
142 if (timer->end < timer->start)
144 ((UINT_MAX - (timer->start - timer->end)) % 1000) * 1000;
147 ((timer->end - timer->start) % 1000) * 1000;
149 #else /* !G_OS_WIN32 */
151 gettimeofday (&timer->end, NULL);
153 if (timer->start.tv_usec > timer->end.tv_usec)
155 timer->end.tv_usec += G_USEC_PER_SEC;
159 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
160 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
162 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
170 else if (microseconds)
171 *microseconds = elapsed.tv_usec;
173 #endif /* !G_OS_WIN32 */
179 g_usleep (gulong microseconds)
182 Sleep (microseconds / 1000);
183 #else /* !G_OS_WIN32 */
184 # ifdef HAVE_NANOSLEEP
185 struct timespec request, remaining;
186 request.tv_sec = microseconds / G_USEC_PER_SEC;
187 request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
188 while (nanosleep (&request, &remaining) == EINTR)
190 # else /* !HAVE_NANOSLEEP */
191 if (g_thread_supported ())
193 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
194 static GCond* cond = NULL;
197 g_get_current_time (&end_time);
198 if (microseconds > G_MAXLONG)
200 microseconds -= G_MAXLONG;
201 g_time_val_add (&end_time, G_MAXLONG);
203 g_time_val_add (&end_time, microseconds);
205 g_static_mutex_lock (&mutex);
208 cond = g_cond_new ();
210 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
214 g_static_mutex_unlock (&mutex);
219 tv.tv_sec = microseconds / G_USEC_PER_SEC;
220 tv.tv_usec = microseconds % G_USEC_PER_SEC;
221 select(0, NULL, NULL, NULL, &tv);
223 # endif /* !HAVE_NANOSLEEP */
224 #endif /* !G_OS_WIN32 */
229 * @time_: a #GTimeVal
230 * @microseconds: number of microseconds to add to @time
232 * Adds the given number of microseconds to @time_. @microseconds can
233 * also be negative to decrease the value of @time_.
236 g_time_val_add (GTimeVal *time_, glong microseconds)
238 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
240 if (microseconds >= 0)
242 time_->tv_usec += microseconds % G_USEC_PER_SEC;
243 time_->tv_sec += microseconds / G_USEC_PER_SEC;
244 if (time_->tv_usec >= G_USEC_PER_SEC)
246 time_->tv_usec -= G_USEC_PER_SEC;
253 time_->tv_usec -= microseconds % G_USEC_PER_SEC;
254 time_->tv_sec -= microseconds / G_USEC_PER_SEC;
255 if (time_->tv_usec < 0)
257 time_->tv_usec += G_USEC_PER_SEC;