]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
Integration de display.c � vout.
[vlc] / src / misc / mtime.c
1 /******************************************************************************
2  * mtime.c: high rezolution time management functions
3  * (c)1998 VideoLAN
4  ******************************************************************************
5  * Functions are prototyped in mtime.h.
6  ******************************************************************************
7  * to-do list:
8  *  see if using Linux real-time extensions is possible and profitable
9  ******************************************************************************/
10
11 /******************************************************************************
12  * Preamble
13  ******************************************************************************/
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <sys/time.h>
17
18 #include "common.h"
19 #include "mtime.h"
20
21 /******************************************************************************
22  * mstrtime: return a date in a readable format
23  ******************************************************************************
24  * This functions is provided for any interface function which need to print a
25  * date. psz_buffer should be a buffer long enough to store the formatted
26  * date.
27  ******************************************************************************/
28 char *mstrtime( char *psz_buffer, mtime_t date )
29 {
30     sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
31              (int) (date / (1000LL * 1000LL * 60LL * 60LL) % 24LL),
32              (int) (date / (1000LL * 1000LL * 60LL) % 60LL),
33              (int) (date / (1000LL * 1000LL) % 60LL),
34              (int) (date / 1000LL % 1000LL),
35              (int) (date % 1000LL) );
36     return( psz_buffer );
37 }
38
39 /******************************************************************************
40  * mdate: return high precision date (inline function)
41  ******************************************************************************
42  * Uses the gettimeofday() function when possible (1 MHz resolution) or the
43  * ftime() function (1 kHz resolution).
44  ******************************************************************************
45  * to-do list: ??
46  *  implement the function when gettimeofday is not available
47  *  this function should be decalred as inline
48  ******************************************************************************/
49 mtime_t mdate( void )
50 {
51     struct timeval tv_date;
52
53     /* gettimeofday() could return an error, and should be tested. However, the
54      * only possible error, according to 'man', is EFAULT, which can not happen
55      * here, since tv is a local variable. */
56     gettimeofday( &tv_date, NULL );
57     return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
58 }
59
60 /******************************************************************************
61  * mwait: wait for a date (inline function)
62  ******************************************************************************
63  * This function uses select() and an system date function to wake up at a
64  * precise date. It should be used for process synchronization. If current date
65  * is posterior to wished date, the function returns immediately.
66  ******************************************************************************
67  * to-do list:
68  *  implement the function when gettimeofday is not available
69  *  optimize delay calculation
70  *  ?? declare as inline
71  ******************************************************************************/
72 void mwait( mtime_t date )
73 {
74     struct timeval tv_date, tv_delay;
75     mtime_t        delay;           /* delay in msec, signed to detect errors */
76
77     /* see mdate() about gettimeofday() possible errors */
78     gettimeofday( &tv_date, NULL );
79
80     /* calculate delay and check if current date is before wished date */
81     delay = date - (mtime_t) tv_date.tv_sec * 1000000 - (mtime_t) tv_date.tv_usec;
82     if( delay <= 0 )                  /* wished date is now or already passed */
83     {
84         return;
85     }
86 #ifndef usleep
87     tv_delay.tv_sec = delay / 1000000;
88     tv_delay.tv_usec = delay % 1000000;
89
90     /* see msleep() about select() errors */
91     select( 0, NULL, NULL, NULL, &tv_delay );
92 #else
93     usleep( delay );    
94 #endif
95 }
96
97 /******************************************************************************
98  * msleep: more precise sleep() (inline function)                        (ok ?)
99  ******************************************************************************
100  * Portable usleep() function.
101  ******************************************************************************/
102 void msleep( mtime_t delay )
103 {
104 #ifndef usleep
105     struct timeval tv_delay;
106
107     tv_delay.tv_sec = delay / 1000000;
108     tv_delay.tv_usec = delay % 1000000;
109     /* select() return value should be tested, since several possible errors
110      * can occur. However, they should only happen in very particular occasions
111      * (i.e. when a signal is sent to the thread, or when memory is full), and
112      * can be ingnored. */
113     select( 0, NULL, NULL, NULL, &tv_delay );
114 #else
115     usleep( delay );
116 #endif
117 }