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.
31 #endif /* HAVE_UNISTD_H */
34 #endif /* NATIVE_WIN32 */
38 #endif /* NATIVE_WIN32 */
40 typedef struct _GRealTimer GRealTimer
;
47 #else /* !NATIVE_WIN32 */
50 #endif /* !NATIVE_WIN32 */
60 timer
= g_new (GRealTimer
, 1);
64 timer
->start
= GetTickCount ();
65 #else /* !NATIVE_WIN32 */
66 gettimeofday (&timer
->start
, NULL
);
67 #endif /* !NATIVE_WIN32 */
69 return ((GTimer
*) timer
);
73 g_timer_destroy (GTimer
*timer
)
75 g_assert (timer
!= NULL
);
81 g_timer_start (GTimer
*timer
)
85 g_assert (timer
!= NULL
);
87 rtimer
= (GRealTimer
*) timer
;
88 rtimer
->active
= TRUE
;
91 rtimer
->start
= GetTickCount ();
92 #else /* !NATIVE_WIN32 */
93 gettimeofday (&rtimer
->start
, NULL
);
94 #endif /* !NATIVE_WIN32 */
98 g_timer_stop (GTimer
*timer
)
102 g_assert (timer
!= NULL
);
104 rtimer
= (GRealTimer
*) timer
;
105 rtimer
->active
= FALSE
;
108 rtimer
->end
= GetTickCount ();
109 #else /* !NATIVE_WIN32 */
110 gettimeofday (&rtimer
->end
, NULL
);
111 #endif /* !NATIVE_WIN32 */
115 g_timer_reset (GTimer
*timer
)
119 g_assert (timer
!= NULL
);
121 rtimer
= (GRealTimer
*) timer
;
124 rtimer
->start
= GetTickCount ();
125 #else /* !NATIVE_WIN32 */
126 gettimeofday (&rtimer
->start
, NULL
);
127 #endif /* !NATIVE_WIN32 */
131 g_timer_elapsed (GTimer
*timer
,
132 gulong
*microseconds
)
137 struct timeval elapsed
;
138 #endif /* NATIVE_WIN32 */
140 g_return_val_if_fail (timer
!= NULL
, 0);
142 rtimer
= (GRealTimer
*) timer
;
146 rtimer
->end
= GetTickCount ();
148 /* Check for wraparound, which happens every 49.7 days.
149 * No, Win95 machines probably are never running for that long,
150 * but NT machines are.
152 if (rtimer
->end
< rtimer
->start
)
153 total
= (UINT_MAX
- (rtimer
->start
- rtimer
->end
)) / 1000.0;
155 total
= (rtimer
->end
- rtimer
->start
) / 1000.0;
159 if (rtimer
->end
< rtimer
->start
)
161 ((UINT_MAX
- (rtimer
->start
- rtimer
->end
)) % 1000) * 1000;
164 ((rtimer
->end
- rtimer
->start
) % 1000) * 1000;
166 #else /* !NATIVE_WIN32 */
168 gettimeofday (&rtimer
->end
, NULL
);
170 if (rtimer
->start
.tv_usec
> rtimer
->end
.tv_usec
)
172 rtimer
->end
.tv_usec
+= 1000000;
173 rtimer
->end
.tv_sec
--;
176 elapsed
.tv_usec
= rtimer
->end
.tv_usec
- rtimer
->start
.tv_usec
;
177 elapsed
.tv_sec
= rtimer
->end
.tv_sec
- rtimer
->start
.tv_sec
;
179 total
= elapsed
.tv_sec
+ ((gdouble
) elapsed
.tv_usec
/ 1e6
);
182 *microseconds
= elapsed
.tv_usec
;
183 #endif /* !NATIVE_WIN32 */