1 /* timing.c
2 ** - Timing functions
3 **
4 ** This program is distributed under the GNU General Public License, version 2.
5 ** A copy of this license is included with this source.
6 */
7 
8 #ifdef HAVE_CONFIG_H
9  #include <config.h>
10 #endif
11 
12 #include <stdlib.h>
13 #include <sys/types.h>
14 
15 #ifdef _WIN32
16 #include <windows.h>
17 #include <mmsystem.h>
18 #else
19 #ifdef TIME_WITH_SYS_TIME
20 #  include <sys/time.h>
21 #  include <time.h>
22 #else
23 #  ifdef HAVE_SYS_TIME_H
24 #    include <sys/time.h>
25 #  else
26 #    include <time.h>
27 #  endif
28 #endif
29 
30 #include <unistd.h>
31 #endif
32 
33 #ifdef HAVE_SYS_SELECT_H
34 #include <sys/select.h>
35 #endif
36 
37 #ifdef HAVE_SYS_TIMEB_H
38 #include <sys/timeb.h>
39 #endif
40 
41 #include "timing.h"
42 
43 /* see timing.h for an explanation of _mangle() */
44 
45 /*
46  * Returns milliseconds no matter what.
47  */
timing_get_time(void)48 uint64_t timing_get_time(void)
49 {
50 #ifdef HAVE_GETTIMEOFDAY
51     struct timeval mtv;
52 
53     gettimeofday(&mtv, NULL);
54 
55     return (uint64_t)(mtv.tv_sec) * 1000 + (uint64_t)(mtv.tv_usec) / 1000;
56 #elif HAVE_FTIME
57     struct timeb t;
58 
59     ftime(&t);
60     return t.time * 1000 + t.millitm;
61 #else
62 #error need time query handler
63 #endif
64 }
65 
66 
timing_sleep(uint64_t sleeptime)67 void timing_sleep(uint64_t sleeptime)
68 {
69     struct timeval sleeper;
70 
71     sleeper.tv_sec = sleeptime / 1000;
72     sleeper.tv_usec = (sleeptime % 1000) * 1000;
73 
74     /* NOTE:
75      * This should be 0 for the first argument.  The linux manpage
76      * says so.  The solaris manpage also says this is a legal
77      * value.  If you think differerntly, please provide references.
78      */
79 #ifdef WIN32
80 	Sleep(sleeptime);
81 #else
82     select(1, NULL, NULL, NULL, &sleeper);
83 #endif
84 }
85