VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/haiku/spinlock-r0drv-haiku.c

Last change on this file was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1/* $Id: spinlock-r0drv-haiku.c 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, Haiku.
4 */
5
6/*
7 * Copyright (C) 2012-2024 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* Header Files *
40*********************************************************************************************************************************/
41#include "the-haiku-kernel.h"
42#include "internal/iprt.h"
43#include <iprt/spinlock.h>
44
45#include <iprt/assert.h>
46#include <iprt/asm.h>
47#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
48#include <iprt/asm-amd64-x86.h>
49#endif
50#include <iprt/errcore.h>
51#include <iprt/mem.h>
52#include <iprt/thread.h>
53
54#include "internal/magics.h"
55
56
57/*********************************************************************************************************************************
58* Structures and Typedefs *
59*********************************************************************************************************************************/
60/**
61 * Wrapper for the KSPIN_LOCK type.
62 */
63typedef struct RTSPINLOCKINTERNAL
64{
65 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
66 uint32_t volatile u32Magic;
67 /** Spinlock creation flags */
68 uint32_t fFlags;
69 /** Saved interrupt CPU status. */
70 cpu_status volatile fIntSaved;
71 /** The Haiku spinlock structure. */
72 spinlock hSpinLock;
73} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
74
75
76
77RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
78{
79 RT_ASSERT_PREEMPTIBLE();
80 NOREF(pszName);
81
82 /*
83 * Allocate.
84 */
85 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
86 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pSpinlockInt));
87 if (RT_UNLIKELY(!pSpinlockInt))
88 return VERR_NO_MEMORY;
89
90 /*
91 * Initialize & return.
92 */
93 pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
94 pSpinlockInt->fFlags = fFlags;
95 pSpinlockInt->fIntSaved = 0;
96 B_INITIALIZE_SPINLOCK(&pSpinlockInt->hSpinLock);
97
98 *pSpinlock = pSpinlockInt;
99 return VINF_SUCCESS;
100}
101
102
103RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
104{
105 /*
106 * Validate input.
107 */
108 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
109 if (RT_UNLIKELY(!pSpinlockInt))
110 return VERR_INVALID_PARAMETER;
111 AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
112 ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
113 VERR_INVALID_PARAMETER);
114
115 /*
116 * Make the lock invalid and release the memory.
117 */
118 ASMAtomicIncU32(&pSpinlockInt->u32Magic);
119
120 B_INITIALIZE_SPINLOCK(&pSpinlockInt->hSpinLock);
121
122 RTMemFree(pSpinlockInt);
123 return VINF_SUCCESS;
124}
125
126
127RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
128{
129 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
130 AssertPtr(pSpinlockInt);
131 Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
132
133 /* Haiku cannot take spinlocks without disabling interrupts. Ignore our spinlock creation flags. */
134 pSpinlockInt->fIntSaved = disable_interrupts();
135 acquire_spinlock(&pSpinlockInt->hSpinLock);
136}
137
138
139RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
140{
141 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
142 AssertPtr(pSpinlockInt);
143 Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
144
145 release_spinlock(&pSpinlockInt->hSpinLock);
146 restore_interrupts(pSpinlockInt->fIntSaved);
147}
148
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