1 | /* $Id: semfastmutex-r0drv-freebsd.c 77120 2019-02-01 15:08:46Z 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-2019 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 | *
|
---|
30 | * This code is based on:
|
---|
31 | *
|
---|
32 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
33 | *
|
---|
34 | * Permission is hereby granted, free of charge, to any person
|
---|
35 | * obtaining a copy of this software and associated documentation
|
---|
36 | * files (the "Software"), to deal in the Software without
|
---|
37 | * restriction, including without limitation the rights to use,
|
---|
38 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
39 | * copies of the Software, and to permit persons to whom the
|
---|
40 | * Software is furnished to do so, subject to the following
|
---|
41 | * conditions:
|
---|
42 | *
|
---|
43 | * The above copyright notice and this permission notice shall be
|
---|
44 | * included in all copies or substantial portions of the Software.
|
---|
45 | *
|
---|
46 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
47 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
48 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
49 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
50 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
51 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
52 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
53 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
54 | */
|
---|
55 |
|
---|
56 |
|
---|
57 | /*********************************************************************************************************************************
|
---|
58 | * Header Files *
|
---|
59 | *********************************************************************************************************************************/
|
---|
60 | #include "the-freebsd-kernel.h"
|
---|
61 |
|
---|
62 | #include <iprt/semaphore.h>
|
---|
63 | #include <iprt/errcore.h>
|
---|
64 | #include <iprt/alloc.h>
|
---|
65 | #include <iprt/assert.h>
|
---|
66 | #include <iprt/asm.h>
|
---|
67 |
|
---|
68 | #include "internal/magics.h"
|
---|
69 |
|
---|
70 |
|
---|
71 | /*********************************************************************************************************************************
|
---|
72 | * Structures and Typedefs *
|
---|
73 | *********************************************************************************************************************************/
|
---|
74 | /**
|
---|
75 | * Wrapper for the FreeBSD (sleep) mutex.
|
---|
76 | */
|
---|
77 | typedef struct RTSEMFASTMUTEXINTERNAL
|
---|
78 | {
|
---|
79 | /** Magic value (RTSEMFASTMUTEX_MAGIC). */
|
---|
80 | uint32_t u32Magic;
|
---|
81 | /** The FreeBSD shared/exclusive lock mutex. */
|
---|
82 | struct sx SxLock;
|
---|
83 | } RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
|
---|
84 |
|
---|
85 |
|
---|
86 | RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
|
---|
87 | {
|
---|
88 | AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
|
---|
89 | AssertPtrReturn(phFastMtx, VERR_INVALID_POINTER);
|
---|
90 |
|
---|
91 | PRTSEMFASTMUTEXINTERNAL pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAllocZ(sizeof(*pThis));
|
---|
92 | if (pThis)
|
---|
93 | {
|
---|
94 | pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
|
---|
95 | sx_init_flags(&pThis->SxLock, "IPRT Fast Mutex Semaphore", SX_DUPOK);
|
---|
96 |
|
---|
97 | *phFastMtx = pThis;
|
---|
98 | return VINF_SUCCESS;
|
---|
99 | }
|
---|
100 | return VERR_NO_MEMORY;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
|
---|
105 | {
|
---|
106 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
107 | if (pThis == NIL_RTSEMFASTMUTEX)
|
---|
108 | return VINF_SUCCESS;
|
---|
109 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
110 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
111 |
|
---|
112 | ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
|
---|
113 | sx_destroy(&pThis->SxLock);
|
---|
114 | RTMemFree(pThis);
|
---|
115 |
|
---|
116 | return VINF_SUCCESS;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
|
---|
121 | {
|
---|
122 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
123 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
124 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
125 |
|
---|
126 | sx_xlock(&pThis->SxLock);
|
---|
127 | return VINF_SUCCESS;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
|
---|
132 | {
|
---|
133 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
134 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
135 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
136 |
|
---|
137 | sx_xunlock(&pThis->SxLock);
|
---|
138 | return VINF_SUCCESS;
|
---|
139 | }
|
---|
140 |
|
---|