core: Improve handling of bad timestamps
[mplayer.git] / osdep / timer-linux.c
blob6e066711d55f6b3b3b74348f42e81ed195d5cb9d
1 // Precise timer routines for LINUX (C) LGB & A'rpi/ASTRAL
3 #include <unistd.h>
4 #ifdef __BEOS__
5 #define usleep(t) snooze(t)
6 #endif
7 #include <stdlib.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #include "config.h"
11 #include "timer.h"
13 const char timer_name[] =
14 #ifdef HAVE_NANOSLEEP
15 "nanosleep()";
16 #else
17 "usleep()";
18 #endif
20 int usec_sleep(int usec_delay)
22 #ifdef HAVE_NANOSLEEP
23 struct timespec ts;
24 ts.tv_sec = usec_delay / 1000000;
25 ts.tv_nsec = (usec_delay % 1000000) * 1000;
26 return nanosleep(&ts, NULL);
27 #else
28 return usleep(usec_delay);
29 #endif
32 // Returns current time in microseconds
33 unsigned int GetTimer(void){
34 struct timeval tv;
35 gettimeofday(&tv,NULL);
36 return tv.tv_sec * 1000000 + tv.tv_usec;
39 // Returns current time in milliseconds
40 unsigned int GetTimerMS(void){
41 struct timeval tv;
42 gettimeofday(&tv,NULL);
43 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
46 // Initialize timer, must be called at least once at start
47 void InitTimer(void){