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