VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/spinlock-generic.cpp@ 44615

Last change on this file since 44615 was 40951, checked in by vboxsync, 13 years ago

Runtime/spinlock-generic: Fix building on SPARC

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/* $Id: spinlock-generic.cpp 40951 2012-04-16 19:25:26Z vboxsync $ */
2/** @file
3 * IPRT - Spinlock, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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* Defined Constants And Macros *
30*******************************************************************************/
31/** @def RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
32 * Force cpu yields after spinning the number of times indicated by the define.
33 * If 0 we will spin forever. */
34#define RT_CFG_SPINLOCK_GENERIC_DO_SLEEP 100000
35
36
37/*******************************************************************************
38* Header Files *
39*******************************************************************************/
40#include <iprt/spinlock.h>
41#include "internal/iprt.h"
42
43#include <iprt/alloc.h>
44#include <iprt/asm.h>
45#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
46# include <iprt/asm-amd64-x86.h>
47#endif
48#include <iprt/err.h>
49#include <iprt/assert.h>
50#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
51# include <iprt/thread.h>
52#endif
53
54#include "internal/magics.h"
55
56
57/*******************************************************************************
58* Structures and Typedefs *
59*******************************************************************************/
60/**
61 * Generic spinlock structure.
62 */
63typedef struct RTSPINLOCKINTERNAL
64{
65 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
66 uint32_t u32Magic;
67 /** The spinlock creation flags. */
68 uint32_t fFlags;
69 /** The spinlock. */
70 uint32_t volatile fLocked;
71 /** The saved CPU interrupt. */
72 uint32_t volatile fIntSaved;
73} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
74
75
76RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
77{
78 PRTSPINLOCKINTERNAL pThis;
79 AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
80
81 /*
82 * Allocate.
83 */
84 pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
85 if (!pThis)
86 return VERR_NO_MEMORY;
87
88 /*
89 * Initialize and return.
90 */
91 pThis->u32Magic = RTSPINLOCK_MAGIC;
92 pThis->fFlags = fFlags;
93 pThis->fIntSaved = 0;
94 ASMAtomicWriteU32(&pThis->fLocked, 0);
95
96 *pSpinlock = pThis;
97 return VINF_SUCCESS;
98}
99RT_EXPORT_SYMBOL(RTSpinlockCreate);
100
101
102RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
103{
104 /*
105 * Validate input.
106 */
107 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
108 if (!pThis)
109 return VERR_INVALID_PARAMETER;
110 if (pThis->u32Magic != RTSPINLOCK_MAGIC)
111 {
112 AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
113 return VERR_INVALID_PARAMETER;
114 }
115
116 ASMAtomicIncU32(&pThis->u32Magic);
117 RTMemFree(pThis);
118 return VINF_SUCCESS;
119}
120RT_EXPORT_SYMBOL(RTSpinlockDestroy);
121
122
123RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
124{
125 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
126 AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
127 ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
128
129 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
130 {
131#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
132 uint32_t fIntSaved = ASMGetFlags();
133#endif
134
135#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
136 for (;;)
137 {
138#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
139 ASMIntDisable();
140#endif
141 for (int c = RT_CFG_SPINLOCK_GENERIC_DO_SLEEP; c > 0; c--)
142 {
143 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
144 {
145# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
146 pThis->fIntSaved = fIntSaved;
147# endif
148 return;
149 }
150 ASMNopPause();
151 }
152#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
153 ASMSetFlags(fIntSaved);
154#endif
155 RTThreadYield();
156 }
157#else
158 for (;;)
159 {
160#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
161 ASMIntDisable();
162#endif
163 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
164 {
165# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
166 pThis->fIntSaved = fIntSaved;
167# endif
168 return;
169 }
170#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
171 ASMSetFlags(fIntSaved);
172#endif
173 ASMNopPause();
174 }
175#endif
176 }
177 else
178 {
179#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
180 for (;;)
181 {
182 for (int c = RT_CFG_SPINLOCK_GENERIC_DO_SLEEP; c > 0; c--)
183 {
184 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
185 return;
186 ASMNopPause();
187 }
188 RTThreadYield();
189 }
190#else
191 while (!ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
192 ASMNopPause();
193#endif
194 }
195}
196RT_EXPORT_SYMBOL(RTSpinlockAcquire);
197
198
199RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
200{
201 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
202 AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
203 ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
204
205 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
206 {
207#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
208 uint32_t fIntSaved = pThis->fIntSaved;
209 pThis->fIntSaved = 0;
210#endif
211
212 if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
213 AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
214
215#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
216 ASMSetFlags(fIntSaved);
217#endif
218 }
219 else
220 {
221 if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
222 AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
223 }
224}
225RT_EXPORT_SYMBOL(RTSpinlockRelease);
226
227
228RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock)
229{
230#if 1
231 if (RT_UNLIKELY(!(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)))
232 RTAssertMsg2("RTSpinlockReleaseNoInts: %p (magic=%#x)\n", Spinlock, Spinlock->u32Magic);
233#else
234 AssertRelease(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE);
235#endif
236 RTSpinlockRelease(Spinlock);
237}
238RT_EXPORT_SYMBOL(RTSpinlockReleaseNoInts);
239
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