VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/semevent-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: 15.1 KB
Line 
1/* $Id: semevent-r0drv-darwin.cpp 109128 2025-05-01 01:31:56Z vboxsync $ */
2/** @file
3 * IPRT - Single 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 RTSEMEVENT_WITHOUT_REMAPPING
42#include "the-darwin-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#elif defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
51# include <iprt/asm-arm.h>
52#endif
53#include <iprt/err.h>
54#include <iprt/list.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* Structures and Typedefs *
66*********************************************************************************************************************************/
67/**
68 * Waiter entry. Lives on the stack.
69 */
70typedef struct RTSEMEVENTDARWINENTRY
71{
72 /** The list node. */
73 RTLISTNODE Node;
74 /** Flag set when waking up the thread by signal or destroy. */
75 bool volatile fWokenUp;
76} RTSEMEVENTDARWINENTRY;
77/** Pointer to waiter entry. */
78typedef RTSEMEVENTDARWINENTRY *PRTSEMEVENTDARWINENTRY;
79
80
81/**
82 * Darwin event semaphore.
83 */
84typedef struct RTSEMEVENTINTERNAL
85{
86 /** Magic value (RTSEMEVENT_MAGIC). */
87 uint32_t volatile u32Magic;
88 /** Reference counter. */
89 uint32_t volatile cRefs;
90 /** Set if there are blocked threads. */
91 bool volatile fHaveBlockedThreads;
92 /** Set if the event object is signaled. */
93 bool volatile fSignaled;
94 /** List of waiting and woken up threads. */
95 RTLISTANCHOR WaitList;
96 /** The spinlock protecting us. */
97 lck_spin_t *pSpinlock;
98} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
99
100
101
102RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
103{
104 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
105}
106
107
108RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
109{
110 RT_NOREF(hClass, pszNameFmt);
111 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
112 AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
113 Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
114 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
115 RT_ASSERT_PREEMPTIBLE();
116 IPRT_DARWIN_SAVE_EFL_AC();
117
118 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
119 if (pThis)
120 {
121 pThis->u32Magic = RTSEMEVENT_MAGIC;
122 pThis->cRefs = 1;
123 pThis->fHaveBlockedThreads = false;
124 pThis->fSignaled = false;
125 RTListInit(&pThis->WaitList);
126 Assert(g_pDarwinLockGroup);
127 pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
128 if (pThis->pSpinlock)
129 {
130 *phEventSem = pThis;
131 IPRT_DARWIN_RESTORE_EFL_AC();
132 return VINF_SUCCESS;
133 }
134
135 pThis->u32Magic = 0;
136 RTMemFree(pThis);
137 }
138 IPRT_DARWIN_RESTORE_EFL_AC();
139 return VERR_NO_MEMORY;
140}
141
142
143/**
144 * Retain a reference to the semaphore.
145 *
146 * @param pThis The semaphore.
147 */
148DECLINLINE(void) rtR0SemEventDarwinRetain(PRTSEMEVENTINTERNAL pThis)
149{
150 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
151 Assert(cRefs && cRefs < 100000); RT_NOREF_PV(cRefs);
152}
153
154
155/**
156 * Release a reference, destroy the thing if necessary.
157 *
158 * @param pThis The semaphore.
159 */
160DECLINLINE(void) rtR0SemEventDarwinRelease(PRTSEMEVENTINTERNAL pThis)
161{
162 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
163 {
164 Assert(pThis->u32Magic != RTSEMEVENT_MAGIC);
165 IPRT_DARWIN_SAVE_EFL_AC();
166
167 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
168 RTMemFree(pThis);
169
170 IPRT_DARWIN_RESTORE_EFL_AC();
171 }
172}
173
174RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
175{
176 PRTSEMEVENTINTERNAL pThis = hEventSem;
177 if (pThis == NIL_RTSEMEVENT)
178 return VINF_SUCCESS;
179 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
180 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
181 RT_ASSERT_INTS_ON();
182 IPRT_DARWIN_SAVE_EFL_AC();
183
184 RTCCUINTREG const fIntSaved = ASMIntDisableFlags();
185 lck_spin_lock(pThis->pSpinlock);
186
187 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC); /* make the handle invalid */
188 ASMAtomicWriteBool(&pThis->fSignaled, false);
189
190 /* abort waiting threads. */
191 PRTSEMEVENTDARWINENTRY pWaiter;
192 RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
193 {
194 pWaiter->fWokenUp = true;
195 thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_RESTART);
196 }
197
198 lck_spin_unlock(pThis->pSpinlock);
199 ASMSetFlags(fIntSaved);
200 rtR0SemEventDarwinRelease(pThis);
201
202 IPRT_DARWIN_RESTORE_EFL_AC();
203 return VINF_SUCCESS;
204}
205
206
207RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
208{
209 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
210 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
211 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
212 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
213 VERR_INVALID_HANDLE);
214 RT_ASSERT_PREEMPT_CPUID_VAR();
215
216 /*
217 * Coming here with interrupts disabled should be okay. The thread_wakeup_prim KPI is used
218 * by the interrupt handler IOFilterInterruptEventSource::disableInterruptOccurred() via
219 * signalWorkAvailable(). The only problem is if we have to destroy the event structure,
220 * as RTMemFree does not work with interrupts disabled (IOFree/kfree takes zone mutex).
221 */
222 //RT_ASSERT_INTS_ON(); - we may be called from interrupt context, which seems to be perfectly fine.
223 IPRT_DARWIN_SAVE_EFL_AC();
224
225 RTCCUINTREG const fIntSaved = ASMIntDisableFlags();
226 rtR0SemEventDarwinRetain(pThis);
227 lck_spin_lock(pThis->pSpinlock);
228
229 /*
230 * Wake up one thread.
231 */
232 ASMAtomicWriteBool(&pThis->fSignaled, true);
233
234 PRTSEMEVENTDARWINENTRY pWaiter;
235 RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
236 {
237 if (!pWaiter->fWokenUp)
238 {
239 pWaiter->fWokenUp = true;
240 thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_AWAKENED);
241 ASMAtomicWriteBool(&pThis->fSignaled, false);
242 break;
243 }
244 }
245
246 lck_spin_unlock(pThis->pSpinlock);
247 ASMSetFlags(fIntSaved);
248 rtR0SemEventDarwinRelease(pThis);
249
250 RT_ASSERT_PREEMPT_CPUID();
251#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
252 AssertMsg((fSavedEfl & X86_EFL_IF) == (ASMGetFlags() & X86_EFL_IF), ("fSavedEfl=%#x cur=%#x\n",(uint32_t)fSavedEfl, ASMGetFlags()));
253#endif
254 IPRT_DARWIN_RESTORE_EFL_AC();
255 return VINF_SUCCESS;
256}
257
258
259/**
260 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
261 *
262 * @returns VBox status code.
263 * @param pThis The event semaphore.
264 * @param fFlags See RTSemEventWaitEx.
265 * @param uTimeout See RTSemEventWaitEx.
266 * @param pSrcPos The source code position of the wait.
267 */
268static int rtR0SemEventDarwinWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
269 PCRTLOCKVALSRCPOS pSrcPos)
270{
271 RT_NOREF(pSrcPos);
272
273 /*
274 * Validate the input.
275 */
276 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
277 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
278 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
279 IPRT_DARWIN_SAVE_EFL_AC();
280
281 RTCCUINTREG const fIntSaved = ASMIntDisableFlags();
282 rtR0SemEventDarwinRetain(pThis);
283 lck_spin_lock(pThis->pSpinlock);
284
285 /*
286 * In the signaled state?
287 */
288 int rc;
289 if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
290 rc = VINF_SUCCESS;
291 else
292 {
293 /*
294 * We have to wait. So, we'll need to convert the timeout and figure
295 * out if it's indefinite or not.
296 */
297 uint64_t uNsAbsTimeout = 1;
298 if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
299 {
300 if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
301 uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
302 ? uTimeout * UINT32_C(1000000)
303 : UINT64_MAX;
304 if (uTimeout == UINT64_MAX)
305 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
306 else
307 {
308 uint64_t u64Now;
309 if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
310 {
311 if (uTimeout != 0)
312 {
313 u64Now = RTTimeSystemNanoTS();
314 uNsAbsTimeout = u64Now + uTimeout;
315 if (uNsAbsTimeout < u64Now) /* overflow */
316 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
317 }
318 }
319 else
320 {
321 uNsAbsTimeout = uTimeout;
322 u64Now = RTTimeSystemNanoTS();
323 uTimeout = u64Now < uTimeout ? uTimeout - u64Now : 0;
324 }
325 }
326 }
327
328 if ( !(fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
329 && uTimeout == 0)
330 {
331 /*
332 * Poll call, we already checked the condition above so no need to
333 * wait for anything.
334 */
335 rc = VERR_TIMEOUT;
336 }
337 else
338 {
339 RTSEMEVENTDARWINENTRY Waiter;
340 Waiter.fWokenUp = false;
341 RTListAppend(&pThis->WaitList, &Waiter.Node);
342
343 for (;;)
344 {
345 /*
346 * Do the actual waiting.
347 */
348 ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, true);
349 wait_interrupt_t fInterruptible = fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE ? THREAD_ABORTSAFE : THREAD_UNINT;
350 wait_result_t rcWait;
351 if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
352 rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)&Waiter, fInterruptible);
353 else
354 {
355 uint64_t u64AbsTime;
356 nanoseconds_to_absolutetime(uNsAbsTimeout, &u64AbsTime);
357 rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
358 (event_t)&Waiter, fInterruptible, u64AbsTime);
359 }
360
361 /*
362 * Deal with the wait result.
363 */
364 if (RT_LIKELY(pThis->u32Magic == RTSEMEVENT_MAGIC))
365 {
366 switch (rcWait)
367 {
368 case THREAD_AWAKENED:
369 if (RT_LIKELY(Waiter.fWokenUp))
370 rc = VINF_SUCCESS;
371 else if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
372 rc = VERR_INTERRUPTED;
373 else
374 continue; /* Seen this happen after fork/exec/something. */
375 break;
376
377 case THREAD_TIMED_OUT:
378 Assert(!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE));
379 rc = !Waiter.fWokenUp ? VERR_TIMEOUT : VINF_SUCCESS;
380 break;
381
382 case THREAD_INTERRUPTED:
383 Assert(fInterruptible != THREAD_UNINT);
384 rc = !Waiter.fWokenUp ? VERR_INTERRUPTED : VINF_SUCCESS;
385 break;
386
387 case THREAD_RESTART:
388 AssertMsg(pThis->u32Magic == ~RTSEMEVENT_MAGIC, ("%#x\n", pThis->u32Magic));
389 rc = VERR_SEM_DESTROYED;
390 break;
391
392 default:
393 AssertMsgFailed(("rcWait=%d\n", rcWait));
394 rc = VERR_INTERNAL_ERROR_3;
395 break;
396 }
397 }
398 else
399 rc = VERR_SEM_DESTROYED;
400 break;
401 }
402
403 RTListNodeRemove(&Waiter.Node);
404 }
405 }
406
407 lck_spin_unlock(pThis->pSpinlock);
408 ASMSetFlags(fIntSaved);
409 rtR0SemEventDarwinRelease(pThis);
410
411 IPRT_DARWIN_RESTORE_EFL_AC();
412 return rc;
413}
414
415
416RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
417{
418#ifndef RTSEMEVENT_STRICT
419 return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, NULL);
420#else
421 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
422 return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
423#endif
424}
425
426
427RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
428 RTHCUINTPTR uId, RT_SRC_POS_DECL)
429{
430 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
431 return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
432}
433
434
435RTDECL(uint32_t) RTSemEventGetResolution(void)
436{
437 uint64_t cNs;
438 absolutetime_to_nanoseconds(1, &cNs);
439 return (uint32_t)cNs ? (uint32_t)cNs : 0;
440}
441
442
443RTR0DECL(bool) RTSemEventIsSignalSafe(void)
444{
445 /** @todo check the code... */
446 return false;
447}
448RT_EXPORT_SYMBOL(RTSemEventIsSignalSafe);
449
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