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 *****************************************************************************/
47 struct timeval tv_date;
49 /* gettimeofday() could return an error, and should be tested. However, the
50 * only possible error, according to 'man', is EFAULT, which can not happen
51 * here, since tv is a local variable. */
52 gettimeofday( &tv_date, NULL );
53 return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
56 /*****************************************************************************
57 * mwait: wait for a date (inline function)
58 *****************************************************************************
59 * This function uses select() and an system date function to wake up at a
60 * precise date. It should be used for process synchronization. If current date
61 * is posterior to wished date, the function returns immediately.
62 *****************************************************************************/
63 void mwait( mtime_t date )
65 struct timeval tv_date, tv_delay;
66 mtime_t delay; /* delay in msec, signed to detect errors */
68 /* see mdate() about gettimeofday() possible errors */
69 gettimeofday( &tv_date, NULL );
71 /* calculate delay and check if current date is before wished date */
72 delay = date - (mtime_t) tv_date.tv_sec * 1000000 - (mtime_t) tv_date.tv_usec;
73 if( delay <= 0 ) /* wished date is now or already passed */
78 tv_delay.tv_sec = delay / 1000000;
79 tv_delay.tv_usec = delay % 1000000;
81 /* see msleep() about select() errors */
82 select( 0, NULL, NULL, NULL, &tv_delay );
88 /*****************************************************************************
89 * msleep: more precise sleep() (inline function) (ok ?)
90 *****************************************************************************
91 * Portable usleep() function.
92 *****************************************************************************/
93 void msleep( mtime_t delay )
96 struct timeval tv_delay;
98 tv_delay.tv_sec = delay / 1000000;
99 tv_delay.tv_usec = delay % 1000000;
100 /* select() return value should be tested, since several possible errors
101 * can occur. However, they should only happen in very particular occasions
102 * (i.e. when a signal is sent to the thread, or when memory is full), and
103 * can be ingnored. */
104 select( 0, NULL, NULL, NULL, &tv_delay );