VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/netbsd/spinlock-r0drv-netbsd.c@ 96445

Last change on this file since 96445 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/* $Id: spinlock-r0drv-netbsd.c 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, NetBSD.
4 */
5
6/*
7 * Contributed by knut st. osmundsen.
8 *
9 * Copyright (C) 2007-2022 Oracle and/or its affiliates.
10 *
11 * This file is part of VirtualBox base platform packages, as
12 * available from https://www.virtualbox.org.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation, in version 3 of the
17 * License.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <https://www.gnu.org/licenses>.
26 *
27 * The contents of this file may alternatively be used under the terms
28 * of the Common Development and Distribution License Version 1.0
29 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
30 * in the VirtualBox distribution, in which case the provisions of the
31 * CDDL are applicable instead of those of the GPL.
32 *
33 * You may elect to license modified versions of this file under the
34 * terms and conditions of either the GPL or the CDDL or both.
35 *
36 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
37 * --------------------------------------------------------------------
38 *
39 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
40 *
41 * Permission is hereby granted, free of charge, to any person
42 * obtaining a copy of this software and associated documentation
43 * files (the "Software"), to deal in the Software without
44 * restriction, including without limitation the rights to use,
45 * copy, modify, merge, publish, distribute, sublicense, and/or sell
46 * copies of the Software, and to permit persons to whom the
47 * Software is furnished to do so, subject to the following
48 * conditions:
49 *
50 * The above copyright notice and this permission notice shall be
51 * included in all copies or substantial portions of the Software.
52 *
53 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
54 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
55 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
56 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
57 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
58 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
59 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
60 * OTHER DEALINGS IN THE SOFTWARE.
61 */
62
63
64/*********************************************************************************************************************************
65* Header Files *
66*********************************************************************************************************************************/
67#include "the-netbsd-kernel.h"
68#include "internal/iprt.h"
69
70#include <iprt/spinlock.h>
71#include <iprt/errcore.h>
72#include <iprt/alloc.h>
73#include <iprt/assert.h>
74#include <iprt/asm.h>
75#include <iprt/asm-amd64-x86.h>
76#include <iprt/thread.h>
77#include <iprt/mp.h>
78
79#include "internal/magics.h"
80
81
82/*********************************************************************************************************************************
83* Structures and Typedefs *
84*********************************************************************************************************************************/
85/**
86 * Wrapper for the struct mtx type.
87 */
88typedef struct RTSPINLOCKINTERNAL
89{
90 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
91 uint32_t volatile u32Magic;
92 /** The spinlock. */
93 kmutex_t pSpinLock;
94 /** Saved interrupt flag. */
95 uint32_t volatile fIntSaved;
96 /** The spinlock creation flags. */
97 uint32_t fFlags;
98} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
99
100
101RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
102{
103 RT_ASSERT_PREEMPTIBLE();
104 AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
105
106 /*
107 * Allocate.
108 */
109 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
110 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pThis));
111 if (!pThis)
112 return VERR_NO_MEMORY;
113
114 /*
115 * Initialize & return.
116 */
117 pThis->u32Magic = RTSPINLOCK_MAGIC;
118 pThis->fFlags = fFlags;
119 pThis->fIntSaved = 0;
120 if (fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE) {
121 mutex_init(&pThis->pSpinLock, MUTEX_DEFAULT, IPL_BIO);
122 } else {
123 mutex_init(&pThis->pSpinLock, MUTEX_DEFAULT, IPL_NONE);
124 }
125
126 *pSpinlock = pThis;
127 return VINF_SUCCESS;
128}
129
130
131RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
132{
133 /*
134 * Validate input.
135 */
136 RT_ASSERT_INTS_ON();
137 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
138 if (!pThis)
139 return VERR_INVALID_PARAMETER;
140 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
141 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
142 VERR_INVALID_PARAMETER);
143
144 /*
145 * Make the lock invalid and release the memory.
146 */
147 ASMAtomicIncU32(&pThis->u32Magic);
148 mutex_destroy(&pThis->pSpinLock);
149 RTMemFree(pThis);
150 return VINF_SUCCESS;
151}
152
153
154RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
155{
156 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
157 RT_ASSERT_PREEMPT_CPUID_VAR();
158 AssertPtr(pThis);
159 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
160
161 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE) {
162 mutex_spin_enter(&pThis->pSpinLock);
163 } else {
164 mutex_enter(&pThis->pSpinLock);
165 }
166}
167
168
169RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
170{
171 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
172 AssertPtr(pThis);
173 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
174
175 if (pThis->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE) {
176 mutex_spin_exit(&pThis->pSpinLock);
177 } else {
178 mutex_exit(&pThis->pSpinLock);
179 }
180}
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