applied glib-tml-981101-1 patch from Tor Lillqvist (ChangeLog entry
[platform/upstream/glib.git] / glib / gtimer.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "glib.h"
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif /* HAVE_UNISTD_H */
28 #ifndef NATIVE_WIN32
29 #include <sys/time.h>
30 #endif /* NATIVE_WIN32 */
31
32 #ifdef NATIVE_WIN32
33 #include <windows.h>
34 #endif /* NATIVE_WIN32 */
35
36 typedef struct _GRealTimer GRealTimer;
37
38 struct _GRealTimer
39 {
40 #ifdef NATIVE_WIN32
41   DWORD start;
42   DWORD end;
43 #else /* !NATIVE_WIN32 */
44   struct timeval start;
45   struct timeval end;
46 #endif /* !NATIVE_WIN32 */
47
48   guint active : 1;
49 };
50
51 GTimer*
52 g_timer_new (void)
53 {
54   GRealTimer *timer;
55
56   timer = g_new (GRealTimer, 1);
57   timer->active = TRUE;
58
59 #ifdef NATIVE_WIN32
60   timer->start = GetTickCount ();
61 #else /* !NATIVE_WIN32 */
62   gettimeofday (&timer->start, NULL);
63 #endif /* !NATIVE_WIN32 */
64
65   return ((GTimer*) timer);
66 }
67
68 void
69 g_timer_destroy (GTimer *timer)
70 {
71   g_assert (timer != NULL);
72
73   g_free (timer);
74 }
75
76 void
77 g_timer_start (GTimer *timer)
78 {
79   GRealTimer *rtimer;
80
81   g_assert (timer != NULL);
82
83   rtimer = (GRealTimer*) timer;
84   rtimer->active = TRUE;
85
86 #ifdef NATIVE_WIN32
87   rtimer->start = GetTickCount ();
88 #else /* !NATIVE_WIN32 */
89   gettimeofday (&rtimer->start, NULL);
90 #endif /* !NATIVE_WIN32 */
91 }
92
93 void
94 g_timer_stop (GTimer *timer)
95 {
96   GRealTimer *rtimer;
97
98   g_assert (timer != NULL);
99
100   rtimer = (GRealTimer*) timer;
101   rtimer->active = FALSE;
102
103 #ifdef NATIVE_WIN32
104   rtimer->end = GetTickCount ();
105 #else /* !NATIVE_WIN32 */
106   gettimeofday (&rtimer->end, NULL);
107 #endif /* !NATIVE_WIN32 */
108 }
109
110 void
111 g_timer_reset (GTimer *timer)
112 {
113   GRealTimer *rtimer;
114
115   g_assert (timer != NULL);
116
117   rtimer = (GRealTimer*) timer;
118
119 #ifdef NATIVE_WIN32
120    rtimer->start = GetTickCount ();
121 #else /* !NATIVE_WIN32 */
122   gettimeofday (&rtimer->start, NULL);
123 #endif /* !NATIVE_WIN32 */
124 }
125
126 gdouble
127 g_timer_elapsed (GTimer *timer,
128                  gulong *microseconds)
129 {
130   GRealTimer *rtimer;
131   gdouble total;
132 #ifndef NATIVE_WIN32
133   struct timeval elapsed;
134 #endif /* NATIVE_WIN32 */
135
136   g_return_val_if_fail (timer != NULL, 0);
137
138   rtimer = (GRealTimer*) timer;
139
140 #ifdef NATIVE_WIN32
141   if (rtimer->active)
142     rtimer->end = GetTickCount ();
143
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.
147    */
148   if (rtimer->end < rtimer->start)
149     total = (UINT_MAX - (rtimer->start - rtimer->end)) / 1000.0;
150   else
151     total = (rtimer->end - rtimer->start) / 1000.0;
152
153   if (microseconds)
154     {
155       if (rtimer->end < rtimer->start)
156         *microseconds =
157           ((UINT_MAX - (rtimer->start - rtimer->end)) % 1000) * 1000;
158       else
159         *microseconds =
160           ((rtimer->end - rtimer->start) % 1000) * 1000;
161     }
162 #else /* !NATIVE_WIN32 */
163   if (rtimer->active)
164     gettimeofday (&rtimer->end, NULL);
165
166   if (rtimer->start.tv_usec > rtimer->end.tv_usec)
167     {
168       rtimer->end.tv_usec += 1000000;
169       rtimer->end.tv_sec--;
170     }
171
172   elapsed.tv_usec = rtimer->end.tv_usec - rtimer->start.tv_usec;
173   elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec;
174
175   total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
176
177   if (microseconds)
178     *microseconds = elapsed.tv_usec;
179 #endif /* !NATIVE_WIN32 */
180
181   return total;
182 }