VirtualBox

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

Last change on this file since 94293 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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