gtimer.c 6.79 KB
Newer Older
Owen Taylor's avatar
Owen Taylor committed
1 2 3 4
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Owen Taylor's avatar
Owen Taylor committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
15 16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
19

20
/*
21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 */

27 28 29 30
/* 
 * MT safe
 */

31
#include "config.h"
32
#include "galias.h"
33
#include "glibconfig.h"
34

35 36 37
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
38

39
#ifndef G_OS_WIN32
40
#include <sys/time.h>
41 42
#include <time.h>
#include <errno.h>
43
#endif /* G_OS_WIN32 */
Owen Taylor's avatar
Owen Taylor committed
44

45
#ifdef G_OS_WIN32
46
#include <windows.h>
47
#endif /* G_OS_WIN32 */
Owen Taylor's avatar
Owen Taylor committed
48

49 50 51
#include "glib.h"


52
struct _GTimer
Owen Taylor's avatar
Owen Taylor committed
53
{
54
#ifdef G_OS_WIN32
55 56
  DWORD start;
  DWORD end;
57
#else /* !G_OS_WIN32 */
Owen Taylor's avatar
Owen Taylor committed
58 59
  struct timeval start;
  struct timeval end;
60
#endif /* !G_OS_WIN32 */
Owen Taylor's avatar
Owen Taylor committed
61

62 63
  guint active : 1;
};
Owen Taylor's avatar
Owen Taylor committed
64

65 66 67 68 69 70 71 72
#ifdef G_OS_WIN32
#  define GETTIME(v) \
     v = GetTickCount ()
#else /* !G_OS_WIN32 */
#  define GETTIME(v) \
     gettimeofday (&v, NULL)
#endif /* !G_OS_WIN32 */

Owen Taylor's avatar
Owen Taylor committed
73 74 75
GTimer*
g_timer_new (void)
{
76
  GTimer *timer;
Owen Taylor's avatar
Owen Taylor committed
77

78
  timer = g_new (GTimer, 1);
Owen Taylor's avatar
Owen Taylor committed
79 80
  timer->active = TRUE;

81
  GETTIME (timer->start);
Owen Taylor's avatar
Owen Taylor committed
82

83
  return timer;
Owen Taylor's avatar
Owen Taylor committed
84 85 86 87 88
}

void
g_timer_destroy (GTimer *timer)
{
89
  g_return_if_fail (timer != NULL);
Owen Taylor's avatar
Owen Taylor committed
90 91 92 93 94 95 96

  g_free (timer);
}

void
g_timer_start (GTimer *timer)
{
97
  g_return_if_fail (timer != NULL);
Owen Taylor's avatar
Owen Taylor committed
98

99
  timer->active = TRUE;
100

101
  GETTIME (timer->start);
Owen Taylor's avatar
Owen Taylor committed
102 103 104 105 106
}

void
g_timer_stop (GTimer *timer)
{
107
  g_return_if_fail (timer != NULL);
Owen Taylor's avatar
Owen Taylor committed
108

109
  timer->active = FALSE;
110

111
  GETTIME(timer->end);
Owen Taylor's avatar
Owen Taylor committed
112 113 114 115 116
}

void
g_timer_reset (GTimer *timer)
{
117
  g_return_if_fail (timer != NULL);
Owen Taylor's avatar
Owen Taylor committed
118

119
  GETTIME (timer->start);
Owen Taylor's avatar
Owen Taylor committed
120 121
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
void
g_timer_continue (GTimer *timer)
{
#ifdef G_OS_WIN32
  DWORD elapsed;
#else
  struct timeval elapsed;
#endif /* G_OS_WIN32 */

  g_return_if_fail (timer != NULL);
  g_return_if_fail (timer->active == FALSE);

  /* Get elapsed time and reset timer start time
   *  to the current time minus the previously
   *  elapsed interval.
   */

#ifdef G_OS_WIN32

  elapsed = timer->end - timer->start;

  GETTIME (timer->start);

  timer->start -= elapsed;

#else /* !G_OS_WIN32 */

  if (timer->start.tv_usec > timer->end.tv_usec)
    {
      timer->end.tv_usec += G_USEC_PER_SEC;
      timer->end.tv_sec--;
    }

  elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
  elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;

  GETTIME (timer->start);

  if (timer->start.tv_usec < elapsed.tv_usec)
    {
      timer->start.tv_usec += G_USEC_PER_SEC;
      timer->start.tv_sec--;
    }

  timer->start.tv_usec -= elapsed.tv_usec;
  timer->start.tv_sec -= elapsed.tv_sec;

#endif /* !G_OS_WIN32 */

  timer->active = TRUE;
}

Owen Taylor's avatar
Owen Taylor committed
174 175 176 177 178
gdouble
g_timer_elapsed (GTimer *timer,
		 gulong *microseconds)
{
  gdouble total;
179
#ifndef G_OS_WIN32
180
  struct timeval elapsed;
181
#endif /* G_OS_WIN32 */
Owen Taylor's avatar
Owen Taylor committed
182

183
  g_return_val_if_fail (timer != NULL, 0);
Owen Taylor's avatar
Owen Taylor committed
184

185
#ifdef G_OS_WIN32
186 187
  if (timer->active)
    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 196

  if (microseconds)
    {
197
      if (timer->end < timer->start)
198
	*microseconds =
199
	  ((UINT_MAX - (timer->start - timer->end - 1)) % 1000) * 1000;
200 201
      else
	*microseconds =
202
	  ((timer->end - timer->start) % 1000) * 1000;
203
    }
204
#else /* !G_OS_WIN32 */
205 206
  if (timer->active)
    gettimeofday (&timer->end, NULL);
Owen Taylor's avatar
Owen Taylor committed
207

208
  if (timer->start.tv_usec > timer->end.tv_usec)
Owen Taylor's avatar
Owen Taylor committed
209
    {
210 211
      timer->end.tv_usec += G_USEC_PER_SEC;
      timer->end.tv_sec--;
Owen Taylor's avatar
Owen Taylor committed
212 213
    }

214 215
  elapsed.tv_usec = timer->end.tv_usec - timer->start.tv_usec;
  elapsed.tv_sec = timer->end.tv_sec - timer->start.tv_sec;
Owen Taylor's avatar
Owen Taylor committed
216 217

  total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
Tim Janik's avatar
Tim Janik committed
218 219 220
  if (total < 0)
    {
      total = 0;
Owen Taylor's avatar
Owen Taylor committed
221

Tim Janik's avatar
Tim Janik committed
222 223 224 225
      if (microseconds)
	*microseconds = 0;
    }
  else if (microseconds)
Owen Taylor's avatar
Owen Taylor committed
226
    *microseconds = elapsed.tv_usec;
Tim Janik's avatar
Tim Janik committed
227

228
#endif /* !G_OS_WIN32 */
Owen Taylor's avatar
Owen Taylor committed
229 230 231

  return total;
}
232 233 234 235

void
g_usleep (gulong microseconds)
{
236
#ifdef G_OS_WIN32
237
  Sleep (microseconds / 1000);
238 239 240 241 242
#else /* !G_OS_WIN32 */
# ifdef HAVE_NANOSLEEP
  struct timespec request, remaining;
  request.tv_sec = microseconds / G_USEC_PER_SEC;
  request.tv_nsec = 1000 * (microseconds % G_USEC_PER_SEC);
243 244
  while (nanosleep (&request, &remaining) == -1 && errno == EINTR)
    request = remaining;
245
# else /* !HAVE_NANOSLEEP */
246 247 248 249 250 251 252
  if (g_thread_supported ())
    {
      static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
      static GCond* cond = NULL;
      GTimeVal end_time;
      
      g_get_current_time (&end_time);
253
      if (microseconds > G_MAXLONG)
254
	{
255 256
	  microseconds -= G_MAXLONG;
	  g_time_val_add (&end_time, G_MAXLONG);
257
	}
258 259
      g_time_val_add (&end_time, microseconds);

260 261 262 263 264 265 266 267 268 269 270 271 272 273
      g_static_mutex_lock (&mutex);
      
      if (!cond)
	cond = g_cond_new ();
      
      while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex), 
				&end_time))
	/* do nothing */;
      
      g_static_mutex_unlock (&mutex);
    }
  else
    {
      struct timeval tv;
274 275
      tv.tv_sec = microseconds / G_USEC_PER_SEC;
      tv.tv_usec = microseconds % G_USEC_PER_SEC;
276 277
      select(0, NULL, NULL, NULL, &tv);
    }
278 279
# endif /* !HAVE_NANOSLEEP */
#endif /* !G_OS_WIN32 */
280 281
}

282 283
/**
 * g_time_val_add:
284
 * @time_: a #GTimeVal
285 286
 * @microseconds: number of microseconds to add to @time
 *
287 288
 * Adds the given number of microseconds to @time_. @microseconds can
 * 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 296

  if (microseconds >= 0)
    {
297 298 299
      time_->tv_usec += microseconds % G_USEC_PER_SEC;
      time_->tv_sec += microseconds / G_USEC_PER_SEC;
      if (time_->tv_usec >= G_USEC_PER_SEC)
300
       {
301 302
         time_->tv_usec -= G_USEC_PER_SEC;
         time_->tv_sec++;
303 304 305 306 307
       }
    }
  else
    {
      microseconds *= -1;
308 309 310
      time_->tv_usec -= microseconds % G_USEC_PER_SEC;
      time_->tv_sec -= microseconds / G_USEC_PER_SEC;
      if (time_->tv_usec < 0)
311
       {
312 313
         time_->tv_usec += G_USEC_PER_SEC;
         time_->tv_sec--;
314 315 316
       }      
    }
}