]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
* include/vlc_common.h: fixed the I64C() macro for mingw.
[vlc] / src / misc / mtime.c
1 /*****************************************************************************
2  * mtime.c: high rezolution time management functions
3  * Functions are prototyped in mtime.h.
4  *****************************************************************************
5  * Copyright (C) 1998-2001 VideoLAN
6  * $Id: mtime.c,v 1.36 2003/06/05 11:52:19 gbazin Exp $
7  *
8  * Authors: Vincent Seguin <seguin@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*
26  * TODO:
27  *  see if using Linux real-time extensions is possible and profitable
28  */
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <stdio.h>                                              /* sprintf() */
34
35 #include <vlc/vlc.h>
36
37 #if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
38 #   include <pth.h>
39 #endif
40
41 #ifdef HAVE_UNISTD_H
42 #   include <unistd.h>                                           /* select() */
43 #endif
44
45 #ifdef HAVE_KERNEL_OS_H
46 #   include <kernel/OS.h>
47 #endif
48
49 #if defined( WIN32 ) || defined( UNDER_CE )
50 #   include <windows.h>
51 #else
52 #   include <sys/time.h>
53 #endif
54
55 #if defined(HAVE_NANOSLEEP) && !defined(HAVE_STRUCT_TIMESPEC)
56 struct timespec
57 {
58     time_t  tv_sec;
59     int32_t tv_nsec;
60 };
61 #endif
62
63 #if defined(HAVE_NANOSLEEP) && !defined(HAVE_DECL_NANOSLEEP)
64 int nanosleep(struct timespec *, struct timespec *);
65 #endif
66
67 /*****************************************************************************
68  * mstrtime: return a date in a readable format
69  *****************************************************************************
70  * This functions is provided for any interface function which need to print a
71  * date. psz_buffer should be a buffer long enough to store the formatted
72  * date.
73  *****************************************************************************/
74 char *mstrtime( char *psz_buffer, mtime_t date )
75 {
76     static mtime_t ll1000 = 1000, ll60 = 60, ll24 = 24;
77
78     sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
79              (int) (date / (ll1000 * ll1000 * ll60 * ll60) % ll24),
80              (int) (date / (ll1000 * ll1000 * ll60) % ll60),
81              (int) (date / (ll1000 * ll1000) % ll60),
82              (int) (date / ll1000 % ll1000),
83              (int) (date % ll1000) );
84     return( psz_buffer );
85 }
86
87 /*****************************************************************************
88  * mdate: return high precision date
89  *****************************************************************************
90  * Uses the gettimeofday() function when possible (1 MHz resolution) or the
91  * ftime() function (1 kHz resolution).
92  *****************************************************************************/
93 mtime_t mdate( void )
94 {
95 #if defined( HAVE_KERNEL_OS_H )
96     return( real_time_clock_usecs() );
97
98 #elif defined( WIN32 ) || defined( UNDER_CE )
99     /* We don't need the real date, just the value of a high precision timer */
100     static mtime_t freq = I64C(-1);
101     mtime_t usec_time;
102
103     if( freq == I64C(-1) )
104     {
105         /* Extract from the Tcl source code:
106          * (http://www.cs.man.ac.uk/fellowsd-bin/TIP/7.html)
107          *
108          * Some hardware abstraction layers use the CPU clock
109          * in place of the real-time clock as a performance counter
110          * reference.  This results in:
111          *    - inconsistent results among the processors on
112          *      multi-processor systems.
113          *    - unpredictable changes in performance counter frequency
114          *      on "gearshift" processors such as Transmeta and
115          *      SpeedStep.
116          * There seems to be no way to test whether the performance
117          * counter is reliable, but a useful heuristic is that
118          * if its frequency is 1.193182 MHz or 3.579545 MHz, it's
119          * derived from a colorburst crystal and is therefore
120          * the RTC rather than the TSC.  If it's anything else, we
121          * presume that the performance counter is unreliable.
122          */
123
124         freq = ( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) &&
125                  (freq == I64C(1193182) || freq == I64C(3579545) ) )
126                ? freq : 0;
127     }
128
129     if( freq != 0 )
130     {
131         /* Microsecond resolution */
132         QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
133         return ( usec_time * 1000000 ) / freq;
134     }
135
136     /* Milisecond resolution (actually, best case is about 10 ms resolution) */
137     return 1000 * GetTickCount();
138
139 #else
140     struct timeval tv_date;
141
142     /* gettimeofday() could return an error, and should be tested. However, the
143      * only possible error, according to 'man', is EFAULT, which can not happen
144      * here, since tv is a local variable. */
145     gettimeofday( &tv_date, NULL );
146     return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
147
148 #endif
149 }
150
151 /*****************************************************************************
152  * mwait: wait for a date
153  *****************************************************************************
154  * This function uses select() and an system date function to wake up at a
155  * precise date. It should be used for process synchronization. If current date
156  * is posterior to wished date, the function returns immediately.
157  *****************************************************************************/
158 void mwait( mtime_t date )
159 {
160 #if defined( HAVE_KERNEL_OS_H )
161     mtime_t delay;
162     
163     delay = date - real_time_clock_usecs();
164     if( delay <= 0 )
165     {
166         return;
167     }
168     snooze( delay );
169
170 #elif defined( WIN32 ) || defined( UNDER_CE )
171     mtime_t usec_time, delay;
172
173     usec_time = mdate();
174     delay = date - usec_time;
175     if( delay <= 0 )
176     {
177         return;
178     }
179     msleep( delay );
180
181 #else
182
183     struct timeval tv_date;
184     mtime_t        delay;          /* delay in msec, signed to detect errors */
185
186     /* see mdate() about gettimeofday() possible errors */
187     gettimeofday( &tv_date, NULL );
188
189     /* calculate delay and check if current date is before wished date */
190     delay = date - (mtime_t) tv_date.tv_sec * 1000000
191                  - (mtime_t) tv_date.tv_usec
192                  - 10000;
193
194     /* Linux/i386 has a granularity of 10 ms. It's better to be in advance
195      * than to be late. */
196     if( delay <= 0 )                 /* wished date is now or already passed */
197     {
198         return;
199     }
200
201 #   if defined( PTH_INIT_IN_PTH_H )
202     pth_usleep( delay );
203
204 #   elif defined( ST_INIT_IN_ST_H )
205     st_usleep( delay );
206
207 #   else
208
209 #       if defined( HAVE_NANOSLEEP )
210     {
211         struct timespec ts_delay;
212         ts_delay.tv_sec = delay / 1000000;
213         ts_delay.tv_nsec = (delay % 1000000) * 1000;
214
215         nanosleep( &ts_delay, NULL );
216     }
217
218 #       else
219     tv_date.tv_sec = delay / 1000000;
220     tv_date.tv_usec = delay % 1000000;
221     /* see msleep() about select() errors */
222     select( 0, NULL, NULL, NULL, &tv_date );
223 #       endif
224
225 #   endif
226
227 #endif
228 }
229
230 /*****************************************************************************
231  * msleep: more precise sleep()
232  *****************************************************************************
233  * Portable usleep() function.
234  *****************************************************************************/
235 void msleep( mtime_t delay )
236 {
237 #if defined( HAVE_KERNEL_OS_H )
238     snooze( delay );
239
240 #elif defined( PTH_INIT_IN_PTH_H )
241     pth_usleep( delay );
242
243 #elif defined( ST_INIT_IN_ST_H )
244     st_usleep( delay );
245
246 #elif defined( WIN32 ) || defined( UNDER_CE )
247     Sleep( (int) (delay / 1000) );
248
249 #elif defined( HAVE_NANOSLEEP )
250     struct timespec ts_delay;
251
252     ts_delay.tv_sec = delay / 1000000;
253     ts_delay.tv_nsec = (delay % 1000000) * 1000;
254
255     nanosleep( &ts_delay, NULL );
256
257 #else
258     struct timeval tv_delay;
259
260     tv_delay.tv_sec = delay / 1000000;
261     tv_delay.tv_usec = delay % 1000000;
262
263     /* select() return value should be tested, since several possible errors
264      * can occur. However, they should only happen in very particular occasions
265      * (i.e. when a signal is sent to the thread, or when memory is full), and
266      * can be ignored. */
267     select( 0, NULL, NULL, NULL, &tv_delay );
268
269 #endif
270 }
271