]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
Initial revision
[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
15 #include <stdio.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 / (1000UL * 1000UL * 60UL * 60UL) % 24UL), 
32              (int) (date / (1000UL * 1000UL * 60UL) % 60UL),
33              (int) (date / (1000UL * 1000UL) % 60UL),
34              (int) (date / 1000UL % 1000UL),
35              (int) (date % 1000UL) );
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     s64            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 already passed */
83     {
84         return;
85     }
86     tv_delay.tv_sec = delay / 1000000;
87     tv_delay.tv_usec = delay % 1000000; 
88     
89     /* see msleep() about select() errors */
90     select( 0, NULL, NULL, NULL, &tv_delay );
91 }
92
93 /*******************************************************************************
94  * msleep: more precise sleep() (inline function)                        (ok ?)
95  *******************************************************************************
96  * This function uses select() in a classical way to implement a sleep() call
97  * with a microsecond precision. 
98  * For synchronization purposes, mwait() should be prefered.
99  *******************************************************************************
100  * ?? decalre as inline
101  *******************************************************************************/
102 void msleep( mtime_t delay )
103 {
104     struct timeval tv_delay;
105
106     tv_delay.tv_sec = delay / 1000000;
107     tv_delay.tv_usec = delay % 1000000;
108     /* select() return value should be tested, since several possible errors
109      * can occur. However, they should only happen in very particular occasions
110      * (i.e. when a signal is sent to the thread, or when memory is full), and
111      * can be ingnored. */
112     select( 0, NULL, NULL, NULL, &tv_delay );
113 }