* mtime.c: high rezolution time management functions
* Functions are prototyped in mtime.h.
*****************************************************************************
- * Copyright (C) 1998, 1999, 2000 VideoLAN
- * $Id: mtime.c,v 1.21 2001/05/31 12:45:39 sam Exp $
+ * Copyright (C) 1998-2001 VideoLAN
+ * $Id: mtime.c,v 1.35 2002/11/11 14:39:12 sam Exp $
*
* Authors: Vincent Seguin <seguin@via.ecp.fr>
*
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include "defs.h"
-
#include <stdio.h> /* sprintf() */
+#include <vlc/vlc.h>
+
+#if defined( PTH_INIT_IN_PTH_H ) /* GNU Pth */
+# include <pth.h>
+#endif
+
#ifdef HAVE_UNISTD_H
# include <unistd.h> /* select() */
#endif
# include <kernel/OS.h>
#endif
-#if defined( WIN32 )
+#if defined( WIN32 ) || defined( UNDER_CE )
# include <windows.h>
#else
# include <sys/time.h>
#endif
-#include "config.h"
-#include "common.h"
-#include "mtime.h"
-
-#if defined( WIN32 )
-/*****************************************************************************
- * usleep: microsecond sleep for win32
- *****************************************************************************
- * This function uses performance counter if available, and Sleep() if not.
- *****************************************************************************/
-static __inline__ void usleep( unsigned int i_useconds )
+#if defined(HAVE_NANOSLEEP) && !defined(HAVE_STRUCT_TIMESPEC)
+struct timespec
{
- s64 i_cur, i_freq;
- s64 i_now, i_then;
-
- if( i_useconds < 1000
- && QueryPerformanceFrequency( (LARGE_INTEGER *) &i_freq ) )
- {
- QueryPerformanceCounter( (LARGE_INTEGER *) &i_cur );
-
- i_now = ( i_cur * 1000 * 1000 / i_freq );
- i_then = i_now + i_useconds;
+ time_t tv_sec;
+ int32_t tv_nsec;
+};
+#endif
- while( i_now < i_then )
- {
- QueryPerformanceCounter( (LARGE_INTEGER *) &i_cur );
- i_now = i_cur * 1000 * 1000 / i_freq;
- }
- }
- else
- {
- Sleep( (int) ((i_useconds + 500) / 1000) );
- }
-}
+#if defined(HAVE_NANOSLEEP) && !defined(HAVE_DECL_NANOSLEEP)
+int nanosleep(struct timespec *, struct timespec *);
#endif
/*****************************************************************************
*****************************************************************************/
char *mstrtime( char *psz_buffer, mtime_t date )
{
+ static mtime_t ll1000 = 1000, ll60 = 60, ll24 = 24;
+
sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
- (int) (date / (I64C(1000) * I64C(1000) * I64C(60) * I64C(60)) % I64C(24)),
- (int) (date / (I64C(1000) * I64C(1000) * I64C(60)) % I64C(60)),
- (int) (date / (I64C(1000) * I64C(1000)) % I64C(60)),
- (int) (date / I64C(1000) % I64C(1000)),
- (int) (date % I64C(1000)) );
+ (int) (date / (ll1000 * ll1000 * ll60 * ll60) % ll24),
+ (int) (date / (ll1000 * ll1000 * ll60) % ll60),
+ (int) (date / (ll1000 * ll1000) % ll60),
+ (int) (date / ll1000 % ll1000),
+ (int) (date % ll1000) );
return( psz_buffer );
}
/*****************************************************************************
- * mdate: return high precision date (inline function)
+ * mdate: return high precision date
*****************************************************************************
* Uses the gettimeofday() function when possible (1 MHz resolution) or the
* ftime() function (1 kHz resolution).
#if defined( HAVE_KERNEL_OS_H )
return( real_time_clock_usecs() );
-#elif defined( WIN32 )
+#elif defined( WIN32 ) || defined( UNDER_CE )
/* We don't get the real date, just the value of a high precision timer.
* this is because the usual time functions have at best only a milisecond
* resolution */
{
/* Microsecond resolution */
QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
- return ( usec_time * 1000000 ) / freq;
- }
- else
- {
- /* Milisecond resolution */
- return 1000 * GetTickCount();
+ return ( usec_time * 1000000 ) / freq;
}
+ /* Milisecond resolution */
+ return 1000 * GetTickCount();
+
#else
struct timeval tv_date;
}
/*****************************************************************************
- * mwait: wait for a date (inline function)
+ * mwait: wait for a date
*****************************************************************************
* This function uses select() and an system date function to wake up at a
* precise date. It should be used for process synchronization. If current date
}
snooze( delay );
-#elif defined( WIN32 )
+#elif defined( WIN32 ) || defined( UNDER_CE )
mtime_t usec_time, delay;
usec_time = mdate();
{
return;
}
-
- usleep( delay );
+ msleep( delay );
#else
-# ifdef HAVE_USLEEP
struct timeval tv_date;
-# else
- struct timeval tv_date, tv_delay;
-# endif
mtime_t delay; /* delay in msec, signed to detect errors */
/* see mdate() about gettimeofday() possible errors */
return;
}
-# ifdef HAVE_USLEEP
- usleep( delay );
+# if defined( PTH_INIT_IN_PTH_H )
+ pth_usleep( delay );
+
+# elif defined( ST_INIT_IN_ST_H )
+ st_usleep( delay );
+
# else
- tv_delay.tv_sec = delay / 1000000;
- tv_delay.tv_usec = delay % 1000000;
+# if defined( HAVE_NANOSLEEP )
+ {
+ struct timespec ts_delay;
+ ts_delay.tv_sec = delay / 1000000;
+ ts_delay.tv_nsec = (delay % 1000000) * 1000;
+
+ nanosleep( &ts_delay, NULL );
+ }
+
+# else
+ tv_date.tv_sec = delay / 1000000;
+ tv_date.tv_usec = delay % 1000000;
/* see msleep() about select() errors */
- select( 0, NULL, NULL, NULL, &tv_delay );
+ select( 0, NULL, NULL, NULL, &tv_date );
+# endif
+
# endif
#endif
}
/*****************************************************************************
- * msleep: more precise sleep() (inline function) (ok ?)
+ * msleep: more precise sleep()
*****************************************************************************
* Portable usleep() function.
*****************************************************************************/
#if defined( HAVE_KERNEL_OS_H )
snooze( delay );
-#elif defined( HAVE_USLEEP ) || defined( WIN32 )
- usleep( delay );
+#elif defined( PTH_INIT_IN_PTH_H )
+ pth_usleep( delay );
+
+#elif defined( ST_INIT_IN_ST_H )
+ st_usleep( delay );
+
+#elif defined( WIN32 ) || defined( UNDER_CE )
+ Sleep( (int) (delay / 1000) );
+
+#elif defined( HAVE_NANOSLEEP )
+ struct timespec ts_delay;
+
+ ts_delay.tv_sec = delay / 1000000;
+ ts_delay.tv_nsec = (delay % 1000000) * 1000;
+
+ nanosleep( &ts_delay, NULL );
#else
struct timeval tv_delay;
tv_delay.tv_sec = delay / 1000000;
tv_delay.tv_usec = delay % 1000000;
+
/* select() return value should be tested, since several possible errors
* can occur. However, they should only happen in very particular occasions
* (i.e. when a signal is sent to the thread, or when memory is full), and
- * can be ingnored. */
+ * can be ignored. */
select( 0, NULL, NULL, NULL, &tv_delay );
#endif