VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/semeventmulti-r0drv-darwin.cpp@ 28800

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

Automated rebranding to Oracle copyright/license strings via filemuncher

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