don't try to export g_strcpy, it is g_stpcpy; updated and added some
[platform/upstream/glib.git] / 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 #endif /* G_OS_WIN32 */
42
43 #ifdef G_OS_WIN32
44 #include <windows.h>
45 #endif /* G_OS_WIN32 */
46
47 typedef struct _GRealTimer GRealTimer;
48
49 struct _GRealTimer
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   GRealTimer *timer;
74
75   timer = g_new (GRealTimer, 1);
76   timer->active = TRUE;
77
78   GETTIME (timer->start);
79
80   return ((GTimer*) 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   GRealTimer *rtimer;
95
96   g_return_if_fail (timer != NULL);
97
98   rtimer = (GRealTimer*) timer;
99   rtimer->active = TRUE;
100
101   GETTIME (rtimer->start);
102 }
103
104 void
105 g_timer_stop (GTimer *timer)
106 {
107   GRealTimer *rtimer;
108
109   g_return_if_fail (timer != NULL);
110
111   rtimer = (GRealTimer*) timer;
112   rtimer->active = FALSE;
113
114   GETTIME(rtimer->end);
115 }
116
117 void
118 g_timer_reset (GTimer *timer)
119 {
120   GRealTimer *rtimer;
121
122   g_return_if_fail (timer != NULL);
123
124   rtimer = (GRealTimer*) timer;
125
126   GETTIME (rtimer->start);
127 }
128
129 gdouble
130 g_timer_elapsed (GTimer *timer,
131                  gulong *microseconds)
132 {
133   GRealTimer *rtimer;
134   gdouble total;
135 #ifndef G_OS_WIN32
136   struct timeval elapsed;
137 #endif /* G_OS_WIN32 */
138
139   g_return_val_if_fail (timer != NULL, 0);
140
141   rtimer = (GRealTimer*) timer;
142
143 #ifdef G_OS_WIN32
144   if (rtimer->active)
145     rtimer->end = GetTickCount ();
146
147   /* Check for wraparound, which happens every 49.7 days. */
148   if (rtimer->end < rtimer->start)
149     total = (UINT_MAX - (rtimer->start - rtimer->end)) / 1000.0;
150   else
151     total = (rtimer->end - rtimer->start) / 1000.0;
152
153   if (microseconds)
154     {
155       if (rtimer->end < rtimer->start)
156         *microseconds =
157           ((UINT_MAX - (rtimer->start - rtimer->end)) % 1000) * 1000;
158       else
159         *microseconds =
160           ((rtimer->end - rtimer->start) % 1000) * 1000;
161     }
162 #else /* !G_OS_WIN32 */
163   if (rtimer->active)
164     gettimeofday (&rtimer->end, NULL);
165
166   if (rtimer->start.tv_usec > rtimer->end.tv_usec)
167     {
168       rtimer->end.tv_usec += G_USEC_PER_SEC;
169       rtimer->end.tv_sec--;
170     }
171
172   elapsed.tv_usec = rtimer->end.tv_usec - rtimer->start.tv_usec;
173   elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec;
174
175   total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
176   if (total < 0)
177     {
178       total = 0;
179
180       if (microseconds)
181         *microseconds = 0;
182     }
183   else if (microseconds)
184     *microseconds = elapsed.tv_usec;
185
186 #endif /* !G_OS_WIN32 */
187
188   return total;
189 }
190
191 void
192 g_usleep (gulong microseconds)
193 {
194 #ifdef G_OS_WIN32
195   Sleep (microseconds / 1000);
196 #else
197   if (g_thread_supported ())
198     {
199       static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
200       static GCond* cond = NULL;
201       GTimeVal end_time;
202       
203       g_get_current_time (&end_time);
204       
205       end_time.tv_sec += microseconds / G_USEC_PER_SEC;
206       end_time.tv_usec += microseconds % G_USEC_PER_SEC;
207       
208       if (end_time.tv_usec >= G_USEC_PER_SEC)
209         {
210           end_time.tv_usec -= G_USEC_PER_SEC;
211           end_time.tv_sec += 1;
212         }
213       
214       g_static_mutex_lock (&mutex);
215       
216       if (!cond)
217         cond = g_cond_new ();
218       
219       while (g_cond_timed_wait (cond, g_static_mutex_get_mutex (&mutex), 
220                                 &end_time))
221         /* do nothing */;
222       
223       g_static_mutex_unlock (&mutex);
224     }
225   else
226     {
227       struct timeval tv;
228       tv.tv_sec = microseconds / G_USEC_PER_SEC;
229       tv.tv_usec = microseconds % G_USEC_PER_SEC;
230       select(0, NULL, NULL, NULL, &tv);
231     }
232 #endif
233 }
234