VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/semevent-r0drv-linux.c

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.3 KB
Line 
1/* $Id: semevent-r0drv-linux.c 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event Semaphores, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2023 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#define RTSEMEVENT_WITHOUT_REMAPPING
42#include "the-linux-kernel.h"
43#include "internal/iprt.h"
44#include <iprt/semaphore.h>
45
46#include <iprt/asm.h>
47#include <iprt/assert.h>
48#include <iprt/err.h>
49#include <iprt/lockvalidator.h>
50#include <iprt/mem.h>
51
52#include "waitqueue-r0drv-linux.h"
53#include "internal/magics.h"
54
55
56/*********************************************************************************************************************************
57* Structures and Typedefs *
58*********************************************************************************************************************************/
59/**
60 * Linux event semaphore.
61 */
62typedef struct RTSEMEVENTINTERNAL
63{
64 /** Magic value (RTSEMEVENT_MAGIC). */
65 uint32_t volatile u32Magic;
66 /** The object status - !0 when signaled and 0 when reset. */
67 uint32_t volatile fState;
68 /** Reference counter. */
69 uint32_t volatile cRefs;
70 /** The wait queue. */
71 wait_queue_head_t Head;
72} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
73
74
75
76RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
77{
78 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
79}
80
81
82RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
83{
84 PRTSEMEVENTINTERNAL pThis;
85 IPRT_LINUX_SAVE_EFL_AC();
86 RT_NOREF_PV(hClass); RT_NOREF_PV(pszNameFmt);
87
88 AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
89 Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
90
91 pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
92 if (!pThis)
93 return VERR_NO_MEMORY;
94
95 pThis->u32Magic = RTSEMEVENT_MAGIC;
96 pThis->fState = 0;
97 pThis->cRefs = 1;
98 init_waitqueue_head(&pThis->Head);
99
100 *phEventSem = pThis;
101 IPRT_LINUX_RESTORE_EFL_AC();
102 return VINF_SUCCESS;
103}
104RT_EXPORT_SYMBOL(RTSemEventCreate);
105
106
107/**
108 * Retains a reference to the event semaphore.
109 *
110 * @param pThis The event semaphore.
111 */
112DECLINLINE(void) rtR0SemEventLnxRetain(PRTSEMEVENTINTERNAL pThis)
113{
114 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
115 Assert(cRefs < 100000); NOREF(cRefs);
116}
117
118
119/**
120 * Releases a reference to the event semaphore.
121 *
122 * @param pThis The event semaphore.
123 */
124DECLINLINE(void) rtR0SemEventLnxRelease(PRTSEMEVENTINTERNAL pThis)
125{
126 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
127 RTMemFree(pThis);
128}
129
130
131RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
132{
133 IPRT_LINUX_SAVE_EFL_AC();
134
135 /*
136 * Validate input.
137 */
138 PRTSEMEVENTINTERNAL pThis = hEventSem;
139 if (pThis == NIL_RTSEMEVENT)
140 return VINF_SUCCESS;
141 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
142 Assert(pThis->cRefs > 0);
143
144 /*
145 * Invalidate it and signal the object just in case.
146 */
147 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
148 ASMAtomicWriteU32(&pThis->fState, 0);
149 Assert(!waitqueue_active(&pThis->Head));
150 wake_up_all(&pThis->Head);
151 rtR0SemEventLnxRelease(pThis);
152
153 IPRT_LINUX_RESTORE_EFL_AC();
154 return VINF_SUCCESS;
155}
156RT_EXPORT_SYMBOL(RTSemEventDestroy);
157
158
159RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
160{
161 IPRT_LINUX_SAVE_EFL_AC();
162
163 /*
164 * Validate input.
165 */
166 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
167 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
168 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
169 rtR0SemEventLnxRetain(pThis);
170
171 /*
172 * Signal the event object.
173 */
174 ASMAtomicWriteU32(&pThis->fState, 1);
175 wake_up(&pThis->Head);
176
177 rtR0SemEventLnxRelease(pThis);
178 IPRT_LINUX_RESTORE_EFL_AC();
179 return VINF_SUCCESS;
180}
181RT_EXPORT_SYMBOL(RTSemEventSignal);
182
183
184/**
185 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
186 *
187 * @returns VBox status code.
188 * @param pThis The event semaphore.
189 * @param fFlags See RTSemEventWaitEx.
190 * @param uTimeout See RTSemEventWaitEx.
191 * @param pSrcPos The source code position of the wait.
192 */
193static int rtR0SemEventLnxWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
194 PCRTLOCKVALSRCPOS pSrcPos)
195{
196 int rc;
197 RT_NOREF_PV(pSrcPos);
198
199 /*
200 * Validate the input.
201 */
202 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
203 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
204 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
205 rtR0SemEventLnxRetain(pThis);
206
207 /*
208 * Try grab the event without setting up the wait.
209 */
210 if ( 1 /** @todo check if there are someone waiting already - waitqueue_active, but then what do we do below? */
211 && ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
212 rc = VINF_SUCCESS;
213 else
214 {
215 /*
216 * We have to wait.
217 */
218 IPRT_LINUX_SAVE_EFL_AC();
219 RTR0SEMLNXWAIT Wait;
220 rc = rtR0SemLnxWaitInit(&Wait, fFlags, uTimeout, &pThis->Head);
221 if (RT_SUCCESS(rc))
222 {
223 IPRT_DEBUG_SEMS_STATE(pThis, 'E');
224 for (;;)
225 {
226 /* The destruction test. */
227 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
228 rc = VERR_SEM_DESTROYED;
229 else
230 {
231 rtR0SemLnxWaitPrepare(&Wait);
232
233 /* Check the exit conditions. */
234 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
235 rc = VERR_SEM_DESTROYED;
236 else if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
237 rc = VINF_SUCCESS;
238 else if (rtR0SemLnxWaitHasTimedOut(&Wait))
239 rc = VERR_TIMEOUT;
240 else if (rtR0SemLnxWaitWasInterrupted(&Wait))
241 rc = VERR_INTERRUPTED;
242 else
243 {
244 /* Do the wait and then recheck the conditions. */
245 rtR0SemLnxWaitDoIt(&Wait);
246 continue;
247 }
248 }
249 break;
250 }
251
252 rtR0SemLnxWaitDelete(&Wait);
253 IPRT_DEBUG_SEMS_STATE_RC(pThis, 'E', rc);
254 }
255 IPRT_LINUX_RESTORE_EFL_AC();
256 }
257
258 rtR0SemEventLnxRelease(pThis);
259 return rc;
260}
261
262
263RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
264{
265#ifndef RTSEMEVENT_STRICT
266 return rtR0SemEventLnxWait(hEventSem, fFlags, uTimeout, NULL);
267#else
268 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
269 return rtR0SemEventLnxWait(hEventSem, fFlags, uTimeout, &SrcPos);
270#endif
271}
272RT_EXPORT_SYMBOL(RTSemEventWaitEx);
273
274
275RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
276 RTHCUINTPTR uId, RT_SRC_POS_DECL)
277{
278 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
279 return rtR0SemEventLnxWait(hEventSem, fFlags, uTimeout, &SrcPos);
280}
281RT_EXPORT_SYMBOL(RTSemEventWaitExDebug);
282
283
284RTDECL(uint32_t) RTSemEventGetResolution(void)
285{
286 return rtR0SemLnxWaitGetResolution();
287}
288RT_EXPORT_SYMBOL(RTSemEventGetResolution);
289
290
291RTR0DECL(bool) RTSemEventIsSignalSafe(void)
292{
293 return true;
294}
295RT_EXPORT_SYMBOL(RTSemEventIsSignalSafe);
296
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use