VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/spinlock-r0drv-solaris.c@ 40806

最後變更 在這個檔案從40806是 40806,由 vboxsync 提交於 13 年 前

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.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.5 KB
 
1/* $Id: spinlock-r0drv-solaris.c 40806 2012-04-06 21:05:19Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, Solaris.
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.alldomusa.eu.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-solaris-kernel.h"
32#include "internal/iprt.h"
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#include <iprt/mp.h>
43#include <iprt/thread.h>
44#include "internal/magics.h"
45
46
47/*******************************************************************************
48* Structures and Typedefs *
49*******************************************************************************/
50/**
51 * Wrapper for the struct mutex type.
52 */
53typedef struct RTSPINLOCKINTERNAL
54{
55 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
56 uint32_t volatile u32Magic;
57 /** Spinlock creation flags. */
58 uint32_t fFlags;
59 /** Saved interrupt flag. */
60 uint32_t volatile fIntSaved;
61 /** A Solaris spinlock. */
62 kmutex_t Mtx;
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
73This code has changed and need testing!;
74
75
76RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
77{
78 RT_ASSERT_PREEMPTIBLE();
79 AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
80
81 /*
82 * Allocate.
83 */
84 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
85 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
86 if (!pThis)
87 return VERR_NO_MEMORY;
88
89 /*
90 * Initialize & return.
91 */
92 pThis->u32Magic = RTSPINLOCK_MAGIC;
93 pThis->fFlags = fFlags;
94 pThis->fIntSaved = 0;
95 /** @todo Consider different PIL when not interrupt safe requirement. */
96 mutex_init(&pThis->Mtx, "IPRT Spinlock", MUTEX_SPIN, (void *)ipltospl(PIL_MAX));
97 *pSpinlock = pThis;
98 return VINF_SUCCESS;
99}
100
101
102RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
103{
104 /*
105 * Validate input.
106 */
107 RT_ASSERT_INTS_ON();
108 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
109 if (!pThis)
110 return VERR_INVALID_PARAMETER;
111 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
112 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
113 VERR_INVALID_PARAMETER);
114
115 /*
116 * Make the lock invalid and release the memory.
117 */
118 ASMAtomicIncU32(&pThis->u32Magic);
119 mutex_destroy(&pThis->Mtx);
120 RTMemFree(pThis);
121 return VINF_SUCCESS;
122}
123
124
125RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
126{
127 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
128 RT_ASSERT_PREEMPT_CPUID_VAR();
129 AssertPtr(pThis);
130 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
131
132 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
133 {
134#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
135 uint32_t fIntSaved = ASMIntDisableFlags();
136#endif
137 mutex_enter(&pThis->Mtx);
138
139 /*
140 * Solaris 10 doesn't preserve the interrupt flag, but since we're at PIL_MAX we should be
141 * fine and not get interrupts while lock is held. Re-disable interrupts to not upset
142 * assertions & assumptions callers might have.
143 */
144#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
145 ASMIntDisable();
146#endif
147
148#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
149 Assert(!ASMIntAreEnabled());
150#endif
151 pThis->fIntSaved = fIntSaved;
152 }
153 else
154 {
155#if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
156 bool fIntsOn = ASMIntAreEnabled();
157#endif
158
159 mutex_enter(&pThis->Mtx);
160
161#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
162 AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
163#endif
164 }
165
166 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
167}
168
169
170RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
171{
172 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
173 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
174
175 AssertPtr(pThis);
176 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
177 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
178
179 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
180 {
181#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
182 uint32_t fIntSaved = pThis->fIntSaved;
183 pThis->fIntSaved = 0;
184#endif
185 mutex_exit(&pThis->Mtx);
186
187#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
188 ASMSetFlags(fIntSaved);
189#endif
190 }
191 else
192 {
193#if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
194 bool fIntsOn = ASMIntAreEnabled();
195#endif
196
197 mutex_exit(&pThis->Mtx);
198
199#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
200 AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
201#endif
202 }
203
204 RT_ASSERT_PREEMPT_CPUID();
205}
206
207
208RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock)
209{
210#if 1
211 if (RT_UNLIKELY(!(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)))
212 RTAssertMsg2("RTSpinlockReleaseNoInts: %p (magic=%#x)\n", Spinlock, Spinlock->u32Magic);
213#else
214 AssertRelease(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE);
215#endif
216 RTSpinlockRelease(Spinlock);
217}
218
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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