Break some long lines.
[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   guint64 start;
56   guint64 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      GetSystemTimeAsFileTime ((FILETIME *)&v)
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   guint64 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 #ifdef G_OS_WIN32
180   gint64 elapsed;
181 #else
182   struct timeval elapsed;
183 #endif /* G_OS_WIN32 */
184
185   g_return_val_if_fail (timer != NULL, 0);
186
187 #ifdef G_OS_WIN32
188   if (timer->active)
189     GETTIME (timer->end);
190
191   elapsed = timer->end - timer->start;
192
193   total = elapsed / 1e7;
194
195   if (microseconds)
196     *microseconds = (elapsed / 10) % 1000000;
197 #else /* !G_OS_WIN32 */
198   if (timer->active)
199     gettimeofday (&timer->end, NULL);
200
201   if (timer->start.tv_usec > timer->end.tv_usec)
202     {
203       timer->end.tv_usec += G_USEC_PER_SEC;
204       timer->end.tv_sec--;
205     }
206
207   elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
208   elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
209
210   total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
211   if (total < 0)
212     {
213       total = 0;
214
215       if (microseconds)
216         *microseconds = 0;
217     }
218   else if (microseconds)
219     *microseconds = elapsed.tv_usec;
220
221 #endif /* !G_OS_WIN32 */
222
223   return total;
224 }
225
226 void
227 g_usleep (gulong microseconds)
228 {
229 #ifdef G_OS_WIN32
230   Sleep (microseconds / 1000);
231 #else /* !G_OS_WIN32 */
232 # ifdef HAVE_NANOSLEEP
233   struct timespec request, remaining;
234   request.tv_sec = microseconds / G_USEC_PER_SEC;
235   request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
236   while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
237     request = remaining;
238 # else /* !HAVE_NANOSLEEP */
239   if (g_thread_supported ())
240     {
241       static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
242       static GCond* cond = NULL;
243       GTimeVal end_time;
244       
245       g_get_current_time (&end_time);
246       if (microseconds > G_MAXLONG)
247         {
248           microseconds -= G_MAXLONG;
249           g_time_val_add (&end_time, G_MAXLONG);
250         }
251       g_time_val_add (&end_time, microseconds);
252
253       g_static_mutex_lock (&mutex);
254       
255       if (!cond)
256         cond = g_cond_new ();
257       
258       while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex), 
259                                 &end_time))
260         /* do nothing */;
261       
262       g_static_mutex_unlock (&mutex);
263     }
264   else
265     {
266       struct timeval tv;
267       tv.tv_sec = microseconds / G_USEC_PER_SEC;
268       tv.tv_usec = microseconds % G_USEC_PER_SEC;
269       select(0, NULL, NULL, NULL, &tv);
270     }
271 # endif /* !HAVE_NANOSLEEP */
272 #endif /* !G_OS_WIN32 */
273 }
274
275 /**
276  * g_time_val_add:
277  * @time_: a #GTimeVal
278  * @microseconds: number of microseconds to add to @time
279  *
280  * Adds the given number of microseconds to @time_. @microseconds can
281  * also be negative to decrease the value of @time_.
282  **/
283 void 
284 g_time_val_add (GTimeVal *time_, glong microseconds)
285 {
286   g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
287
288   if (microseconds >= 0)
289     {
290       time_->tv_usec += microseconds % G_USEC_PER_SEC;
291       time_->tv_sec += microseconds / G_USEC_PER_SEC;
292       if (time_->tv_usec >= G_USEC_PER_SEC)
293        {
294          time_->tv_usec -= G_USEC_PER_SEC;
295          time_->tv_sec++;
296        }
297     }
298   else
299     {
300       microseconds *= -1;
301       time_->tv_usec -= microseconds % G_USEC_PER_SEC;
302       time_->tv_sec -= microseconds / G_USEC_PER_SEC;
303       if (time_->tv_usec < 0)
304        {
305          time_->tv_usec += G_USEC_PER_SEC;
306          time_->tv_sec--;
307        }      
308     }
309 }
310
311 #define __G_TIMER_C__
312 #include "galiasdef.c"