1 | /* $Id: spinlock-r0drv-nt.cpp 29254 2010-05-09 18:11:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlocks, Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
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 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include "the-nt-kernel.h"
|
---|
32 |
|
---|
33 | #include <iprt/spinlock.h>
|
---|
34 |
|
---|
35 | #include <iprt/asm.h>
|
---|
36 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
37 | # include <iprt/asm-amd64-x86.h>
|
---|
38 | #endif
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/mem.h>
|
---|
42 |
|
---|
43 | #include "internal/magics.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Structures and Typedefs *
|
---|
48 | *******************************************************************************/
|
---|
49 | /**
|
---|
50 | * Wrapper for the KSPIN_LOCK type.
|
---|
51 | */
|
---|
52 | typedef struct RTSPINLOCKINTERNAL
|
---|
53 | {
|
---|
54 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
55 | uint32_t volatile u32Magic;
|
---|
56 | /** The NT spinlock structure. */
|
---|
57 | KSPIN_LOCK Spinlock;
|
---|
58 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
|
---|
63 | {
|
---|
64 | /*
|
---|
65 | * Allocate.
|
---|
66 | */
|
---|
67 | Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
68 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
|
---|
69 | if (!pSpinlockInt)
|
---|
70 | return VERR_NO_MEMORY;
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Initialize & return.
|
---|
74 | */
|
---|
75 | pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
|
---|
76 | KeInitializeSpinLock(&pSpinlockInt->Spinlock);
|
---|
77 | Assert(sizeof(KIRQL) == sizeof(unsigned char));
|
---|
78 |
|
---|
79 | *pSpinlock = pSpinlockInt;
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
85 | {
|
---|
86 | /*
|
---|
87 | * Validate input.
|
---|
88 | */
|
---|
89 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
90 | if (!pSpinlockInt)
|
---|
91 | return VERR_INVALID_PARAMETER;
|
---|
92 | if (pSpinlockInt->u32Magic != RTSPINLOCK_MAGIC)
|
---|
93 | {
|
---|
94 | AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic));
|
---|
95 | return VERR_INVALID_PARAMETER;
|
---|
96 | }
|
---|
97 |
|
---|
98 | ASMAtomicIncU32(&pSpinlockInt->u32Magic);
|
---|
99 | RTMemFree(pSpinlockInt);
|
---|
100 | return VINF_SUCCESS;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
105 | {
|
---|
106 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
107 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pSpinlockInt->u32Magic));
|
---|
108 |
|
---|
109 | KeAcquireSpinLock(&pSpinlockInt->Spinlock, &pTmp->uchIrqL);
|
---|
110 | pTmp->uFlags = ASMGetFlags();
|
---|
111 | ASMIntDisable();
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
116 | {
|
---|
117 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
118 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pSpinlockInt->u32Magic));
|
---|
119 |
|
---|
120 | ASMSetFlags(pTmp->uFlags);
|
---|
121 | KeReleaseSpinLock(&pSpinlockInt->Spinlock, pTmp->uchIrqL);
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
126 | {
|
---|
127 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
128 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pSpinlockInt->u32Magic));
|
---|
129 |
|
---|
130 | KeAcquireSpinLock(&pSpinlockInt->Spinlock, &pTmp->uchIrqL);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
135 | {
|
---|
136 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
137 | AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pSpinlockInt->u32Magic));
|
---|
138 |
|
---|
139 | KeReleaseSpinLock(&pSpinlockInt->Spinlock, pTmp->uchIrqL);
|
---|
140 | }
|
---|