VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/semfastmutex-r0drv-freebsd.c@ 86669

Last change on this file since 86669 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: semfastmutex-r0drv-freebsd.c 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - Fast Mutex Semaphores, Ring-0 Driver, FreeBSD.
4 */
5
6/*
7 * Contributed by knut st. osmundsen.
8 *
9 * Copyright (C) 2007-2020 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * The contents of this file may alternatively be used under the terms
20 * of the Common Development and Distribution License Version 1.0
21 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
22 * VirtualBox OSE distribution, in which case the provisions of the
23 * CDDL are applicable instead of those of the GPL.
24 *
25 * You may elect to license modified versions of this file under the
26 * terms and conditions of either the GPL or the CDDL or both.
27 * --------------------------------------------------------------------
28 *
29 * This code is based on:
30 *
31 * Copyright (c) 2007 knut st. osmundsen <[email protected]>
32 *
33 * Permission is hereby granted, free of charge, to any person
34 * obtaining a copy of this software and associated documentation
35 * files (the "Software"), to deal in the Software without
36 * restriction, including without limitation the rights to use,
37 * copy, modify, merge, publish, distribute, sublicense, and/or sell
38 * copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following
40 * conditions:
41 *
42 * The above copyright notice and this permission notice shall be
43 * included in all copies or substantial portions of the Software.
44 *
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
46 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
47 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
48 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
49 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
50 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
52 * OTHER DEALINGS IN THE SOFTWARE.
53 */
54
55
56/*********************************************************************************************************************************
57* Header Files *
58*********************************************************************************************************************************/
59#include "the-freebsd-kernel.h"
60
61#include <iprt/semaphore.h>
62#include <iprt/errcore.h>
63#include <iprt/alloc.h>
64#include <iprt/assert.h>
65#include <iprt/asm.h>
66
67#include "internal/magics.h"
68
69
70/*********************************************************************************************************************************
71* Structures and Typedefs *
72*********************************************************************************************************************************/
73/**
74 * Wrapper for the FreeBSD (sleep) mutex.
75 */
76typedef struct RTSEMFASTMUTEXINTERNAL
77{
78 /** Magic value (RTSEMFASTMUTEX_MAGIC). */
79 uint32_t u32Magic;
80 /** The FreeBSD shared/exclusive lock mutex. */
81 struct sx SxLock;
82} RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
83
84
85RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
86{
87 AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
88 AssertPtrReturn(phFastMtx, VERR_INVALID_POINTER);
89
90 PRTSEMFASTMUTEXINTERNAL pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAllocZ(sizeof(*pThis));
91 if (pThis)
92 {
93 pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
94 sx_init_flags(&pThis->SxLock, "IPRT Fast Mutex Semaphore", SX_DUPOK);
95
96 *phFastMtx = pThis;
97 return VINF_SUCCESS;
98 }
99 return VERR_NO_MEMORY;
100}
101
102
103RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
104{
105 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
106 if (pThis == NIL_RTSEMFASTMUTEX)
107 return VINF_SUCCESS;
108 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
109 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
110
111 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
112 sx_destroy(&pThis->SxLock);
113 RTMemFree(pThis);
114
115 return VINF_SUCCESS;
116}
117
118
119RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
120{
121 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
122 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
123 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
124
125 sx_xlock(&pThis->SxLock);
126 return VINF_SUCCESS;
127}
128
129
130RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
131{
132 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
133 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
134 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
135
136 sx_xunlock(&pThis->SxLock);
137 return VINF_SUCCESS;
138}
139
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