]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
Bon. On ne rit pas, je m'�tais juste plant� dans l'en-t�te des
[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  *
7  * Authors:
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*
25  * TODO:
26  *  see if using Linux real-time extensions is possible and profitable
27  */
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include "defs.h"
33
34 #include <stdio.h>                                              /* sprintf() */
35 #include <unistd.h>                                              /* select() */
36 #include <sys/time.h>
37
38 #ifdef HAVE_KERNEL_OS_H
39 #include <kernel/OS.h>
40 #endif
41
42 #include "config.h"
43 #include "common.h"
44 #include "mtime.h"
45
46 /*****************************************************************************
47  * mstrtime: return a date in a readable format
48  *****************************************************************************
49  * This functions is provided for any interface function which need to print a
50  * date. psz_buffer should be a buffer long enough to store the formatted
51  * date.
52  *****************************************************************************/
53 char *mstrtime( char *psz_buffer, mtime_t date )
54 {
55     sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
56              (int) (date / (1000LL * 1000LL * 60LL * 60LL) % 24LL),
57              (int) (date / (1000LL * 1000LL * 60LL) % 60LL),
58              (int) (date / (1000LL * 1000LL) % 60LL),
59              (int) (date / 1000LL % 1000LL),
60              (int) (date % 1000LL) );
61     return( psz_buffer );
62 }
63
64 /*****************************************************************************
65  * mdate: return high precision date (inline function)
66  *****************************************************************************
67  * Uses the gettimeofday() function when possible (1 MHz resolution) or the
68  * ftime() function (1 kHz resolution).
69  *****************************************************************************/
70 mtime_t mdate( void )
71 {
72 #ifdef HAVE_KERNEL_OS_H
73     return( real_time_clock_usecs() );
74     
75 #else
76     struct timeval tv_date;
77
78     /* gettimeofday() could return an error, and should be tested. However, the
79      * only possible error, according to 'man', is EFAULT, which can not happen
80      * here, since tv is a local variable. */
81     gettimeofday( &tv_date, NULL );
82     return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
83     
84 #endif
85 }
86
87 /*****************************************************************************
88  * mwait: wait for a date (inline function)
89  *****************************************************************************
90  * This function uses select() and an system date function to wake up at a
91  * precise date. It should be used for process synchronization. If current date
92  * is posterior to wished date, the function returns immediately.
93  *****************************************************************************/
94 void mwait( mtime_t date )
95 {
96 #ifdef HAVE_KERNEL_OS_H
97         
98     mtime_t delay;
99     
100     delay = date - real_time_clock_usecs();
101     if( delay <= 0 )
102     {
103         return;
104     }
105     snooze( delay );
106 #else
107
108 #ifdef HAVE_USLEEP
109     struct timeval tv_date;
110
111 #else
112     struct timeval tv_date, tv_delay;
113
114 #endif
115     mtime_t        delay;          /* delay in msec, signed to detect errors */
116
117     /* see mdate() about gettimeofday() possible errors */
118     gettimeofday( &tv_date, NULL );
119
120     /* calculate delay and check if current date is before wished date */
121     delay = date - (mtime_t) tv_date.tv_sec * 1000000 - (mtime_t) tv_date.tv_usec;
122     if( delay <= 0 )                 /* wished date is now or already passed */
123     {
124         return;
125     }
126
127 #ifdef HAVE_USLEEP
128     usleep( delay );
129 #else
130     tv_delay.tv_sec = delay / 1000000;
131     tv_delay.tv_usec = delay % 1000000;
132
133     /* see msleep() about select() errors */
134     select( 0, NULL, NULL, NULL, &tv_delay );
135 #endif
136
137 #endif /* HAVE_KERNEL_OS_H */
138 }
139
140 /*****************************************************************************
141  * msleep: more precise sleep() (inline function)                        (ok ?)
142  *****************************************************************************
143  * Portable usleep() function.
144  *****************************************************************************/
145 void msleep( mtime_t delay )
146 {
147 #ifdef HAVE_KERNEL_OS_H
148     snooze( delay );
149 #else
150
151 #ifdef HAVE_USLEEP
152     usleep( delay );
153 #else
154     struct timeval tv_delay;
155
156     tv_delay.tv_sec = delay / 1000000;
157     tv_delay.tv_usec = delay % 1000000;
158     /* select() return value should be tested, since several possible errors
159      * can occur. However, they should only happen in very particular occasions
160      * (i.e. when a signal is sent to the thread, or when memory is full), and
161      * can be ingnored. */
162     select( 0, NULL, NULL, NULL, &tv_delay );
163 #endif
164
165 #endif /* HAVE_KERNEL_OS_H */
166 }