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 */
41 #endif /* G_OS_WIN32 */
45 #endif /* G_OS_WIN32 */
47 typedef struct _GRealTimer GRealTimer;
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 (GRealTimer, 1);
78 GETTIME (timer->start);
80 return ((GTimer*) timer);
84 g_timer_destroy (GTimer *timer)
86 g_return_if_fail (timer != NULL);
92 g_timer_start (GTimer *timer)
96 g_return_if_fail (timer != NULL);
98 rtimer = (GRealTimer*) timer;
99 rtimer->active = TRUE;
101 GETTIME (rtimer->start);
105 g_timer_stop (GTimer *timer)
109 g_return_if_fail (timer != NULL);
111 rtimer = (GRealTimer*) timer;
112 rtimer->active = FALSE;
114 GETTIME(rtimer->end);
118 g_timer_reset (GTimer *timer)
122 g_return_if_fail (timer != NULL);
124 rtimer = (GRealTimer*) timer;
126 GETTIME (rtimer->start);
130 g_timer_elapsed (GTimer *timer,
131 gulong *microseconds)
136 struct timeval elapsed;
137 #endif /* G_OS_WIN32 */
139 g_return_val_if_fail (timer != NULL, 0);
141 rtimer = (GRealTimer*) timer;
145 rtimer->end = GetTickCount ();
147 /* Check for wraparound, which happens every 49.7 days. */
148 if (rtimer->end < rtimer->start)
149 total = (UINT_MAX - (rtimer->start - rtimer->end)) / 1000.0;
151 total = (rtimer->end - rtimer->start) / 1000.0;
155 if (rtimer->end < rtimer->start)
157 ((UINT_MAX - (rtimer->start - rtimer->end)) % 1000) * 1000;
160 ((rtimer->end - rtimer->start) % 1000) * 1000;
162 #else /* !G_OS_WIN32 */
164 gettimeofday (&rtimer->end, NULL);
166 if (rtimer->start.tv_usec > rtimer->end.tv_usec)
168 rtimer->end.tv_usec += G_USEC_PER_SEC;
169 rtimer->end.tv_sec--;
172 elapsed.tv_usec = rtimer->end.tv_usec - rtimer->start.tv_usec;
173 elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec;
175 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
183 else if (microseconds)
184 *microseconds = elapsed.tv_usec;
186 #endif /* !G_OS_WIN32 */
192 g_usleep (gulong microseconds)
195 Sleep (microseconds / 1000);
197 if (g_thread_supported ())
199 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
200 static GCond* cond = NULL;
203 g_get_current_time (&end_time);
205 end_time.tv_sec += microseconds / G_USEC_PER_SEC;
206 end_time.tv_usec += microseconds % G_USEC_PER_SEC;
208 if (end_time.tv_usec >= G_USEC_PER_SEC)
210 end_time.tv_usec -= G_USEC_PER_SEC;
211 end_time.tv_sec += 1;
214 g_static_mutex_lock (&mutex);
217 cond = g_cond_new ();
219 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
223 g_static_mutex_unlock (&mutex);
228 tv.tv_sec = microseconds / G_USEC_PER_SEC;
229 tv.tv_usec = microseconds % G_USEC_PER_SEC;
230 select(0, NULL, NULL, NULL, &tv);