]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
Add more uses of msecstotimestr. Remove OFFSETTOTIME_MAX_SIZE -
[vlc] / src / misc / mtime.c
1 /*****************************************************************************
2  * mtime.c: high resolution time management functions
3  * Functions are prototyped in mtime.h.
4  *****************************************************************************
5  * Copyright (C) 1998-2001, 2003 VideoLAN
6  * $Id: mtime.c,v 1.38 2003/12/03 13:27:51 rocky 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 function converts a mtime date into a string.
71  * psz_buffer should be a buffer long enough to store the formatted
72  * date.
73  *****************************************************************************/
74 /**
75  * \brief return a date in a readable format
76  * \param date to be converted
77  * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
78  * \return psz_buffer is returned so this can be used as printf parameter.
79  */
80 char *mstrtime( char *psz_buffer, mtime_t date )
81 {
82     static mtime_t ll1000 = 1000, ll60 = 60, ll24 = 24;
83
84     snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%02d:%02d:%02d-%03d.%03d",
85              (int) (date / (ll1000 * ll1000 * ll60 * ll60) % ll24),
86              (int) (date / (ll1000 * ll1000 * ll60) % ll60),
87              (int) (date / (ll1000 * ll1000) % ll60),
88              (int) (date / ll1000 % ll1000),
89              (int) (date % ll1000) );
90     return( psz_buffer );
91 }
92
93 /*****************************************************************************
94  * secstotimestr: convert seconds to a time in the format h:mm:ss 
95  *****************************************************************************
96  * This function is provided for any interface function which need to print a
97  * time string in the format h:mm:ss
98  * date.
99  *****************************************************************************/
100 /**
101  * \brief convert seconds to a time in the format h:mm:ss 
102  * \param secs  the date to be converted
103  * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
104  * \return psz_buffer is returned so this can be used as printf parameter.
105  */
106 char *secstotimestr( char *psz_buffer, int i_seconds )
107 {
108   snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%d:%2.2d:%2.2d",
109             (int) (i_seconds / (60 *60)), 
110             (int) ((i_seconds / 60) % 60), 
111             (int) (i_seconds % 60) );
112   return( psz_buffer );
113 }
114
115 /*****************************************************************************
116  * mdate: return high precision date
117  *****************************************************************************
118  * Uses the gettimeofday() function when possible (1 MHz resolution) or the
119  * ftime() function (1 kHz resolution).
120  *****************************************************************************/
121 mtime_t mdate( void )
122 {
123 #if defined( HAVE_KERNEL_OS_H )
124     return( real_time_clock_usecs() );
125
126 #elif defined( WIN32 ) || defined( UNDER_CE )
127     /* We don't need the real date, just the value of a high precision timer */
128     static mtime_t freq = I64C(-1);
129     mtime_t usec_time;
130
131     if( freq == I64C(-1) )
132     {
133         /* Extract from the Tcl source code:
134          * (http://www.cs.man.ac.uk/fellowsd-bin/TIP/7.html)
135          *
136          * Some hardware abstraction layers use the CPU clock
137          * in place of the real-time clock as a performance counter
138          * reference.  This results in:
139          *    - inconsistent results among the processors on
140          *      multi-processor systems.
141          *    - unpredictable changes in performance counter frequency
142          *      on "gearshift" processors such as Transmeta and
143          *      SpeedStep.
144          * There seems to be no way to test whether the performance
145          * counter is reliable, but a useful heuristic is that
146          * if its frequency is 1.193182 MHz or 3.579545 MHz, it's
147          * derived from a colorburst crystal and is therefore
148          * the RTC rather than the TSC.  If it's anything else, we
149          * presume that the performance counter is unreliable.
150          */
151
152         freq = ( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) &&
153                  (freq == I64C(1193182) || freq == I64C(3579545) ) )
154                ? freq : 0;
155     }
156
157     if( freq != 0 )
158     {
159         /* Microsecond resolution */
160         QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
161         return ( usec_time * 1000000 ) / freq;
162     }
163
164     /* Milisecond resolution (actually, best case is about 10 ms resolution) */
165     return 1000 * GetTickCount();
166
167 #else
168     struct timeval tv_date;
169
170     /* gettimeofday() could return an error, and should be tested. However, the
171      * only possible error, according to 'man', is EFAULT, which can not happen
172      * here, since tv is a local variable. */
173     gettimeofday( &tv_date, NULL );
174     return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
175
176 #endif
177 }
178
179 /*****************************************************************************
180  * mwait: wait for a date
181  *****************************************************************************
182  * This function uses select() and an system date function to wake up at a
183  * precise date. It should be used for process synchronization. If current date
184  * is posterior to wished date, the function returns immediately.
185  *****************************************************************************/
186 void mwait( mtime_t date )
187 {
188 #if defined( HAVE_KERNEL_OS_H )
189     mtime_t delay;
190     
191     delay = date - real_time_clock_usecs();
192     if( delay <= 0 )
193     {
194         return;
195     }
196     snooze( delay );
197
198 #elif defined( WIN32 ) || defined( UNDER_CE )
199     mtime_t usec_time, delay;
200
201     usec_time = mdate();
202     delay = date - usec_time;
203     if( delay <= 0 )
204     {
205         return;
206     }
207     msleep( delay );
208
209 #else
210
211     struct timeval tv_date;
212     mtime_t        delay;          /* delay in msec, signed to detect errors */
213
214     /* see mdate() about gettimeofday() possible errors */
215     gettimeofday( &tv_date, NULL );
216
217     /* calculate delay and check if current date is before wished date */
218     delay = date - (mtime_t) tv_date.tv_sec * 1000000
219                  - (mtime_t) tv_date.tv_usec
220                  - 10000;
221
222     /* Linux/i386 has a granularity of 10 ms. It's better to be in advance
223      * than to be late. */
224     if( delay <= 0 )                 /* wished date is now or already passed */
225     {
226         return;
227     }
228
229 #   if defined( PTH_INIT_IN_PTH_H )
230     pth_usleep( delay );
231
232 #   elif defined( ST_INIT_IN_ST_H )
233     st_usleep( delay );
234
235 #   else
236
237 #       if defined( HAVE_NANOSLEEP )
238     {
239         struct timespec ts_delay;
240         ts_delay.tv_sec = delay / 1000000;
241         ts_delay.tv_nsec = (delay % 1000000) * 1000;
242
243         nanosleep( &ts_delay, NULL );
244     }
245
246 #       else
247     tv_date.tv_sec = delay / 1000000;
248     tv_date.tv_usec = delay % 1000000;
249     /* see msleep() about select() errors */
250     select( 0, NULL, NULL, NULL, &tv_date );
251 #       endif
252
253 #   endif
254
255 #endif
256 }
257
258 /*****************************************************************************
259  * msleep: more precise sleep()
260  *****************************************************************************
261  * Portable usleep() function.
262  *****************************************************************************/
263 void msleep( mtime_t delay )
264 {
265 #if defined( HAVE_KERNEL_OS_H )
266     snooze( delay );
267
268 #elif defined( PTH_INIT_IN_PTH_H )
269     pth_usleep( delay );
270
271 #elif defined( ST_INIT_IN_ST_H )
272     st_usleep( delay );
273
274 #elif defined( WIN32 ) || defined( UNDER_CE )
275     Sleep( (int) (delay / 1000) );
276
277 #elif defined( HAVE_NANOSLEEP )
278     struct timespec ts_delay;
279
280     ts_delay.tv_sec = delay / 1000000;
281     ts_delay.tv_nsec = (delay % 1000000) * 1000;
282
283     nanosleep( &ts_delay, NULL );
284
285 #else
286     struct timeval tv_delay;
287
288     tv_delay.tv_sec = delay / 1000000;
289     tv_delay.tv_usec = delay % 1000000;
290
291     /* select() return value should be tested, since several possible errors
292      * can occur. However, they should only happen in very particular occasions
293      * (i.e. when a signal is sent to the thread, or when memory is full), and
294      * can be ignored. */
295     select( 0, NULL, NULL, NULL, &tv_delay );
296
297 #endif
298 }
299