- {
- /* Fallback on GetTickCount() which has a milisecond resolution
- * (actually, best case is about 10 ms resolution)
- * GetTickCount() only returns a DWORD thus will wrap after
- * about 49.7 days so we try to detect the wrapping. */
-
- static CRITICAL_SECTION date_lock;
- static mtime_t i_previous_time = I64C(-1);
- static int i_wrap_counts = -1;
-
- if( i_wrap_counts == -1 )
- {
- /* Initialization */
- i_previous_time = I64C(1000) * GetTickCount();
- InitializeCriticalSection( &date_lock );
- i_wrap_counts = 0;
- }
-
- EnterCriticalSection( &date_lock );
- res = I64C(1000) *
- (i_wrap_counts * I64C(0x100000000) + GetTickCount());
- if( i_previous_time > res )
- {
- /* Counter wrapped */
- i_wrap_counts++;
- usec_time += I64C(0x100000000) * 1000;
- }
- i_previous_time = usec_time;
- LeaveCriticalSection( &date_lock );
- }
-#else
- struct timeval tv_date;
-
- /* gettimeofday() cannot fail given &tv_date is a valid address */
- (void)gettimeofday( &tv_date, NULL );
- res = (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec;
-#endif
-
- return cached_time = res;
-}
-
-/**
- * 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
- * is posterior to wished date, the function returns immediately.
- * \param date The date to wake up at
- */
-void mwait( mtime_t date )
-{
- if( prec == 0 )
- prec = mprec();
-
- /* If the deadline is already elapsed, or within the clock precision,
- * do not even bother the clock. */
- if( ( date - cached_time ) < (mtime_t)prec ) // OK: mtime_t is signed
- return;
-
-#if 0 && defined (HAVE_CLOCK_NANOSLEEP)
- lldiv_t d = lldiv( date, 1000000 );
- struct timespec ts = { d.quot, d.rem * 1000 };
-
-# if (_POSIX_MONOTONIC_CLOCK - 0 >= 0)
- if( clock_nanosleep( CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL ) )
-# endif
- clock_nanosleep( CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL );
-#else
-
- mtime_t delay = date - mdate();
- if( delay > 0 )
- msleep( delay );
-
-#endif
-}
-
-/**
- * More precise sleep()
- *
- * Portable usleep() function.
- * \param delay the amount of time to sleep
- */
-void msleep( mtime_t delay )
-{
- mtime_t earlier = cached_time;
-
-#if defined( HAVE_CLOCK_NANOSLEEP )
- lldiv_t d = lldiv( delay, 1000000 );
- struct timespec ts = { d.quot, d.rem * 1000 };
-
-# if (_POSIX_MONOTONIC_CLOCK - 0 >= 0)
- if( clock_nanosleep( CLOCK_MONOTONIC, 0, &ts, NULL ) )
-# endif
- clock_nanosleep( CLOCK_REALTIME, 0, &ts, NULL );
-
-#elif defined( HAVE_KERNEL_OS_H )
- snooze( 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 ignored. */
- select( 0, NULL, NULL, NULL, &tv_delay );
-#endif
-
- earlier += delay;
- if( cached_time < earlier )
- cached_time = earlier;