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 $
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() */
35 #include <videolan/vlc.h>
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>
52 # include <sys/time.h>
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
61 *****************************************************************************/
62 char *mstrtime( char *psz_buffer, mtime_t date )
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)) );
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 *****************************************************************************/
81 #if defined( HAVE_KERNEL_OS_H )
82 return( real_time_clock_usecs() );
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
88 mtime_t freq, usec_time;
90 if( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) )
92 /* Microsecond resolution */
93 QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
94 return ( usec_time * 1000000 ) / freq;
97 /* Milisecond resolution */
98 return 1000 * GetTickCount();
101 struct timeval tv_date;
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 );
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 )
121 #if defined( HAVE_KERNEL_OS_H )
124 delay = date - real_time_clock_usecs();
131 #elif defined( WIN32 )
132 mtime_t usec_time, delay;
135 delay = date - usec_time;
145 struct timeval tv_date;
147 struct timeval tv_date, tv_delay;
149 mtime_t delay; /* delay in msec, signed to detect errors */
151 /* see mdate() about gettimeofday() possible errors */
152 gettimeofday( &tv_date, NULL );
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
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 */
166 # if defined( PTH_INIT_IN_PTH_H )
169 # elif defined( HAVE_USLEEP )
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 );
183 /*****************************************************************************
184 * msleep: more precise sleep() (inline function) (ok ?)
185 *****************************************************************************
186 * Portable usleep() function.
187 *****************************************************************************/
188 void msleep( mtime_t delay )
190 #if defined( HAVE_KERNEL_OS_H )
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 );
199 #elif defined( HAVE_USLEEP )
202 #elif defined( WIN32 )
203 Sleep( (int) (delay / 1000) );
206 struct timeval tv_delay;
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 );