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 ******************************************************************************/
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
26 ******************************************************************************/
27 char *mstrtime( char *psz_buffer, mtime_t date )
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) );
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 ******************************************************************************
45 * implement the function when gettimeofday is not available
46 * this function should be decalred as inline
47 ******************************************************************************/
50 struct timeval tv_date;
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 );
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 ******************************************************************************
67 * implement the function when gettimeofday is not available
68 * optimize delay calculation
69 * ?? declare as inline
70 ******************************************************************************/
71 void mwait( mtime_t date )
73 struct timeval tv_date, tv_delay;
74 mtime_t delay; /* delay in msec, signed to detect errors */
76 /* see mdate() about gettimeofday() possible errors */
77 gettimeofday( &tv_date, NULL );
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 */
85 tv_delay.tv_sec = delay / 1000000;
86 tv_delay.tv_usec = delay % 1000000;
88 /* see msleep() about select() errors */
89 select( 0, NULL, NULL, NULL, &tv_delay );
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 )
103 struct timeval tv_delay;
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 );