VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/semevent-r0drv-darwin.cpp@ 75704

Last change on this file since 75704 was 75704, checked in by vboxsync, 6 years ago

IPRT/r0drv/darwin: RTSemEventSignal and RTSemEventMultiSignal seems to work fine with interrupts disabled and/or from interrupt context. bugref:4686

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 14.4 KB
Line 
1/* $Id: semevent-r0drv-darwin.cpp 75704 2018-11-25 01:44:09Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event Semaphores, Ring-0 Driver, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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 */
58typedef 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. */
66typedef RTSEMEVENTDARWINENTRY *PRTSEMEVENTDARWINENTRY;
67
68
69/**
70 * Darwin event semaphore.
71 */
72typedef 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
90RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
91{
92 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
93}
94
95
96RTDECL(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 */
136DECLINLINE(void) rtR0SemEventDarwinRetain(PRTSEMEVENTINTERNAL pThis)
137{
138 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
139 Assert(cRefs && cRefs < 100000); RT_NOREF_PV(cRefs);
140}
141
142
143/**
144 * Release a reference, destroy the thing if necessary.
145 *
146 * @param pThis The semaphore.
147 */
148DECLINLINE(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
162RTDECL(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
193RTDECL(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
202 /*
203 * Coming here with interrupts disabled should be okay. The thread_wakeup_prim KPI is used
204 * by the interrupt handler IOFilterInterruptEventSource::disableInterruptOccurred() via
205 * signalWorkAvailable(). The only problem is if we have to destroy the event structure,
206 * as RTMemFree does not work with interrupts disabled (IOFree/kfree takes zone mutex).
207 */
208 //RT_ASSERT_INTS_ON(); - we may be called from interrupt context, which seems to be perfectly fine.
209 IPRT_DARWIN_SAVE_EFL_AC();
210
211 rtR0SemEventDarwinRetain(pThis);
212
213 /** @todo should probably disable interrupts here... update
214 * semspinmutex-r0drv-generic.c when done. */
215 lck_spin_lock(pThis->pSpinlock);
216
217 /*
218 * Wake up one thread.
219 */
220 ASMAtomicWriteBool(&pThis->fSignaled, true);
221
222 PRTSEMEVENTDARWINENTRY pWaiter;
223 RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTDARWINENTRY, Node)
224 {
225 if (!pWaiter->fWokenUp)
226 {
227 pWaiter->fWokenUp = true;
228 thread_wakeup_prim((event_t)pWaiter, FALSE /* all threads */, THREAD_AWAKENED);
229 ASMAtomicWriteBool(&pThis->fSignaled, false);
230 break;
231 }
232 }
233
234 lck_spin_unlock(pThis->pSpinlock);
235 rtR0SemEventDarwinRelease(pThis);
236
237 RT_ASSERT_PREEMPT_CPUID();
238 AssertMsg((fSavedEfl & X86_EFL_IF) == (ASMGetFlags() & X86_EFL_IF), ("fSavedEfl=%#x cur=%#x\n",(uint32_t)fSavedEfl, ASMGetFlags()));
239 IPRT_DARWIN_RESTORE_EFL_AC();
240 return VINF_SUCCESS;
241}
242
243
244/**
245 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
246 *
247 * @returns VBox status code.
248 * @param pThis The event semaphore.
249 * @param fFlags See RTSemEventWaitEx.
250 * @param uTimeout See RTSemEventWaitEx.
251 * @param pSrcPos The source code position of the wait.
252 */
253static int rtR0SemEventDarwinWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
254 PCRTLOCKVALSRCPOS pSrcPos)
255{
256 RT_NOREF(pSrcPos);
257
258 /*
259 * Validate the input.
260 */
261 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
262 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
263 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
264 IPRT_DARWIN_SAVE_EFL_AC();
265
266 rtR0SemEventDarwinRetain(pThis);
267 lck_spin_lock(pThis->pSpinlock);
268
269 /*
270 * In the signaled state?
271 */
272 int rc;
273 if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
274 rc = VINF_SUCCESS;
275 else
276 {
277 /*
278 * We have to wait. So, we'll need to convert the timeout and figure
279 * out if it's indefinite or not.
280 */
281 uint64_t uNsAbsTimeout = 1;
282 if (!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE))
283 {
284 if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
285 uTimeout = uTimeout < UINT64_MAX / UINT32_C(1000000) * UINT32_C(1000000)
286 ? uTimeout * UINT32_C(1000000)
287 : UINT64_MAX;
288 if (uTimeout == UINT64_MAX)
289 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
290 else
291 {
292 uint64_t u64Now;
293 if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
294 {
295 if (uTimeout != 0)
296 {
297 u64Now = RTTimeSystemNanoTS();
298 uNsAbsTimeout = u64Now + uTimeout;
299 if (uNsAbsTimeout < u64Now) /* overflow */
300 fFlags |= RTSEMWAIT_FLAGS_INDEFINITE;
301 }
302 }
303 else
304 {
305 uNsAbsTimeout = uTimeout;
306 u64Now = RTTimeSystemNanoTS();
307 uTimeout = u64Now < uTimeout ? uTimeout - u64Now : 0;
308 }
309 }
310 }
311
312 if ( !(fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
313 && uTimeout == 0)
314 {
315 /*
316 * Poll call, we already checked the condition above so no need to
317 * wait for anything.
318 */
319 rc = VERR_TIMEOUT;
320 }
321 else
322 {
323 RTSEMEVENTDARWINENTRY Waiter;
324 Waiter.fWokenUp = false;
325 RTListAppend(&pThis->WaitList, &Waiter.Node);
326
327 for (;;)
328 {
329 /*
330 * Do the actual waiting.
331 */
332 ASMAtomicWriteBool(&pThis->fHaveBlockedThreads, true);
333 wait_interrupt_t fInterruptible = fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE ? THREAD_ABORTSAFE : THREAD_UNINT;
334 wait_result_t rcWait;
335 if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
336 rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)&Waiter, fInterruptible);
337 else
338 {
339 uint64_t u64AbsTime;
340 nanoseconds_to_absolutetime(uNsAbsTimeout, &u64AbsTime);
341 rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
342 (event_t)&Waiter, fInterruptible, u64AbsTime);
343 }
344
345 /*
346 * Deal with the wait result.
347 */
348 if (RT_LIKELY(pThis->u32Magic == RTSEMEVENT_MAGIC))
349 {
350 switch (rcWait)
351 {
352 case THREAD_AWAKENED:
353 if (RT_LIKELY(Waiter.fWokenUp))
354 rc = VINF_SUCCESS;
355 else if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
356 rc = VERR_INTERRUPTED;
357 else
358 continue; /* Seen this happen after fork/exec/something. */
359 break;
360
361 case THREAD_TIMED_OUT:
362 Assert(!(fFlags & RTSEMWAIT_FLAGS_INDEFINITE));
363 rc = !Waiter.fWokenUp ? VERR_TIMEOUT : VINF_SUCCESS;
364 break;
365
366 case THREAD_INTERRUPTED:
367 Assert(fInterruptible != THREAD_UNINT);
368 rc = !Waiter.fWokenUp ? VERR_INTERRUPTED : VINF_SUCCESS;
369 break;
370
371 case THREAD_RESTART:
372 AssertMsg(pThis->u32Magic == ~RTSEMEVENT_MAGIC, ("%#x\n", pThis->u32Magic));
373 rc = VERR_SEM_DESTROYED;
374 break;
375
376 default:
377 AssertMsgFailed(("rcWait=%d\n", rcWait));
378 rc = VERR_INTERNAL_ERROR_3;
379 break;
380 }
381 }
382 else
383 rc = VERR_SEM_DESTROYED;
384 break;
385 }
386
387 RTListNodeRemove(&Waiter.Node);
388 }
389 }
390
391 lck_spin_unlock(pThis->pSpinlock);
392 rtR0SemEventDarwinRelease(pThis);
393 IPRT_DARWIN_RESTORE_EFL_AC();
394 return rc;
395}
396
397
398RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
399{
400#ifndef RTSEMEVENT_STRICT
401 return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, NULL);
402#else
403 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
404 return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
405#endif
406}
407
408
409RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
410 RTHCUINTPTR uId, RT_SRC_POS_DECL)
411{
412 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
413 return rtR0SemEventDarwinWait(hEventSem, fFlags, uTimeout, &SrcPos);
414}
415
416
417RTDECL(uint32_t) RTSemEventGetResolution(void)
418{
419 uint64_t cNs;
420 absolutetime_to_nanoseconds(1, &cNs);
421 return (uint32_t)cNs ? (uint32_t)cNs : 0;
422}
423
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