VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/spinlock-r0drv-darwin.cpp

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.8 KB
RevLine 
[1]1/* $Id: spinlock-r0drv-darwin.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
[8245]3 * IPRT - Spinlocks, Ring-0 Driver, Darwin.
[1]4 */
5
6/*
[98103]7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
[1]8 *
[96407]9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
[5999]11 *
[96407]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 *
[5999]25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
[96407]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
[5999]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.
[96407]33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
[1]35 */
36
37
[57358]38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
[1]41#include "the-darwin-kernel.h"
[22052]42#include "internal/iprt.h"
[1]43#include <iprt/spinlock.h>
[22052]44
[1]45#include <iprt/assert.h>
46#include <iprt/asm.h>
[29255]47#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
48# include <iprt/asm-amd64-x86.h>
49#endif
[76452]50#include <iprt/errcore.h>
[22052]51#include <iprt/mem.h>
52#include <iprt/thread.h>
[1]53
[1816]54#include "internal/magics.h"
[1]55
[1816]56
[57358]57/*********************************************************************************************************************************
58* Structures and Typedefs *
59*********************************************************************************************************************************/
[1]60/**
61 * Wrapper for the KSPIN_LOCK type.
62 */
63typedef struct RTSPINLOCKINTERNAL
64{
65 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
66 uint32_t volatile u32Magic;
[40806]67 /** Saved interrupt flag. */
68 uint32_t volatile fIntSaved;
69 /** Creation flags. */
70 uint32_t fFlags;
[1]71 /** The Darwin spinlock structure. */
72 lck_spin_t *pSpinLock;
[40806]73 /** The spinlock name. */
74 const char *pszName;
[1]75} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
76
77
78
[40806]79RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
[1]80{
[22052]81 RT_ASSERT_PREEMPTIBLE();
[57246]82 AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
[57228]83 IPRT_DARWIN_SAVE_EFL_AC();
[22052]84
[1]85 /*
86 * Allocate.
87 */
88 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
[40806]89 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
[57228]90 if (pThis)
91 {
92 /*
93 * Initialize & return.
94 */
95 pThis->u32Magic = RTSPINLOCK_MAGIC;
96 pThis->fIntSaved = 0;
97 pThis->fFlags = fFlags;
98 pThis->pszName = pszName;
99 Assert(g_pDarwinLockGroup);
100 pThis->pSpinLock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
101 if (pThis->pSpinLock)
102 {
103 *pSpinlock = pThis;
104 IPRT_DARWIN_RESTORE_EFL_AC();
105 return VINF_SUCCESS;
106 }
[1]107
[40806]108 RTMemFree(pThis);
[1]109 }
[57228]110 IPRT_DARWIN_RESTORE_EFL_AC();
111 return VERR_NO_MEMORY;
[1]112}
113
114
115RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
116{
117 /*
118 * Validate input.
119 */
[40806]120 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
121 if (!pThis)
[1]122 return VERR_INVALID_PARAMETER;
[40806]123 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
124 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
[1]125 VERR_INVALID_PARAMETER);
126
127 /*
128 * Make the lock invalid and release the memory.
129 */
[40806]130 ASMAtomicIncU32(&pThis->u32Magic);
[57228]131 IPRT_DARWIN_SAVE_EFL_AC();
[1]132
133 Assert(g_pDarwinLockGroup);
[57230]134 lck_spin_free(pThis->pSpinLock, g_pDarwinLockGroup);
[40806]135 pThis->pSpinLock = NULL;
[1]136
[40806]137 RTMemFree(pThis);
[57228]138
139 IPRT_DARWIN_RESTORE_EFL_AC();
[1]140 return VINF_SUCCESS;
141}
142
143
[40806]144RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
[1]145{
[40806]146 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
147 AssertPtr(pThis);
148 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
[1]149
[40806]150 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
151 {
152 uint32_t fIntSaved = ASMGetFlags();
153 ASMIntDisable();
154 lck_spin_lock(pThis->pSpinLock);
155 pThis->fIntSaved = fIntSaved;
[57228]156 IPRT_DARWIN_RESTORE_EFL_ONLY_AC_EX(fIntSaved);
[40806]157 }
158 else
[57228]159 {
160 IPRT_DARWIN_SAVE_EFL_AC();
[40806]161 lck_spin_lock(pThis->pSpinLock);
[57228]162 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
163 }
[1]164}
165
166
[40806]167RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
[1]168{
[40806]169 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
170 AssertPtr(pThis);
171 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
[1]172
[40806]173 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE)
174 {
175 uint32_t fIntSaved = pThis->fIntSaved;
176 pThis->fIntSaved = 0;
177 lck_spin_unlock(pThis->pSpinLock);
178 ASMSetFlags(fIntSaved);
179 }
180 else
[57228]181 {
182 IPRT_DARWIN_SAVE_EFL_AC();
[40806]183 lck_spin_unlock(pThis->pSpinLock);
[57228]184 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
185 }
[1]186}
187
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use