1 | /* $Id: semfastmutex-r0drv-linux.c 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Fast Mutex Semaphores, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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-linux-kernel.h"
|
---|
42 | #include "internal/iprt.h"
|
---|
43 | #include <iprt/semaphore.h>
|
---|
44 | #include <iprt/alloc.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/asm.h>
|
---|
47 | #include <iprt/errcore.h>
|
---|
48 | #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
|
---|
49 | # include <iprt/thread.h>
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | #include "internal/magics.h"
|
---|
53 |
|
---|
54 |
|
---|
55 | /*********************************************************************************************************************************
|
---|
56 | * Structures and Typedefs *
|
---|
57 | *********************************************************************************************************************************/
|
---|
58 | /**
|
---|
59 | * Wrapper for the linux semaphore structure.
|
---|
60 | */
|
---|
61 | typedef struct RTSEMFASTMUTEXINTERNAL
|
---|
62 | {
|
---|
63 | /** Magic value (RTSEMFASTMUTEX_MAGIC). */
|
---|
64 | uint32_t u32Magic;
|
---|
65 | /** the linux semaphore. */
|
---|
66 | struct semaphore Semaphore;
|
---|
67 | #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
|
---|
68 | /** For check. */
|
---|
69 | RTNATIVETHREAD volatile Owner;
|
---|
70 | #endif
|
---|
71 | } RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
|
---|
72 |
|
---|
73 |
|
---|
74 | RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
|
---|
75 | {
|
---|
76 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * Allocate.
|
---|
80 | */
|
---|
81 | PRTSEMFASTMUTEXINTERNAL pThis;
|
---|
82 | pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
83 | if (!pThis)
|
---|
84 | return VERR_NO_MEMORY;
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Initialize.
|
---|
88 | */
|
---|
89 | pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
|
---|
90 | sema_init(&pThis->Semaphore, 1);
|
---|
91 | #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
|
---|
92 | pThis->Owner = NIL_RTNATIVETHREAD;
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | *phFastMtx = pThis;
|
---|
96 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
97 | return VINF_SUCCESS;
|
---|
98 | }
|
---|
99 | RT_EXPORT_SYMBOL(RTSemFastMutexCreate);
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
|
---|
103 | {
|
---|
104 | /*
|
---|
105 | * Validate.
|
---|
106 | */
|
---|
107 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
108 | if (pThis == NIL_RTSEMFASTMUTEX)
|
---|
109 | return VINF_SUCCESS;
|
---|
110 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
111 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
112 |
|
---|
113 | ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
|
---|
114 | RTMemFree(pThis);
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 | RT_EXPORT_SYMBOL(RTSemFastMutexDestroy);
|
---|
118 |
|
---|
119 |
|
---|
120 | RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
|
---|
121 | {
|
---|
122 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Validate.
|
---|
126 | */
|
---|
127 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
128 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
129 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
130 |
|
---|
131 | IPRT_DEBUG_SEMS_STATE(pThis, 'd');
|
---|
132 | down(&pThis->Semaphore);
|
---|
133 | #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
|
---|
134 | IPRT_DEBUG_SEMS_STATE(pThis, 'o');
|
---|
135 | AssertRelease(pThis->Owner == NIL_RTNATIVETHREAD);
|
---|
136 | ASMAtomicUoWriteSize(&pThis->Owner, RTThreadNativeSelf());
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | IPRT_LINUX_RESTORE_EFL_ONLY_AC();
|
---|
140 | return VINF_SUCCESS;
|
---|
141 | }
|
---|
142 | RT_EXPORT_SYMBOL(RTSemFastMutexRequest);
|
---|
143 |
|
---|
144 |
|
---|
145 | RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
|
---|
146 | {
|
---|
147 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * Validate.
|
---|
151 | */
|
---|
152 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
153 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
154 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
155 |
|
---|
156 | #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
|
---|
157 | AssertRelease(pThis->Owner == RTThreadNativeSelf());
|
---|
158 | ASMAtomicUoWriteSize(&pThis->Owner, NIL_RTNATIVETHREAD);
|
---|
159 | #endif
|
---|
160 | up(&pThis->Semaphore);
|
---|
161 | IPRT_DEBUG_SEMS_STATE(pThis, 'u');
|
---|
162 |
|
---|
163 | IPRT_LINUX_RESTORE_EFL_ONLY_AC();
|
---|
164 | return VINF_SUCCESS;
|
---|
165 | }
|
---|
166 | RT_EXPORT_SYMBOL(RTSemFastMutexRelease);
|
---|
167 |
|
---|