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.37 2003/12/02 01:54:30 rocky Exp $
8 * Authors: Vincent Seguin <seguin@via.ecp.fr>
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.
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.
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 *****************************************************************************/
27 * see if using Linux real-time extensions is possible and profitable
30 /*****************************************************************************
32 *****************************************************************************/
33 #include <stdio.h> /* sprintf() */
37 #if defined( PTH_INIT_IN_PTH_H ) /* GNU Pth */
42 # include <unistd.h> /* select() */
45 #ifdef HAVE_KERNEL_OS_H
46 # include <kernel/OS.h>
49 #if defined( WIN32 ) || defined( UNDER_CE )
52 # include <sys/time.h>
55 #if defined(HAVE_NANOSLEEP) && !defined(HAVE_STRUCT_TIMESPEC)
63 #if defined(HAVE_NANOSLEEP) && !defined(HAVE_DECL_NANOSLEEP)
64 int nanosleep(struct timespec *, struct timespec *);
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
73 *****************************************************************************/
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.
80 char *mstrtime( char *psz_buffer, mtime_t date )
82 static mtime_t ll1000 = 1000, ll60 = 60, ll24 = 24;
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) );
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
99 *****************************************************************************/
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.
106 char *secstotimestr( char *psz_buffer, int secs )
108 snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%d:%2.2d:%2.2d",
109 (int) (secs / 3600), (int)(( secs % 3600 ) / 60),
111 return( psz_buffer );
114 /*****************************************************************************
115 * mdate: return high precision date
116 *****************************************************************************
117 * Uses the gettimeofday() function when possible (1 MHz resolution) or the
118 * ftime() function (1 kHz resolution).
119 *****************************************************************************/
120 mtime_t mdate( void )
122 #if defined( HAVE_KERNEL_OS_H )
123 return( real_time_clock_usecs() );
125 #elif defined( WIN32 ) || defined( UNDER_CE )
126 /* We don't need the real date, just the value of a high precision timer */
127 static mtime_t freq = I64C(-1);
130 if( freq == I64C(-1) )
132 /* Extract from the Tcl source code:
133 * (http://www.cs.man.ac.uk/fellowsd-bin/TIP/7.html)
135 * Some hardware abstraction layers use the CPU clock
136 * in place of the real-time clock as a performance counter
137 * reference. This results in:
138 * - inconsistent results among the processors on
139 * multi-processor systems.
140 * - unpredictable changes in performance counter frequency
141 * on "gearshift" processors such as Transmeta and
143 * There seems to be no way to test whether the performance
144 * counter is reliable, but a useful heuristic is that
145 * if its frequency is 1.193182 MHz or 3.579545 MHz, it's
146 * derived from a colorburst crystal and is therefore
147 * the RTC rather than the TSC. If it's anything else, we
148 * presume that the performance counter is unreliable.
151 freq = ( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) &&
152 (freq == I64C(1193182) || freq == I64C(3579545) ) )
158 /* Microsecond resolution */
159 QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
160 return ( usec_time * 1000000 ) / freq;
163 /* Milisecond resolution (actually, best case is about 10 ms resolution) */
164 return 1000 * GetTickCount();
167 struct timeval tv_date;
169 /* gettimeofday() could return an error, and should be tested. However, the
170 * only possible error, according to 'man', is EFAULT, which can not happen
171 * here, since tv is a local variable. */
172 gettimeofday( &tv_date, NULL );
173 return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
178 /*****************************************************************************
179 * mwait: wait for a date
180 *****************************************************************************
181 * This function uses select() and an system date function to wake up at a
182 * precise date. It should be used for process synchronization. If current date
183 * is posterior to wished date, the function returns immediately.
184 *****************************************************************************/
185 void mwait( mtime_t date )
187 #if defined( HAVE_KERNEL_OS_H )
190 delay = date - real_time_clock_usecs();
197 #elif defined( WIN32 ) || defined( UNDER_CE )
198 mtime_t usec_time, delay;
201 delay = date - usec_time;
210 struct timeval tv_date;
211 mtime_t delay; /* delay in msec, signed to detect errors */
213 /* see mdate() about gettimeofday() possible errors */
214 gettimeofday( &tv_date, NULL );
216 /* calculate delay and check if current date is before wished date */
217 delay = date - (mtime_t) tv_date.tv_sec * 1000000
218 - (mtime_t) tv_date.tv_usec
221 /* Linux/i386 has a granularity of 10 ms. It's better to be in advance
222 * than to be late. */
223 if( delay <= 0 ) /* wished date is now or already passed */
228 # if defined( PTH_INIT_IN_PTH_H )
231 # elif defined( ST_INIT_IN_ST_H )
236 # if defined( HAVE_NANOSLEEP )
238 struct timespec ts_delay;
239 ts_delay.tv_sec = delay / 1000000;
240 ts_delay.tv_nsec = (delay % 1000000) * 1000;
242 nanosleep( &ts_delay, NULL );
246 tv_date.tv_sec = delay / 1000000;
247 tv_date.tv_usec = delay % 1000000;
248 /* see msleep() about select() errors */
249 select( 0, NULL, NULL, NULL, &tv_date );
257 /*****************************************************************************
258 * msleep: more precise sleep()
259 *****************************************************************************
260 * Portable usleep() function.
261 *****************************************************************************/
262 void msleep( mtime_t delay )
264 #if defined( HAVE_KERNEL_OS_H )
267 #elif defined( PTH_INIT_IN_PTH_H )
270 #elif defined( ST_INIT_IN_ST_H )
273 #elif defined( WIN32 ) || defined( UNDER_CE )
274 Sleep( (int) (delay / 1000) );
276 #elif defined( HAVE_NANOSLEEP )
277 struct timespec ts_delay;
279 ts_delay.tv_sec = delay / 1000000;
280 ts_delay.tv_nsec = (delay % 1000000) * 1000;
282 nanosleep( &ts_delay, NULL );
285 struct timeval tv_delay;
287 tv_delay.tv_sec = delay / 1000000;
288 tv_delay.tv_usec = delay % 1000000;
290 /* select() return value should be tested, since several possible errors
291 * can occur. However, they should only happen in very particular occasions
292 * (i.e. when a signal is sent to the thread, or when memory is full), and
294 select( 0, NULL, NULL, NULL, &tv_delay );