1 | /* $Id: timer-r0drv-nt.cpp 26344 2010-02-09 03:39:45Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Timers, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2008 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include "the-nt-kernel.h"
|
---|
35 |
|
---|
36 | #include <iprt/timer.h>
|
---|
37 | #include <iprt/mp.h>
|
---|
38 | #include <iprt/cpuset.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/asm.h>
|
---|
41 | #include <iprt/assert.h>
|
---|
42 | #include <iprt/alloc.h>
|
---|
43 |
|
---|
44 | #include "internal-r0drv-nt.h"
|
---|
45 | #include "internal/magics.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *******************************************************************************/
|
---|
51 | /**
|
---|
52 | * A sub timer structure.
|
---|
53 | *
|
---|
54 | * This is used for keeping the per-cpu tick and DPC object.
|
---|
55 | */
|
---|
56 | typedef struct RTTIMERNTSUBTIMER
|
---|
57 | {
|
---|
58 | /** The tick counter. */
|
---|
59 | uint64_t iTick;
|
---|
60 | /** Pointer to the parent timer. */
|
---|
61 | PRTTIMER pParent;
|
---|
62 | /** The NT DPC object. */
|
---|
63 | KDPC NtDpc;
|
---|
64 | } RTTIMERNTSUBTIMER;
|
---|
65 | /** Pointer to a NT sub-timer structure. */
|
---|
66 | typedef RTTIMERNTSUBTIMER *PRTTIMERNTSUBTIMER;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * The internal representation of an Linux timer handle.
|
---|
70 | */
|
---|
71 | typedef struct RTTIMER
|
---|
72 | {
|
---|
73 | /** Magic.
|
---|
74 | * This is RTTIMER_MAGIC, but changes to something else before the timer
|
---|
75 | * is destroyed to indicate clearly that thread should exit. */
|
---|
76 | uint32_t volatile u32Magic;
|
---|
77 | /** Flag indicating the timer is suspended. */
|
---|
78 | bool volatile fSuspended;
|
---|
79 | /** Whether the timer must run on one specific CPU or not. */
|
---|
80 | bool fSpecificCpu;
|
---|
81 | /** Whether the timer must run on all CPUs or not. */
|
---|
82 | bool fOmniTimer;
|
---|
83 | /** The CPU it must run on if fSpecificCpu is set.
|
---|
84 | * The master CPU for an omni-timer. */
|
---|
85 | RTCPUID idCpu;
|
---|
86 | /** Callback. */
|
---|
87 | PFNRTTIMER pfnTimer;
|
---|
88 | /** User argument. */
|
---|
89 | void *pvUser;
|
---|
90 | /** The timer interval. 0 if one-shot. */
|
---|
91 | uint64_t u64NanoInterval;
|
---|
92 | /** The Nt timer object. */
|
---|
93 | KTIMER NtTimer;
|
---|
94 | /** The number of sub-timers. */
|
---|
95 | RTCPUID cSubTimers;
|
---|
96 | /** Sub-timers.
|
---|
97 | * Normally there is just one, but for RTTIMER_FLAGS_CPU_ALL this will contain
|
---|
98 | * an entry for all possible cpus. In that case the index will be the same as
|
---|
99 | * for the RTCpuSet. */
|
---|
100 | RTTIMERNTSUBTIMER aSubTimers[1];
|
---|
101 | } RTTIMER;
|
---|
102 |
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Timer callback function for the non-omni timers.
|
---|
107 | *
|
---|
108 | * @returns HRTIMER_NORESTART or HRTIMER_RESTART depending on whether it's a one-shot or interval timer.
|
---|
109 | * @param pDpc Pointer to the the DPC.
|
---|
110 | * @param pvUser Pointer to our internal timer structure.
|
---|
111 | * @param SystemArgument1 Some system argument.
|
---|
112 | * @param SystemArgument2 Some system argument.
|
---|
113 | */
|
---|
114 | static void _stdcall rtTimerNtSimpleCallback(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
|
---|
115 | {
|
---|
116 | PRTTIMER pTimer = (PRTTIMER)pvUser;
|
---|
117 | AssertPtr(pTimer);
|
---|
118 | #ifdef RT_STRICT
|
---|
119 | if (KeGetCurrentIrql() < DISPATCH_LEVEL)
|
---|
120 | RTAssertMsg2Weak("rtTimerNtSimpleCallback: Irql=%d expected >=%d\n", KeGetCurrentIrql(), DISPATCH_LEVEL);
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Check that we haven't been suspended before doing the callout.
|
---|
125 | */
|
---|
126 | if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
|
---|
127 | && pTimer->u32Magic == RTTIMER_MAGIC)
|
---|
128 | pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->aSubTimers[0].iTick);
|
---|
129 |
|
---|
130 | NOREF(pDpc); NOREF(SystemArgument1); NOREF(SystemArgument2);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * The slave DPC callback for an omni timer.
|
---|
136 | *
|
---|
137 | * @param pDpc The DPC object.
|
---|
138 | * @param pvUser Pointer to the sub-timer.
|
---|
139 | * @param SystemArgument1 Some system stuff.
|
---|
140 | * @param SystemArgument2 Some system stuff.
|
---|
141 | */
|
---|
142 | static void _stdcall rtTimerNtOmniSlaveCallback(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
|
---|
143 | {
|
---|
144 | PRTTIMERNTSUBTIMER pSubTimer = (PRTTIMERNTSUBTIMER)pvUser;
|
---|
145 | PRTTIMER pTimer = pSubTimer->pParent;
|
---|
146 |
|
---|
147 | AssertPtr(pTimer);
|
---|
148 | #ifdef RT_STRICT
|
---|
149 | if (KeGetCurrentIrql() < DISPATCH_LEVEL)
|
---|
150 | RTAssertMsg2Weak("rtTimerNtOmniSlaveCallback: Irql=%d expected >=%d\n", KeGetCurrentIrql(), DISPATCH_LEVEL);
|
---|
151 | int iCpuSelf = RTMpCpuIdToSetIndex(RTMpCpuId());
|
---|
152 | if (pSubTimer - &pTimer->aSubTimers[0] != iCpuSelf)
|
---|
153 | RTAssertMsg2Weak("rtTimerNtOmniSlaveCallback: iCpuSelf=%d pSubTimer=%p / %d\n", iCpuSelf, pSubTimer, pSubTimer - &pTimer->aSubTimers[0]);
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * Check that we haven't been suspended before doing the callout.
|
---|
158 | */
|
---|
159 | if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
|
---|
160 | && pTimer->u32Magic == RTTIMER_MAGIC)
|
---|
161 | pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
|
---|
162 |
|
---|
163 | NOREF(pDpc); NOREF(SystemArgument1); NOREF(SystemArgument2);
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * The timer callback for an omni-timer.
|
---|
169 | *
|
---|
170 | * This is responsible for queueing the DPCs for the other CPUs and
|
---|
171 | * perform the callback on the CPU on which it is called.
|
---|
172 | *
|
---|
173 | * @param pDpc The DPC object.
|
---|
174 | * @param pvUser Pointer to the sub-timer.
|
---|
175 | * @param SystemArgument1 Some system stuff.
|
---|
176 | * @param SystemArgument2 Some system stuff.
|
---|
177 | */
|
---|
178 | static void _stdcall rtTimerNtOmniMasterCallback(IN PKDPC pDpc, IN PVOID pvUser, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
|
---|
179 | {
|
---|
180 | PRTTIMERNTSUBTIMER pSubTimer = (PRTTIMERNTSUBTIMER)pvUser;
|
---|
181 | PRTTIMER pTimer = pSubTimer->pParent;
|
---|
182 | int iCpuSelf = RTMpCpuIdToSetIndex(RTMpCpuId());
|
---|
183 |
|
---|
184 | AssertPtr(pTimer);
|
---|
185 | #ifdef RT_STRICT
|
---|
186 | if (KeGetCurrentIrql() < DISPATCH_LEVEL)
|
---|
187 | RTAssertMsg2Weak("rtTimerNtOmniMasterCallback: Irql=%d expected >=%d\n", KeGetCurrentIrql(), DISPATCH_LEVEL);
|
---|
188 | if (pSubTimer - &pTimer->aSubTimers[0] != iCpuSelf)
|
---|
189 | RTAssertMsg2Weak("rtTimerNtOmniMasterCallback: iCpuSelf=%d pSubTimer=%p / %d\n", iCpuSelf, pSubTimer, pSubTimer - &pTimer->aSubTimers[0]);
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Check that we haven't been suspended before scheduling the other DPCs
|
---|
194 | * and doing the callout.
|
---|
195 | */
|
---|
196 | if ( !ASMAtomicUoReadBool(&pTimer->fSuspended)
|
---|
197 | && pTimer->u32Magic == RTTIMER_MAGIC)
|
---|
198 | {
|
---|
199 | RTCPUSET OnlineSet;
|
---|
200 | RTMpGetOnlineSet(&OnlineSet);
|
---|
201 | for (int iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
|
---|
202 | if ( RTCpuSetIsMemberByIndex(&OnlineSet, iCpu)
|
---|
203 | && iCpuSelf != iCpu)
|
---|
204 | KeInsertQueueDpc(&pTimer->aSubTimers[iCpu].NtDpc, 0, 0);
|
---|
205 |
|
---|
206 | pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pSubTimer->iTick);
|
---|
207 | }
|
---|
208 |
|
---|
209 | NOREF(pDpc); NOREF(SystemArgument1); NOREF(SystemArgument2);
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 |
|
---|
214 | RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
|
---|
215 | {
|
---|
216 | /*
|
---|
217 | * Validate.
|
---|
218 | */
|
---|
219 | AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
|
---|
220 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
|
---|
221 |
|
---|
222 | if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
|
---|
223 | return VERR_TIMER_ACTIVE;
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Start the timer.
|
---|
227 | */
|
---|
228 | PKDPC pMasterDpc = pTimer->fOmniTimer
|
---|
229 | ? &pTimer->aSubTimers[RTMpCpuIdToSetIndex(pTimer->idCpu)].NtDpc
|
---|
230 | : &pTimer->aSubTimers[0].NtDpc;
|
---|
231 |
|
---|
232 | uint64_t u64Interval = pTimer->u64NanoInterval / 1000000; /* This is ms, believe it or not. */
|
---|
233 | ULONG ulInterval = (ULONG)u64Interval;
|
---|
234 | if (ulInterval != u64Interval)
|
---|
235 | ulInterval = MAXLONG;
|
---|
236 | else if (!ulInterval && pTimer->u64NanoInterval)
|
---|
237 | ulInterval = 1;
|
---|
238 |
|
---|
239 | LARGE_INTEGER DueTime;
|
---|
240 | DueTime.QuadPart = -(int64_t)(u64First / 100); /* Relative, NT time. */
|
---|
241 | if (DueTime.QuadPart)
|
---|
242 | DueTime.QuadPart = -1;
|
---|
243 |
|
---|
244 | ASMAtomicWriteBool(&pTimer->fSuspended, false);
|
---|
245 | KeSetTimerEx(&pTimer->NtTimer, DueTime, ulInterval, pMasterDpc);
|
---|
246 | return VINF_SUCCESS;
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Worker function that stops an active timer.
|
---|
252 | *
|
---|
253 | * Shared by RTTimerStop and RTTimerDestroy.
|
---|
254 | *
|
---|
255 | * @param pTimer The active timer.
|
---|
256 | */
|
---|
257 | static void rtTimerNtStopWorker(PRTTIMER pTimer)
|
---|
258 | {
|
---|
259 | /*
|
---|
260 | * Just cancel the timer, dequeue the DPCs and flush them (if this is supported).
|
---|
261 | */
|
---|
262 | ASMAtomicWriteBool(&pTimer->fSuspended, true);
|
---|
263 | KeCancelTimer(&pTimer->NtTimer);
|
---|
264 |
|
---|
265 | for (RTCPUID iCpu = 0; iCpu < pTimer->cSubTimers; iCpu++)
|
---|
266 | KeRemoveQueueDpc(&pTimer->aSubTimers[iCpu].NtDpc);
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * I'm a bit uncertain whether this should be done during RTTimerStop
|
---|
270 | * or only in RTTimerDestroy()... Linux and Solaris will wait AFAIK,
|
---|
271 | * which is why I'm keeping this here for now.
|
---|
272 | */
|
---|
273 | if (g_pfnrtNtKeFlushQueuedDpcs)
|
---|
274 | g_pfnrtNtKeFlushQueuedDpcs();
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | RTDECL(int) RTTimerStop(PRTTIMER pTimer)
|
---|
279 | {
|
---|
280 | /*
|
---|
281 | * Validate.
|
---|
282 | */
|
---|
283 | AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
|
---|
284 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
|
---|
285 |
|
---|
286 | if (ASMAtomicUoReadBool(&pTimer->fSuspended))
|
---|
287 | return VERR_TIMER_SUSPENDED;
|
---|
288 |
|
---|
289 | /*
|
---|
290 | * Call the worker we share with RTTimerDestroy.
|
---|
291 | */
|
---|
292 | rtTimerNtStopWorker(pTimer);
|
---|
293 | return VINF_SUCCESS;
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
|
---|
298 | {
|
---|
299 | /* It's ok to pass NULL pointer. */
|
---|
300 | if (pTimer == /*NIL_RTTIMER*/ NULL)
|
---|
301 | return VINF_SUCCESS;
|
---|
302 | AssertPtrReturn(pTimer, VERR_INVALID_HANDLE);
|
---|
303 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE);
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * Invalidate the timer, stop it if it's running and finally .
|
---|
307 | * free up the memory.
|
---|
308 | */
|
---|
309 | ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
|
---|
310 | if (!ASMAtomicUoReadBool(&pTimer->fSuspended))
|
---|
311 | rtTimerNtStopWorker(pTimer);
|
---|
312 | RTMemFree(pTimer);
|
---|
313 |
|
---|
314 | return VINF_SUCCESS;
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
|
---|
319 | {
|
---|
320 | *ppTimer = NULL;
|
---|
321 |
|
---|
322 | /*
|
---|
323 | * Validate flags.
|
---|
324 | */
|
---|
325 | if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
|
---|
326 | return VERR_INVALID_PARAMETER;
|
---|
327 | if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
|
---|
328 | && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
|
---|
329 | && !RTMpIsCpuOnline(fFlags & RTTIMER_FLAGS_CPU_MASK))
|
---|
330 | return (fFlags & RTTIMER_FLAGS_CPU_MASK) > RTMpGetMaxCpuId()
|
---|
331 | ? VERR_CPU_NOT_FOUND
|
---|
332 | : VERR_CPU_OFFLINE;
|
---|
333 |
|
---|
334 | /*
|
---|
335 | * Allocate the timer handler.
|
---|
336 | */
|
---|
337 | RTCPUID cSubTimers = 1;
|
---|
338 | if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
|
---|
339 | {
|
---|
340 | cSubTimers = RTMpGetMaxCpuId() + 1;
|
---|
341 | Assert(cSubTimers <= RTCPUSET_MAX_CPUS); /* On Windows we have a 1:1 relationship between cpuid and set index. */
|
---|
342 | }
|
---|
343 |
|
---|
344 | PRTTIMER pTimer = (PRTTIMER)RTMemAllocZ(RT_OFFSETOF(RTTIMER, aSubTimers[cSubTimers]));
|
---|
345 | if (!pTimer)
|
---|
346 | return VERR_NO_MEMORY;
|
---|
347 |
|
---|
348 | /*
|
---|
349 | * Initialize it.
|
---|
350 | */
|
---|
351 | pTimer->u32Magic = RTTIMER_MAGIC;
|
---|
352 | pTimer->fSuspended = true;
|
---|
353 | pTimer->fSpecificCpu = (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC) && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL;
|
---|
354 | pTimer->fOmniTimer = (fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL;
|
---|
355 | pTimer->idCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
|
---|
356 | pTimer->cSubTimers = cSubTimers;
|
---|
357 | pTimer->pfnTimer = pfnTimer;
|
---|
358 | pTimer->pvUser = pvUser;
|
---|
359 | pTimer->u64NanoInterval = u64NanoInterval;
|
---|
360 | KeInitializeTimerEx(&pTimer->NtTimer, SynchronizationTimer);
|
---|
361 | if (pTimer->fOmniTimer)
|
---|
362 | {
|
---|
363 | /*
|
---|
364 | * Initialize the per-cpu "sub-timers", select the first online cpu
|
---|
365 | * to be the master.
|
---|
366 | * ASSUMES that no cpus will ever go offline.
|
---|
367 | */
|
---|
368 | pTimer->idCpu = NIL_RTCPUID; /* */
|
---|
369 | for (unsigned iCpu = 0; iCpu < cSubTimers; iCpu++)
|
---|
370 | {
|
---|
371 | pTimer->aSubTimers[iCpu].iTick = 0;
|
---|
372 | pTimer->aSubTimers[iCpu].pParent = pTimer;
|
---|
373 |
|
---|
374 | if ( pTimer->idCpu == NIL_RTCPUID
|
---|
375 | && RTMpIsCpuOnline(RTMpCpuIdFromSetIndex(iCpu)))
|
---|
376 | {
|
---|
377 | pTimer->idCpu = RTMpCpuIdFromSetIndex(iCpu);
|
---|
378 | KeInitializeDpc(&pTimer->aSubTimers[iCpu].NtDpc, rtTimerNtOmniMasterCallback, &pTimer->aSubTimers[iCpu]);
|
---|
379 | }
|
---|
380 | else
|
---|
381 | KeInitializeDpc(&pTimer->aSubTimers[iCpu].NtDpc, rtTimerNtOmniSlaveCallback, &pTimer->aSubTimers[iCpu]);
|
---|
382 | KeSetImportanceDpc(&pTimer->aSubTimers[iCpu].NtDpc, HighImportance);
|
---|
383 | KeSetTargetProcessorDpc(&pTimer->aSubTimers[iCpu].NtDpc, (int)RTMpCpuIdFromSetIndex(iCpu));
|
---|
384 | }
|
---|
385 | Assert(pTimer->idCpu != NIL_RTCPUID);
|
---|
386 | }
|
---|
387 | else
|
---|
388 | {
|
---|
389 | /*
|
---|
390 | * Initialize the first "sub-timer", target the DPC on a specific processor
|
---|
391 | * if requested to do so.
|
---|
392 | */
|
---|
393 | pTimer->aSubTimers[0].iTick = 0;
|
---|
394 | pTimer->aSubTimers[0].pParent = pTimer;
|
---|
395 |
|
---|
396 | KeInitializeDpc(&pTimer->aSubTimers[0].NtDpc, rtTimerNtSimpleCallback, pTimer);
|
---|
397 | KeSetImportanceDpc(&pTimer->aSubTimers[0].NtDpc, HighImportance);
|
---|
398 | if (pTimer->fSpecificCpu)
|
---|
399 | KeSetTargetProcessorDpc(&pTimer->aSubTimers[0].NtDpc, (int)pTimer->idCpu);
|
---|
400 | }
|
---|
401 |
|
---|
402 | *ppTimer = pTimer;
|
---|
403 | return VINF_SUCCESS;
|
---|
404 | }
|
---|
405 |
|
---|
406 |
|
---|
407 | RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
|
---|
408 | {
|
---|
409 | /*
|
---|
410 | * Get the default/max timer increment value, return it if ExSetTimerResolution
|
---|
411 | * isn't available. Accoring to the sysinternals guys NtQueryTimerResolution
|
---|
412 | * is only available in userland and they find it equally annoying.
|
---|
413 | */
|
---|
414 | ULONG ulTimeInc = KeQueryTimeIncrement();
|
---|
415 | if (!g_pfnrtNtExSetTimerResolution)
|
---|
416 | return ulTimeInc * 100; /* The value is in 100ns, the funny NT unit. */
|
---|
417 |
|
---|
418 | /*
|
---|
419 | * Use the value returned by ExSetTimerResolution. Since the kernel is keeping
|
---|
420 | * count of these calls, we have to do two calls that cancel each other out.
|
---|
421 | */
|
---|
422 | ULONG ulResolution1 = g_pfnrtNtExSetTimerResolution(ulTimeInc, TRUE);
|
---|
423 | ULONG ulResolution2 = g_pfnrtNtExSetTimerResolution(0 /*ignored*/, FALSE);
|
---|
424 | AssertMsg(ulResolution1 == ulResolution2, ("%ld, %ld\n", ulResolution1, ulResolution2)); /* not supposed to change it! */
|
---|
425 | return ulResolution2 * 100; /* NT -> ns */
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
|
---|
430 | {
|
---|
431 | if (!g_pfnrtNtExSetTimerResolution)
|
---|
432 | return VERR_NOT_SUPPORTED;
|
---|
433 |
|
---|
434 | ULONG ulGranted = g_pfnrtNtExSetTimerResolution(u32Request / 100, TRUE);
|
---|
435 | if (pu32Granted)
|
---|
436 | *pu32Granted = ulGranted * 100; /* NT -> ns */
|
---|
437 | return VINF_SUCCESS;
|
---|
438 | }
|
---|
439 |
|
---|
440 |
|
---|
441 | RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
|
---|
442 | {
|
---|
443 | if (!g_pfnrtNtExSetTimerResolution)
|
---|
444 | return VERR_NOT_SUPPORTED;
|
---|
445 |
|
---|
446 | g_pfnrtNtExSetTimerResolution(0 /* ignored */, FALSE);
|
---|
447 | NOREF(u32Granted);
|
---|
448 | return VINF_SUCCESS;
|
---|
449 | }
|
---|
450 |
|
---|