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