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.
27 #endif /* HAVE_UNISTD_H */
30 #endif /* NATIVE_WIN32 */
34 #endif /* NATIVE_WIN32 */
36 typedef struct _GRealTimer GRealTimer;
43 #else /* !NATIVE_WIN32 */
46 #endif /* !NATIVE_WIN32 */
56 timer = g_new (GRealTimer, 1);
60 timer->start = GetTickCount ();
61 #else /* !NATIVE_WIN32 */
62 gettimeofday (&timer->start, NULL);
63 #endif /* !NATIVE_WIN32 */
65 return ((GTimer*) timer);
69 g_timer_destroy (GTimer *timer)
71 g_assert (timer != NULL);
77 g_timer_start (GTimer *timer)
81 g_assert (timer != NULL);
83 rtimer = (GRealTimer*) timer;
84 rtimer->active = TRUE;
87 rtimer->start = GetTickCount ();
88 #else /* !NATIVE_WIN32 */
89 gettimeofday (&rtimer->start, NULL);
90 #endif /* !NATIVE_WIN32 */
94 g_timer_stop (GTimer *timer)
98 g_assert (timer != NULL);
100 rtimer = (GRealTimer*) timer;
101 rtimer->active = FALSE;
104 rtimer->end = GetTickCount ();
105 #else /* !NATIVE_WIN32 */
106 gettimeofday (&rtimer->end, NULL);
107 #endif /* !NATIVE_WIN32 */
111 g_timer_reset (GTimer *timer)
115 g_assert (timer != NULL);
117 rtimer = (GRealTimer*) timer;
120 rtimer->start = GetTickCount ();
121 #else /* !NATIVE_WIN32 */
122 gettimeofday (&rtimer->start, NULL);
123 #endif /* !NATIVE_WIN32 */
127 g_timer_elapsed (GTimer *timer,
128 gulong *microseconds)
133 struct timeval elapsed;
134 #endif /* NATIVE_WIN32 */
136 g_return_val_if_fail (timer != NULL, 0);
138 rtimer = (GRealTimer*) timer;
142 rtimer->end = GetTickCount ();
144 /* Check for wraparound, which happens every 49.7 days.
145 * No, Win95 machines probably are never running for that long,
146 * but NT machines are.
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 /* !NATIVE_WIN32 */
164 gettimeofday (&rtimer->end, NULL);
166 if (rtimer->start.tv_usec > rtimer->end.tv_usec)
168 rtimer->end.tv_usec += 1000000;
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);
178 *microseconds = elapsed.tv_usec;
179 #endif /* !NATIVE_WIN32 */