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