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 / (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) );
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 mtime_t 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 now or already passed */
87 tv_delay.tv_sec = delay / 1000000;
88 tv_delay.tv_usec = delay % 1000000;
90 /* see msleep() about select() errors */
91 select( 0, NULL, NULL, NULL, &tv_delay );
97 /******************************************************************************
98 * msleep: more precise sleep() (inline function) (ok ?)
99 ******************************************************************************
100 * Portable usleep() function.
101 ******************************************************************************/
102 void msleep( mtime_t delay )
105 struct timeval tv_delay;
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 );