]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
* Header cleaning: filled all empty authors fields, added CVS $Id stuff.
[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.16 2001/03/21 13:42:34 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 #include "config.h"
44 #include "common.h"
45 #include "mtime.h"
46
47 /*****************************************************************************
48  * mstrtime: return a date in a readable format
49  *****************************************************************************
50  * This functions is provided for any interface function which need to print a
51  * date. psz_buffer should be a buffer long enough to store the formatted
52  * date.
53  *****************************************************************************/
54 char *mstrtime( char *psz_buffer, mtime_t date )
55 {
56     sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
57              (int) (date / (1000LL * 1000LL * 60LL * 60LL) % 24LL),
58              (int) (date / (1000LL * 1000LL * 60LL) % 60LL),
59              (int) (date / (1000LL * 1000LL) % 60LL),
60              (int) (date / 1000LL % 1000LL),
61              (int) (date % 1000LL) );
62     return( psz_buffer );
63 }
64
65 /*****************************************************************************
66  * mdate: return high precision date (inline function)
67  *****************************************************************************
68  * Uses the gettimeofday() function when possible (1 MHz resolution) or the
69  * ftime() function (1 kHz resolution).
70  *****************************************************************************/
71 mtime_t mdate( void )
72 {
73 #ifdef HAVE_KERNEL_OS_H
74     return( real_time_clock_usecs() );
75     
76 #else
77     struct timeval tv_date;
78
79     /* gettimeofday() could return an error, and should be tested. However, the
80      * only possible error, according to 'man', is EFAULT, which can not happen
81      * here, since tv is a local variable. */
82     gettimeofday( &tv_date, NULL );
83     return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
84     
85 #endif
86 }
87
88 /*****************************************************************************
89  * mwait: wait for a date (inline function)
90  *****************************************************************************
91  * This function uses select() and an system date function to wake up at a
92  * precise date. It should be used for process synchronization. If current date
93  * is posterior to wished date, the function returns immediately.
94  *****************************************************************************/
95 void mwait( mtime_t date )
96 {
97 #ifdef HAVE_KERNEL_OS_H
98
99     mtime_t delay;
100     
101     delay = date - real_time_clock_usecs();
102     if( delay <= 0 )
103     {
104         return;
105     }
106     snooze( delay );
107 #else
108
109 #ifdef HAVE_USLEEP
110     struct timeval tv_date;
111
112 #else
113     struct timeval tv_date, tv_delay;
114
115 #endif
116     mtime_t        delay;          /* delay in msec, signed to detect errors */
117
118     /* see mdate() about gettimeofday() possible errors */
119     gettimeofday( &tv_date, NULL );
120
121     /* calculate delay and check if current date is before wished date */
122     delay = date - (mtime_t) tv_date.tv_sec * 1000000 - (mtime_t) tv_date.tv_usec - 10000;
123     /* Linux/i386 has a granularity of 10 ms. It's better to be in advance
124      * than to be late. */
125     if( delay <= 0 )                 /* wished date is now or already passed */
126     {
127         return;
128     }
129
130 #ifdef HAVE_USLEEP
131     usleep( delay );
132 #else
133     tv_delay.tv_sec = delay / 1000000;
134     tv_delay.tv_usec = delay % 1000000;
135
136     /* see msleep() about select() errors */
137     select( 0, NULL, NULL, NULL, &tv_delay );
138 #endif
139
140 #endif /* HAVE_KERNEL_OS_H */
141 }
142
143 /*****************************************************************************
144  * msleep: more precise sleep() (inline function)                        (ok ?)
145  *****************************************************************************
146  * Portable usleep() function.
147  *****************************************************************************/
148 void msleep( mtime_t delay )
149 {
150 #ifdef HAVE_KERNEL_OS_H
151     snooze( delay );
152 #else
153
154 #ifdef HAVE_USLEEP
155     usleep( delay );
156 #else
157     struct timeval tv_delay;
158
159     tv_delay.tv_sec = delay / 1000000;
160     tv_delay.tv_usec = delay % 1000000;
161     /* select() return value should be tested, since several possible errors
162      * can occur. However, they should only happen in very particular occasions
163      * (i.e. when a signal is sent to the thread, or when memory is full), and
164      * can be ingnored. */
165     select( 0, NULL, NULL, NULL, &tv_delay );
166 #endif
167
168 #endif /* HAVE_KERNEL_OS_H */
169 }