1 | /* $Id: spinlock-r0drv-nt.cpp 4071 2007-08-07 17:07:59Z 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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include "the-nt-kernel.h"
|
---|
23 |
|
---|
24 | #include <iprt/spinlock.h>
|
---|
25 | #include <iprt/err.h>
|
---|
26 | #include <iprt/alloc.h>
|
---|
27 | #include <iprt/assert.h>
|
---|
28 | #include <iprt/asm.h>
|
---|
29 |
|
---|
30 | #include "internal/magics.h"
|
---|
31 |
|
---|
32 |
|
---|
33 | /*******************************************************************************
|
---|
34 | * Structures and Typedefs *
|
---|
35 | *******************************************************************************/
|
---|
36 | /**
|
---|
37 | * Wrapper for the KSPIN_LOCK type.
|
---|
38 | */
|
---|
39 | typedef struct RTSPINLOCKINTERNAL
|
---|
40 | {
|
---|
41 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
42 | uint32_t volatile u32Magic;
|
---|
43 | /** The NT spinlock structure. */
|
---|
44 | KSPIN_LOCK Spinlock;
|
---|
45 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
|
---|
50 | {
|
---|
51 | /*
|
---|
52 | * Allocate.
|
---|
53 | */
|
---|
54 | Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
55 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
|
---|
56 | if (!pSpinlockInt)
|
---|
57 | return VERR_NO_MEMORY;
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * Initialize & return.
|
---|
61 | */
|
---|
62 | pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
|
---|
63 | KeInitializeSpinLock(&pSpinlockInt->Spinlock);
|
---|
64 | Assert(sizeof(KIRQL) == sizeof(unsigned char));
|
---|
65 |
|
---|
66 | *pSpinlock = pSpinlockInt;
|
---|
67 | return VINF_SUCCESS;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
72 | {
|
---|
73 | /*
|
---|
74 | * Validate input.
|
---|
75 | */
|
---|
76 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
77 | if (!pSpinlockInt)
|
---|
78 | return VERR_INVALID_PARAMETER;
|
---|
79 | if (pSpinlockInt->u32Magic != RTSPINLOCK_MAGIC)
|
---|
80 | {
|
---|
81 | AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic));
|
---|
82 | return VERR_INVALID_PARAMETER;
|
---|
83 | }
|
---|
84 |
|
---|
85 | ASMAtomicIncU32(&pSpinlockInt->u32Magic);
|
---|
86 | RTMemFree(pSpinlockInt);
|
---|
87 | return VINF_SUCCESS;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
92 | {
|
---|
93 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
94 | Assert(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
95 |
|
---|
96 | KeAcquireSpinLock(&pSpinlockInt->Spinlock, &pTmp->uchIrqL);
|
---|
97 | pTmp->uFlags = ASMGetFlags();
|
---|
98 | ASMIntDisable();
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
103 | {
|
---|
104 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
105 | Assert(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
106 |
|
---|
107 | ASMSetFlags(pTmp->uFlags);
|
---|
108 | KeReleaseSpinLock(&pSpinlockInt->Spinlock, pTmp->uchIrqL);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
113 | {
|
---|
114 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
115 | Assert(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
116 |
|
---|
117 | KeAcquireSpinLock(&pSpinlockInt->Spinlock, &pTmp->uchIrqL);
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
122 | {
|
---|
123 | PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
124 | Assert(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
|
---|
125 |
|
---|
126 | KeReleaseSpinLock(&pSpinlockInt->Spinlock, pTmp->uchIrqL);
|
---|
127 | }
|
---|
128 |
|
---|