]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
* ./AUTHORS : added lool :-p ;
[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-2001 VideoLAN
6  * $Id: mtime.c,v 1.33 2002/07/12 21:57:25 massiot 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 )
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 functions is provided for any interface function which need to print a
71  * date. psz_buffer should be a buffer long enough to store the formatted
72  * date.
73  *****************************************************************************/
74 char *mstrtime( char *psz_buffer, mtime_t date )
75 {
76     sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
77              (int) (date / (I64C(1000) * I64C(1000) * I64C(60) * I64C(60)) % I64C(24)),
78              (int) (date / (I64C(1000) * I64C(1000) * I64C(60)) % I64C(60)),
79              (int) (date / (I64C(1000) * I64C(1000)) % I64C(60)),
80              (int) (date / I64C(1000) % I64C(1000)),
81              (int) (date % I64C(1000)) );
82     return( psz_buffer );
83 }
84
85 /*****************************************************************************
86  * mdate: return high precision date
87  *****************************************************************************
88  * Uses the gettimeofday() function when possible (1 MHz resolution) or the
89  * ftime() function (1 kHz resolution).
90  *****************************************************************************/
91 mtime_t mdate( void )
92 {
93 #if defined( HAVE_KERNEL_OS_H )
94     return( real_time_clock_usecs() );
95
96 #elif defined( WIN32 )
97     /* We don't get the real date, just the value of a high precision timer.
98      * this is because the usual time functions have at best only a milisecond
99      * resolution */
100     mtime_t freq, usec_time;
101
102     if( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) )
103     {
104         /* Microsecond resolution */
105         QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
106         return ( usec_time * 1000000 ) / freq;
107     }
108
109     /* Milisecond resolution */
110     return 1000 * GetTickCount();
111
112 #else
113     struct timeval tv_date;
114
115     /* gettimeofday() could return an error, and should be tested. However, the
116      * only possible error, according to 'man', is EFAULT, which can not happen
117      * here, since tv is a local variable. */
118     gettimeofday( &tv_date, NULL );
119     return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
120
121 #endif
122 }
123
124 /*****************************************************************************
125  * mwait: wait for a date
126  *****************************************************************************
127  * This function uses select() and an system date function to wake up at a
128  * precise date. It should be used for process synchronization. If current date
129  * is posterior to wished date, the function returns immediately.
130  *****************************************************************************/
131 void mwait( mtime_t date )
132 {
133 #if defined( HAVE_KERNEL_OS_H )
134     mtime_t delay;
135     
136     delay = date - real_time_clock_usecs();
137     if( delay <= 0 )
138     {
139         return;
140     }
141     snooze( delay );
142
143 #elif defined( WIN32 )
144     mtime_t usec_time, delay;
145
146     usec_time = mdate();
147     delay = date - usec_time;
148     if( delay <= 0 )
149     {
150         return;
151     }
152     msleep( delay );
153
154 #else
155
156     struct timeval tv_date;
157     mtime_t        delay;          /* delay in msec, signed to detect errors */
158
159     /* see mdate() about gettimeofday() possible errors */
160     gettimeofday( &tv_date, NULL );
161
162     /* calculate delay and check if current date is before wished date */
163     delay = date - (mtime_t) tv_date.tv_sec * 1000000
164                  - (mtime_t) tv_date.tv_usec
165                  - 10000;
166
167     /* Linux/i386 has a granularity of 10 ms. It's better to be in advance
168      * than to be late. */
169     if( delay <= 0 )                 /* wished date is now or already passed */
170     {
171         return;
172     }
173
174 #   if defined( PTH_INIT_IN_PTH_H )
175     pth_usleep( delay );
176
177 #   elif defined( ST_INIT_IN_ST_H )
178     st_usleep( delay );
179
180 #   else
181
182 #       if defined( HAVE_NANOSLEEP )
183     {
184         struct timespec ts_delay;
185         ts_delay.tv_sec = delay / 1000000;
186         ts_delay.tv_nsec = (delay % 1000000) * 1000;
187
188         nanosleep( &ts_delay, NULL );
189     }
190
191 #       else
192     tv_date.tv_sec = delay / 1000000;
193     tv_date.tv_usec = delay % 1000000;
194     /* see msleep() about select() errors */
195     select( 0, NULL, NULL, NULL, &tv_date );
196 #       endif
197
198 #   endif
199
200 #endif
201 }
202
203 /*****************************************************************************
204  * msleep: more precise sleep()
205  *****************************************************************************
206  * Portable usleep() function.
207  *****************************************************************************/
208 void msleep( mtime_t delay )
209 {
210 #if defined( HAVE_KERNEL_OS_H )
211     snooze( delay );
212
213 #elif defined( PTH_INIT_IN_PTH_H )
214     pth_usleep( delay );
215
216 #elif defined( ST_INIT_IN_ST_H )
217     st_usleep( delay );
218
219 #elif defined( WIN32 )
220     Sleep( (int) (delay / 1000) );
221
222 #elif defined( HAVE_NANOSLEEP )
223     struct timespec ts_delay;
224
225     ts_delay.tv_sec = delay / 1000000;
226     ts_delay.tv_nsec = (delay % 1000000) * 1000;
227
228     nanosleep( &ts_delay, NULL );
229
230 #else
231     struct timeval tv_delay;
232
233     tv_delay.tv_sec = delay / 1000000;
234     tv_delay.tv_usec = delay % 1000000;
235
236     /* select() return value should be tested, since several possible errors
237      * can occur. However, they should only happen in very particular occasions
238      * (i.e. when a signal is sent to the thread, or when memory is full), and
239      * can be ignored. */
240     select( 0, NULL, NULL, NULL, &tv_delay );
241
242 #endif
243 }
244