1 /*******************************************************************************
2 * mtime.c: high rezolution time management functions
4 *******************************************************************************
5 * Functions are prototyped in mtime.h.
6 *******************************************************************************
8 * see if using Linux real-time extensions is possible and profitable
9 *******************************************************************************/
11 /*******************************************************************************
13 *******************************************************************************/
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
27 *******************************************************************************/
28 char *mstrtime( char *psz_buffer, mtime_t date )
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) );
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 *******************************************************************************
46 * implement the function when gettimeofday is not available
47 * this function should be decalred as inline
48 *******************************************************************************/
51 struct timeval tv_date;
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 );
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 *******************************************************************************
68 * implement the function when gettimeofday is not available
69 * optimize delay calculation
70 * ?? declare as inline
71 *******************************************************************************/
72 void mwait( mtime_t date )
74 struct timeval tv_date, tv_delay;
75 s64 delay; /* delay in msec, signed to detect errors */
77 /* see mdate() about gettimeofday() possible errors */
78 gettimeofday( &tv_date, NULL );
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 */
86 tv_delay.tv_sec = delay / 1000000;
87 tv_delay.tv_usec = delay % 1000000;
89 /* see msleep() about select() errors */
90 select( 0, NULL, NULL, NULL, &tv_delay );
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 )
104 struct timeval tv_delay;
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 );