]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
* Win2000 DVD input by Jon Lech Johansen <jon-vl@nanocrew.net>.
[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, 1999, 2000 VideoLAN
6  * $Id: mtime.c,v 1.20 2001/05/31 03:12:49 sam 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 "defs.h"
34
35 #include <stdio.h>                                              /* sprintf() */
36
37 #ifdef HAVE_UNISTD_H
38 #   include <unistd.h>                                           /* select() */
39 #endif
40
41 #ifdef HAVE_KERNEL_OS_H
42 #   include <kernel/OS.h>
43 #endif
44
45 #if defined( WIN32 )
46 #   include <windows.h>
47 #else
48 #   include <sys/time.h>
49 #endif
50
51 #include "config.h"
52 #include "common.h"
53 #include "mtime.h"
54
55 #if defined( WIN32 )
56 /*****************************************************************************
57  * usleep: microsecond sleep for win32
58  *****************************************************************************
59  * This function uses performance counter if available, and Sleep() if not.
60  *****************************************************************************/
61 static __inline__ void usleep( unsigned int i_useconds )
62 {
63     s64 i_cur, i_freq;
64     s64 i_now, i_then;
65
66     if( i_useconds < 1000
67          && QueryPerformanceFrequency( (LARGE_INTEGER *) &i_freq ) )
68     {
69         QueryPerformanceCounter( (LARGE_INTEGER *) &i_cur );
70
71         i_now = ( cur * 1000 * 1000 / i_freq );
72         i_then = i_now + i_useconds;
73
74         while( i_now < i_then )
75         {
76             QueryPerformanceCounter( (LARGE_INTEGER *) &i_cur );
77             now = cur * 1000 * 1000 / i_freq;
78         }
79     }
80     else
81     {
82         Sleep( (int) ((i_useconds + 500) / 1000) );
83     }
84 }
85 #endif
86
87 /*****************************************************************************
88  * mstrtime: return a date in a readable format
89  *****************************************************************************
90  * This functions is provided for any interface function which need to print a
91  * date. psz_buffer should be a buffer long enough to store the formatted
92  * date.
93  *****************************************************************************/
94 char *mstrtime( char *psz_buffer, mtime_t date )
95 {
96     sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
97              (int) (date / (I64C(1000) * I64C(1000) * I64C(60) * I64C(60)) % I64C(24)),
98              (int) (date / (I64C(1000) * I64C(1000) * I64C(60)) % I64C(60)),
99              (int) (date / (I64C(1000) * I64C(1000)) % I64C(60)),
100              (int) (date / I64C(1000) % I64C(1000)),
101              (int) (date % I64C(1000)) );
102     return( psz_buffer );
103 }
104
105 /*****************************************************************************
106  * mdate: return high precision date (inline function)
107  *****************************************************************************
108  * Uses the gettimeofday() function when possible (1 MHz resolution) or the
109  * ftime() function (1 kHz resolution).
110  *****************************************************************************/
111 mtime_t mdate( void )
112 {
113 #if defined( HAVE_KERNEL_OS_H )
114     return( real_time_clock_usecs() );
115
116 #elif defined( WIN32 )
117     /* We don't get the real date, just the value of a high precision timer.
118      * this is because the usual time functions have at best only a milisecond
119      * resolution */
120     mtime_t freq, usec_time;
121
122     if( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) )
123     {
124         /* Microsecond resolution */
125         QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
126         return ( usec_time * 1000000 ) / freq;
127     }
128     else
129     {
130         /* Milisecond resolution */
131         return 1000 * GetTickCount();
132     }
133
134 #else
135     struct timeval tv_date;
136
137     /* gettimeofday() could return an error, and should be tested. However, the
138      * only possible error, according to 'man', is EFAULT, which can not happen
139      * here, since tv is a local variable. */
140     gettimeofday( &tv_date, NULL );
141     return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
142
143 #endif
144 }
145
146 /*****************************************************************************
147  * mwait: wait for a date (inline function)
148  *****************************************************************************
149  * This function uses select() and an system date function to wake up at a
150  * precise date. It should be used for process synchronization. If current date
151  * is posterior to wished date, the function returns immediately.
152  *****************************************************************************/
153 void mwait( mtime_t date )
154 {
155 #if defined( HAVE_KERNEL_OS_H )
156     mtime_t delay;
157     
158     delay = date - real_time_clock_usecs();
159     if( delay <= 0 )
160     {
161         return;
162     }
163     snooze( delay );
164
165 #elif defined( WIN32 )
166     mtime_t usec_time, delay;
167
168     usec_time = mdate();
169     delay = date - usec_time;
170     if( delay <= 0 )
171     {
172         return;
173     }
174
175     usleep( delay );
176
177 #else
178
179 #   ifdef HAVE_USLEEP
180     struct timeval tv_date;
181 #   else
182     struct timeval tv_date, tv_delay;
183 #   endif
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 #   ifdef HAVE_USLEEP
202     usleep( delay );
203 #   else
204     tv_delay.tv_sec = delay / 1000000;
205     tv_delay.tv_usec = delay % 1000000;
206
207     /* see msleep() about select() errors */
208     select( 0, NULL, NULL, NULL, &tv_delay );
209 #   endif
210
211 #endif
212 }
213
214 /*****************************************************************************
215  * msleep: more precise sleep() (inline function)                        (ok ?)
216  *****************************************************************************
217  * Portable usleep() function.
218  *****************************************************************************/
219 void msleep( mtime_t delay )
220 {
221 #if defined( HAVE_KERNEL_OS_H )
222     snooze( delay );
223
224 #elif defined( HAVE_USLEEP ) || defined( WIN32 )
225     usleep( delay );
226
227 #else
228     struct timeval tv_delay;
229
230     tv_delay.tv_sec = delay / 1000000;
231     tv_delay.tv_usec = delay % 1000000;
232     /* select() return value should be tested, since several possible errors
233      * can occur. However, they should only happen in very particular occasions
234      * (i.e. when a signal is sent to the thread, or when memory is full), and
235      * can be ingnored. */
236     select( 0, NULL, NULL, NULL, &tv_delay );
237
238 #endif
239 }
240