VirtualBox

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

Last change on this file was 109128, checked in by vboxsync, 10 days ago

IPRT/r0drv: Build adjustments for darwin.arm64. jiraref:VBP-1653

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette