VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/semevent-r0drv-solaris.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 Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: semevent-r0drv-solaris.c 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event Semaphores, Ring-0 Driver, Solaris.
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-solaris-kernel.h"
43#include "internal/iprt.h"
44#include <iprt/semaphore.h>
45
46#include <iprt/assert.h>
47#include <iprt/asm.h>
48#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
49# include <iprt/asm-amd64-x86.h>
50#endif
51#include <iprt/err.h>
52#include <iprt/list.h>
53#include <iprt/lockvalidator.h>
54#include <iprt/mem.h>
55#include <iprt/mp.h>
56#include <iprt/thread.h>
57#include "internal/magics.h"
58#include "semeventwait-r0drv-solaris.h"
59
60
61/*********************************************************************************************************************************
62* Structures and Typedefs *
63*********************************************************************************************************************************/
64/**
65 * Waiter entry. Lives on the stack.
66 *
67 * @remarks Unfortunately, we cannot easily use cv_signal because we cannot
68 * distinguish between it and the spurious wakeups we get after fork.
69 * So, we keep an unprioritized FIFO with the sleeping threads.
70 */
71typedef struct RTSEMEVENTSOLENTRY
72{
73 /** The list node. */
74 RTLISTNODE Node;
75 /** The thread. */
76 kthread_t *pThread;
77 /** Set to @c true when waking up the thread by signal or destroy. */
78 uint32_t volatile fWokenUp;
79} RTSEMEVENTSOLENTRY;
80/** Pointer to waiter entry. */
81typedef RTSEMEVENTSOLENTRY *PRTSEMEVENTSOLENTRY;
82
83
84/**
85 * Solaris event semaphore.
86 */
87typedef struct RTSEMEVENTINTERNAL
88{
89 /** Magic value (RTSEMEVENT_MAGIC). */
90 uint32_t volatile u32Magic;
91 /** The number of threads referencing this object. */
92 uint32_t volatile cRefs;
93 /** Set if the object is signalled when there are no waiters. */
94 bool fSignaled;
95 /** List of waiting and woken up threads. */
96 RTLISTANCHOR WaitList;
97 /** The Solaris mutex protecting this structure and pairing up the with the cv. */
98 kmutex_t Mtx;
99 /** The Solaris condition variable. */
100 kcondvar_t Cnd;
101} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
102
103
104
105RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
106{
107 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
108}
109
110
111RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
112{
113 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
114 AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
115 Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
116 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
117 RT_ASSERT_PREEMPTIBLE();
118
119 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
120 if (!pThis)
121 return VERR_NO_MEMORY;
122
123 pThis->u32Magic = RTSEMEVENT_MAGIC;
124 pThis->cRefs = 1;
125 pThis->fSignaled = false;
126 RTListInit(&pThis->WaitList);
127 mutex_init(&pThis->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
128 cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
129
130 *phEventSem = pThis;
131 return VINF_SUCCESS;
132}
133
134
135/**
136 * Retain a reference to the semaphore.
137 *
138 * @param pThis The semaphore.
139 */
140DECLINLINE(void) rtR0SemEventSolRetain(PRTSEMEVENTINTERNAL pThis)
141{
142 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
143 Assert(cRefs && cRefs < 100000);
144 NOREF(cRefs);
145}
146
147
148/**
149 * The destruct.
150 *
151 * @param pThis The semaphore.
152 */
153static void rtR0SemEventSolDtor(PRTSEMEVENTINTERNAL pThis)
154{
155 Assert(pThis->u32Magic != RTSEMEVENT_MAGIC);
156 cv_destroy(&pThis->Cnd);
157 mutex_destroy(&pThis->Mtx);
158 RTMemFree(pThis);
159}
160
161
162/**
163 * Release a reference, destroy the thing if necessary.
164 *
165 * @param pThis The semaphore.
166 */
167DECLINLINE(void) rtR0SemEventSolRelease(PRTSEMEVENTINTERNAL pThis)
168{
169 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
170 rtR0SemEventSolDtor(pThis);
171}
172
173
174RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
175{
176 /*
177 * Validate input.
178 */
179 PRTSEMEVENTINTERNAL pThis = hEventSem;
180 if (pThis == NIL_RTSEMEVENT)
181 return VINF_SUCCESS;
182 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
183 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
184 Assert(pThis->cRefs > 0);
185 RT_ASSERT_INTS_ON();
186
187 mutex_enter(&pThis->Mtx);
188
189 /*
190 * Invalidate the semaphore.
191 */
192 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
193 ASMAtomicWriteBool(&pThis->fSignaled, false);
194
195 /*
196 * Abort and wake up all threads.
197 */
198 PRTSEMEVENTSOLENTRY pWaiter;
199 RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTSOLENTRY, Node)
200 {
201 pWaiter->fWokenUp = true;
202 }
203 cv_broadcast(&pThis->Cnd);
204
205 /*
206 * Release the reference from RTSemEventCreateEx.
207 */
208 mutex_exit(&pThis->Mtx);
209 rtR0SemEventSolRelease(pThis);
210
211 return VINF_SUCCESS;
212}
213
214
215RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
216{
217 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
218 RT_ASSERT_PREEMPT_CPUID_VAR();
219 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
220 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
221 RT_ASSERT_INTS_ON();
222
223 rtR0SemEventSolRetain(pThis);
224 rtR0SemSolWaitEnterMutexWithUnpinningHack(&pThis->Mtx);
225
226 /*
227 * Wake up one thread.
228 */
229 ASMAtomicWriteBool(&pThis->fSignaled, true);
230
231 PRTSEMEVENTSOLENTRY pWaiter;
232 RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTSOLENTRY, Node)
233 {
234 if (!pWaiter->fWokenUp)
235 {
236 pWaiter->fWokenUp = true;
237 setrun(pWaiter->pThread);
238 ASMAtomicWriteBool(&pThis->fSignaled, false);
239 break;
240 }
241 }
242
243 mutex_exit(&pThis->Mtx);
244 rtR0SemEventSolRelease(pThis);
245
246#ifdef DEBUG_ramshankar
247 /** See @bugref{6318} comment#11 */
248 return VINF_SUCCESS;
249#endif
250 RT_ASSERT_PREEMPT_CPUID();
251 return VINF_SUCCESS;
252}
253
254
255/**
256 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
257 *
258 * @returns VBox status code.
259 * @param pThis The event semaphore.
260 * @param fFlags See RTSemEventWaitEx.
261 * @param uTimeout See RTSemEventWaitEx.
262 * @param pSrcPos The source code position of the wait.
263 */
264static int rtR0SemEventSolWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
265 PCRTLOCKVALSRCPOS pSrcPos)
266{
267 /*
268 * Validate the input.
269 */
270 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
271 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
272 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
273
274 rtR0SemEventSolRetain(pThis);
275 mutex_enter(&pThis->Mtx);
276
277 /*
278 * In the signaled state?
279 */
280 int rc;
281 if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
282 rc = VINF_SUCCESS;
283 else
284 {
285 /*
286 * We have to wait.
287 */
288 RTR0SEMSOLWAIT Wait;
289 rc = rtR0SemSolWaitInit(&Wait, fFlags, uTimeout);
290 if (RT_SUCCESS(rc))
291 {
292 RTSEMEVENTSOLENTRY Waiter; /* ASSUMES we won't get swapped out while waiting (TS_DONT_SWAP). */
293 Waiter.pThread = curthread;
294 Waiter.fWokenUp = false;
295 RTListAppend(&pThis->WaitList, &Waiter.Node);
296
297 for (;;)
298 {
299 /* The destruction test. */
300 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
301 rc = VERR_SEM_DESTROYED;
302 else
303 {
304 /* Check the exit conditions. */
305 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
306 rc = VERR_SEM_DESTROYED;
307 else if (Waiter.fWokenUp)
308 rc = VINF_SUCCESS;
309 else if (rtR0SemSolWaitHasTimedOut(&Wait))
310 rc = VERR_TIMEOUT;
311 else if (rtR0SemSolWaitWasInterrupted(&Wait))
312 rc = VERR_INTERRUPTED;
313 else
314 {
315 /* Do the wait and then recheck the conditions. */
316 rtR0SemSolWaitDoIt(&Wait, &pThis->Cnd, &pThis->Mtx, &Waiter.fWokenUp, false);
317 continue;
318 }
319 }
320 break;
321 }
322
323 rtR0SemSolWaitDelete(&Wait);
324 RTListNodeRemove(&Waiter.Node);
325 }
326 }
327
328 mutex_exit(&pThis->Mtx);
329 rtR0SemEventSolRelease(pThis);
330 return rc;
331}
332
333
334RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
335{
336#ifndef RTSEMEVENT_STRICT
337 return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, NULL);
338#else
339 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
340 return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, &SrcPos);
341#endif
342}
343
344
345RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
346 RTHCUINTPTR uId, RT_SRC_POS_DECL)
347{
348 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
349 return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, &SrcPos);
350}
351
352
353RTDECL(uint32_t) RTSemEventGetResolution(void)
354{
355 return rtR0SemSolWaitGetResolution();
356}
357
358
359RTR0DECL(bool) RTSemEventIsSignalSafe(void)
360{
361 /* I don't trust Solaris not to preempt us. */
362 return false;
363}
364
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use