Mark functions with G_GNUC_MALLOC when appropriate.
[glib.git] / glib / gtimer.c
blob6a05198c1a2ebeddb2c574e1872873832822bf9c
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 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.
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.
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.
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/.
27 /*
28 * MT safe
31 #include "config.h"
32 #include "galias.h"
33 #include "glibconfig.h"
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #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 */
45 #ifdef G_OS_WIN32
46 #include <windows.h>
47 #endif /* G_OS_WIN32 */
49 #include "glib.h"
52 struct _GTimer
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 */
62 guint active : 1;
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 */
73 GTimer*
74 g_timer_new (void)
76 GTimer *timer;
78 timer = g_new (GTimer, 1);
79 timer->active = TRUE;
81 GETTIME (timer->start);
83 return timer;
86 void
87 g_timer_destroy (GTimer *timer)
89 g_return_if_fail (timer != NULL);
91 g_free (timer);
94 void
95 g_timer_start (GTimer *timer)
97 g_return_if_fail (timer != NULL);
99 timer->active = TRUE;
101 GETTIME (timer->start);
104 void
105 g_timer_stop (GTimer *timer)
107 g_return_if_fail (timer != NULL);
109 timer->active = FALSE;
111 GETTIME(timer->end);
114 void
115 g_timer_reset (GTimer *timer)
117 g_return_if_fail (timer != NULL);
119 GETTIME (timer->start);
122 void
123 g_timer_continue (GTimer *timer)
125 #ifdef G_OS_WIN32
126 DWORD elapsed;
127 #else
128 struct timeval elapsed;
129 #endif /* G_OS_WIN32 */
131 g_return_if_fail (timer != NULL);
132 g_return_if_fail (timer->active == FALSE);
134 /* Get elapsed time and reset timer start time
135 * to the current time minus the previously
136 * elapsed interval.
139 #ifdef G_OS_WIN32
141 elapsed = timer->end - timer->start;
143 GETTIME (timer->start);
145 timer->start -= elapsed;
147 #else /* !G_OS_WIN32 */
149 if (timer->start.tv_usec > timer->end.tv_usec)
151 timer->end.tv_usec += G_USEC_PER_SEC;
152 timer->end.tv_sec--;
155 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
156 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
158 GETTIME (timer->start);
160 if (timer->start.tv_usec < elapsed.tv_usec)
162 timer->start.tv_usec += G_USEC_PER_SEC;
163 timer->start.tv_sec--;
166 timer->start.tv_usec -= elapsed.tv_usec;
167 timer->start.tv_sec -= elapsed.tv_sec;
169 #endif /* !G_OS_WIN32 */
171 timer->active = TRUE;
174 gdouble
175 g_timer_elapsed (GTimer *timer,
176 gulong *microseconds)
178 gdouble total;
179 #ifndef G_OS_WIN32
180 struct timeval elapsed;
181 #endif /* G_OS_WIN32 */
183 g_return_val_if_fail (timer != NULL, 0);
185 #ifdef G_OS_WIN32
186 if (timer->active)
187 timer->end = GetTickCount ();
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;
195 if (microseconds)
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;
204 #else /* !G_OS_WIN32 */
205 if (timer->active)
206 gettimeofday (&timer->end, NULL);
208 if (timer->start.tv_usec > timer->end.tv_usec)
210 timer->end.tv_usec += G_USEC_PER_SEC;
211 timer->end.tv_sec--;
214 elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
215 elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
217 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
218 if (total < 0)
220 total = 0;
222 if (microseconds)
223 *microseconds = 0;
225 else if (microseconds)
226 *microseconds = elapsed.tv_usec;
228 #endif /* !G_OS_WIN32 */
230 return total;
233 void
234 g_usleep (gulong microseconds)
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) == EINTR)
244 request = remaining;
245 # else /* !HAVE_NANOSLEEP */
246 if (g_thread_supported ())
248 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
249 static GCond* cond = NULL;
250 GTimeVal end_time;
252 g_get_current_time (&end_time);
253 if (microseconds > G_MAXLONG)
255 microseconds -= G_MAXLONG;
256 g_time_val_add (&end_time, G_MAXLONG);
258 g_time_val_add (&end_time, microseconds);
260 g_static_mutex_lock (&mutex);
262 if (!cond)
263 cond = g_cond_new ();
265 while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex),
266 &end_time))
267 /* do nothing */;
269 g_static_mutex_unlock (&mutex);
271 else
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);
278 # endif /* !HAVE_NANOSLEEP */
279 #endif /* !G_OS_WIN32 */
283 * g_time_val_add:
284 * @time_: a #GTimeVal
285 * @microseconds: number of microseconds to add to @time
287 * Adds the given number of microseconds to @time_. @microseconds can
288 * also be negative to decrease the value of @time_.
290 void
291 g_time_val_add (GTimeVal *time_, glong microseconds)
293 g_return_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC);
295 if (microseconds >= 0)
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)
301 time_->tv_usec -= G_USEC_PER_SEC;
302 time_->tv_sec++;
305 else
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)
312 time_->tv_usec += G_USEC_PER_SEC;
313 time_->tv_sec--;