VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/spinlock-r0drv-freebsd.c@ 24181

Last change on this file since 24181 was 22677, checked in by vboxsync, 15 years ago

Runtime/FreeBSD: Initialize spinlock variables and fix panic with debug builds

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1/* $Id: spinlock-r0drv-freebsd.c 22677 2009-09-01 15:10:42Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, FreeBSD.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include "the-freebsd-kernel.h"
35#include "internal/iprt.h"
36
37#include <iprt/spinlock.h>
38#include <iprt/err.h>
39#include <iprt/alloc.h>
40#include <iprt/assert.h>
41#include <iprt/asm.h>
42#include <iprt/thread.h>
43#include <iprt/mp.h>
44
45#include "internal/magics.h"
46
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51/**
52 * Wrapper for the struct mtx type.
53 */
54typedef struct RTSPINLOCKINTERNAL
55{
56 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
57 uint32_t volatile u32Magic;
58 /** The spinlock. */
59 uint32_t volatile fLocked;
60 /** Reserved to satisfy compile assertion below. */
61 uint32_t uReserved;
62#ifdef RT_MORE_STRICT
63 /** The idAssertCpu variable before acquring the lock for asserting after
64 * releasing the spinlock. */
65 RTCPUID volatile idAssertCpu;
66 /** The CPU that owns the lock. */
67 RTCPUID volatile idCpuOwner;
68#endif
69} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
70
71
72RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
73{
74 /*
75 * Allocate.
76 */
77 RT_ASSERT_PREEMPTIBLE();
78 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
79 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pThis));
80 if (!pThis)
81 return VERR_NO_MEMORY;
82
83 /*
84 * Initialize & return.
85 */
86 pThis->u32Magic = RTSPINLOCK_MAGIC;
87 pThis->fLocked = 0;
88 *pSpinlock = pThis;
89 return VINF_SUCCESS;
90}
91
92
93RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
94{
95 /*
96 * Validate input.
97 */
98 RT_ASSERT_INTS_ON();
99 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
100 if (!pThis)
101 return VERR_INVALID_PARAMETER;
102 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
103 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
104 VERR_INVALID_PARAMETER);
105
106 /*
107 * Make the lock invalid and release the memory.
108 */
109 ASMAtomicIncU32(&pThis->u32Magic);
110 RTMemFree(pThis);
111 return VINF_SUCCESS;
112}
113
114
115RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
116{
117 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
118 AssertPtr(pThis);
119 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
120 RT_ASSERT_PREEMPT_CPUID_VAR();
121 Assert(pTmp->uFlags == 0);
122
123 for (;;)
124 {
125 pTmp->uFlags = ASMIntDisableFlags();
126 critical_enter();
127
128 int c = 50;
129 for (;;)
130 {
131 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
132 {
133 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
134 return;
135 }
136 if (--c <= 0)
137 break;
138 cpu_spinwait();
139 }
140
141 /* Enable interrupts while we sleep. */
142 ASMSetFlags(pTmp->uFlags);
143 critical_exit();
144 DELAY(1);
145 }
146}
147
148
149RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
150{
151 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
152 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
153
154 AssertPtr(pThis);
155 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
156 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
157
158 if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
159 AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
160
161 ASMSetFlags(pTmp->uFlags);
162 critical_exit();
163 pTmp->uFlags = 0;
164}
165
166
167RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
168{
169 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
170 RT_ASSERT_PREEMPT_CPUID_VAR();
171 AssertPtr(pThis);
172 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
173#ifdef RT_STRICT
174 Assert(pTmp->uFlags == 0);
175 pTmp->uFlags = 42;
176#endif
177
178 NOREF(pTmp);
179
180 for (;;)
181 {
182 critical_enter();
183
184 int c = 50;
185 for (;;)
186 {
187 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
188 {
189 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
190 return;
191 }
192 if (--c <= 0)
193 break;
194 cpu_spinwait();
195 }
196
197 critical_exit();
198 DELAY(1);
199 }
200}
201
202
203RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
204{
205 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
206 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
207
208 AssertPtr(pThis);
209 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
210 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
211#ifdef RT_STRICT
212 Assert(pTmp->uFlags == 42);
213 pTmp->uFlags = 0;
214#endif
215 NOREF(pTmp);
216
217 if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
218 AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
219
220 critical_exit();
221}
222
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