1 | /* $Id: spinlock-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Spinlock, generic implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Defined Constants And Macros *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | /** @def RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
|
---|
42 | * Force cpu yields after spinning the number of times indicated by the define.
|
---|
43 | * If 0 we will spin forever. */
|
---|
44 | #define RT_CFG_SPINLOCK_GENERIC_DO_SLEEP 100000
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Header Files *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | #include <iprt/spinlock.h>
|
---|
51 | #include "internal/iprt.h"
|
---|
52 |
|
---|
53 | #include <iprt/alloc.h>
|
---|
54 | #include <iprt/asm.h>
|
---|
55 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
56 | # include <iprt/asm-amd64-x86.h>
|
---|
57 | #endif
|
---|
58 | #include <iprt/errcore.h>
|
---|
59 | #include <iprt/assert.h>
|
---|
60 | #if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
|
---|
61 | # include <iprt/thread.h>
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | #include "internal/magics.h"
|
---|
65 |
|
---|
66 |
|
---|
67 | /*********************************************************************************************************************************
|
---|
68 | * Structures and Typedefs *
|
---|
69 | *********************************************************************************************************************************/
|
---|
70 | /**
|
---|
71 | * Generic spinlock structure.
|
---|
72 | */
|
---|
73 | typedef struct RTSPINLOCKINTERNAL
|
---|
74 | {
|
---|
75 | /** Spinlock magic value (RTSPINLOCK_GEN_MAGIC). */
|
---|
76 | uint32_t u32Magic;
|
---|
77 | /** The spinlock creation flags. */
|
---|
78 | uint32_t fFlags;
|
---|
79 | /** The spinlock. */
|
---|
80 | uint32_t volatile fLocked;
|
---|
81 | /** The saved CPU interrupt. */
|
---|
82 | uint32_t volatile fIntSaved;
|
---|
83 | } RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
|
---|
84 |
|
---|
85 |
|
---|
86 | RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
|
---|
87 | {
|
---|
88 | PRTSPINLOCKINTERNAL pThis;
|
---|
89 | AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
|
---|
90 | RT_NOREF_PV(pszName);
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Allocate.
|
---|
94 | */
|
---|
95 | pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
96 | if (!pThis)
|
---|
97 | return VERR_NO_MEMORY;
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Initialize and return.
|
---|
101 | */
|
---|
102 | pThis->u32Magic = RTSPINLOCK_GEN_MAGIC;
|
---|
103 | pThis->fFlags = fFlags;
|
---|
104 | pThis->fIntSaved = 0;
|
---|
105 | ASMAtomicWriteU32(&pThis->fLocked, 0);
|
---|
106 |
|
---|
107 | *pSpinlock = pThis;
|
---|
108 | return VINF_SUCCESS;
|
---|
109 | }
|
---|
110 | RT_EXPORT_SYMBOL(RTSpinlockCreate);
|
---|
111 |
|
---|
112 |
|
---|
113 | RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
|
---|
114 | {
|
---|
115 | /*
|
---|
116 | * Validate input.
|
---|
117 | */
|
---|
118 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
119 | if (!pThis)
|
---|
120 | return VERR_INVALID_PARAMETER;
|
---|
121 | if (pThis->u32Magic != RTSPINLOCK_GEN_MAGIC)
|
---|
122 | {
|
---|
123 | AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
|
---|
124 | return VERR_INVALID_PARAMETER;
|
---|
125 | }
|
---|
126 |
|
---|
127 | ASMAtomicIncU32(&pThis->u32Magic);
|
---|
128 | RTMemFree(pThis);
|
---|
129 | return VINF_SUCCESS;
|
---|
130 | }
|
---|
131 | RT_EXPORT_SYMBOL(RTSpinlockDestroy);
|
---|
132 |
|
---|
133 |
|
---|
134 | RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
|
---|
135 | {
|
---|
136 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
137 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_GEN_MAGIC,
|
---|
138 | ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
|
---|
139 |
|
---|
140 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
141 | {
|
---|
142 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
143 | uint32_t fIntSaved = ASMGetFlags();
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | #if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
|
---|
147 | for (;;)
|
---|
148 | {
|
---|
149 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
150 | ASMIntDisable();
|
---|
151 | #endif
|
---|
152 | for (int c = RT_CFG_SPINLOCK_GENERIC_DO_SLEEP; c > 0; c--)
|
---|
153 | {
|
---|
154 | if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
|
---|
155 | {
|
---|
156 | # if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
157 | pThis->fIntSaved = fIntSaved;
|
---|
158 | # endif
|
---|
159 | return;
|
---|
160 | }
|
---|
161 | ASMNopPause();
|
---|
162 | }
|
---|
163 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
164 | ASMSetFlags(fIntSaved);
|
---|
165 | #endif
|
---|
166 | RTThreadYield();
|
---|
167 | }
|
---|
168 | #else
|
---|
169 | for (;;)
|
---|
170 | {
|
---|
171 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
172 | ASMIntDisable();
|
---|
173 | #endif
|
---|
174 | if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
|
---|
175 | {
|
---|
176 | # if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
177 | pThis->fIntSaved = fIntSaved;
|
---|
178 | # endif
|
---|
179 | return;
|
---|
180 | }
|
---|
181 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
182 | ASMSetFlags(fIntSaved);
|
---|
183 | #endif
|
---|
184 | ASMNopPause();
|
---|
185 | }
|
---|
186 | #endif
|
---|
187 | }
|
---|
188 | else
|
---|
189 | {
|
---|
190 | #if RT_CFG_SPINLOCK_GENERIC_DO_SLEEP
|
---|
191 | for (;;)
|
---|
192 | {
|
---|
193 | for (int c = RT_CFG_SPINLOCK_GENERIC_DO_SLEEP; c > 0; c--)
|
---|
194 | {
|
---|
195 | if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
|
---|
196 | return;
|
---|
197 | ASMNopPause();
|
---|
198 | }
|
---|
199 | RTThreadYield();
|
---|
200 | }
|
---|
201 | #else
|
---|
202 | while (!ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
|
---|
203 | ASMNopPause();
|
---|
204 | #endif
|
---|
205 | }
|
---|
206 | }
|
---|
207 | RT_EXPORT_SYMBOL(RTSpinlockAcquire);
|
---|
208 |
|
---|
209 |
|
---|
210 | RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
|
---|
211 | {
|
---|
212 | PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
|
---|
213 | AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_GEN_MAGIC,
|
---|
214 | ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
|
---|
215 |
|
---|
216 | if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
|
---|
217 | {
|
---|
218 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
219 | uint32_t fIntSaved = pThis->fIntSaved;
|
---|
220 | pThis->fIntSaved = 0;
|
---|
221 | #endif
|
---|
222 |
|
---|
223 | if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
|
---|
224 | AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
|
---|
225 |
|
---|
226 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
227 | ASMSetFlags(fIntSaved);
|
---|
228 | #endif
|
---|
229 | }
|
---|
230 | else
|
---|
231 | {
|
---|
232 | if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
|
---|
233 | AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
|
---|
234 | }
|
---|
235 | }
|
---|
236 | RT_EXPORT_SYMBOL(RTSpinlockRelease);
|
---|
237 |
|
---|