VirtualBox

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

Last change on this file since 37976 was 29281, checked in by vboxsync, 15 years ago

iprt/r0drv/solaris: asm*.h fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1/* $Id: spinlock-r0drv-solaris.c 29281 2010-05-09 23:40:43Z 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.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-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 /** A Solaris spinlock. */
58 kmutex_t Mtx;
59#ifdef RT_MORE_STRICT
60 /** The idAssertCpu variable before acquring the lock for asserting after
61 * releasing the spinlock. */
62 RTCPUID volatile idAssertCpu;
63 /** The CPU that owns the lock. */
64 RTCPUID volatile idCpuOwner;
65#endif
66} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
67
68
69
70RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock)
71{
72 /*
73 * Allocate.
74 */
75 RT_ASSERT_PREEMPTIBLE();
76 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
77 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
78 if (!pThis)
79 return VERR_NO_MEMORY;
80
81 /*
82 * Initialize & return.
83 */
84 pThis->u32Magic = RTSPINLOCK_MAGIC;
85 mutex_init(&pThis->Mtx, "IPRT Spinlock", MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
86 *pSpinlock = pThis;
87 return VINF_SUCCESS;
88}
89
90
91RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
92{
93 /*
94 * Validate input.
95 */
96 RT_ASSERT_INTS_ON();
97 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
98 if (!pThis)
99 return VERR_INVALID_PARAMETER;
100 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
101 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
102 VERR_INVALID_PARAMETER);
103
104 /*
105 * Make the lock invalid and release the memory.
106 */
107 ASMAtomicIncU32(&pThis->u32Magic);
108 mutex_destroy(&pThis->Mtx);
109 RTMemFree(pThis);
110 return VINF_SUCCESS;
111}
112
113
114RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
115{
116 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
117 RT_ASSERT_PREEMPT_CPUID_VAR();
118
119 AssertPtr(pThis);
120 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
121
122#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
123 pTmp->uFlags = ASMIntDisableFlags();
124#else
125 pTmp->uFlags = 0;
126#endif
127 mutex_enter(&pThis->Mtx);
128
129#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
130 Assert(!ASMIntAreEnabled());
131#endif
132 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
133}
134
135
136RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
137{
138 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
139 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
140
141 AssertPtr(pThis);
142 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
143 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
144 NOREF(pTmp);
145
146 mutex_exit(&pThis->Mtx);
147#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
148 ASMSetFlags(pTmp->uFlags);
149#endif
150
151 RT_ASSERT_PREEMPT_CPUID();
152}
153
154
155RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
156{
157 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
158 RT_ASSERT_PREEMPT_CPUID_VAR();
159 AssertPtr(pThis);
160 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
161 NOREF(pTmp);
162#if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
163 bool fIntsOn = ASMIntAreEnabled();
164#endif
165
166 mutex_enter(&pThis->Mtx);
167
168#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
169 AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
170#endif
171
172 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
173}
174
175
176RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
177{
178 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
179 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
180
181 AssertPtr(pThis);
182 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
183 RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
184 NOREF(pTmp);
185#if defined(RT_STRICT) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
186 bool fIntsOn = ASMIntAreEnabled();
187#endif
188
189 mutex_exit(&pThis->Mtx);
190
191#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
192 AssertMsg(fIntsOn == ASMIntAreEnabled(), ("fIntsOn=%RTbool\n", fIntsOn));
193#endif
194 RT_ASSERT_PREEMPT_CPUID();
195}
196
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