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