1 /*****************************************************************************
2 * mtime.c: high rezolution time management functions
3 * Functions are prototyped in mtime.h.
4 *****************************************************************************
5 * Copyright (C) 1998, 1999, 2000 VideoLAN
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 *****************************************************************************/
27 * see if using Linux real-time extensions is possible and profitable
30 /*****************************************************************************
32 *****************************************************************************/
33 #include <stdio.h> /* sprintf() */
39 /*****************************************************************************
40 * mstrtime: return a date in a readable format
41 *****************************************************************************
42 * This functions is provided for any interface function which need to print a
43 * date. psz_buffer should be a buffer long enough to store the formatted
45 *****************************************************************************/
46 char *mstrtime( char *psz_buffer, mtime_t date )
48 sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
49 (int) (date / (1000LL * 1000LL * 60LL * 60LL) % 24LL),
50 (int) (date / (1000LL * 1000LL * 60LL) % 60LL),
51 (int) (date / (1000LL * 1000LL) % 60LL),
52 (int) (date / 1000LL % 1000LL),
53 (int) (date % 1000LL) );
57 /*****************************************************************************
58 * mdate: return high precision date (inline function)
59 *****************************************************************************
60 * Uses the gettimeofday() function when possible (1 MHz resolution) or the
61 * ftime() function (1 kHz resolution).
62 *****************************************************************************/
65 struct timeval tv_date;
67 /* gettimeofday() could return an error, and should be tested. However, the
68 * only possible error, according to 'man', is EFAULT, which can not happen
69 * here, since tv is a local variable. */
70 gettimeofday( &tv_date, NULL );
71 return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec );
74 /*****************************************************************************
75 * mwait: wait for a date (inline function)
76 *****************************************************************************
77 * This function uses select() and an system date function to wake up at a
78 * precise date. It should be used for process synchronization. If current date
79 * is posterior to wished date, the function returns immediately.
80 *****************************************************************************/
81 void mwait( mtime_t date )
83 struct timeval tv_date, tv_delay;
84 mtime_t delay; /* delay in msec, signed to detect errors */
86 /* see mdate() about gettimeofday() possible errors */
87 gettimeofday( &tv_date, NULL );
89 /* calculate delay and check if current date is before wished date */
90 delay = date - (mtime_t) tv_date.tv_sec * 1000000 - (mtime_t) tv_date.tv_usec;
91 if( delay <= 0 ) /* wished date is now or already passed */
96 tv_delay.tv_sec = delay / 1000000;
97 tv_delay.tv_usec = delay % 1000000;
99 /* see msleep() about select() errors */
100 select( 0, NULL, NULL, NULL, &tv_delay );
106 /*****************************************************************************
107 * msleep: more precise sleep() (inline function) (ok ?)
108 *****************************************************************************
109 * Portable usleep() function.
110 *****************************************************************************/
111 void msleep( mtime_t delay )
114 struct timeval tv_delay;
116 tv_delay.tv_sec = delay / 1000000;
117 tv_delay.tv_usec = delay % 1000000;
118 /* select() return value should be tested, since several possible errors
119 * can occur. However, they should only happen in very particular occasions
120 * (i.e. when a signal is sent to the thread, or when memory is full), and
121 * can be ingnored. */
122 select( 0, NULL, NULL, NULL, &tv_delay );