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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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 */
67 timer = g_new (GRealTimer, 1);
71 timer->start = GetTickCount ();
72 #else /* !G_OS_WIN32 */
73 gettimeofday (&timer->start, NULL);
74 #endif /* !G_OS_WIN32 */
76 return ((GTimer*) timer);
80 g_timer_destroy (GTimer *timer)
82 g_return_if_fail (timer != NULL);
88 g_timer_start (GTimer *timer)
92 g_return_if_fail (timer != NULL);
94 rtimer = (GRealTimer*) timer;
95 rtimer->active = TRUE;
98 rtimer->start = GetTickCount ();
99 #else /* !G_OS_WIN32 */
100 gettimeofday (&rtimer->start, NULL);
101 #endif /* !G_OS_WIN32 */
105 g_timer_stop (GTimer *timer)
109 g_return_if_fail (timer != NULL);
111 rtimer = (GRealTimer*) timer;
112 rtimer->active = FALSE;
115 rtimer->end = GetTickCount ();
116 #else /* !G_OS_WIN32 */
117 gettimeofday (&rtimer->end, NULL);
118 #endif /* !G_OS_WIN32 */
122 g_timer_reset (GTimer *timer)
126 g_return_if_fail (timer != NULL);
128 rtimer = (GRealTimer*) timer;
131 rtimer->start = GetTickCount ();
132 #else /* !G_OS_WIN32 */
133 gettimeofday (&rtimer->start, NULL);
134 #endif /* !G_OS_WIN32 */
138 g_timer_elapsed (GTimer *timer,
139 gulong *microseconds)
144 struct timeval elapsed;
145 #endif /* G_OS_WIN32 */
147 g_return_val_if_fail (timer != NULL, 0);
149 rtimer = (GRealTimer*) timer;
153 rtimer->end = GetTickCount ();
155 /* Check for wraparound, which happens every 49.7 days.
156 * No, Win95 machines probably are never running for that long,
157 * but NT machines are.
159 if (rtimer->end < rtimer->start)
160 total = (UINT_MAX - (rtimer->start - rtimer->end)) / 1000.0;
162 total = (rtimer->end - rtimer->start) / 1000.0;
166 if (rtimer->end < rtimer->start)
168 ((UINT_MAX - (rtimer->start - rtimer->end)) % 1000) * 1000;
171 ((rtimer->end - rtimer->start) % 1000) * 1000;
173 #else /* !G_OS_WIN32 */
175 gettimeofday (&rtimer->end, NULL);
177 if (rtimer->start.tv_usec > rtimer->end.tv_usec)
179 rtimer->end.tv_usec += G_MICROSEC;
180 rtimer->end.tv_sec--;
183 elapsed.tv_usec = rtimer->end.tv_usec - rtimer->start.tv_usec;
184 elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec;
186 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
194 else if (microseconds)
195 *microseconds = elapsed.tv_usec;
197 #endif /* !G_OS_WIN32 */
203 g_usleep (gulong microseconds)
206 Sleep (microseconds / 1000);
209 tv.tv_sec = microseconds / G_MICROSEC;
210 tv.tv_usec = microseconds % G_MICROSEC;
211 select(0, NULL, NULL, NULL, &tv);