* mtime.c: high rezolution time management functions
* Functions are prototyped in mtime.h.
*****************************************************************************
- * Copyright (C) 1998, 1999, 2000 VideoLAN
- * $Id: mtime.c,v 1.17 2001/04/28 03:36:25 sam Exp $
+ * Copyright (C) 1998-2001 VideoLAN
+ * $Id: mtime.c,v 1.27 2001/12/30 07:09:56 sam Exp $
*
* Authors: Vincent Seguin <seguin@via.ecp.fr>
*
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include "defs.h"
-
#include <stdio.h> /* sprintf() */
-#include <unistd.h> /* select() */
-#include <sys/time.h>
-#ifdef HAVE_KERNEL_OS_H
-#include <kernel/OS.h>
+#include <videolan/vlc.h>
+
+#if defined( PTH_INIT_IN_PTH_H ) /* GNU Pth */
+# include <pth.h>
#endif
-#ifdef WIN32
-#include <windows.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h> /* select() */
+#endif
+
+#ifdef HAVE_KERNEL_OS_H
+# include <kernel/OS.h>
#endif
-#include "config.h"
-#include "common.h"
-#include "mtime.h"
+#if defined( WIN32 )
+# include <windows.h>
+#else
+# include <sys/time.h>
+#endif
/*****************************************************************************
* mstrtime: return a date in a readable format
char *mstrtime( char *psz_buffer, mtime_t date )
{
sprintf( psz_buffer, "%02d:%02d:%02d-%03d.%03d",
- (int) (date / (1000LL * 1000LL * 60LL * 60LL) % 24LL),
- (int) (date / (1000LL * 1000LL * 60LL) % 60LL),
- (int) (date / (1000LL * 1000LL) % 60LL),
- (int) (date / 1000LL % 1000LL),
- (int) (date % 1000LL) );
+ (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)) );
return( psz_buffer );
}
/* 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 */
- mtime_t freq,usec_time;
+ mtime_t freq, usec_time;
- if( !QueryPerformanceFrequency((LARGE_INTEGER *)&freq) )
- {
- /* Milisecond resolution */
- FILETIME file_time;
- GetSystemTimeAsFileTime((FILETIME *)&file_time);
- usec_time *= 1000;
- }
- else
+ if( QueryPerformanceFrequency( (LARGE_INTEGER *)&freq ) )
{
/* Microsecond resolution */
- QueryPerformanceCounter((LARGE_INTEGER *)&usec_time);
- usec_time /= (freq/1000000);
+ QueryPerformanceCounter( (LARGE_INTEGER *)&usec_time );
+ return ( usec_time * 1000000 ) / freq;
}
- return( usec_time );
+
+ /* Milisecond resolution */
+ return 1000 * GetTickCount();
#else
struct timeval tv_date;
{
return;
}
- /* Sleep only has milisecond resolution */
- Sleep( (DWORD)(delay/1000) );
+ msleep( delay );
#else
gettimeofday( &tv_date, NULL );
/* calculate delay and check if current date is before wished date */
- delay = date - (mtime_t) tv_date.tv_sec * 1000000 - (mtime_t) tv_date.tv_usec - 10000;
+ delay = date - (mtime_t) tv_date.tv_sec * 1000000
+ - (mtime_t) tv_date.tv_usec
+ - 10000;
+
/* Linux/i386 has a granularity of 10 ms. It's better to be in advance
* than to be late. */
if( delay <= 0 ) /* wished date is now or already passed */
return;
}
-# ifdef HAVE_USLEEP
+# if defined( PTH_INIT_IN_PTH_H )
+ pth_usleep( delay );
+
+# elif defined( HAVE_USLEEP )
usleep( delay );
+
# else
tv_delay.tv_sec = delay / 1000000;
tv_delay.tv_usec = delay % 1000000;
-
/* see msleep() about select() errors */
select( 0, NULL, NULL, NULL, &tv_delay );
+
# endif
#endif
#if defined( HAVE_KERNEL_OS_H )
snooze( delay );
-#elif defined( WIN32 )
- Sleep( delay/1000 ); /* Sleep only has milisecond resolution */
- /* Maybe we could use the multimedia timer to reach the right resolution, */
- /* or the old Winsock select() function ?*/
+#elif defined( PTH_INIT_IN_PTH_H )
+ struct timeval tv_delay;
+ tv_delay.tv_sec = delay / 1000000;
+ tv_delay.tv_usec = delay % 1000000;
+ pth_select( 0, NULL, NULL, NULL, &tv_delay );
#elif defined( HAVE_USLEEP )
usleep( delay );
+#elif defined( WIN32 )
+ Sleep( (int) (delay / 1000) );
+
#else
struct timeval tv_delay;