Make PLT-reduction work with gcc4, and don't include everything in
[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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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 /*
21  * Modified by the GLib Team and others 1997-2000.  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/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #include "config.h"
32 #include "glibconfig.h"
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif /* HAVE_UNISTD_H */
37
38 #ifndef G_OS_WIN32
39 #include <sys/time.h>
40 #include <time.h>
41 #include <errno.h>
42 #endif /* G_OS_WIN32 */
43
44 #ifdef G_OS_WIN32
45 #include <windows.h>
46 #endif /* G_OS_WIN32 */
47
48 #include "glib.h"
49 #include "galias.h"
50
51
52 struct _GTimer
53 {
54 #ifdef G_OS_WIN32
55   DWORD start;
56   DWORD end;
57 #else /* !G_OS_WIN32 */
58   struct timeval start;
59   struct timeval end;
60 #endif /* !G_OS_WIN32 */
61
62   guint active : 1;
63 };
64
65 #ifdef G_OS_WIN32
66 #  define GETTIME(v) \
67      v = GetTickCount ()
68 #else /* !G_OS_WIN32 */
69 #  define GETTIME(v) \
70      gettimeofday (&v, NULL)
71 #endif /* !G_OS_WIN32 */
72
73 GTimer*
74 g_timer_new (void)
75 {
76   GTimer *timer;
77
78   timer = g_new (GTimer, 1);
79   timer->active = TRUE;
80
81   GETTIME (timer->start);
82
83   return timer;
84 }
85
86 void
87 g_timer_destroy (GTimer *timer)
88 {
89   g_return_if_fail (timer != NULL);
90
91   g_free (timer);
92 }
93
94 void
95 g_timer_start (GTimer *timer)
96 {
97   g_return_if_fail (timer != NULL);
98
99   timer->active = TRUE;
100
101   GETTIME (timer->start);
102 }
103
104 void
105 g_timer_stop (GTimer *timer)
106 {
107   g_return_if_fail (timer != NULL);
108
109   timer->active = FALSE;
110
111   GETTIME(timer->end);
112 }
113
114 void
115 g_timer_reset (GTimer *timer)
116 {
117   g_return_if_fail (timer != NULL);
118
119   GETTIME (timer->start);
120 }
121
122 void
123 g_timer_continue (GTimer *timer)
124 {
125 #ifdef G_OS_WIN32
126   DWORD elapsed;
127 #else
128   struct timeval elapsed;
129 #endif /* G_OS_WIN32 */
130
131   g_return_if_fail (timer != NULL);
132   g_return_if_fail (timer->active == FALSE);
133
134   /* Get elapsed time and reset timer start time
135    *  to the current time minus the previously
136    *  elapsed interval.
137    */
138
139 #ifdef G_OS_WIN32
140
141   elapsed = timer->end - timer->start;
142
143   GETTIME (timer->start);
144
145   timer->start -= elapsed;
146
147 #else /* !G_OS_WIN32 */
148
149   if (timer->start.tv_usec > timer->end.tv_usec)
150     {
151       timer->end.tv_usec += G_USEC_PER_SEC;
152       timer->end.tv_sec--;
153     }
154
155   elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
156   elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
157
158   GETTIME (timer->start);
159
160   if (timer->start.tv_usec < elapsed.tv_usec)
161     {
162       timer->start.tv_usec += G_USEC_PER_SEC;
163       timer->start.tv_sec--;
164     }
165
166   timer->start.tv_usec -= elapsed.tv_usec;
167   timer->start.tv_sec -= elapsed.tv_sec;
168
169 #endif /* !G_OS_WIN32 */
170
171   timer->active = TRUE;
172 }
173
174 gdouble
175 g_timer_elapsed (GTimer *timer,
176                  gulong *microseconds)
177 {
178   gdouble total;
179 #ifndef G_OS_WIN32
180   struct timeval elapsed;
181 #endif /* G_OS_WIN32 */
182
183   g_return_val_if_fail (timer != NULL, 0);
184
185 #ifdef G_OS_WIN32
186   if (timer->active)
187     timer->end = GetTickCount ();
188
189   /* Check for wraparound, which happens every 49.7 days. */
190   if (timer->end < timer->start)
191     total = (UINT_MAX - (timer->start - timer->end - 1)) / 1000.0;
192   else
193     total = (timer->end - timer->start) / 1000.0;
194
195   if (microseconds)
196     {
197       if (timer->end < timer->start)
198         *microseconds =
199           ((UINT_MAX - (timer->start - timer->end - 1)) % 1000) * 1000;
200       else
201         *microseconds =
202           ((timer->end - timer->start) % 1000) * 1000;
203     }
204 #else /* !G_OS_WIN32 */
205   if (timer->active)
206     gettimeofday (&timer->end, NULL);
207
208   if (timer->start.tv_usec > timer->end.tv_usec)
209     {
210       timer->end.tv_usec += G_USEC_PER_SEC;
211       timer->end.tv_sec--;
212     }
213
214   elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
215   elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
216
217   total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
218   if (total < 0)
219     {
220       total = 0;
221
222       if (microseconds)
223         *microseconds = 0;
224     }
225   else if (microseconds)
226     *microseconds = elapsed.tv_usec;
227
228 #endif /* !G_OS_WIN32 */
229
230   return total;
231 }
232
233 void
234 g_usleep (gulong microseconds)
235 {
236 #ifdef G_OS_WIN32
237   Sleep (microseconds / 1000);
238 #else /* !G_OS_WIN32 */
239 # ifdef HAVE_NANOSLEEP
240   struct timespec request, remaining;
241   request.tv_sec = microseconds / G_USEC_PER_SEC;
242   request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
243   while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
244     request = remaining;
245 # else /* !HAVE_NANOSLEEP */
246   if (g_thread_supported ())
247     {
248       static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
249       static GCond* cond = NULL;
250       GTimeVal end_time;
251       
252       g_get_current_time (&end_time);
253       if (microseconds > G_MAXLONG)
254         {
255           microseconds -= G_MAXLONG;
256           g_time_val_add (&end_time, G_MAXLONG);
257         }
258       g_time_val_add (&end_time, microseconds);
259
260       g_static_mutex_lock (&mutex);
261       
262       if (!cond)
263         cond = g_cond_new ();
264       
265       while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex), 
266                                 &end_time))
267         /* do nothing */;
268       
269       g_static_mutex_unlock (&mutex);
270     }
271   else
272     {
273       struct timeval tv;
274       tv.tv_sec = microseconds / G_USEC_PER_SEC;
275       tv.tv_usec = microseconds % G_USEC_PER_SEC;
276       select(0, NULL, NULL, NULL, &tv);
277     }
278 # endif /* !HAVE_NANOSLEEP */
279 #endif /* !G_OS_WIN32 */
280 }
281
282 /**
283  * g_time_val_add:
284  * @time_: a #GTimeVal
285  * @microseconds: number of microseconds to add to @time
286  *
287  * Adds the given number of microseconds to @time_. @microseconds can
288  * also be negative to decrease the value of @time_.
289  **/
290 void 
291 g_time_val_add (GTimeVal *time_, glong microseconds)
292 {
293   g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
294
295   if (microseconds >= 0)
296     {
297       time_->tv_usec += microseconds % G_USEC_PER_SEC;
298       time_->tv_sec += microseconds / G_USEC_PER_SEC;
299       if (time_->tv_usec >= G_USEC_PER_SEC)
300        {
301          time_->tv_usec -= G_USEC_PER_SEC;
302          time_->tv_sec++;
303        }
304     }
305   else
306     {
307       microseconds *= -1;
308       time_->tv_usec -= microseconds % G_USEC_PER_SEC;
309       time_->tv_sec -= microseconds / G_USEC_PER_SEC;
310       if (time_->tv_usec < 0)
311        {
312          time_->tv_usec += G_USEC_PER_SEC;
313          time_->tv_sec--;
314        }      
315     }
316 }
317
318 #define __G_TIMER_C__
319 #include "galiasdef.c"