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