1 | /* $Id: spinlock-r0drv-solaris.c 28800 2010-04-27 08:22:32Z 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 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/mp.h>
|
---|
40 | #include <iprt/thread.h>
|
---|
41 | #include "internal/magics.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Structures and Typedefs *
|
---|
46 | *******************************************************************************/
|
---|
47 | /**
|
---|
48 | * Wrapper for the struct mutex type.
|
---|
49 | */
|
---|
50 | typedef struct RTSPINLOCKINTERNAL
|
---|
51 | {
|
---|
52 | /** Spinlock magic value (RTSPINLOCK_MAGIC). */
|
---|
53 | uint32_t volatile u32Magic;
|
---|
54 | /** A Solaris spinlock. */
|
---|
55 | kmutex_t Mtx;
|
---|
56 | #ifdef RT_MORE_STRICT
|
---|
57 | /** The idAssertCpu variable before acquring the lock for asserting after
|
---|
58 | * releasing the spinlock. */
|
---|
59 | RTCPUID volatile idAssertCpu;
|
---|
60 | /** The CPU that owns the lock. */
|
---|
61 | RTCPUID volatile idCpuOwner;
|
---|
62 | #endif
|
---|
63 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
|
---|
68 | {
|
---|
69 | /*
|
---|
70 | * Allocate.
|
---|
71 | */
|
---|
72 | RT_ASSERT_PREEMPTIBLE();
|
---|
73 | AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
|
---|
74 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
75 | if (!pThis)
|
---|
76 | return VERR_NO_MEMORY;
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * Initialize & return.
|
---|
80 | */
|
---|
81 | pThis->u32Magic = RTSPINLOCK_MAGIC;
|
---|
82 | mutex_init(&pThis->Mtx, "IPRT Spinlock", MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
|
---|
83 | *pSpinlock = pThis;
|
---|
84 | return VINF_SUCCESS;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
89 | {
|
---|
90 | /*
|
---|
91 | * Validate input.
|
---|
92 | */
|
---|
93 | RT_ASSERT_INTS_ON();
|
---|
94 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
95 | if (!pThis)
|
---|
96 | return VERR_INVALID_PARAMETER;
|
---|
97 | AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
|
---|
98 | ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
|
---|
99 | VERR_INVALID_PARAMETER);
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * Make the lock invalid and release the memory.
|
---|
103 | */
|
---|
104 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
105 | mutex_destroy(&pThis->Mtx);
|
---|
106 | RTMemFree(pThis);
|
---|
107 | return VINF_SUCCESS;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
112 | {
|
---|
113 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
114 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
115 |
|
---|
116 | AssertPtr(pThis);
|
---|
117 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
118 |
|
---|
119 | pTmp->uFlags = ASMIntDisableFlags();
|
---|
120 | mutex_enter(&pThis->Mtx);
|
---|
121 |
|
---|
122 | Assert(!ASMIntAreEnabled());
|
---|
123 | RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
128 | {
|
---|
129 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
130 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
|
---|
131 |
|
---|
132 | AssertPtr(pThis);
|
---|
133 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
134 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
|
---|
135 | NOREF(pTmp);
|
---|
136 |
|
---|
137 | mutex_exit(&pThis->Mtx);
|
---|
138 | ASMSetFlags(pTmp->uFlags);
|
---|
139 |
|
---|
140 | RT_ASSERT_PREEMPT_CPUID();
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
145 | {
|
---|
146 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
147 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
148 | AssertPtr(pThis);
|
---|
149 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
150 | NOREF(pTmp);
|
---|
151 | #ifdef RT_STRICT
|
---|
152 | bool fIntsOn = ASMIntAreEnabled();
|
---|
153 | #endif
|
---|
154 |
|
---|
155 | mutex_enter(&pThis->Mtx);
|
---|
156 |
|
---|
157 | AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
|
---|
158 |
|
---|
159 | RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
|
---|
164 | {
|
---|
165 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
166 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
|
---|
167 |
|
---|
168 | AssertPtr(pThis);
|
---|
169 | Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
|
---|
170 | RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
|
---|
171 | NOREF(pTmp);
|
---|
172 | #ifdef RT_STRICT
|
---|
173 | bool fIntsOn = ASMIntAreEnabled();
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | mutex_exit(&pThis->Mtx);
|
---|
177 |
|
---|
178 | AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
|
---|
179 | RT_ASSERT_PREEMPT_CPUID();
|
---|
180 | }
|
---|
181 |
|
---|