VirtualBox

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

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

iprt/darwin/r0drv: More saving of EFLAGS/AC, seems anything that may block is evil (not a surprise).

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