Mercurial > projects > dynamin
annotate dynamin/core/windows_environment.d @ 106:acdbb30fee7e
Port to D2.
Most of the effort was dealing with immutable and const.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Mon, 17 Dec 2012 23:41:50 -0600 |
parents | 73060bc3f004 |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
5 * Distributed under the Boost Software License, Version 1.0. |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
6 * (See accompanying file BOOST_LICENSE.txt or copy at |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
7 * http://www.boost.org/LICENSE_1_0.txt) |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.core.windows_environment; | |
12 | |
13 import dynamin.c.windows; | |
14 | |
15 template EnvironmentBackend() { | |
16 long pStart; // processor time in milliseconds | |
17 long tStart; // computer uptime in milliseconds | |
18 long startDiff; // pStart-tStart | |
19 static this() { | |
20 backend_increaseTimerRes(); | |
21 QueryPerformanceFrequency(&freq); | |
22 freq /= 1000; | |
23 pStart = processorTime; | |
24 tStart = timeGetTime(); | |
25 startDiff = pStart-tStart; | |
26 } | |
27 public void backend_increaseTimerRes() { | |
28 static int period = -1; | |
29 if(period >= 0) | |
30 timeEndPeriod(period); | |
31 TIMECAPS tc; | |
32 timeGetDevCaps(&tc, TIMECAPS.sizeof); | |
33 period = tc.wPeriodMin > 0 ? tc.wPeriodMin : 1; | |
34 timeBeginPeriod(period); | |
35 } | |
36 long backend_runningTime() { | |
37 // NOTE: might be a faster way to do this...ProcessorTime is slow | |
38 // Use ProcessorTime to fix when timeGetTime() rolls over | |
106 | 39 enum strayMs = 18_000_000; // 5 hours |
0 | 40 long pNow = processorTime; |
41 long tNow = timeGetTime(); | |
42 // pNow-startDiff would equal tNow except that: | |
43 // - tNow has possibly rolled over | |
44 // - pNow has strayed because it runs at a slightly different speed | |
45 while(pNow-startDiff > tNow+strayMs) | |
46 tNow += 0xFFFF_FFFF; // tNow has rolled over, so fix it | |
47 return tNow-tStart; | |
48 } | |
49 long backend_systemTime() { | |
50 long t; | |
51 // gets time as 100 ns since Jan 1, 1601 | |
52 GetSystemTimeAsFileTime(cast(FILETIME*)&t); | |
53 // This code gets the difference between 1601 and 1970 | |
54 //SYSTEMTIME st; | |
55 //st.wYear = 1970; | |
56 //st.wMonth = 1; | |
57 //st.wDay = 1; | |
58 //long diff; | |
59 //SystemTimeToFileTime(&st, cast(FILETIME*)&diff); | |
60 t -= 116444736000000000; // change to Jan 1, 1970 | |
61 t /= 10000; // change to milliseconds | |
62 return t; | |
63 } | |
64 int backend_processorCount() { | |
65 SYSTEM_INFO si; | |
66 GetSystemInfo(&si); | |
67 return si.dwNumberOfProcessors; | |
68 } | |
69 ulong freq = 0; | |
70 long backend_processorTime() { | |
71 ulong count; | |
72 QueryPerformanceCounter(&count); | |
73 return count/freq; | |
74 } | |
75 } | |
76 |