1 | /* $Id: semevent-r0drv-darwin.cpp 48935 2013-10-07 21:19:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Single Release Event Semaphores, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 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 RTSEMEVENT_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/list.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 | * Structures and Typedefs *
|
---|
54 | *******************************************************************************/
|
---|
55 | /**
|
---|
56 | * Waiter entry. Lives on the stack.
|
---|
57 | */
|
---|
58 | typedef struct RTSEMEVENTDARWINENTRY
|
---|
59 | {
|
---|
60 | /** The list node. */
|
---|
61 | RTLISTNODE Node;
|
---|
62 | /** Flag set when waking up the thread by signal or destroy. */
|
---|
63 | bool volatile fWokenUp;
|
---|
64 | } RTSEMEVENTDARWINENTRY;
|
---|
65 | /** Pointer to waiter entry. */
|
---|
66 | typedef RTSEMEVENTDARWINENTRY *PRTSEMEVENTDARWINENTRY;
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Darwin event semaphore.
|
---|
71 | */
|
---|
72 | typedef struct RTSEMEVENTINTERNAL
|
---|
73 | {
|
---|
74 | /** Magic value (RTSEMEVENT_MAGIC). */
|
---|
75 | uint32_t volatile u32Magic;
|
---|
76 | /** Reference counter. */
|
---|
77 | uint32_t volatile cRefs;
|
---|
78 | /** Set if there are blocked threads. */
|
---|
79 | bool volatile fHaveBlockedThreads;
|
---|
80 | /** Set if the event object is signaled. */
|
---|
81 | bool volatile fSignaled;
|
---|
82 | /** List of waiting and woken up threads. */
|
---|
83 | RTLISTANCHOR WaitList;
|
---|
84 | /** The spinlock protecting us. */
|
---|
85 | lck_spin_t *pSpinlock;
|
---|
86 | } RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
90 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
|
---|
91 | {
|
---|
92 | return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
|
---|
97 | {
|
---|
98 | AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
|
---|
99 | AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
|
---|
100 | Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
|
---|
101 | AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
|
---|
102 | RT_ASSERT_PREEMPTIBLE();
|
---|
103 |
|
---|
104 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
105 | if (pThis)
|
---|
106 | {
|
---|
107 | pThis->u32Magic = RTSEMEVENT_MAGIC;
|
---|
108 | pThis->cRefs = 1;
|
---|
109 | pThis->fHaveBlockedThreads = false;
|
---|
110 | pThis->fSignaled = false;
|
---|
111 | RTListInit(&pThis->WaitList);
|
---|
112 | Assert(g_pDarwinLockGroup);
|
---|
113 | pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
|
---|
114 | if (pThis->pSpinlock)
|
---|
115 | {
|
---|
116 | *phEventSem = pThis;
|
---|
117 | return VINF_SUCCESS;
|
---|
118 | }
|
---|
119 |
|
---|
120 | pThis->u32Magic = 0;
|
---|
121 | RTMemFree(pThis);
|
---|
122 | }
|
---|
123 | return VERR_NO_MEMORY;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Retain a reference to the semaphore.
|
---|
129 | *
|
---|
130 | * @param pThis The semaphore.
|
---|
131 | */
|
---|
132 | DECLINLINE(void) rtR0SemEventDarwinRetain(PRTSEMEVENTINTERNAL pThis)
|
---|
133 | {
|
---|
134 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
135 | Assert(cRefs && cRefs < 100000);
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Release a reference, destroy the thing if necessary.
|
---|
141 | *
|
---|
142 | * @param pThis The semaphore.
|
---|
143 | */
|
---|
144 | DECLINLINE(void) rtR0SemEventDarwinRelease(PRTSEMEVENTINTERNAL pThis)
|
---|
145 | {
|
---|
146 | if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
|
---|
147 | {
|
---|
148 | Assert(pThis->u32Magic != RTSEMEVENT_MAGIC);
|
---|
149 | lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
|
---|
150 | RTMemFree(pThis);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
|
---|
155 | {
|
---|
156 | PRTSEMEVENTINTERNAL pThis = hEventSem;
|
---|
157 | if (pThis == NIL_RTSEMEVENT)
|
---|
158 | return VINF_SUCCESS;
|
---|
159 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
160 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
161 | RT_ASSERT_INTS_ON();
|
---|
162 |
|
---|
163 | lck_spin_lock(pThis->pSpinlock);
|
---|
164 |
|
---|
165 | ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC); /* make the handle invalid */
|
---|
166 | ASMAtomicWriteBool(&pThis->fSignaled, false);
|
---|
167 |
|
---|
168 | /* abort waiting threads. */
|
---|
169 | PRTSEMEVENTDARWINENTRY pWaiter;
|
---|
170 | RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
|
---|
171 | {
|
---|
172 | pWaiter->fWokenUp = true;
|
---|
173 | thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_RESTART);
|
---|
174 | }
|
---|
175 |
|
---|
176 | lck_spin_unlock(pThis->pSpinlock);
|
---|
177 | rtR0SemEventDarwinRelease(pThis);
|
---|
178 |
|
---|
179 | return VINF_SUCCESS;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
|
---|
184 | {
|
---|
185 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
|
---|
186 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
187 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
|
---|
188 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
189 | VERR_INVALID_HANDLE);
|
---|
190 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
191 | RT_ASSERT_INTS_ON();
|
---|
192 |
|
---|
193 | rtR0SemEventDarwinRetain(pThis);
|
---|
194 |
|
---|
195 | /** @todo should probably disable interrupts here... update
|
---|
196 | * semspinmutex-r0drv-generic.c when done. */
|
---|
197 | lck_spin_lock(pThis->pSpinlock);
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * Wake up one thread.
|
---|
201 | */
|
---|
202 | ASMAtomicWriteBool(&pThis->fSignaled, true);
|
---|
203 |
|
---|
204 | PRTSEMEVENTDARWINENTRY pWaiter;
|
---|
205 | RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
|
---|
206 | {
|
---|
207 | if (!pWaiter->fWokenUp)
|
---|
208 | {
|
---|
209 | pWaiter->fWokenUp = true;
|
---|
210 | thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_AWAKENED);
|
---|
211 | ASMAtomicWriteBool(&pThis->fSignaled, false);
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | lck_spin_unlock(pThis->pSpinlock);
|
---|
217 | rtR0SemEventDarwinRelease(pThis);
|
---|
218 |
|
---|
219 | RT_ASSERT_PREEMPT_CPUID();
|
---|
220 | return VINF_SUCCESS;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
|
---|
226 | *
|
---|
227 | * @returns VBox status code.
|
---|
228 | * @param pThis The event semaphore.
|
---|
229 | * @param fFlags See RTSemEventWaitEx.
|
---|
230 | * @param uTimeout See RTSemEventWaitEx.
|
---|
231 | * @param pSrcPos The source code position of the wait.
|
---|
232 | */
|
---|
233 | static int rtR0SemEventDarwinWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
234 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
235 | {
|
---|
236 | /*
|
---|
237 | * Validate the input.
|
---|
238 | */
|
---|
239 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
240 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
241 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
242 |
|
---|
243 | rtR0SemEventDarwinRetain(pThis);
|
---|
244 | lck_spin_lock(pThis->pSpinlock);
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * In the signaled state?
|
---|
248 | */
|
---|
249 | int rc;
|
---|
250 | if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
|
---|
251 | rc = VINF_SUCCESS;
|
---|
252 | else
|
---|
253 | {
|
---|
254 | /*
|
---|
255 | * We have to wait. So, we'll need to convert the timeout and figure
|
---|
256 | * out if it's indefinite or not.
|
---|
257 | */
|
---|
258 | uint64_t uNsAbsTimeout = 1;
|
---|
259 | if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
|
---|
260 | {
|
---|
261 | if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
|
---|
262 | uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
|
---|
263 | ? uTimeout * UINT32_C(1000000)
|
---|
264 | : UINT64_MAX;
|
---|
265 | if (uTimeout == UINT64_MAX)
|
---|
266 | fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
|
---|
267 | else
|
---|
268 | {
|
---|
269 | uint64_t u64Now;
|
---|
270 | if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
|
---|
271 | {
|
---|
272 | if (uTimeout != 0)
|
---|
273 | {
|
---|
274 | u64Now = RTTimeSystemNanoTS();
|
---|
275 | uNsAbsTimeout = u64Now + uTimeout;
|
---|
276 | if (uNsAbsTimeout < u64Now) /* overflow */
|
---|
277 | fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
|
---|
278 | }
|
---|
279 | }
|
---|
280 | else
|
---|
281 | {
|
---|
282 | uNsAbsTimeout = uTimeout;
|
---|
283 | u64Now = RTTimeSystemNanoTS();
|
---|
284 | uTimeout = u64Now < uTimeout ? uTimeout - u64Now : 0;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | if ( !(fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
|
---|
290 | && uTimeout == 0)
|
---|
291 | {
|
---|
292 | /*
|
---|
293 | * Poll call, we already checked the condition above so no need to
|
---|
294 | * wait for anything.
|
---|
295 | */
|
---|
296 | rc = VERR_TIMEOUT;
|
---|
297 | }
|
---|
298 | else
|
---|
299 | {
|
---|
300 | RTSEMEVENTDARWINENTRY Waiter;
|
---|
301 | Waiter.fWokenUp = false;
|
---|
302 | RTListAppend(&pThis->WaitList, &Waiter.Node);
|
---|
303 |
|
---|
304 | for (;;)
|
---|
305 | {
|
---|
306 | /*
|
---|
307 | * Do the actual waiting.
|
---|
308 | */
|
---|
309 | ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, true);
|
---|
310 | wait_interrupt_t fInterruptible = fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE ? THREAD_ABORTSAFE : THREAD_UNINT;
|
---|
311 | wait_result_t rcWait;
|
---|
312 | if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
|
---|
313 | rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)&Waiter, fInterruptible);
|
---|
314 | else
|
---|
315 | {
|
---|
316 | uint64_t u64AbsTime;
|
---|
317 | nanoseconds_to_absolutetime(uNsAbsTimeout, &u64AbsTime);
|
---|
318 | rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
|
---|
319 | (event_t)&Waiter, fInterruptible, u64AbsTime);
|
---|
320 | }
|
---|
321 |
|
---|
322 | /*
|
---|
323 | * Deal with the wait result.
|
---|
324 | */
|
---|
325 | if (RT_LIKELY(pThis->u32Magic == RTSEMEVENT_MAGIC))
|
---|
326 | {
|
---|
327 | switch (rcWait)
|
---|
328 | {
|
---|
329 | case THREAD_AWAKENED:
|
---|
330 | if (RT_LIKELY(Waiter.fWokenUp))
|
---|
331 | rc = VINF_SUCCESS;
|
---|
332 | else if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
|
---|
333 | rc = VERR_INTERRUPTED;
|
---|
334 | else
|
---|
335 | continue; /* Seen this happen after fork/exec/something. */
|
---|
336 | break;
|
---|
337 |
|
---|
338 | case THREAD_TIMED_OUT:
|
---|
339 | Assert(!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE));
|
---|
340 | rc = !Waiter.fWokenUp ? VERR_TIMEOUT : VINF_SUCCESS;
|
---|
341 | break;
|
---|
342 |
|
---|
343 | case THREAD_INTERRUPTED:
|
---|
344 | Assert(fInterruptible != THREAD_UNINT);
|
---|
345 | rc = !Waiter.fWokenUp ? VERR_INTERRUPTED : VINF_SUCCESS;
|
---|
346 | break;
|
---|
347 |
|
---|
348 | case THREAD_RESTART:
|
---|
349 | AssertMsg(pThis->u32Magic == ~RTSEMEVENT_MAGIC, ("%#x\n", pThis->u32Magic));
|
---|
350 | rc = VERR_SEM_DESTROYED;
|
---|
351 | break;
|
---|
352 |
|
---|
353 | default:
|
---|
354 | AssertMsgFailed(("rcWait=%d\n", rcWait));
|
---|
355 | rc = VERR_INTERNAL_ERROR_3;
|
---|
356 | break;
|
---|
357 | }
|
---|
358 | }
|
---|
359 | else
|
---|
360 | rc = VERR_SEM_DESTROYED;
|
---|
361 | break;
|
---|
362 | }
|
---|
363 |
|
---|
364 | RTListNodeRemove(&Waiter.Node);
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | lck_spin_unlock(pThis->pSpinlock);
|
---|
369 | rtR0SemEventDarwinRelease(pThis);
|
---|
370 | return rc;
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
375 | {
|
---|
376 | #ifndef RTSEMEVENT_STRICT
|
---|
377 | return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, NULL);
|
---|
378 | #else
|
---|
379 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
380 | return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
381 | #endif
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
386 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
387 | {
|
---|
388 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
389 | return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
390 | }
|
---|
391 |
|
---|
392 |
|
---|
393 | RTDECL(uint32_t) RTSemEventGetResolution(void)
|
---|
394 | {
|
---|
395 | uint64_t cNs;
|
---|
396 | absolutetime_to_nanoseconds(1, &cNs);
|
---|
397 | return (uint32_t)cNs ? (uint32_t)cNs : 0;
|
---|
398 | }
|
---|
399 |
|
---|