VirtualBox

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

Last change on this file since 82877 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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