1 | /* $Id: semevent-r0drv-darwin.cpp 63509 2016-08-15 22:54:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Single 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 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 | RT_NOREF(hClass, pszNameFmt);
|
---|
99 | AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
|
---|
100 | AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
|
---|
101 | Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
|
---|
102 | AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
|
---|
103 | RT_ASSERT_PREEMPTIBLE();
|
---|
104 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
105 |
|
---|
106 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
107 | if (pThis)
|
---|
108 | {
|
---|
109 | pThis->u32Magic = RTSEMEVENT_MAGIC;
|
---|
110 | pThis->cRefs = 1;
|
---|
111 | pThis->fHaveBlockedThreads = false;
|
---|
112 | pThis->fSignaled = false;
|
---|
113 | RTListInit(&pThis->WaitList);
|
---|
114 | Assert(g_pDarwinLockGroup);
|
---|
115 | pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
|
---|
116 | if (pThis->pSpinlock)
|
---|
117 | {
|
---|
118 | *phEventSem = pThis;
|
---|
119 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
120 | return VINF_SUCCESS;
|
---|
121 | }
|
---|
122 |
|
---|
123 | pThis->u32Magic = 0;
|
---|
124 | RTMemFree(pThis);
|
---|
125 | }
|
---|
126 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
127 | return VERR_NO_MEMORY;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Retain a reference to the semaphore.
|
---|
133 | *
|
---|
134 | * @param pThis The semaphore.
|
---|
135 | */
|
---|
136 | DECLINLINE(void) rtR0SemEventDarwinRetain(PRTSEMEVENTINTERNAL pThis)
|
---|
137 | {
|
---|
138 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
139 | Assert(cRefs && cRefs < 100000);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Release a reference, destroy the thing if necessary.
|
---|
145 | *
|
---|
146 | * @param pThis The semaphore.
|
---|
147 | */
|
---|
148 | DECLINLINE(void) rtR0SemEventDarwinRelease(PRTSEMEVENTINTERNAL pThis)
|
---|
149 | {
|
---|
150 | if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
|
---|
151 | {
|
---|
152 | Assert(pThis->u32Magic != RTSEMEVENT_MAGIC);
|
---|
153 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
154 |
|
---|
155 | lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
|
---|
156 | RTMemFree(pThis);
|
---|
157 |
|
---|
158 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
|
---|
163 | {
|
---|
164 | PRTSEMEVENTINTERNAL pThis = hEventSem;
|
---|
165 | if (pThis == NIL_RTSEMEVENT)
|
---|
166 | return VINF_SUCCESS;
|
---|
167 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
168 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
169 | RT_ASSERT_INTS_ON();
|
---|
170 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
171 |
|
---|
172 | lck_spin_lock(pThis->pSpinlock);
|
---|
173 |
|
---|
174 | ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC); /* make the handle invalid */
|
---|
175 | ASMAtomicWriteBool(&pThis->fSignaled, false);
|
---|
176 |
|
---|
177 | /* abort waiting threads. */
|
---|
178 | PRTSEMEVENTDARWINENTRY pWaiter;
|
---|
179 | RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
|
---|
180 | {
|
---|
181 | pWaiter->fWokenUp = true;
|
---|
182 | thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_RESTART);
|
---|
183 | }
|
---|
184 |
|
---|
185 | lck_spin_unlock(pThis->pSpinlock);
|
---|
186 | rtR0SemEventDarwinRelease(pThis);
|
---|
187 |
|
---|
188 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
189 | return VINF_SUCCESS;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
|
---|
194 | {
|
---|
195 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
|
---|
196 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
197 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
|
---|
198 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
199 | VERR_INVALID_HANDLE);
|
---|
200 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
201 | RT_ASSERT_INTS_ON();
|
---|
202 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
203 |
|
---|
204 | rtR0SemEventDarwinRetain(pThis);
|
---|
205 |
|
---|
206 | /** @todo should probably disable interrupts here... update
|
---|
207 | * semspinmutex-r0drv-generic.c when done. */
|
---|
208 | lck_spin_lock(pThis->pSpinlock);
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * Wake up one thread.
|
---|
212 | */
|
---|
213 | ASMAtomicWriteBool(&pThis->fSignaled, true);
|
---|
214 |
|
---|
215 | PRTSEMEVENTDARWINENTRY pWaiter;
|
---|
216 | RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
|
---|
217 | {
|
---|
218 | if (!pWaiter->fWokenUp)
|
---|
219 | {
|
---|
220 | pWaiter->fWokenUp = true;
|
---|
221 | thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_AWAKENED);
|
---|
222 | ASMAtomicWriteBool(&pThis->fSignaled, false);
|
---|
223 | break;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | lck_spin_unlock(pThis->pSpinlock);
|
---|
228 | rtR0SemEventDarwinRelease(pThis);
|
---|
229 |
|
---|
230 | RT_ASSERT_PREEMPT_CPUID();
|
---|
231 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
232 | return VINF_SUCCESS;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
|
---|
238 | *
|
---|
239 | * @returns VBox status code.
|
---|
240 | * @param pThis The event semaphore.
|
---|
241 | * @param fFlags See RTSemEventWaitEx.
|
---|
242 | * @param uTimeout See RTSemEventWaitEx.
|
---|
243 | * @param pSrcPos The source code position of the wait.
|
---|
244 | */
|
---|
245 | static int rtR0SemEventDarwinWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
246 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
247 | {
|
---|
248 | RT_NOREF(pSrcPos);
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Validate the input.
|
---|
252 | */
|
---|
253 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
254 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
255 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
256 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
257 |
|
---|
258 | rtR0SemEventDarwinRetain(pThis);
|
---|
259 | lck_spin_lock(pThis->pSpinlock);
|
---|
260 |
|
---|
261 | /*
|
---|
262 | * In the signaled state?
|
---|
263 | */
|
---|
264 | int rc;
|
---|
265 | if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
|
---|
266 | rc = VINF_SUCCESS;
|
---|
267 | else
|
---|
268 | {
|
---|
269 | /*
|
---|
270 | * We have to wait. So, we'll need to convert the timeout and figure
|
---|
271 | * out if it's indefinite or not.
|
---|
272 | */
|
---|
273 | uint64_t uNsAbsTimeout = 1;
|
---|
274 | if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
|
---|
275 | {
|
---|
276 | if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
|
---|
277 | uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
|
---|
278 | ? uTimeout * UINT32_C(1000000)
|
---|
279 | : UINT64_MAX;
|
---|
280 | if (uTimeout == UINT64_MAX)
|
---|
281 | fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
|
---|
282 | else
|
---|
283 | {
|
---|
284 | uint64_t u64Now;
|
---|
285 | if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
|
---|
286 | {
|
---|
287 | if (uTimeout != 0)
|
---|
288 | {
|
---|
289 | u64Now = RTTimeSystemNanoTS();
|
---|
290 | uNsAbsTimeout = u64Now + uTimeout;
|
---|
291 | if (uNsAbsTimeout < u64Now) /* overflow */
|
---|
292 | fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
|
---|
293 | }
|
---|
294 | }
|
---|
295 | else
|
---|
296 | {
|
---|
297 | uNsAbsTimeout = uTimeout;
|
---|
298 | u64Now = RTTimeSystemNanoTS();
|
---|
299 | uTimeout = u64Now < uTimeout ? uTimeout - u64Now : 0;
|
---|
300 | }
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | if ( !(fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
|
---|
305 | && uTimeout == 0)
|
---|
306 | {
|
---|
307 | /*
|
---|
308 | * Poll call, we already checked the condition above so no need to
|
---|
309 | * wait for anything.
|
---|
310 | */
|
---|
311 | rc = VERR_TIMEOUT;
|
---|
312 | }
|
---|
313 | else
|
---|
314 | {
|
---|
315 | RTSEMEVENTDARWINENTRY Waiter;
|
---|
316 | Waiter.fWokenUp = false;
|
---|
317 | RTListAppend(&pThis->WaitList, &Waiter.Node);
|
---|
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)&Waiter, 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)&Waiter, fInterruptible, u64AbsTime);
|
---|
335 | }
|
---|
336 |
|
---|
337 | /*
|
---|
338 | * Deal with the wait result.
|
---|
339 | */
|
---|
340 | if (RT_LIKELY(pThis->u32Magic == RTSEMEVENT_MAGIC))
|
---|
341 | {
|
---|
342 | switch (rcWait)
|
---|
343 | {
|
---|
344 | case THREAD_AWAKENED:
|
---|
345 | if (RT_LIKELY(Waiter.fWokenUp))
|
---|
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 = !Waiter.fWokenUp ? VERR_TIMEOUT : VINF_SUCCESS;
|
---|
356 | break;
|
---|
357 |
|
---|
358 | case THREAD_INTERRUPTED:
|
---|
359 | Assert(fInterruptible != THREAD_UNINT);
|
---|
360 | rc = !Waiter.fWokenUp ? VERR_INTERRUPTED : VINF_SUCCESS;
|
---|
361 | break;
|
---|
362 |
|
---|
363 | case THREAD_RESTART:
|
---|
364 | AssertMsg(pThis->u32Magic == ~RTSEMEVENT_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 | RTListNodeRemove(&Waiter.Node);
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | lck_spin_unlock(pThis->pSpinlock);
|
---|
384 | rtR0SemEventDarwinRelease(pThis);
|
---|
385 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
386 | return rc;
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
391 | {
|
---|
392 | #ifndef RTSEMEVENT_STRICT
|
---|
393 | return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, NULL);
|
---|
394 | #else
|
---|
395 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
396 | return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
397 | #endif
|
---|
398 | }
|
---|
399 |
|
---|
400 |
|
---|
401 | RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
402 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
403 | {
|
---|
404 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
405 | return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
406 | }
|
---|
407 |
|
---|
408 |
|
---|
409 | RTDECL(uint32_t) RTSemEventGetResolution(void)
|
---|
410 | {
|
---|
411 | uint64_t cNs;
|
---|
412 | absolutetime_to_nanoseconds(1, &cNs);
|
---|
413 | return (uint32_t)cNs ? (uint32_t)cNs : 0;
|
---|
414 | }
|
---|
415 |
|
---|