timing.c 1.09 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1
2
3
4
/* timing.c
** - Timing functions
*/

5
6
7
8
#ifdef HAVE_CONFIG_H
 #include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
9
10
#include <stdlib.h>
#include <sys/types.h>
11
12
13
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
Jack Moffitt's avatar
Jack Moffitt committed
14
15

#ifdef _WIN32
16
17
18
19
20
#include <windows.h>
#include <mmsystem.h>
#else
#include <sys/time.h>
#include <unistd.h>
Jack Moffitt's avatar
Jack Moffitt committed
21
22
#endif

brendan's avatar
whoops    
brendan committed
23
24
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
25
26
#endif

Jack Moffitt's avatar
Jack Moffitt committed
27
28
#include "timing.h"

29
30
/* see timing.h for an explanation of _mangle() */

Jack Moffitt's avatar
Jack Moffitt committed
31
32
33
/* 
 * Returns milliseconds no matter what. 
 */
34
uint64_t timing_get_time(void)
Jack Moffitt's avatar
Jack Moffitt committed
35
36
{
#ifdef _WIN32
37
    return timeGetTime();
Jack Moffitt's avatar
Jack Moffitt committed
38
#else 
39
    struct timeval mtv;
Jack Moffitt's avatar
Jack Moffitt committed
40

41
    gettimeofday(&mtv, NULL);
Jack Moffitt's avatar
Jack Moffitt committed
42

43
    return (uint64_t)(mtv.tv_sec) * 1000 + (uint64_t)(mtv.tv_usec) / 1000;
Jack Moffitt's avatar
Jack Moffitt committed
44
45
46
#endif
}

47
void timing_sleep(uint64_t sleeptime)
Jack Moffitt's avatar
Jack Moffitt committed
48
{
49
    struct timeval sleeper;
Jack Moffitt's avatar
Jack Moffitt committed
50

51
52
    sleeper.tv_sec = sleeptime / 1000;
    sleeper.tv_usec = (sleeptime % 1000) * 1000;
Jack Moffitt's avatar
Jack Moffitt committed
53

54
55
56
57
58
59
    /* NOTE:
     * This should be 0 for the first argument.  The linux manpage
     * says so.  The solaris manpage also says this is a legal
     * value.  If you think differerntly, please provide references.
     */
    select(0, NULL, NULL, NULL, &sleeper);
Jack Moffitt's avatar
Jack Moffitt committed
60
}