1 | /* $Id: thread-r0drv-nt.cpp 13262 2008-10-14 13:09:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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/thread.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/asm.h>
|
---|
40 |
|
---|
41 | __BEGIN_DECLS
|
---|
42 | NTSTATUS NTAPI ZwYieldExecution(void);
|
---|
43 | __END_DECLS
|
---|
44 |
|
---|
45 |
|
---|
46 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
47 | {
|
---|
48 | return (RTNATIVETHREAD)PsGetCurrentThread();
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | RTDECL(int) RTThreadSleep(unsigned cMillies)
|
---|
53 | {
|
---|
54 | LARGE_INTEGER Interval;
|
---|
55 | Interval.QuadPart = -(int64_t)cMillies * 10000;
|
---|
56 | NTSTATUS rcNt = KeDelayExecutionThread(KernelMode, TRUE, &Interval);
|
---|
57 | switch (rcNt)
|
---|
58 | {
|
---|
59 | case STATUS_SUCCESS:
|
---|
60 | return VINF_SUCCESS;
|
---|
61 | case STATUS_ALERTED:
|
---|
62 | case STATUS_USER_APC:
|
---|
63 | return VERR_INTERRUPTED;
|
---|
64 | default:
|
---|
65 | return RTErrConvertFromNtStatus(rcNt);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | RTDECL(bool) RTThreadYield(void)
|
---|
71 | {
|
---|
72 | return ZwYieldExecution() != STATUS_NO_YIELD_PERFORMED;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
|
---|
77 | {
|
---|
78 | Assert(hThread == NIL_RTTHREAD);
|
---|
79 | KIRQL Irql = KeGetCurrentIrql();
|
---|
80 | if (Irql > APC_LEVEL)
|
---|
81 | return false;
|
---|
82 | #if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
|
---|
83 | if (!(ASMGetFlags() & 0x00000200 /* X86_EFL_IF */))
|
---|
84 | return false;
|
---|
85 | #endif
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
|
---|
91 | {
|
---|
92 | AssertPtr(pState);
|
---|
93 | Assert(pState->uchOldIrql == 255);
|
---|
94 | Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
|
---|
95 |
|
---|
96 | KeRaiseIrql(DISPATCH_LEVEL, &pState->uchOldIrql);
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
|
---|
101 | {
|
---|
102 | AssertPtr(pState);
|
---|
103 |
|
---|
104 | KeLowerIrql(pState->uchOldIrql);
|
---|
105 | pState->uchOldIrql = 255;
|
---|
106 | }
|
---|
107 |
|
---|