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