VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/spinlock-r0drv-nt.cpp@ 40806

Last change on this file since 40806 was 40806, checked in by vboxsync, 13 years ago

RTSpinlock: Redid the interface, eliminating NoInts and Tmp. Whether a spinlock is interrupt safe or not is now defined at creation time, preventing stupid bugs arrising from calling the wrong acquire and/or release methods somewhere. The saved flags are stored in the spinlock strucutre, eliminating the annoying Tmp variable. Needs testing on each platform before fixing the build burn.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/* $Id: spinlock-r0drv-nt.cpp 40806 2012-04-06 21:05:19Z 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* Defined Constants And Macros *
48*******************************************************************************/
49/** Apply the NoIrq hack if defined. */
50#define RTSPINLOCK_NT_HACK_NOIRQ
51
52#ifdef RTSPINLOCK_NT_HACK_NOIRQ
53/** Indicates that the spinlock is taken. */
54# define RTSPINLOCK_NT_HACK_NOIRQ_TAKEN UINT32(0x00c0ffee)
55/** Indicates that the spinlock is taken. */
56# define RTSPINLOCK_NT_HACK_NOIRQ_FREE UINT32(0xfe0000fe)
57#endif
58
59
60/*******************************************************************************
61* Structures and Typedefs *
62*******************************************************************************/
63/**
64 * Wrapper for the KSPIN_LOCK type.
65 */
66typedef struct RTSPINLOCKINTERNAL
67{
68 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
69 uint32_t volatile u32Magic;
70#ifdef RTSPINLOCK_NT_HACK_NOIRQ
71 /** Spinlock hack. */
72 uint32_t volatile u32Hack;
73#endif
74 /** The saved IRQL. */
75 KIRQL volatile SavedIrql;
76 /** The saved interrupt flag. */
77 uint32_t volatile fIntSaved;
78 /** The spinlock creation flags. */
79 uint32_t fFlags;
80 /** The NT spinlock structure. */
81 KSPIN_LOCK Spinlock;
82} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
83
84
85This code has changed and need testing!;
86
87
88RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
89{
90 AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
91
92 /*
93 * Allocate.
94 */
95 Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
96 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
97 if (!pThis)
98 return VERR_NO_MEMORY;
99
100 /*
101 * Initialize & return.
102 */
103 pThis->u32Magic = RTSPINLOCK_MAGIC;
104#ifdef RTSPINLOCK_NT_HACK_NOIRQ
105 pThis->u32Hack = RTSPINLOCK_NT_HACK_NOIRQ_FREE;
106#endif
107 pThis->SavedIrql = 0;
108 pThis->fIntSaved = 0;
109 pThis->fFlags = fFlags;
110 KeInitializeSpinLock(&pThis->Spinlock);
111
112 *pSpinlock = pThis;
113 return VINF_SUCCESS;
114}
115
116
117RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
118{
119 /*
120 * Validate input.
121 */
122 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
123 if (!pThis)
124 return VERR_INVALID_PARAMETER;
125 if (pThis->u32Magic != RTSPINLOCK_MAGIC)
126 {
127 AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
128 return VERR_INVALID_PARAMETER;
129 }
130
131 ASMAtomicIncU32(&pThis->u32Magic);
132 RTMemFree(pThis);
133 return VINF_SUCCESS;
134}
135
136
137RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
138{
139 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
140 AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pThis->u32Magic));
141
142 KIRQL SavedIrql;
143 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
144 {
145#ifndef RTSPINLOCK_NT_HACK_NOIRQ
146 uint32_t fIntSaved = ASMGetFlags();
147 ASMIntDisable();
148 KeAcquireSpinLock(&pThis->Spinlock, &SavedIrql);
149#else
150 SavedIrql = KeGetCurrentIrql();
151 if (SavedIrql < DISPATCH_LEVEL)
152 {
153 KeRaiseIrql(DISPATCH_LEVEL, &SavedIrql);
154 Assert(SavedIrql < DISPATCH_LEVEL);
155 }
156 uint32_t fIntSaved = ASMGetFlags();
157 ASMIntDisable();
158
159 if (!ASMAtomicCmpXchgU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_TAKEN, RTSPINLOCK_NT_HACK_NOIRQ_FREE))
160 {
161 while (!ASMAtomicCmpXchgU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_TAKEN, RTSPINLOCK_NT_HACK_NOIRQ_FREE))
162 ASMNopPause();
163 }
164
165 pThis->fIntSaved = fIntSaved;
166#endif
167 }
168 else
169 KeAcquireSpinLock(&pThis->Spinlock, &SavedIrql);
170 pThis->SavedIrql = SavedIrql;
171}
172
173
174RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
175{
176 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
177 AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC, ("magic=%#x\n", pThis->u32Magic));
178
179 KIRQL SavedIrql = pThis->SavedIrql;
180 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
181 {
182 uint32_t fIntSaved = pThis->fIntSaved;
183 pThis->fIntSaved = 0;
184
185#ifndef RTSPINLOCK_NT_HACK_NOIRQ
186 KeReleaseSpinLock(&pThis->Spinlock, SavedIrql);
187 ASMSetFlags(fIntSaved);
188#else
189 Assert(pThis->u32Hack == RTSPINLOCK_NT_HACK_NOIRQ_TAKEN);
190
191 ASMAtomicWriteU32(&pThis->u32Hack, RTSPINLOCK_NT_HACK_NOIRQ_FREE);
192 ASMSetFlags(fIntSaved);
193 if (SavedIrql < DISPATCH_LEVEL)
194 KeLowerIrql(SavedIrql);
195#endif
196 }
197 else
198 KeReleaseSpinLock(&pThis->Spinlock, SavedIrql);
199}
200
201
202RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock)
203{
204#if 1
205 if (RT_UNLIKELY(!(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)))
206 RTAssertMsg2("RTSpinlockReleaseNoInts: %p (magic=%#x)\n", Spinlock, Spinlock->u32Magic);
207#else
208 AssertRelease(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE);
209#endif
210 RTSpinlockRelease(Spinlock);
211}
212
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette