0
|
1 // Written in the D programming language
|
|
2 // www.digitalmars.com/d/
|
|
3
|
|
4 /*
|
|
5 * The contents of this file are subject to the Mozilla Public License Version
|
|
6 * 1.1 (the "License"); you may not use this file except in compliance with
|
|
7 * the License. You may obtain a copy of the License at
|
|
8 * http://www.mozilla.org/MPL/
|
|
9 *
|
|
10 * Software distributed under the License is distributed on an "AS IS" basis,
|
|
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
12 * for the specific language governing rights and limitations under the
|
|
13 * License.
|
|
14 *
|
|
15 * The Original Code is the Dynamin library.
|
|
16 *
|
|
17 * The Initial Developer of the Original Code is Jordan Miner.
|
|
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009
|
|
19 * the Initial Developer. All Rights Reserved.
|
|
20 *
|
|
21 * Contributor(s):
|
|
22 * Jordan Miner <jminer7@gmail.com>
|
|
23 *
|
|
24 */
|
|
25
|
|
26 module dynamin.core.windows_environment;
|
|
27
|
|
28 import dynamin.c.windows;
|
|
29
|
|
30 template EnvironmentBackend() {
|
|
31 long pStart; // processor time in milliseconds
|
|
32 long tStart; // computer uptime in milliseconds
|
|
33 long startDiff; // pStart-tStart
|
|
34 static this() {
|
|
35 backend_increaseTimerRes();
|
|
36 QueryPerformanceFrequency(&freq);
|
|
37 freq /= 1000;
|
|
38 pStart = processorTime;
|
|
39 tStart = timeGetTime();
|
|
40 startDiff = pStart-tStart;
|
|
41 }
|
|
42 public void backend_increaseTimerRes() {
|
|
43 static int period = -1;
|
|
44 if(period >= 0)
|
|
45 timeEndPeriod(period);
|
|
46 TIMECAPS tc;
|
|
47 timeGetDevCaps(&tc, TIMECAPS.sizeof);
|
|
48 period = tc.wPeriodMin > 0 ? tc.wPeriodMin : 1;
|
|
49 timeBeginPeriod(period);
|
|
50 }
|
|
51 long backend_runningTime() {
|
|
52 // NOTE: might be a faster way to do this...ProcessorTime is slow
|
|
53 // Use ProcessorTime to fix when timeGetTime() rolls over
|
|
54 const strayMs = 18_000_000; // 5 hours
|
|
55 long pNow = processorTime;
|
|
56 long tNow = timeGetTime();
|
|
57 // pNow-startDiff would equal tNow except that:
|
|
58 // - tNow has possibly rolled over
|
|
59 // - pNow has strayed because it runs at a slightly different speed
|
|
60 while(pNow-startDiff > tNow+strayMs)
|
|
61 tNow += 0xFFFF_FFFF; // tNow has rolled over, so fix it
|
|
62 return tNow-tStart;
|
|
63 }
|
|
64 long backend_systemTime() {
|
|
65 long t;
|
|
66 // gets time as 100 ns since Jan 1, 1601
|
|
67 GetSystemTimeAsFileTime(cast(FILETIME*)&t);
|
|
68 // This code gets the difference between 1601 and 1970
|
|
69 //SYSTEMTIME st;
|
|
70 //st.wYear = 1970;
|
|
71 //st.wMonth = 1;
|
|
72 //st.wDay = 1;
|
|
73 //long diff;
|
|
74 //SystemTimeToFileTime(&st, cast(FILETIME*)&diff);
|
|
75 t -= 116444736000000000; // change to Jan 1, 1970
|
|
76 t /= 10000; // change to milliseconds
|
|
77 return t;
|
|
78 }
|
|
79 int backend_processorCount() {
|
|
80 SYSTEM_INFO si;
|
|
81 GetSystemInfo(&si);
|
|
82 return si.dwNumberOfProcessors;
|
|
83 }
|
|
84 ulong freq = 0;
|
|
85 long backend_processorTime() {
|
|
86 ulong count;
|
|
87 QueryPerformanceCounter(&count);
|
|
88 return count/freq;
|
|
89 }
|
|
90 }
|
|
91
|