VirtualBox

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

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

IPRT: warnings (clang)

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