Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/compat/gettimeofday.c
1201 views
1
#include < time.h >
2
#include <windows.h> //I've ommited this line.
3
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
4
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
5
#else
6
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
7
#endif
8
9
struct timezone
10
{
11
int tz_minuteswest; /* minutes W of Greenwich */
12
int tz_dsttime; /* type of dst correction */
13
};
14
15
int gettimeofday(struct timeval *tv, struct timezone *tz)
16
{
17
FILETIME ft;
18
unsigned __int64 tmpres = 0;
19
static int tzflag;
20
21
if (NULL != tv)
22
{
23
GetSystemTimeAsFileTime(&ft);
24
25
tmpres |= ft.dwHighDateTime;
26
tmpres <<= 32;
27
tmpres |= ft.dwLowDateTime;
28
29
/*converting file time to unix epoch*/
30
tmpres /= 10; /*convert into microseconds*/
31
tmpres -= DELTA_EPOCH_IN_MICROSECS;
32
tv->tv_sec = (long)(tmpres / 1000000UL);
33
tv->tv_usec = (long)(tmpres % 1000000UL);
34
}
35
36
if (NULL != tz)
37
{
38
if (!tzflag)
39
{
40
_tzset();
41
tzflag++;
42
}
43
tz->tz_minuteswest = _timezone / 60;
44
tz->tz_dsttime = _daylight;
45
}
46
47
return 0;
48
}
49
50
void usleep(__int64 waitTime)
51
{
52
if (waitTime > 0)
53
{
54
if (waitTime > 100)
55
{
56
// use a waitable timer for larger intervals > 0.1ms
57
58
HANDLE timer;
59
LARGE_INTEGER ft;
60
61
ft.QuadPart = -(10*waitTime); // Convert to 100 nanosecond interval, negative value indicates relative time
62
63
timer = CreateWaitableTimer(NULL, TRUE, NULL);
64
SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
65
WaitForSingleObject(timer, INFINITE);
66
CloseHandle(timer);
67
}
68
else
69
{
70
// use a polling loop for short intervals <= 100ms
71
72
LARGE_INTEGER perfCnt, start, now;
73
__int64 elapsed;
74
75
QueryPerformanceFrequency(&perfCnt);
76
QueryPerformanceCounter(&start);
77
do {
78
QueryPerformanceCounter((LARGE_INTEGER*) &now);
79
elapsed = (__int64)((now.QuadPart - start.QuadPart) / (float)perfCnt.QuadPart * 1000 * 1000);
80
} while ( elapsed < waitTime );
81
}
82
}
83
}
84
85