VirtualBox

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

最後變更 在這個檔案從37099是 29500,由 vboxsync 提交於 15 年 前

Runtime/R0: FreeBSD build fixes

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.2 KB
 
1/* $Id: spinlock-r0drv-freebsd.c 29500 2010-05-14 21:43:06Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, FreeBSD.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <[email protected]>
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/asm-amd64-x86.h>
43#include <iprt/thread.h>
44#include <iprt/mp.h>
45
46#include "internal/magics.h"
47
48
49/*******************************************************************************
50* Structures and Typedefs *
51*******************************************************************************/
52/**
53 * Wrapper for the struct mtx type.
54 */
55typedef struct RTSPINLOCKINTERNAL
56{
57 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
58 uint32_t volatile u32Magic;
59 /** The spinlock. */
60 uint32_t volatile fLocked;
61 /** Reserved to satisfy compile assertion below. */
62 uint32_t uReserved;
63#ifdef RT_MORE_STRICT
64 /** The idAssertCpu variable before acquring the lock for asserting after
65 * releasing the spinlock. */
66 RTCPUID volatile idAssertCpu;
67 /** The CPU that owns the lock. */
68 RTCPUID volatile idCpuOwner;
69#endif
70} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
71
72
73RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
74{
75 /*
76 * Allocate.
77 */
78 RT_ASSERT_PREEMPTIBLE();
79 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
80 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pThis));
81 if (!pThis)
82 return VERR_NO_MEMORY;
83
84 /*
85 * Initialize & return.
86 */
87 pThis->u32Magic = RTSPINLOCK_MAGIC;
88 pThis->fLocked = 0;
89 *pSpinlock = pThis;
90 return VINF_SUCCESS;
91}
92
93
94RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
95{
96 /*
97 * Validate input.
98 */
99 RT_ASSERT_INTS_ON();
100 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
101 if (!pThis)
102 return VERR_INVALID_PARAMETER;
103 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
104 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
105 VERR_INVALID_PARAMETER);
106
107 /*
108 * Make the lock invalid and release the memory.
109 */
110 ASMAtomicIncU32(&pThis->u32Magic);
111 RTMemFree(pThis);
112 return VINF_SUCCESS;
113}
114
115
116RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
117{
118 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
119 AssertPtr(pThis);
120 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
121 RT_ASSERT_PREEMPT_CPUID_VAR();
122 Assert(pTmp->uFlags == 0);
123
124 for (;;)
125 {
126 pTmp->uFlags = ASMIntDisableFlags();
127 critical_enter();
128
129 int c = 50;
130 for (;;)
131 {
132 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
133 {
134 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
135 return;
136 }
137 if (--c <= 0)
138 break;
139 cpu_spinwait();
140 }
141
142 /* Enable interrupts while we sleep. */
143 ASMSetFlags(pTmp->uFlags);
144 critical_exit();
145 DELAY(1);
146 }
147}
148
149
150RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
151{
152 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
153 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
154
155 AssertPtr(pThis);
156 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
157 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
158
159 if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
160 AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
161
162 ASMSetFlags(pTmp->uFlags);
163 critical_exit();
164 pTmp->uFlags = 0;
165}
166
167
168RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
169{
170 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
171 RT_ASSERT_PREEMPT_CPUID_VAR();
172 AssertPtr(pThis);
173 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
174#ifdef RT_STRICT
175 Assert(pTmp->uFlags == 0);
176 pTmp->uFlags = 42;
177#endif
178
179 NOREF(pTmp);
180
181 for (;;)
182 {
183 critical_enter();
184
185 int c = 50;
186 for (;;)
187 {
188 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
189 {
190 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
191 return;
192 }
193 if (--c <= 0)
194 break;
195 cpu_spinwait();
196 }
197
198 critical_exit();
199 DELAY(1);
200 }
201}
202
203
204RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
205{
206 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
207 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
208
209 AssertPtr(pThis);
210 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
211 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
212#ifdef RT_STRICT
213 Assert(pTmp->uFlags == 42);
214 pTmp->uFlags = 0;
215#endif
216 NOREF(pTmp);
217
218 if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
219 AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
220
221 critical_exit();
222}
223
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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