VirtualBox

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

Last change on this file since 32043 was 29255, checked in by vboxsync, 15 years ago

darwin+asm.h build fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.0 KB
Line 
1/* $Id: semevent-r0drv-darwin.cpp 29255 2010-05-09 18:11:24Z vboxsync $ */
2/** @file
3 * IPRT - 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/mem.h>
42#include <iprt/mp.h>
43#include <iprt/thread.h>
44
45#include "internal/magics.h"
46
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51/**
52 * Darwin event semaphore.
53 */
54typedef struct RTSEMEVENTINTERNAL
55{
56 /** Magic value (RTSEMEVENT_MAGIC). */
57 uint32_t volatile u32Magic;
58 /** The number of waiting threads. */
59 uint32_t volatile cWaiters;
60 /** Set if the event object is signaled. */
61 uint8_t volatile fSignaled;
62 /** The number of threads in the process of waking up. */
63 uint32_t volatile cWaking;
64 /** The spinlock protecting us. */
65 lck_spin_t *pSpinlock;
66} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
67
68
69
70RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
71{
72 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
73}
74
75
76RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
77{
78 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
79 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
80 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
81 RT_ASSERT_PREEMPTIBLE();
82
83 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
84 if (pThis)
85 {
86 pThis->u32Magic = RTSEMEVENT_MAGIC;
87 pThis->cWaiters = 0;
88 pThis->cWaking = 0;
89 pThis->fSignaled = 0;
90 Assert(g_pDarwinLockGroup);
91 pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
92 if (pThis->pSpinlock)
93 {
94 *phEventSem = pThis;
95 return VINF_SUCCESS;
96 }
97
98 pThis->u32Magic = 0;
99 RTMemFree(pThis);
100 }
101 return VERR_NO_MEMORY;
102}
103
104
105RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
106{
107 PRTSEMEVENTINTERNAL pThis = hEventSem;
108 if (pThis == NIL_RTSEMEVENT)
109 return VINF_SUCCESS;
110 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
111 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
112 RT_ASSERT_INTS_ON();
113
114 lck_spin_lock(pThis->pSpinlock);
115 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
116 if (pThis->cWaiters > 0)
117 {
118 /* abort waiting thread, last man cleans up. */
119 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
120 thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_RESTART);
121 lck_spin_unlock(pThis->pSpinlock);
122 }
123 else if (pThis->cWaking)
124 /* the last waking thread is gonna do the cleanup */
125 lck_spin_unlock(pThis->pSpinlock);
126 else
127 {
128 lck_spin_unlock(pThis->pSpinlock);
129 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
130 RTMemFree(pThis);
131 }
132
133 return VINF_SUCCESS;
134}
135
136
137RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
138{
139 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
140 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
141 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
142 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
143 VERR_INVALID_HANDLE);
144 RT_ASSERT_PREEMPT_CPUID_VAR();
145 RT_ASSERT_INTS_ON();
146
147 /** @todo should probably disable interrupts here... update
148 * semspinmutex-r0drv-generic.c when done. */
149 lck_spin_lock(pThis->pSpinlock);
150
151 if (pThis->cWaiters > 0)
152 {
153 ASMAtomicDecU32(&pThis->cWaiters);
154 ASMAtomicIncU32(&pThis->cWaking);
155 thread_wakeup_prim((event_t)pThis, TRUE /* one thread */, THREAD_AWAKENED);
156 /** @todo this isn't safe. a scheduling interrupt on the other cpu while we're in here
157 * could cause the thread to be timed out before we manage to wake it up and the event
158 * ends up in the wrong state. ditto for posix signals.
159 * Update: check the return code; it will return KERN_NOT_WAITING if no one is around. */
160 }
161 else
162 ASMAtomicXchgU8(&pThis->fSignaled, true);
163
164 lck_spin_unlock(pThis->pSpinlock);
165
166 RT_ASSERT_PREEMPT_CPUID();
167 return VINF_SUCCESS;
168}
169
170
171static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, wait_interrupt_t fInterruptible)
172{
173 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
174 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
175 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
176 if (cMillies)
177 RT_ASSERT_PREEMPTIBLE();
178
179 lck_spin_lock(pThis->pSpinlock);
180
181 int rc;
182 if (pThis->fSignaled)
183 {
184 Assert(!pThis->cWaiters);
185 ASMAtomicXchgU8(&pThis->fSignaled, false);
186 rc = VINF_SUCCESS;
187 }
188 else if (!cMillies)
189 rc = VERR_TIMEOUT;
190 else
191 {
192 ASMAtomicIncU32(&pThis->cWaiters);
193
194 wait_result_t rcWait;
195 if (cMillies == RT_INDEFINITE_WAIT)
196 rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pThis, fInterruptible);
197 else
198 {
199 uint64_t u64AbsTime;
200 nanoseconds_to_absolutetime(cMillies * UINT64_C(1000000), &u64AbsTime);
201 u64AbsTime += mach_absolute_time();
202
203 rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
204 (event_t)pThis, fInterruptible, u64AbsTime);
205 }
206 switch (rcWait)
207 {
208 case THREAD_AWAKENED:
209 Assert(pThis->cWaking > 0);
210 if ( !ASMAtomicDecU32(&pThis->cWaking)
211 && pThis->u32Magic != RTSEMEVENT_MAGIC)
212 {
213 /* the event was destroyed after we woke up, as the last thread do the cleanup. */
214 lck_spin_unlock(pThis->pSpinlock);
215 Assert(g_pDarwinLockGroup);
216 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
217 RTMemFree(pThis);
218 return VINF_SUCCESS;
219 }
220 rc = VINF_SUCCESS;
221 break;
222
223 case THREAD_TIMED_OUT:
224 Assert(cMillies != RT_INDEFINITE_WAIT);
225 ASMAtomicDecU32(&pThis->cWaiters);
226 rc = VERR_TIMEOUT;
227 break;
228
229 case THREAD_INTERRUPTED:
230 Assert(fInterruptible);
231 ASMAtomicDecU32(&pThis->cWaiters);
232 rc = VERR_INTERRUPTED;
233 break;
234
235 case THREAD_RESTART:
236 /* Last one out does the cleanup. */
237 if (!ASMAtomicDecU32(&pThis->cWaking))
238 {
239 lck_spin_unlock(pThis->pSpinlock);
240 Assert(g_pDarwinLockGroup);
241 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
242 RTMemFree(pThis);
243 return VERR_SEM_DESTROYED;
244 }
245
246 rc = VERR_SEM_DESTROYED;
247 break;
248
249 default:
250 AssertMsgFailed(("rcWait=%d\n", rcWait));
251 rc = VERR_GENERAL_FAILURE;
252 break;
253 }
254 }
255
256 lck_spin_unlock(pThis->pSpinlock);
257 return rc;
258}
259
260
261RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
262{
263 return rtSemEventWait(hEventSem, cMillies, THREAD_UNINT);
264}
265
266
267RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
268{
269 return rtSemEventWait(hEventSem, cMillies, THREAD_ABORTSAFE);
270}
271
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