VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/netbsd/semfastmutex-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: 5.5 KB
Line 
1/* $Id: semfastmutex-r0drv-netbsd.c 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Fast Mutex Semaphores, 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
69#include <iprt/semaphore.h>
70#include <iprt/errcore.h>
71#include <iprt/alloc.h>
72#include <iprt/assert.h>
73#include <iprt/asm.h>
74
75#include "internal/magics.h"
76
77
78/*********************************************************************************************************************************
79* Structures and Typedefs *
80*********************************************************************************************************************************/
81/**
82 * Wrapper for the NetBSD (sleep) mutex.
83 */
84typedef struct RTSEMFASTMUTEXINTERNAL
85{
86 /** Magic value (RTSEMFASTMUTEX_MAGIC). */
87 uint32_t u32Magic;
88 /** The NetBSD shared/exclusive lock mutex. */
89 krwlock_t Mtx;
90} RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
91
92
93RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
94{
95 AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
96 AssertPtrReturn(phFastMtx, VERR_INVALID_POINTER);
97
98 PRTSEMFASTMUTEXINTERNAL pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAllocZ(sizeof(*pThis));
99 if (pThis)
100 {
101 pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
102 rw_init(&pThis->Mtx);
103
104 *phFastMtx = pThis;
105 return VINF_SUCCESS;
106 }
107 return VERR_NO_MEMORY;
108}
109
110
111RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
112{
113 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
114 if (pThis == NIL_RTSEMFASTMUTEX)
115 return VINF_SUCCESS;
116 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
117 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
118
119 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
120 rw_destroy(&pThis->Mtx);
121 RTMemFree(pThis);
122
123 return VINF_SUCCESS;
124}
125
126
127RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
128{
129 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
130 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
131 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
132
133 rw_enter(&pThis->Mtx, RW_WRITER);
134 return VINF_SUCCESS;
135}
136
137
138RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
139{
140 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
141 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
142 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
143
144 rw_exit(&pThis->Mtx);
145 return VINF_SUCCESS;
146}
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