vlc_mtime.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * vlc_mtime.h: high resolution time management functions
00003  *****************************************************************************
00004  * This header provides portable high precision time management functions,
00005  * which should be the only ones used in other segments of the program, since
00006  * functions like gettimeofday() and ftime() are not always supported.
00007  * Most functions are declared as inline or as macros since they are only
00008  * interfaces to system calls and have to be called frequently.
00009  * 'm' stands for 'micro', since maximum resolution is the microsecond.
00010  * Functions prototyped are implemented in interface/mtime.c.
00011  *****************************************************************************
00012  * Copyright (C) 1996, 1997, 1998, 1999, 2000 the VideoLAN team
00013  * $Id: 8d51250dab38177845a3c4ff2fab79e30128506c $
00014  *
00015  * Authors: Vincent Seguin <seguin@via.ecp.fr>
00016  *
00017  * This program is free software; you can redistribute it and/or modify
00018  * it under the terms of the GNU General Public License as published by
00019  * the Free Software Foundation; either version 2 of the License, or
00020  * (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful,
00023  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025  * GNU General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00030  *****************************************************************************/
00031 
00032 #ifndef __VLC_MTIME_H
00033 # define __VLC_MTIME_H 1
00034 
00035 /*****************************************************************************
00036  * LAST_MDATE: date which will never happen
00037  *****************************************************************************
00038  * This date can be used as a 'never' date, to mark missing events in a function
00039  * supposed to return a date, such as nothing to display in a function
00040  * returning the date of the first image to be displayed. It can be used in
00041  * comparaison with other values: all existing dates will be earlier.
00042  *****************************************************************************/
00043 #define LAST_MDATE ((mtime_t)((uint64_t)(-1)/2))
00044 
00045 /*****************************************************************************
00046  * MSTRTIME_MAX_SIZE: maximum possible size of mstrtime
00047  *****************************************************************************
00048  * This values is the maximal possible size of the string returned by the
00049  * mstrtime() function, including '-' and the final '\0'. It should be used to
00050  * allocate the buffer.
00051  *****************************************************************************/
00052 #define MSTRTIME_MAX_SIZE 22
00053 
00054 /*****************************************************************************
00055  * Prototypes
00056  *****************************************************************************/
00057 VLC_EXPORT( char *,  mstrtime, ( char *psz_buffer, mtime_t date ) );
00058 VLC_EXPORT( mtime_t, mdate,    ( void ) );
00059 VLC_EXPORT( void,    mwait,    ( mtime_t date ) );
00060 VLC_EXPORT( void,    msleep,   ( mtime_t delay ) );
00061 VLC_EXPORT( char *,  secstotimestr, ( char *psz_buffer, int32_t secs ) );
00062 
00063 # define VLC_HARD_MIN_SLEEP 10000   /* 10 milliseconds = 1 tick at 100Hz */
00064 
00065 #if defined (__GNUC__) \
00066  && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
00067 /* Linux has 100, 250, 300 or 1000Hz
00068  *
00069  * HZ=100 by default on FreeBSD, but some architectures use a 1000Hz timer
00070  */
00071 # define VLC_SOFT_MIN_SLEEP 9000000 /* 9 seconds */
00072 
00073 static
00074 __attribute__((unused))
00075 __attribute__((noinline))
00076 __attribute__((error("sorry, cannot sleep for such short a time")))
00077 mtime_t impossible_delay( mtime_t delay )
00078 {
00079     (void) delay;
00080     return VLC_HARD_MIN_SLEEP;
00081 }
00082 
00083 static
00084 __attribute__((unused))
00085 __attribute__((noinline))
00086 __attribute__((warning("use proper event handling instead of short delay")))
00087 mtime_t harmful_delay( mtime_t delay )
00088 {
00089     return delay;
00090 }
00091 
00092 # define check_delay( d ) \
00093     ((__builtin_constant_p(d < VLC_HARD_MIN_SLEEP) \
00094    && (d < VLC_HARD_MIN_SLEEP)) \
00095        ? impossible_delay(d) \
00096        : ((__builtin_constant_p(d < VLC_SOFT_MIN_SLEEP) \
00097        && (d < VLC_SOFT_MIN_SLEEP)) \
00098            ? harmful_delay(d) \
00099            : d))
00100 
00101 static
00102 __attribute__((unused))
00103 __attribute__((noinline))
00104 __attribute__((error("deadlines can not be constant")))
00105 mtime_t impossible_deadline( mtime_t deadline )
00106 {
00107     return deadline;
00108 }
00109 
00110 # define check_deadline( d ) \
00111     (__builtin_constant_p(d) ? impossible_deadline(d) : d)
00112 #else
00113 # define check_delay(d) (d)
00114 # define check_deadline(d) (d)
00115 #endif
00116 
00117 #define msleep(d) msleep(check_delay(d))
00118 #define mwait(d) mwait(check_deadline(d))
00119 
00120 /*****************************************************************************
00121  * date_t: date incrementation without long-term rounding errors
00122  *****************************************************************************/
00123 struct date_t
00124 {
00125     mtime_t  date;
00126     uint32_t i_divider_num;
00127     uint32_t i_divider_den;
00128     uint32_t i_remainder;
00129 };
00130 
00131 VLC_EXPORT( void,    date_Init,      ( date_t *, uint32_t, uint32_t ) );
00132 VLC_EXPORT( void,    date_Change,    ( date_t *, uint32_t, uint32_t ) );
00133 VLC_EXPORT( void,    date_Set,       ( date_t *, mtime_t ) );
00134 VLC_EXPORT( mtime_t, date_Get,       ( const date_t * ) );
00135 VLC_EXPORT( void,    date_Move,      ( date_t *, mtime_t ) );
00136 VLC_EXPORT( mtime_t, date_Increment, ( date_t *, uint32_t ) );
00137 VLC_EXPORT( mtime_t, date_Decrement, ( date_t *, uint32_t ) );
00138 VLC_EXPORT( uint64_t, NTPtime64,     ( void ) );
00139 #endif /* !__VLC_MTIME_ */

Generated on Mon Nov 22 07:55:20 2010 for VLC by  doxygen 1.5.6