VirtualBox

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

Last change on this file since 58591 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.2 KB
Line 
1/* $Id: semeventmulti-r0drv-darwin.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
103 AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
104 AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
105 RT_ASSERT_PREEMPTIBLE();
106 IPRT_DARWIN_SAVE_EFL_AC();
107
108 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
109 if (pThis)
110 {
111 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
112 pThis->fStateAndGen = RTSEMEVENTMULTIDARWIN_STATE_GEN_INIT;
113 pThis->cRefs = 1;
114 pThis->fHaveBlockedThreads = false;
115 Assert(g_pDarwinLockGroup);
116 pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
117 if (pThis->pSpinlock)
118 {
119 *phEventMultiSem = pThis;
120 IPRT_DARWIN_RESTORE_EFL_AC();
121 return VINF_SUCCESS;
122 }
123
124 pThis->u32Magic = 0;
125 RTMemFree(pThis);
126 }
127 IPRT_DARWIN_RESTORE_EFL_AC();
128 return VERR_NO_MEMORY;
129}
130
131
132/**
133 * Retain a reference to the semaphore.
134 *
135 * @param pThis The semaphore.
136 */
137DECLINLINE(void) rtR0SemEventMultiDarwinRetain(PRTSEMEVENTMULTIINTERNAL pThis)
138{
139 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
140 Assert(cRefs && cRefs < 100000);
141}
142
143
144/**
145 * Release a reference, destroy the thing if necessary.
146 *
147 * @param pThis The semaphore.
148 */
149DECLINLINE(void) rtR0SemEventMultiDarwinRelease(PRTSEMEVENTMULTIINTERNAL pThis)
150{
151 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
152 {
153 IPRT_DARWIN_SAVE_EFL_AC();
154 Assert(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC);
155
156 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
157 RTMemFree(pThis);
158
159 IPRT_DARWIN_RESTORE_EFL_AC();
160 }
161}
162
163
164RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
165{
166 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
167 if (pThis == NIL_RTSEMEVENTMULTI)
168 return VINF_SUCCESS;
169 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
170 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
171 Assert(pThis->cRefs > 0);
172 RT_ASSERT_INTS_ON();
173 IPRT_DARWIN_SAVE_EFL_AC();
174
175 lck_spin_lock(pThis->pSpinlock);
176
177 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENTMULTI_MAGIC); /* make the handle invalid */
178 ASMAtomicAndU32(&pThis->fStateAndGen, RTSEMEVENTMULTIDARWIN_GEN_MASK);
179 if (pThis->fHaveBlockedThreads)
180 {
181 /* abort waiting threads. */
182 thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_RESTART);
183 }
184
185 lck_spin_unlock(pThis->pSpinlock);
186 rtR0SemEventMultiDarwinRelease(pThis);
187
188 IPRT_DARWIN_RESTORE_EFL_AC();
189 return VINF_SUCCESS;
190}
191
192
193RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
194{
195 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
196 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
197 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
198 RT_ASSERT_PREEMPT_CPUID_VAR();
199 RT_ASSERT_INTS_ON();
200 IPRT_DARWIN_SAVE_EFL_AC();
201
202 rtR0SemEventMultiDarwinRetain(pThis);
203 lck_spin_lock(pThis->pSpinlock);
204
205 /*
206 * Set the signal and increment the generation counter.
207 */
208 uint32_t fNew = ASMAtomicUoReadU32(&pThis->fStateAndGen);
209 fNew += 1 << RTSEMEVENTMULTIDARWIN_GEN_SHIFT;
210 fNew |= RTSEMEVENTMULTIDARWIN_STATE_MASK;
211 ASMAtomicWriteU32(&pThis->fStateAndGen, fNew);
212
213 /*
214 * Wake up all sleeping threads.
215 */
216 if (pThis->fHaveBlockedThreads)
217 {
218 ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, false);
219 thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_AWAKENED);
220 }
221
222 lck_spin_unlock(pThis->pSpinlock);
223 rtR0SemEventMultiDarwinRelease(pThis);
224
225 RT_ASSERT_PREEMPT_CPUID();
226 IPRT_DARWIN_RESTORE_EFL_AC();
227 return VINF_SUCCESS;
228}
229
230
231RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
232{
233 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
234 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
235 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
236 RT_ASSERT_PREEMPT_CPUID_VAR();
237 RT_ASSERT_INTS_ON();
238 IPRT_DARWIN_SAVE_EFL_AC();
239
240 rtR0SemEventMultiDarwinRetain(pThis);
241 lck_spin_lock(pThis->pSpinlock);
242
243 ASMAtomicAndU32(&pThis->fStateAndGen, ~RTSEMEVENTMULTIDARWIN_STATE_MASK);
244
245 lck_spin_unlock(pThis->pSpinlock);
246 rtR0SemEventMultiDarwinRelease(pThis);
247
248 RT_ASSERT_PREEMPT_CPUID();
249 IPRT_DARWIN_RESTORE_EFL_AC();
250 return VINF_SUCCESS;
251}
252
253
254/**
255 * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
256 *
257 * @returns VBox status code.
258 * @param pThis The event semaphore.
259 * @param fFlags See RTSemEventMultiWaitEx.
260 * @param uTimeout See RTSemEventMultiWaitEx.
261 * @param pSrcPos The source code position of the wait.
262 */
263static int rtR0SemEventMultiDarwinWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
264 PCRTLOCKVALSRCPOS pSrcPos)
265{
266 /*
267 * Validate input.
268 */
269 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
270 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
271 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
272 if (uTimeout != 0 || (fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
273 RT_ASSERT_PREEMPTIBLE();
274 IPRT_DARWIN_SAVE_EFL_AC();
275
276 rtR0SemEventMultiDarwinRetain(pThis);
277 lck_spin_lock(pThis->pSpinlock);
278
279 /*
280 * Is the event already signalled or do we have to wait?
281 */
282 int rc;
283 uint32_t const fOrgStateAndGen = ASMAtomicUoReadU32(&pThis->fStateAndGen);
284 if (fOrgStateAndGen & RTSEMEVENTMULTIDARWIN_STATE_MASK)
285 rc = VINF_SUCCESS;
286 else
287 {
288 /*
289 * We have to wait. So, we'll need to convert the timeout and figure
290 * out if it's indefinite or not.
291 */
292 uint64_t uNsAbsTimeout = 1;
293 if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
294 {
295 if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
296 uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
297 ? uTimeout * UINT32_C(1000000)
298 : UINT64_MAX;
299 if (uTimeout == UINT64_MAX)
300 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
301 else
302 {
303 uint64_t u64Now;
304 if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
305 {
306 if (uTimeout != 0)
307 {
308 u64Now = RTTimeSystemNanoTS();
309 uNsAbsTimeout = u64Now + uTimeout;
310 if (uNsAbsTimeout < u64Now) /* overflow */
311 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
312 }
313 }
314 else
315 {
316 uNsAbsTimeout = uTimeout;
317 u64Now = RTTimeSystemNanoTS();
318 uTimeout = u64Now < uTimeout ? uTimeout - u64Now : 0;
319 }
320 }
321 }
322
323 if ( !(fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
324 && uTimeout == 0)
325 {
326 /*
327 * Poll call, we already checked the condition above so no need to
328 * wait for anything.
329 */
330 rc = VERR_TIMEOUT;
331 }
332 else
333 {
334 for (;;)
335 {
336 /*
337 * Do the actual waiting.
338 */
339 ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, true);
340 wait_interrupt_t fInterruptible = fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE ? THREAD_ABORTSAFE : THREAD_UNINT;
341 wait_result_t rcWait;
342 if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
343 rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pThis, fInterruptible);
344 else
345 {
346 uint64_t u64AbsTime;
347 nanoseconds_to_absolutetime(uNsAbsTimeout, &u64AbsTime);
348 rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
349 (event_t)pThis, fInterruptible, u64AbsTime);
350 }
351
352 /*
353 * Deal with the wait result.
354 */
355 if (RT_LIKELY(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC))
356 {
357 switch (rcWait)
358 {
359 case THREAD_AWAKENED:
360 if (RT_LIKELY(ASMAtomicUoReadU32(&pThis->fStateAndGen) != fOrgStateAndGen))
361 rc = VINF_SUCCESS;
362 else if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
363 rc = VERR_INTERRUPTED;
364 else
365 continue; /* Seen this happen after fork/exec/something. */
366 break;
367
368 case THREAD_TIMED_OUT:
369 Assert(!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE));
370 rc = VERR_TIMEOUT;
371 break;
372
373 case THREAD_INTERRUPTED:
374 Assert(fInterruptible != THREAD_UNINT);
375 rc = VERR_INTERRUPTED;
376 break;
377
378 case THREAD_RESTART:
379 AssertMsg(pThis->u32Magic == ~RTSEMEVENTMULTI_MAGIC, ("%#x\n", pThis->u32Magic));
380 rc = VERR_SEM_DESTROYED;
381 break;
382
383 default:
384 AssertMsgFailed(("rcWait=%d\n", rcWait));
385 rc = VERR_INTERNAL_ERROR_3;
386 break;
387 }
388 }
389 else
390 rc = VERR_SEM_DESTROYED;
391 break;
392 }
393 }
394 }
395
396 lck_spin_unlock(pThis->pSpinlock);
397 rtR0SemEventMultiDarwinRelease(pThis);
398
399 IPRT_DARWIN_RESTORE_EFL_AC();
400 return rc;
401}
402
403RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
404{
405#ifndef RTSEMEVENT_STRICT
406 return rtR0SemEventMultiDarwinWait(hEventMultiSem, fFlags, uTimeout, NULL);
407#else
408 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
409 return rtR0SemEventMultiDarwinWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
410#endif
411}
412
413
414RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
415 RTHCUINTPTR uId, RT_SRC_POS_DECL)
416{
417 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
418 return rtR0SemEventMultiDarwinWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
419}
420
421
422RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
423{
424 uint64_t cNs;
425 absolutetime_to_nanoseconds(1, &cNs);
426 return (uint32_t)cNs ? (uint32_t)cNs : 0;
427}
428
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