Skip to content

Commit 8c3b47d

Browse files
Merge pull request #425 from serenial/msvc-timeprofiler-fix
Added MSVC utime_now implementation
2 parents 122ab9b + 466c0ce commit 8c3b47d

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

common/time_util.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ 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+
(void) init_once;
40+
(void) parameter;
41+
(void) context;
42+
43+
LARGE_INTEGER freq;
44+
QueryPerformanceFrequency(&freq);
45+
profiler_perf_frequency = freq.QuadPart;
46+
return true;
47+
}
48+
49+
#endif
50+
3251
struct timeutil_rest
3352
{
3453
int64_t acc_time;
@@ -48,9 +67,23 @@ void timeutil_rest_destroy(timeutil_rest_t *rest)
4867

4968
int64_t utime_now() // blacklist-ignore
5069
{
70+
#ifdef _MSC_VER
71+
72+
// initialize profiler (will only run once and block for all threads)
73+
InitOnceExecuteOnce(&profiler_initd, profiler_init, NULL, NULL);
74+
75+
LARGE_INTEGER counter;
76+
77+
QueryPerformanceCounter(&counter);
78+
79+
// convert to microseconds
80+
return (int64_t) (counter.QuadPart * 1000000LL) / profiler_perf_frequency;
81+
82+
#else
5183
struct timeval tv;
5284
gettimeofday (&tv, NULL); // blacklist-ignore
5385
return (int64_t) tv.tv_sec * 1000000 + tv.tv_usec;
86+
#endif
5487
}
5588

5689
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)