summaryrefslogtreecommitdiffstats
path: root/osdep/timer-linux.c
blob: f32f92f3923fa565b116a6edc3f3ac72d8ecdbfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Precise timer routines for LINUX  (C) LGB & A'rpi/ASTRAL

#include <unistd.h>
#ifdef __BEOS__
#define usleep(t) snooze(t)
#endif
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "config.h"

const char timer_name[] =
#ifdef HAVE_NANOSLEEP
  "nanosleep()";
#else
  "usleep()";
#endif

int usec_sleep(int usec_delay)
{
#ifdef HAVE_NANOSLEEP
    struct timespec ts;
    ts.tv_sec  =  usec_delay / 1000000;
    ts.tv_nsec = (usec_delay % 1000000) * 1000;
    return nanosleep(&ts, NULL);
#else
    return usleep(usec_delay);
#endif
}

// Returns current time in microseconds
unsigned int GetTimer(void){
  struct timeval tv;
  gettimeofday(&tv,NULL);
  return (tv.tv_sec*1000000+tv.tv_usec);
}  

// Returns current time in milliseconds
unsigned int GetTimerMS(void){
  struct timeval tv;
  gettimeofday(&tv,NULL);
  return (tv.tv_sec*1000+tv.tv_usec/1000);
}  

// Initialize timer, must be called at least once at start
void InitTimer(void){
}