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.
22 #endif /* HAVE_UNISTD_H */
25 #endif /* NATIVE_WIN32 */
29 #endif /* NATIVE_WIN32 */
31 typedef struct _GRealTimer GRealTimer;
38 #else /* !NATIVE_WIN32 */
41 #endif /* !NATIVE_WIN32 */
51 timer = g_new (GRealTimer, 1);
55 timer->start = GetTickCount ();
56 #else /* !NATIVE_WIN32 */
57 gettimeofday (&timer->start, NULL);
58 #endif /* !NATIVE_WIN32 */
60 return ((GTimer*) timer);
64 g_timer_destroy (GTimer *timer)
66 g_assert (timer != NULL);
72 g_timer_start (GTimer *timer)
76 g_assert (timer != NULL);
78 rtimer = (GRealTimer*) timer;
79 rtimer->active = TRUE;
82 rtimer->start = GetTickCount ();
83 #else /* !NATIVE_WIN32 */
84 gettimeofday (&rtimer->start, NULL);
85 #endif /* !NATIVE_WIN32 */
89 g_timer_stop (GTimer *timer)
93 g_assert (timer != NULL);
95 rtimer = (GRealTimer*) timer;
96 rtimer->active = FALSE;
99 rtimer->end = GetTickCount ();
100 #else /* !NATIVE_WIN32 */
101 gettimeofday (&rtimer->end, NULL);
102 #endif /* !NATIVE_WIN32 */
106 g_timer_reset (GTimer *timer)
110 g_assert (timer != NULL);
112 rtimer = (GRealTimer*) timer;
115 rtimer->start = GetTickCount ();
116 #else /* !NATIVE_WIN32 */
117 gettimeofday (&rtimer->start, NULL);
118 #endif /* !NATIVE_WIN32 */
122 g_timer_elapsed (GTimer *timer,
123 gulong *microseconds)
128 struct timeval elapsed;
129 #endif /* NATIVE_WIN32 */
131 g_return_val_if_fail (timer != NULL, 0);
133 rtimer = (GRealTimer*) timer;
137 rtimer->end = GetTickCount ();
139 /* Check for wraparound, which happens every 49.7 days.
140 * No, Win95 machines probably are never running for that long,
141 * but NT machines are.
143 if (rtimer->end < rtimer->start)
144 total = (UINT_MAX - (rtimer->start - rtimer->end)) / 1000.0;
146 total = (rtimer->end - rtimer->start) / 1000.0;
150 if (rtimer->end < rtimer->start)
152 ((UINT_MAX - (rtimer->start - rtimer->end)) % 1000) * 1000;
155 ((rtimer->end - rtimer->start) % 1000) * 1000;
157 #else /* !NATIVE_WIN32 */
159 gettimeofday (&rtimer->end, NULL);
161 if (rtimer->start.tv_usec > rtimer->end.tv_usec)
163 rtimer->end.tv_usec += 1000000;
164 rtimer->end.tv_sec--;
167 elapsed.tv_usec = rtimer->end.tv_usec - rtimer->start.tv_usec;
168 elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec;
170 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
173 *microseconds = elapsed.tv_usec;
174 #endif /* !NATIVE_WIN32 */