VirtualBox

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

Last change on this file since 62873 was 62564, checked in by vboxsync, 8 years ago

IPRT: Mark unused parameters.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.2 KB
Line 
1/* $Id: spinlock-generic.cpp 62564 2016-07-26 14:43:03Z vboxsync $ */
2/** @file
3 * IPRT - Spinlock, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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_GEN_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 RT_NOREF_PV(pszName);
81
82 /*
83 * Allocate.
84 */
85 pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
86 if (!pThis)
87 return VERR_NO_MEMORY;
88
89 /*
90 * Initialize and return.
91 */
92 pThis->u32Magic = RTSPINLOCK_GEN_MAGIC;
93 pThis->fFlags = fFlags;
94 pThis->fIntSaved = 0;
95 ASMAtomicWriteU32(&pThis->fLocked, 0);
96
97 *pSpinlock = pThis;
98 return VINF_SUCCESS;
99}
100RT_EXPORT_SYMBOL(RTSpinlockCreate);
101
102
103RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
104{
105 /*
106 * Validate input.
107 */
108 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
109 if (!pThis)
110 return VERR_INVALID_PARAMETER;
111 if (pThis->u32Magic != RTSPINLOCK_GEN_MAGIC)
112 {
113 AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
114 return VERR_INVALID_PARAMETER;
115 }
116
117 ASMAtomicIncU32(&pThis->u32Magic);
118 RTMemFree(pThis);
119 return VINF_SUCCESS;
120}
121RT_EXPORT_SYMBOL(RTSpinlockDestroy);
122
123
124RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
125{
126 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
127 AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_GEN_MAGIC,
128 ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
129
130 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
131 {
132#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
133 uint32_t fIntSaved = ASMGetFlags();
134#endif
135
136#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
137 for (;;)
138 {
139#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
140 ASMIntDisable();
141#endif
142 for (int c = RT_CFG_SPINLOCK_GENERIC_DO_SLEEP; c > 0; c--)
143 {
144 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
145 {
146# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
147 pThis->fIntSaved = fIntSaved;
148# endif
149 return;
150 }
151 ASMNopPause();
152 }
153#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
154 ASMSetFlags(fIntSaved);
155#endif
156 RTThreadYield();
157 }
158#else
159 for (;;)
160 {
161#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
162 ASMIntDisable();
163#endif
164 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
165 {
166# if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
167 pThis->fIntSaved = fIntSaved;
168# endif
169 return;
170 }
171#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
172 ASMSetFlags(fIntSaved);
173#endif
174 ASMNopPause();
175 }
176#endif
177 }
178 else
179 {
180#if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
181 for (;;)
182 {
183 for (int c = RT_CFG_SPINLOCK_GENERIC_DO_SLEEP; c > 0; c--)
184 {
185 if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
186 return;
187 ASMNopPause();
188 }
189 RTThreadYield();
190 }
191#else
192 while (!ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
193 ASMNopPause();
194#endif
195 }
196}
197RT_EXPORT_SYMBOL(RTSpinlockAcquire);
198
199
200RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
201{
202 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
203 AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_GEN_MAGIC,
204 ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
205
206 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
207 {
208#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
209 uint32_t fIntSaved = pThis->fIntSaved;
210 pThis->fIntSaved = 0;
211#endif
212
213 if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
214 AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
215
216#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
217 ASMSetFlags(fIntSaved);
218#endif
219 }
220 else
221 {
222 if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
223 AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
224 }
225}
226RT_EXPORT_SYMBOL(RTSpinlockRelease);
227
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