Patch from Darin Adler to remove GReal* structures in favor of simple
[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 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include "glib.h"
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif /* HAVE_UNISTD_H */
39 #ifndef G_OS_WIN32
40 #include <sys/time.h>
41 #include <time.h>
42 #include <errno.h>
43 #endif /* G_OS_WIN32 */
44
45 #ifdef G_OS_WIN32
46 #include <windows.h>
47 #endif /* G_OS_WIN32 */
48
49 struct _GTimer
50 {
51 #ifdef G_OS_WIN32
52   DWORD start;
53   DWORD end;
54 #else /* !G_OS_WIN32 */
55   struct timeval start;
56   struct timeval end;
57 #endif /* !G_OS_WIN32 */
58
59   guint active : 1;
60 };
61
62 #ifdef G_OS_WIN32
63 #  define GETTIME(v) \
64      v = GetTickCount ()
65 #else /* !G_OS_WIN32 */
66 #  define GETTIME(v) \
67      gettimeofday (&v, NULL)
68 #endif /* !G_OS_WIN32 */
69
70 GTimer*
71 g_timer_new (void)
72 {
73   GTimer *timer;
74
75   timer = g_new (GTimer, 1);
76   timer->active = TRUE;
77
78   GETTIME (timer->start);
79
80   return timer;
81 }
82
83 void
84 g_timer_destroy (GTimer *timer)
85 {
86   g_return_if_fail (timer != NULL);
87
88   g_free (timer);
89 }
90
91 void
92 g_timer_start (GTimer *timer)
93 {
94   g_return_if_fail (timer != NULL);
95
96   timer->active = TRUE;
97
98   GETTIME (timer->start);
99 }
100
101 void
102 g_timer_stop (GTimer *timer)
103 {
104   g_return_if_fail (timer != NULL);
105
106   timer->active = FALSE;
107
108   GETTIME(timer->end);
109 }
110
111 void
112 g_timer_reset (GTimer *timer)
113 {
114   g_return_if_fail (timer != NULL);
115
116   GETTIME (timer->start);
117 }
118
119 gdouble
120 g_timer_elapsed (GTimer *timer,
121                  gulong *microseconds)
122 {
123   gdouble total;
124 #ifndef G_OS_WIN32
125   struct timeval elapsed;
126 #endif /* G_OS_WIN32 */
127
128   g_return_val_if_fail (timer != NULL, 0);
129
130 #ifdef G_OS_WIN32
131   if (timer->active)
132     timer->end = GetTickCount ();
133
134   /* Check for wraparound, which happens every 49.7 days. */
135   if (timer->end < timer->start)
136     total = (UINT_MAX - (timer->start - timer->end)) / 1000.0;
137   else
138     total = (timer->end - timer->start) / 1000.0;
139
140   if (microseconds)
141     {
142       if (timer->end < timer->start)
143         *microseconds =
144           ((UINT_MAX - (timer->start - timer->end)) % 1000) * 1000;
145       else
146         *microseconds =
147           ((timer->end - timer->start) % 1000) * 1000;
148     }
149 #else /* !G_OS_WIN32 */
150   if (timer->active)
151     gettimeofday (&timer->end, NULL);
152
153   if (timer->start.tv_usec > timer->end.tv_usec)
154     {
155       timer->end.tv_usec += G_USEC_PER_SEC;
156       timer->end.tv_sec--;
157     }
158
159   elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
160   elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
161
162   total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
163   if (total < 0)
164     {
165       total = 0;
166
167       if (microseconds)
168         *microseconds = 0;
169     }
170   else if (microseconds)
171     *microseconds = elapsed.tv_usec;
172
173 #endif /* !G_OS_WIN32 */
174
175   return total;
176 }
177
178 void
179 g_usleep (gulong microseconds)
180 {
181 #ifdef G_OS_WIN32
182   Sleep (microseconds / 1000);
183 #else /* !G_OS_WIN32 */
184 # ifdef HAVE_NANOSLEEP
185   struct timespec request, remaining;
186   request.tv_sec = microseconds / G_USEC_PER_SEC;
187   request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
188   while (nanosleep (&request, &remaining) == EINTR)
189     request = remaining;
190 # else /* !HAVE_NANOSLEEP */
191   if (g_thread_supported ())
192     {
193       static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
194       static GCond* cond = NULL;
195       GTimeVal end_time;
196       
197       g_get_current_time (&end_time);
198       if (microseconds > G_MAXLONG)
199         {
200           microseconds -= G_MAXLONG;
201           g_time_val_add (&end_time, G_MAXLONG);
202         }
203       g_time_val_add (&end_time, microseconds);
204
205       g_static_mutex_lock (&mutex);
206       
207       if (!cond)
208         cond = g_cond_new ();
209       
210       while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex), 
211                                 &end_time))
212         /* do nothing */;
213       
214       g_static_mutex_unlock (&mutex);
215     }
216   else
217     {
218       struct timeval tv;
219       tv.tv_sec = microseconds / G_USEC_PER_SEC;
220       tv.tv_usec = microseconds % G_USEC_PER_SEC;
221       select(0, NULL, NULL, NULL, &tv);
222     }
223 # endif /* !HAVE_NANOSLEEP */
224 #endif /* !G_OS_WIN32 */
225 }
226
227 /**
228  * g_time_val_add:
229  * @time: a #GTimeVal
230  * @microseconds: number of microseconds to add to @time
231  *
232  * Adds the given number of microseconds to @time. @microseconds can
233  * also be negative to decrease the value of @time.
234  **/
235 void 
236 g_time_val_add (GTimeVal *time, glong microseconds)
237 {
238   g_return_if_fail (time->tv_usec >= 0 && time->tv_usec < G_USEC_PER_SEC);
239
240   if (microseconds >= 0)
241     {
242       time->tv_usec += microseconds % G_USEC_PER_SEC;
243       time->tv_sec += microseconds / G_USEC_PER_SEC;
244       if (time->tv_usec >= G_USEC_PER_SEC)
245        {
246          time->tv_usec -= G_USEC_PER_SEC;
247          time->tv_sec++;
248        }
249     }
250   else
251     {
252       microseconds *= -1;
253       time->tv_usec -= microseconds % G_USEC_PER_SEC;
254       time->tv_sec -= microseconds / G_USEC_PER_SEC;
255       if (time->tv_usec < 0)
256        {
257          time->tv_usec += G_USEC_PER_SEC;
258          time->tv_sec--;
259        }      
260     }
261 }