]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
d8445581e14d449840d8a67e7180dde7ae8edca6
[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.23 2001/06/14 20:21:04 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 #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 )
50 #   include <windows.h>
51 #else
52 #   include <sys/time.h>
53 #endif
54
55 #include "config.h"
56 #include "common.h"
57 #include "mtime.h"
58
59 #if defined( WIN32 )
60 /*****************************************************************************
61  * usleep: microsecond sleep for win32
62  *****************************************************************************
63  * This function uses performance counter if available, and Sleep() if not.
64  *****************************************************************************/
65 static __inline__ void usleep( unsigned int i_useconds )
66 {
67     s64 i_cur, i_freq;
68     s64 i_now, i_then;
69
70     if( i_useconds < 1000
71          && QueryPerformanceFrequency( (LARGE_INTEGER *) &i_freq ) )
72     {
73         QueryPerformanceCounter( (LARGE_INTEGER *) &i_cur );
74
75         i_now = ( i_cur * 1000 * 1000 / i_freq );
76         i_then = i_now + i_useconds;
77
78         while( i_now < i_then )
79         {
80             QueryPerformanceCounter( (LARGE_INTEGER *) &i_cur );
81             i_now = i_cur * 1000 * 1000 / i_freq;
82         }
83     }
84     else
85     {
86         Sleep( (int) ((i_useconds + 500) / 1000) );
87     }
88 }
89 #endif
90
91 /*****************************************************************************
92  * mstrtime: return a date in a readable format
93  *****************************************************************************
94  * This functions is provided for any interface function which need to print a
95  * date. psz_buffer should be a buffer long enough to store the formatted
96  * date.
97  *****************************************************************************/
98 char *mstrtime( char *psz_buffer, mtime_t date )
99 {
100     sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
101              (int) (date / (I64C(1000) * I64C(1000) * I64C(60) * I64C(60)) % I64C(24)),
102              (int) (date / (I64C(1000) * I64C(1000) * I64C(60)) % I64C(60)),
103              (int) (date / (I64C(1000) * I64C(1000)) % I64C(60)),
104              (int) (date / I64C(1000) % I64C(1000)),
105              (int) (date % I64C(1000)) );
106     return( psz_buffer );
107 }
108
109 /*****************************************************************************
110  * mdate: return high precision date (inline function)
111  *****************************************************************************
112  * Uses the gettimeofday() function when possible (1 MHz resolution) or the
113  * ftime() function (1 kHz resolution).
114  *****************************************************************************/
115 mtime_t mdate( void )
116 {
117 #if defined( HAVE_KERNEL_OS_H )
118     return( real_time_clock_usecs() );
119
120 #elif defined( WIN32 )
121     /* We don't get the real date, just the value of a high precision timer.
122      * this is because the usual time functions have at best only a milisecond
123      * resolution */
124     mtime_t freq, usec_time;
125
126     if( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) )
127     {
128         /* Microsecond resolution */
129         QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
130         return ( usec_time * 1000000 ) / freq;
131     }
132     else
133     {
134         /* Milisecond resolution */
135         return 1000 * GetTickCount();
136     }
137
138 #else
139     struct timeval tv_date;
140
141     /* gettimeofday() could return an error, and should be tested. However, the
142      * only possible error, according to 'man', is EFAULT, which can not happen
143      * here, since tv is a local variable. */
144     gettimeofday( &tv_date, NULL );
145     return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
146
147 #endif
148 }
149
150 /*****************************************************************************
151  * mwait: wait for a date (inline function)
152  *****************************************************************************
153  * This function uses select() and an system date function to wake up at a
154  * precise date. It should be used for process synchronization. If current date
155  * is posterior to wished date, the function returns immediately.
156  *****************************************************************************/
157 void mwait( mtime_t date )
158 {
159 #if defined( HAVE_KERNEL_OS_H )
160     mtime_t delay;
161     
162     delay = date - real_time_clock_usecs();
163     if( delay <= 0 )
164     {
165         return;
166     }
167     snooze( delay );
168
169 #elif defined( WIN32 )
170     mtime_t usec_time, delay;
171
172     usec_time = mdate();
173     delay = date - usec_time;
174     if( delay <= 0 )
175     {
176         return;
177     }
178
179     usleep( delay );
180
181 #else
182
183 #   ifdef HAVE_USLEEP
184     struct timeval tv_date;
185 #   else
186     struct timeval tv_date, tv_delay;
187 #   endif
188     mtime_t        delay;          /* delay in msec, signed to detect errors */
189
190     /* see mdate() about gettimeofday() possible errors */
191     gettimeofday( &tv_date, NULL );
192
193     /* calculate delay and check if current date is before wished date */
194     delay = date - (mtime_t) tv_date.tv_sec * 1000000
195                  - (mtime_t) tv_date.tv_usec
196                  - 10000;
197
198     /* Linux/i386 has a granularity of 10 ms. It's better to be in advance
199      * than to be late. */
200     if( delay <= 0 )                 /* wished date is now or already passed */
201     {
202         return;
203     }
204
205 #   if defined( PTH_INIT_IN_PTH_H )
206     pth_usleep( delay );
207
208 #   elif defined( HAVE_USLEEP )
209     usleep( delay );
210
211 #   else
212     tv_delay.tv_sec = delay / 1000000;
213     tv_delay.tv_usec = delay % 1000000;
214     /* see msleep() about select() errors */
215     select( 0, NULL, NULL, NULL, &tv_delay );
216
217 #   endif
218
219 #endif
220 }
221
222 /*****************************************************************************
223  * msleep: more precise sleep() (inline function)                        (ok ?)
224  *****************************************************************************
225  * Portable usleep() function.
226  *****************************************************************************/
227 void msleep( mtime_t delay )
228 {
229 #if defined( HAVE_KERNEL_OS_H )
230     snooze( delay );
231
232 #elif defined( PTH_INIT_IN_PTH_H )
233     struct timeval tv_delay;
234     tv_delay.tv_sec = delay / 1000000;
235     tv_delay.tv_usec = delay % 1000000;
236     pth_select( 0, NULL, NULL, NULL, &tv_delay );
237
238 #elif defined( HAVE_USLEEP ) || defined( WIN32 )
239     usleep( delay );
240
241 #else
242     struct timeval tv_delay;
243
244     tv_delay.tv_sec = delay / 1000000;
245     tv_delay.tv_usec = delay % 1000000;
246     /* select() return value should be tested, since several possible errors
247      * can occur. However, they should only happen in very particular occasions
248      * (i.e. when a signal is sent to the thread, or when memory is full), and
249      * can be ingnored. */
250     select( 0, NULL, NULL, NULL, &tv_delay );
251
252 #endif
253 }
254