VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/semeventmulti-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.4 KB
Line 
1/* $Id: semeventmulti-r0drv-darwin.cpp 29255 2010-05-09 18:11:24Z vboxsync $ */
2/** @file
3 * IPRT - Multiple Release 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 multiple release event semaphore.
53 */
54typedef struct RTSEMEVENTMULTIINTERNAL
55{
56 /** Magic value (RTSEMEVENTMULTI_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} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
67
68
69
70RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
71{
72 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
73}
74
75
76RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
77 const char *pszNameFmt, ...)
78{
79 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
80 AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
81 AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
82 RT_ASSERT_PREEMPTIBLE();
83
84 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
85 if (pThis)
86 {
87 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
88 pThis->cWaiters = 0;
89 pThis->cWaking = 0;
90 pThis->fSignaled = 0;
91 Assert(g_pDarwinLockGroup);
92 pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
93 if (pThis->pSpinlock)
94 {
95 *phEventMultiSem = pThis;
96 return VINF_SUCCESS;
97 }
98
99 pThis->u32Magic = 0;
100 RTMemFree(pThis);
101 }
102 return VERR_NO_MEMORY;
103}
104
105
106RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
107{
108 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
109 if (pThis == NIL_RTSEMEVENTMULTI)
110 return VINF_SUCCESS;
111 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
112 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
113 RT_ASSERT_INTS_ON();
114
115 lck_spin_lock(pThis->pSpinlock);
116 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
117 if (pThis->cWaiters > 0)
118 {
119 /* abort waiting thread, last man cleans up. */
120 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
121 thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_RESTART);
122 lck_spin_unlock(pThis->pSpinlock);
123 }
124 else if (pThis->cWaking)
125 /* the last waking thread is gonna do the cleanup */
126 lck_spin_unlock(pThis->pSpinlock);
127 else
128 {
129 lck_spin_unlock(pThis->pSpinlock);
130 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
131 RTMemFree(pThis);
132 }
133
134 return VINF_SUCCESS;
135}
136
137
138RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
139{
140 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
141 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
142 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
143 RT_ASSERT_PREEMPT_CPUID_VAR();
144 RT_ASSERT_INTS_ON();
145
146 lck_spin_lock(pThis->pSpinlock);
147
148 ASMAtomicXchgU8(&pThis->fSignaled, true);
149 if (pThis->cWaiters > 0)
150 {
151 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
152 ASMAtomicXchgU32(&pThis->cWaiters, 0);
153 thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_AWAKENED);
154 }
155
156 lck_spin_unlock(pThis->pSpinlock);
157
158 RT_ASSERT_PREEMPT_CPUID();
159 return VINF_SUCCESS;
160}
161
162
163RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
164{
165 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
166 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
167 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
168 RT_ASSERT_PREEMPT_CPUID_VAR();
169 RT_ASSERT_INTS_ON();
170
171 lck_spin_lock(pThis->pSpinlock);
172 ASMAtomicXchgU8(&pThis->fSignaled, false);
173 lck_spin_unlock(pThis->pSpinlock);
174
175 RT_ASSERT_PREEMPT_CPUID();
176 return VINF_SUCCESS;
177}
178
179
180static int rtSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies, wait_interrupt_t fInterruptible)
181{
182 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
183 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
184 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
185 if (cMillies)
186 RT_ASSERT_PREEMPTIBLE();
187
188 lck_spin_lock(pThis->pSpinlock);
189
190 int rc;
191 if (pThis->fSignaled)
192 rc = VINF_SUCCESS;
193 else if (!cMillies)
194 rc = VERR_TIMEOUT;
195 else
196 {
197 ASMAtomicIncU32(&pThis->cWaiters);
198
199 wait_result_t rcWait;
200 if (cMillies == RT_INDEFINITE_WAIT)
201 rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pThis, fInterruptible);
202 else
203 {
204 uint64_t u64AbsTime;
205 nanoseconds_to_absolutetime(cMillies * UINT64_C(1000000), &u64AbsTime);
206 u64AbsTime += mach_absolute_time();
207
208 rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
209 (event_t)pThis, fInterruptible, u64AbsTime);
210 }
211 switch (rcWait)
212 {
213 case THREAD_AWAKENED:
214 Assert(pThis->cWaking > 0);
215 if ( !ASMAtomicDecU32(&pThis->cWaking)
216 && pThis->u32Magic != RTSEMEVENTMULTI_MAGIC)
217 {
218 /* the event was destroyed after we woke up, as the last thread do the cleanup. */
219 lck_spin_unlock(pThis->pSpinlock);
220 Assert(g_pDarwinLockGroup);
221 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
222 RTMemFree(pThis);
223 return VINF_SUCCESS;
224 }
225 rc = VINF_SUCCESS;
226 break;
227
228 case THREAD_TIMED_OUT:
229 Assert(cMillies != RT_INDEFINITE_WAIT);
230 ASMAtomicDecU32(&pThis->cWaiters);
231 rc = VERR_TIMEOUT;
232 break;
233
234 case THREAD_INTERRUPTED:
235 Assert(fInterruptible);
236 ASMAtomicDecU32(&pThis->cWaiters);
237 rc = VERR_INTERRUPTED;
238 break;
239
240 case THREAD_RESTART:
241 /* Last one out does the cleanup. */
242 if (!ASMAtomicDecU32(&pThis->cWaking))
243 {
244 lck_spin_unlock(pThis->pSpinlock);
245 Assert(g_pDarwinLockGroup);
246 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
247 RTMemFree(pThis);
248 return VERR_SEM_DESTROYED;
249 }
250
251 rc = VERR_SEM_DESTROYED;
252 break;
253
254 default:
255 AssertMsgFailed(("rcWait=%d\n", rcWait));
256 rc = VERR_GENERAL_FAILURE;
257 break;
258 }
259 }
260
261 lck_spin_unlock(pThis->pSpinlock);
262 return rc;
263}
264
265
266RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
267{
268 return rtSemEventMultiWait(hEventMultiSem, cMillies, THREAD_UNINT);
269}
270
271
272RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
273{
274 return rtSemEventMultiWait(hEventMultiSem, cMillies, THREAD_ABORTSAFE);
275}
276
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