Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / glib / gtimer.c
blobf4b72edf68f3cf50948b049329e974109a3078bd
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.
21 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 /*
28 * MT safe
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include "glib.h"
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
39 #ifndef NATIVE_WIN32
40 #include <sys/time.h>
41 #endif /* NATIVE_WIN32 */
43 #ifdef NATIVE_WIN32
44 #include <windows.h>
45 #endif /* NATIVE_WIN32 */
47 typedef struct _GRealTimer GRealTimer;
49 struct _GRealTimer
51 #ifdef NATIVE_WIN32
52 DWORD start;
53 DWORD end;
54 #else /* !NATIVE_WIN32 */
55 struct timeval start;
56 struct timeval end;
57 #endif /* !NATIVE_WIN32 */
59 guint active : 1;
62 GTimer*
63 g_timer_new (void)
65 GRealTimer *timer;
67 timer = g_new (GRealTimer, 1);
68 timer->active = TRUE;
70 #ifdef NATIVE_WIN32
71 timer->start = GetTickCount ();
72 #else /* !NATIVE_WIN32 */
73 gettimeofday (&timer->start, NULL);
74 #endif /* !NATIVE_WIN32 */
76 return ((GTimer*) timer);
79 void
80 g_timer_destroy (GTimer *timer)
82 g_return_if_fail (timer != NULL);
84 g_free (timer);
87 void
88 g_timer_start (GTimer *timer)
90 GRealTimer *rtimer;
92 g_return_if_fail (timer != NULL);
94 rtimer = (GRealTimer*) timer;
95 rtimer->active = TRUE;
97 #ifdef NATIVE_WIN32
98 rtimer->start = GetTickCount ();
99 #else /* !NATIVE_WIN32 */
100 gettimeofday (&rtimer->start, NULL);
101 #endif /* !NATIVE_WIN32 */
104 void
105 g_timer_stop (GTimer *timer)
107 GRealTimer *rtimer;
109 g_return_if_fail (timer != NULL);
111 rtimer = (GRealTimer*) timer;
112 rtimer->active = FALSE;
114 #ifdef NATIVE_WIN32
115 rtimer->end = GetTickCount ();
116 #else /* !NATIVE_WIN32 */
117 gettimeofday (&rtimer->end, NULL);
118 #endif /* !NATIVE_WIN32 */
121 void
122 g_timer_reset (GTimer *timer)
124 GRealTimer *rtimer;
126 g_return_if_fail (timer != NULL);
128 rtimer = (GRealTimer*) timer;
130 #ifdef NATIVE_WIN32
131 rtimer->start = GetTickCount ();
132 #else /* !NATIVE_WIN32 */
133 gettimeofday (&rtimer->start, NULL);
134 #endif /* !NATIVE_WIN32 */
137 gdouble
138 g_timer_elapsed (GTimer *timer,
139 gulong *microseconds)
141 GRealTimer *rtimer;
142 gdouble total;
143 #ifndef NATIVE_WIN32
144 struct timeval elapsed;
145 #endif /* NATIVE_WIN32 */
147 g_return_val_if_fail (timer != NULL, 0);
149 rtimer = (GRealTimer*) timer;
151 #ifdef NATIVE_WIN32
152 if (rtimer->active)
153 rtimer->end = GetTickCount ();
155 /* Check for wraparound, which happens every 49.7 days.
156 * No, Win95 machines probably are never running for that long,
157 * but NT machines are.
159 if (rtimer->end < rtimer->start)
160 total = (UINT_MAX - (rtimer->start - rtimer->end)) / 1000.0;
161 else
162 total = (rtimer->end - rtimer->start) / 1000.0;
164 if (microseconds)
166 if (rtimer->end < rtimer->start)
167 *microseconds =
168 ((UINT_MAX - (rtimer->start - rtimer->end)) % 1000) * 1000;
169 else
170 *microseconds =
171 ((rtimer->end - rtimer->start) % 1000) * 1000;
173 #else /* !NATIVE_WIN32 */
174 if (rtimer->active)
175 gettimeofday (&rtimer->end, NULL);
177 if (rtimer->start.tv_usec > rtimer->end.tv_usec)
179 rtimer->end.tv_usec += 1000000;
180 rtimer->end.tv_sec--;
183 elapsed.tv_usec = rtimer->end.tv_usec - rtimer->start.tv_usec;
184 elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec;
186 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
187 if (total < 0)
189 total = 0;
191 if (microseconds)
192 *microseconds = 0;
194 else
195 if (microseconds)
196 *microseconds = elapsed.tv_usec;
198 #endif /* !NATIVE_WIN32 */
200 return total;