Skip to content

Commit ae69c22

Browse files
committed
Added MSVC utime_now implementation
Remove gettimeofday msvc fix f
1 parent 122ab9b commit ae69c22

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

common/time_util.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ either expressed or implied, of the Regents of The University of Michigan.
2929
#include <math.h>
3030
#include "time_util.h"
3131

32+
#ifdef _MSC_VER
33+
34+
static INIT_ONCE profiler_initd = INIT_ONCE_STATIC_INIT; // static-initialization struct
35+
static volatile LONGLONG profiler_perf_frequency;
36+
37+
static BOOL __stdcall profiler_init(PINIT_ONCE init_once, PVOID parameter, LPVOID *context)
38+
{
39+
LARGE_INTEGER freq;
40+
QueryPerformanceFrequency(&freq);
41+
profiler_perf_frequency = freq.QuadPart;
42+
return true;
43+
}
44+
45+
#endif
46+
3247
struct timeutil_rest
3348
{
3449
int64_t acc_time;
@@ -48,9 +63,24 @@ void timeutil_rest_destroy(timeutil_rest_t *rest)
4863

4964
int64_t utime_now() // blacklist-ignore
5065
{
66+
#ifdef _MSC_VER
67+
68+
// initialize profiler (will only run once and block for all threads)
69+
InitOnceExecuteOnce(&profiler_initd, profiler_init, NULL, NULL);
70+
71+
LARGE_INTEGER counter;
72+
LONGLONG elapsed;
73+
74+
QueryPerformanceCounter(&counter);
75+
76+
// convert to microseconds
77+
return (int64_t) (counter.QuadPart * 1000000LL) / profiler_perf_frequency;
78+
79+
#else
5180
struct timeval tv;
5281
gettimeofday (&tv, NULL); // blacklist-ignore
5382
return (int64_t) tv.tv_sec * 1000000 + tv.tv_usec;
83+
#endif
5484
}
5585

5686
int64_t utime_get_seconds(int64_t v)

common/time_util.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,7 @@ either expressed or implied, of the Regents of The University of Michigan.
4040
typedef long long suseconds_t;
4141
#endif
4242

43-
#ifdef _MSC_VER
44-
45-
inline int gettimeofday(struct timeval* tp, void* tzp)
46-
{
47-
(void)tzp;
48-
49-
unsigned long t;
50-
t = time(NULL);
51-
tp->tv_sec = t / 1000;
52-
tp->tv_usec = t % 1000;
53-
return 0;
54-
}
55-
#else
43+
#ifndef _MSC_VER
5644
#include <sys/time.h>
5745
#include <unistd.h>
5846
#endif

0 commit comments

Comments
 (0)