VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/semeventmulti-r0drv-solaris.c@ 22073

Last change on this file since 22073 was 22073, checked in by vboxsync, 16 years ago

iprt/r0drv/solaris: context assertions (RT_MORE_STRICT).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 KB
Line 
1/* $Id: semeventmulti-r0drv-solaris.c 22073 2009-08-07 15:26:56Z vboxsync $ */
2/** @file
3 * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-solaris-kernel.h"
36#include "internal/iprt.h"
37#include <iprt/semaphore.h>
38
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/err.h>
42#include <iprt/mem.h>
43#include <iprt/mp.h>
44#include <iprt/thread.h>
45#include "internal/magics.h"
46
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51/**
52 * FreeBSD 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 Solaris mutex protecting this structure and pairing up the with the cv. */
65 kmutex_t Mtx;
66 /** The Solaris condition variable. */
67 kcondvar_t Cnd;
68} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
69
70
71
72RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
73{
74 Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
75 AssertPtrReturn(pEventMultiSem, VERR_INVALID_POINTER);
76 RT_ASSERT_PREEMPTIBLE();
77
78 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
79 if (pThis)
80 {
81 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
82 pThis->cWaiters = 0;
83 pThis->cWaking = 0;
84 pThis->fSignaled = 0;
85 mutex_init(&pThis->Mtx, "IPRT Multiple Release Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
86 cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
87 *pEventMultiSem = pThis;
88 return VINF_SUCCESS;
89 }
90 return VERR_NO_MEMORY;
91}
92
93
94RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
95{
96 if (EventMultiSem == NIL_RTSEMEVENTMULTI) /* don't bitch */
97 return VERR_INVALID_HANDLE;
98 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
99 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
100 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
101 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
102 VERR_INVALID_HANDLE);
103 RT_ASSERT_INTS_ON();
104
105 mutex_enter(&pThis->Mtx);
106 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
107 if (pThis->cWaiters > 0)
108 {
109 /* abort waiting thread, last man cleans up. */
110 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
111 cv_broadcast(&pThis->Cnd);
112 mutex_exit(&pThis->Mtx);
113 }
114 else if (pThis->cWaking)
115 /* the last waking thread is gonna do the cleanup */
116 mutex_exit(&pThis->Mtx);
117 else
118 {
119 mutex_exit(&pThis->Mtx);
120 cv_destroy(&pThis->Cnd);
121 mutex_destroy(&pThis->Mtx);
122 RTMemFree(pThis);
123 }
124
125 return VINF_SUCCESS;
126}
127
128
129RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
130{
131 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
132 RT_ASSERT_PREEMPT_CPUID_VAR();
133
134 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
135 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
136 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
137 VERR_INVALID_HANDLE);
138 RT_ASSERT_INTS_ON();
139
140 mutex_enter(&pThis->Mtx);
141
142 ASMAtomicXchgU8(&pThis->fSignaled, true);
143 if (pThis->cWaiters > 0)
144 {
145 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
146 ASMAtomicXchgU32(&pThis->cWaiters, 0);
147 cv_broadcast(&pThis->Cnd);
148 }
149
150 mutex_exit(&pThis->Mtx);
151
152 RT_ASSERT_PREEMPT_CPUID();
153 return VINF_SUCCESS;
154}
155
156
157RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
158{
159 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
160 RT_ASSERT_PREEMPT_CPUID_VAR();
161
162 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
163 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
164 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
165 VERR_INVALID_HANDLE);
166 RT_ASSERT_INTS_ON();
167
168 mutex_enter(&pThis->Mtx);
169 ASMAtomicXchgU8(&pThis->fSignaled, false);
170 mutex_exit(&pThis->Mtx);
171
172 RT_ASSERT_PREEMPT_CPUID();
173 return VINF_SUCCESS;
174}
175
176
177static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fInterruptible)
178{
179 int rc;
180 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
181 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
182 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
183 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
184 VERR_INVALID_HANDLE);
185 if (cMillies)
186 RT_ASSERT_PREEMPTIBLE();
187
188 mutex_enter(&pThis->Mtx);
189
190 if (pThis->fSignaled)
191 rc = VINF_SUCCESS;
192 else if (!cMillies)
193 rc = VERR_TIMEOUT;
194 else
195 {
196 ASMAtomicIncU32(&pThis->cWaiters);
197
198 /*
199 * Translate milliseconds into ticks and go to sleep.
200 */
201 if (cMillies != RT_INDEFINITE_WAIT)
202 {
203 clock_t cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
204 clock_t timeout = ddi_get_lbolt();
205 timeout += cTicks;
206 if (fInterruptible)
207 rc = cv_timedwait_sig(&pThis->Cnd, &pThis->Mtx, timeout);
208 else
209 rc = cv_timedwait(&pThis->Cnd, &pThis->Mtx, timeout);
210 }
211 else
212 {
213 if (fInterruptible)
214 rc = cv_wait_sig(&pThis->Cnd, &pThis->Mtx);
215 else
216 {
217 cv_wait(&pThis->Cnd, &pThis->Mtx);
218 rc = 1;
219 }
220 }
221 if (rc > 0)
222 {
223 /* Retured due to call to cv_signal() or cv_broadcast() */
224 if (RT_LIKELY(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC))
225 rc = VINF_SUCCESS;
226 else
227 {
228 rc = VERR_SEM_DESTROYED;
229 if (!ASMAtomicDecU32(&pThis->cWaking))
230 {
231 mutex_exit(&pThis->Mtx);
232 cv_destroy(&pThis->Cnd);
233 mutex_destroy(&pThis->Mtx);
234 RTMemFree(pThis);
235 return rc;
236 }
237 }
238 ASMAtomicDecU32(&pThis->cWaking);
239 }
240 else if (rc == -1)
241 {
242 /* Returned due to timeout being reached */
243 if (pThis->cWaiters > 0)
244 ASMAtomicDecU32(&pThis->cWaiters);
245 rc = VERR_TIMEOUT;
246 }
247 else
248 {
249 /* Returned due to pending signal */
250 if (pThis->cWaiters > 0)
251 ASMAtomicDecU32(&pThis->cWaiters);
252 rc = VERR_INTERRUPTED;
253 }
254 }
255
256 mutex_exit(&pThis->Mtx);
257 return rc;
258}
259
260
261RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
262{
263 return rtSemEventMultiWait(EventMultiSem, cMillies, false /* not interruptible */);
264}
265
266
267RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
268{
269 return rtSemEventMultiWait(EventMultiSem, cMillies, true /* interruptible */);
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