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