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.39 2003/12/03 21:50:50 sigmunau 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 *);
68 * Return a date in a readable format
70 * This function converts a mtime date into a string.
71 * psz_buffer should be a buffer long enough to store the formatted
73 * \param date to be converted
74 * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
75 * \return psz_buffer is returned so this can be used as printf parameter.
77 char *mstrtime( char *psz_buffer, mtime_t date )
79 static mtime_t ll1000 = 1000, ll60 = 60, ll24 = 24;
81 snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%02d:%02d:%02d-%03d.%03d",
82 (int) (date / (ll1000 * ll1000 * ll60 * ll60) % ll24),
83 (int) (date / (ll1000 * ll1000 * ll60) % ll60),
84 (int) (date / (ll1000 * ll1000) % ll60),
85 (int) (date / ll1000 % ll1000),
86 (int) (date % ll1000) );
91 * Convert seconds to a time in the format h:mm:ss.
93 * This function is provided for any interface function which need to print a
94 * time string in the format h:mm:ss
96 * \param secs the date to be converted
97 * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
98 * \return psz_buffer is returned so this can be used as printf parameter.
100 char *secstotimestr( char *psz_buffer, int i_seconds )
102 snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%d:%2.2d:%2.2d",
103 (int) (i_seconds / (60 *60)),
104 (int) ((i_seconds / 60) % 60),
105 (int) (i_seconds % 60) );
106 return( psz_buffer );
110 * Return high precision date
112 * Uses the gettimeofday() function when possible (1 MHz resolution) or the
113 * ftime() function (1 kHz resolution).
115 mtime_t mdate( void )
117 #if defined( HAVE_KERNEL_OS_H )
118 return( real_time_clock_usecs() );
120 #elif defined( WIN32 ) || defined( UNDER_CE )
121 /* We don't need the real date, just the value of a high precision timer */
122 static mtime_t freq = I64C(-1);
125 if( freq == I64C(-1) )
127 /* Extract from the Tcl source code:
128 * (http://www.cs.man.ac.uk/fellowsd-bin/TIP/7.html)
130 * Some hardware abstraction layers use the CPU clock
131 * in place of the real-time clock as a performance counter
132 * reference. This results in:
133 * - inconsistent results among the processors on
134 * multi-processor systems.
135 * - unpredictable changes in performance counter frequency
136 * on "gearshift" processors such as Transmeta and
138 * There seems to be no way to test whether the performance
139 * counter is reliable, but a useful heuristic is that
140 * if its frequency is 1.193182 MHz or 3.579545 MHz, it's
141 * derived from a colorburst crystal and is therefore
142 * the RTC rather than the TSC. If it's anything else, we
143 * presume that the performance counter is unreliable.
146 freq = ( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) &&
147 (freq == I64C(1193182) || freq == I64C(3579545) ) )
153 /* Microsecond resolution */
154 QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
155 return ( usec_time * 1000000 ) / freq;
158 /* Milisecond resolution (actually, best case is about 10 ms resolution) */
159 return 1000 * GetTickCount();
162 struct timeval tv_date;
164 /* gettimeofday() could return an error, and should be tested. However, the
165 * only possible error, according to 'man', is EFAULT, which can not happen
166 * here, since tv is a local variable. */
167 gettimeofday( &tv_date, NULL );
168 return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
176 * This function uses select() and an system date function to wake up at a
177 * precise date. It should be used for process synchronization. If current date
178 * is posterior to wished date, the function returns immediately.
179 * \param date The date to wake up at
181 void mwait( mtime_t date )
183 #if defined( HAVE_KERNEL_OS_H )
186 delay = date - real_time_clock_usecs();
193 #elif defined( WIN32 ) || defined( UNDER_CE )
194 mtime_t usec_time, delay;
197 delay = date - usec_time;
206 struct timeval tv_date;
207 mtime_t delay; /* delay in msec, signed to detect errors */
209 /* see mdate() about gettimeofday() possible errors */
210 gettimeofday( &tv_date, NULL );
212 /* calculate delay and check if current date is before wished date */
213 delay = date - (mtime_t) tv_date.tv_sec * 1000000
214 - (mtime_t) tv_date.tv_usec
217 /* Linux/i386 has a granularity of 10 ms. It's better to be in advance
218 * than to be late. */
219 if( delay <= 0 ) /* wished date is now or already passed */
224 # if defined( PTH_INIT_IN_PTH_H )
227 # elif defined( ST_INIT_IN_ST_H )
232 # if defined( HAVE_NANOSLEEP )
234 struct timespec ts_delay;
235 ts_delay.tv_sec = delay / 1000000;
236 ts_delay.tv_nsec = (delay % 1000000) * 1000;
238 nanosleep( &ts_delay, NULL );
242 tv_date.tv_sec = delay / 1000000;
243 tv_date.tv_usec = delay % 1000000;
244 /* see msleep() about select() errors */
245 select( 0, NULL, NULL, NULL, &tv_date );
254 * More precise sleep()
256 * Portable usleep() function.
257 * \param delay the amount of time to sleep
259 void msleep( mtime_t delay )
261 #if defined( HAVE_KERNEL_OS_H )
264 #elif defined( PTH_INIT_IN_PTH_H )
267 #elif defined( ST_INIT_IN_ST_H )
270 #elif defined( WIN32 ) || defined( UNDER_CE )
271 Sleep( (int) (delay / 1000) );
273 #elif defined( HAVE_NANOSLEEP )
274 struct timespec ts_delay;
276 ts_delay.tv_sec = delay / 1000000;
277 ts_delay.tv_nsec = (delay % 1000000) * 1000;
279 nanosleep( &ts_delay, NULL );
282 struct timeval tv_delay;
284 tv_delay.tv_sec = delay / 1000000;
285 tv_delay.tv_usec = delay % 1000000;
287 /* select() return value should be tested, since several possible errors
288 * can occur. However, they should only happen in very particular occasions
289 * (i.e. when a signal is sent to the thread, or when memory is full), and
291 select( 0, NULL, NULL, NULL, &tv_delay );