VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/semeventmulti-r0drv-darwin.cpp

Last change on this file was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 16.7 KB
Line 
1/* $Id: semeventmulti-r0drv-darwin.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Darwin.
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#define RTSEMEVENTMULTI_WITHOUT_REMAPPING
42#define RTMEM_NO_WRAP_TO_EF_APIS /* rtR0MemObjNativeProtect depends on this code, so no electrical fences here or we'll \#DF. */
43#include "the-darwin-kernel.h"
44#include "internal/iprt.h"
45#include <iprt/semaphore.h>
46
47#include <iprt/assert.h>
48#include <iprt/asm.h>
49#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
50# include <iprt/asm-amd64-x86.h>
51#endif
52#include <iprt/err.h>
53#include <iprt/lockvalidator.h>
54#include <iprt/mem.h>
55#include <iprt/mp.h>
56#include <iprt/thread.h>
57#include <iprt/time.h>
58
59#include "internal/magics.h"
60
61
62/*********************************************************************************************************************************
63* Defined Constants And Macros *
64*********************************************************************************************************************************/
65/** @name fStateAndGen values
66 * @{ */
67/** The state bit number. */
68#define RTSEMEVENTMULTIDARWIN_STATE_BIT 0
69/** The state mask. */
70#define RTSEMEVENTMULTIDARWIN_STATE_MASK RT_BIT_32(RTSEMEVENTMULTIDARWIN_STATE_BIT)
71/** The generation mask. */
72#define RTSEMEVENTMULTIDARWIN_GEN_MASK ~RTSEMEVENTMULTIDARWIN_STATE_MASK
73/** The generation shift. */
74#define RTSEMEVENTMULTIDARWIN_GEN_SHIFT 1
75/** The initial variable value. */
76#define RTSEMEVENTMULTIDARWIN_STATE_GEN_INIT UINT32_C(0xfffffffc)
77/** @} */
78
79
80/*********************************************************************************************************************************
81* Structures and Typedefs *
82*********************************************************************************************************************************/
83/**
84 * Darwin multiple release event semaphore.
85 */
86typedef struct RTSEMEVENTMULTIINTERNAL
87{
88 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
89 uint32_t volatile u32Magic;
90 /** The object state bit and generation counter.
91 * The generation counter is incremented every time the object is
92 * signalled. */
93 uint32_t volatile fStateAndGen;
94 /** Reference counter. */
95 uint32_t volatile cRefs;
96 /** Set if there are blocked threads. */
97 bool volatile fHaveBlockedThreads;
98 /** The spinlock protecting us. */
99 lck_spin_t *pSpinlock;
100} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
101
102
103
104RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
105{
106 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
107}
108
109
110RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
111 const char *pszNameFmt, ...)
112{
113 RT_NOREF(hClass, pszNameFmt);
114 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
115 AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
116 AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
117 RT_ASSERT_PREEMPTIBLE();
118 IPRT_DARWIN_SAVE_EFL_AC();
119
120 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
121 if (pThis)
122 {
123 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
124 pThis->fStateAndGen = RTSEMEVENTMULTIDARWIN_STATE_GEN_INIT;
125 pThis->cRefs = 1;
126 pThis->fHaveBlockedThreads = false;
127 Assert(g_pDarwinLockGroup);
128 pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
129 if (pThis->pSpinlock)
130 {
131 *phEventMultiSem = pThis;
132 IPRT_DARWIN_RESTORE_EFL_AC();
133 return VINF_SUCCESS;
134 }
135
136 pThis->u32Magic = 0;
137 RTMemFree(pThis);
138 }
139 IPRT_DARWIN_RESTORE_EFL_AC();
140 return VERR_NO_MEMORY;
141}
142
143
144/**
145 * Retain a reference to the semaphore.
146 *
147 * @param pThis The semaphore.
148 */
149DECLINLINE(void) rtR0SemEventMultiDarwinRetain(PRTSEMEVENTMULTIINTERNAL pThis)
150{
151 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
152 Assert(cRefs && cRefs < 100000);
153 RT_NOREF_PV(cRefs);
154}
155
156
157/**
158 * Release a reference, destroy the thing if necessary.
159 *
160 * @param pThis The semaphore.
161 */
162DECLINLINE(void) rtR0SemEventMultiDarwinRelease(PRTSEMEVENTMULTIINTERNAL pThis)
163{
164 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
165 {
166 IPRT_DARWIN_SAVE_EFL_AC();
167 Assert(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC);
168
169 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
170 RTMemFree(pThis);
171
172 IPRT_DARWIN_RESTORE_EFL_AC();
173 }
174}
175
176
177RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
178{
179 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
180 if (pThis == NIL_RTSEMEVENTMULTI)
181 return VINF_SUCCESS;
182 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
183 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
184 Assert(pThis->cRefs > 0);
185 RT_ASSERT_INTS_ON();
186 IPRT_DARWIN_SAVE_EFL_AC();
187
188 RTCCUINTREG const fIntSaved = ASMIntDisableFlags();
189 lck_spin_lock(pThis->pSpinlock);
190
191 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENTMULTI_MAGIC); /* make the handle invalid */
192 ASMAtomicAndU32(&pThis->fStateAndGen, RTSEMEVENTMULTIDARWIN_GEN_MASK);
193 if (pThis->fHaveBlockedThreads)
194 {
195 /* abort waiting threads. */
196 thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_RESTART);
197 }
198
199 lck_spin_unlock(pThis->pSpinlock);
200 ASMSetFlags(fIntSaved);
201 rtR0SemEventMultiDarwinRelease(pThis);
202
203 IPRT_DARWIN_RESTORE_EFL_AC();
204 return VINF_SUCCESS;
205}
206
207
208RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
209{
210 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
211 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
212 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
213 RT_ASSERT_PREEMPT_CPUID_VAR();
214
215 /*
216 * Coming here with interrupts disabled should be okay. The thread_wakeup_prim KPI is used
217 * by the interrupt handler IOFilterInterruptEventSource::disableInterruptOccurred() via
218 * signalWorkAvailable(). The only problem is if we have to destroy the event structure,
219 * as RTMemFree does not work with interrupts disabled (IOFree/kfree takes zone mutex).
220 */
221 //RT_ASSERT_INTS_ON(); - we may be called from interrupt context, which seems to be perfectly fine if we disable interrupts.
222
223 IPRT_DARWIN_SAVE_EFL_AC();
224
225 RTCCUINTREG const fIntSaved = ASMIntDisableFlags();
226 rtR0SemEventMultiDarwinRetain(pThis);
227 lck_spin_lock(pThis->pSpinlock);
228
229 /*
230 * Set the signal and increment the generation counter.
231 */
232 uint32_t fNew = ASMAtomicUoReadU32(&pThis->fStateAndGen);
233 fNew += 1 << RTSEMEVENTMULTIDARWIN_GEN_SHIFT;
234 fNew |= RTSEMEVENTMULTIDARWIN_STATE_MASK;
235 ASMAtomicWriteU32(&pThis->fStateAndGen, fNew);
236
237 /*
238 * Wake up all sleeping threads.
239 */
240 if (pThis->fHaveBlockedThreads)
241 {
242 ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, false);
243 thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_AWAKENED);
244 }
245
246 lck_spin_unlock(pThis->pSpinlock);
247 ASMSetFlags(fIntSaved);
248 rtR0SemEventMultiDarwinRelease(pThis);
249
250 RT_ASSERT_PREEMPT_CPUID();
251 AssertMsg((fSavedEfl & X86_EFL_IF) == (ASMGetFlags() & X86_EFL_IF), ("fSavedEfl=%#x cur=%#x\n",(uint32_t)fSavedEfl, ASMGetFlags()));
252 IPRT_DARWIN_RESTORE_EFL_AC();
253 return VINF_SUCCESS;
254}
255
256
257RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
258{
259 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
260 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
261 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
262 RT_ASSERT_PREEMPT_CPUID_VAR();
263 RT_ASSERT_INTS_ON();
264 IPRT_DARWIN_SAVE_EFL_AC();
265
266 RTCCUINTREG const fIntSaved = ASMIntDisableFlags();
267 rtR0SemEventMultiDarwinRetain(pThis);
268 lck_spin_lock(pThis->pSpinlock);
269
270 ASMAtomicAndU32(&pThis->fStateAndGen, ~RTSEMEVENTMULTIDARWIN_STATE_MASK);
271
272 lck_spin_unlock(pThis->pSpinlock);
273 ASMSetFlags(fIntSaved);
274 rtR0SemEventMultiDarwinRelease(pThis);
275
276 RT_ASSERT_PREEMPT_CPUID();
277 IPRT_DARWIN_RESTORE_EFL_AC();
278 return VINF_SUCCESS;
279}
280
281
282/**
283 * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
284 *
285 * @returns VBox status code.
286 * @param pThis The event semaphore.
287 * @param fFlags See RTSemEventMultiWaitEx.
288 * @param uTimeout See RTSemEventMultiWaitEx.
289 * @param pSrcPos The source code position of the wait.
290 */
291static int rtR0SemEventMultiDarwinWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
292 PCRTLOCKVALSRCPOS pSrcPos)
293{
294 RT_NOREF(pSrcPos);
295
296 /*
297 * Validate input.
298 */
299 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
300 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
301 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
302 if (uTimeout != 0 || (fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
303 RT_ASSERT_PREEMPTIBLE();
304 IPRT_DARWIN_SAVE_EFL_AC();
305
306 RTCCUINTREG const fIntSaved = ASMIntDisableFlags();
307 rtR0SemEventMultiDarwinRetain(pThis);
308 lck_spin_lock(pThis->pSpinlock);
309
310 /*
311 * Is the event already signalled or do we have to wait?
312 */
313 int rc;
314 uint32_t const fOrgStateAndGen = ASMAtomicUoReadU32(&pThis->fStateAndGen);
315 if (fOrgStateAndGen & RTSEMEVENTMULTIDARWIN_STATE_MASK)
316 rc = VINF_SUCCESS;
317 else
318 {
319 /*
320 * We have to wait. So, we'll need to convert the timeout and figure
321 * out if it's indefinite or not.
322 */
323 uint64_t uNsAbsTimeout = 1;
324 if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
325 {
326 if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
327 uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
328 ? uTimeout * UINT32_C(1000000)
329 : UINT64_MAX;
330 if (uTimeout == UINT64_MAX)
331 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
332 else
333 {
334 uint64_t u64Now;
335 if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
336 {
337 if (uTimeout != 0)
338 {
339 u64Now = RTTimeSystemNanoTS();
340 uNsAbsTimeout = u64Now + uTimeout;
341 if (uNsAbsTimeout < u64Now) /* overflow */
342 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
343 }
344 }
345 else
346 {
347 uNsAbsTimeout = uTimeout;
348 u64Now = RTTimeSystemNanoTS();
349 uTimeout = u64Now < uTimeout ? uTimeout - u64Now : 0;
350 }
351 }
352 }
353
354 if ( !(fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
355 && uTimeout == 0)
356 {
357 /*
358 * Poll call, we already checked the condition above so no need to
359 * wait for anything.
360 */
361 rc = VERR_TIMEOUT;
362 }
363 else
364 {
365 for (;;)
366 {
367 /*
368 * Do the actual waiting.
369 */
370 ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, true);
371 wait_interrupt_t fInterruptible = fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE ? THREAD_ABORTSAFE : THREAD_UNINT;
372 wait_result_t rcWait;
373 if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
374 rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pThis, fInterruptible);
375 else
376 {
377 uint64_t u64AbsTime;
378 nanoseconds_to_absolutetime(uNsAbsTimeout, &u64AbsTime);
379 rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
380 (event_t)pThis, fInterruptible, u64AbsTime);
381 }
382
383 /*
384 * Deal with the wait result.
385 */
386 if (RT_LIKELY(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC))
387 {
388 switch (rcWait)
389 {
390 case THREAD_AWAKENED:
391 if (RT_LIKELY(ASMAtomicUoReadU32(&pThis->fStateAndGen) != fOrgStateAndGen))
392 rc = VINF_SUCCESS;
393 else if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
394 rc = VERR_INTERRUPTED;
395 else
396 continue; /* Seen this happen after fork/exec/something. */
397 break;
398
399 case THREAD_TIMED_OUT:
400 Assert(!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE));
401 rc = VERR_TIMEOUT;
402 break;
403
404 case THREAD_INTERRUPTED:
405 Assert(fInterruptible != THREAD_UNINT);
406 rc = VERR_INTERRUPTED;
407 break;
408
409 case THREAD_RESTART:
410 AssertMsg(pThis->u32Magic == ~RTSEMEVENTMULTI_MAGIC, ("%#x\n", pThis->u32Magic));
411 rc = VERR_SEM_DESTROYED;
412 break;
413
414 default:
415 AssertMsgFailed(("rcWait=%d\n", rcWait));
416 rc = VERR_INTERNAL_ERROR_3;
417 break;
418 }
419 }
420 else
421 rc = VERR_SEM_DESTROYED;
422 break;
423 }
424 }
425 }
426
427 lck_spin_unlock(pThis->pSpinlock);
428 ASMSetFlags(fIntSaved);
429 rtR0SemEventMultiDarwinRelease(pThis);
430
431 IPRT_DARWIN_RESTORE_EFL_AC();
432 return rc;
433}
434
435RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
436{
437#ifndef RTSEMEVENT_STRICT
438 return rtR0SemEventMultiDarwinWait(hEventMultiSem, fFlags, uTimeout, NULL);
439#else
440 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
441 return rtR0SemEventMultiDarwinWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
442#endif
443}
444
445
446RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
447 RTHCUINTPTR uId, RT_SRC_POS_DECL)
448{
449 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
450 return rtR0SemEventMultiDarwinWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
451}
452
453
454RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
455{
456 uint64_t cNs;
457 absolutetime_to_nanoseconds(1, &cNs);
458 return (uint32_t)cNs ? (uint32_t)cNs : 0;
459}
460
461
462RTR0DECL(bool) RTSemEventMultiIsSignalSafe(void)
463{
464 /** @todo check the code... */
465 return false;
466}
467
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