VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/semevent-r0drv-solaris.c@ 20793

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

semevent*-r0drv-solaris.c(+spinlock-r0drv-solaris.c+the-solaris-kernel.h): Protect the CV with a spinlock to make it possible to signal the sem with preemption disabled.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/* $Id: semevent-r0drv-solaris.c 20793 2009-06-22 17:05:03Z vboxsync $ */
2/** @file
3 * IPRT - 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* Header Files *
33*******************************************************************************/
34#include "the-solaris-kernel.h"
35
36#include <iprt/semaphore.h>
37#include <iprt/alloc.h>
38#include <iprt/assert.h>
39#include <iprt/asm.h>
40#include <iprt/err.h>
41
42#include "internal/magics.h"
43
44
45/*******************************************************************************
46* Structures and Typedefs *
47*******************************************************************************/
48/**
49 * Solaris event semaphore.
50 */
51typedef struct RTSEMEVENTINTERNAL
52{
53 /** Magic value (RTSEMEVENT_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 Solaris mutex protecting this structure and pairing up the with the cv. */
62 kmutex_t Mtx;
63 /** The Solaris condition variable. */
64 kcondvar_t Cnd;
65} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
66
67
68RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
69{
70 Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
71 AssertPtrReturn(pEventSem, VERR_INVALID_POINTER);
72
73 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
74 if (pEventInt)
75 {
76 pEventInt->u32Magic = RTSEMEVENT_MAGIC;
77 pEventInt->cWaiters = 0;
78 pEventInt->cWaking = 0;
79 pEventInt->fSignaled = 0;
80 mutex_init(&pEventInt->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
81 cv_init(&pEventInt->Cnd, "IPRT CV", CV_DRIVER, NULL);
82 *pEventSem = pEventInt;
83 return VINF_SUCCESS;
84 }
85 return VERR_NO_MEMORY;
86}
87
88
89RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
90{
91 if (EventSem == NIL_RTSEMEVENT)
92 return VERR_INVALID_HANDLE;
93 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
94 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
95 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
96 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
97 VERR_INVALID_HANDLE);
98
99 mutex_enter(&pEventInt->Mtx);
100 ASMAtomicIncU32(&pEventInt->u32Magic); /* make the handle invalid */
101 if (pEventInt->cWaiters > 0)
102 {
103 /* abort waiting thread, last man cleans up. */
104 ASMAtomicXchgU32(&pEventInt->cWaking, pEventInt->cWaking + pEventInt->cWaiters);
105 cv_broadcast(&pEventInt->Cnd);
106 mutex_exit(&pEventInt->Mtx);
107 }
108 else if (pEventInt->cWaking)
109 {
110 /* the last waking thread is gonna do the cleanup */
111 mutex_exit(&pEventInt->Mtx);
112 }
113 else
114 {
115 mutex_exit(&pEventInt->Mtx);
116 cv_destroy(&pEventInt->Cnd);
117 mutex_destroy(&pEventInt->Mtx);
118 RTMemFree(pEventInt);
119 }
120
121 return VINF_SUCCESS;
122}
123
124
125RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
126{
127 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
128 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
129 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
130 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
131 VERR_INVALID_HANDLE);
132
133 mutex_enter(&pEventInt->Mtx);
134
135 if (pEventInt->cWaiters > 0)
136 {
137 ASMAtomicDecU32(&pEventInt->cWaiters);
138 ASMAtomicIncU32(&pEventInt->cWaking);
139 cv_signal(&pEventInt->Cnd);
140 }
141 else
142 ASMAtomicXchgU8(&pEventInt->fSignaled, true);
143
144 mutex_exit(&pEventInt->Mtx);
145 return VINF_SUCCESS;
146}
147
148static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)
149{
150 int rc;
151 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
152 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
153 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
154 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
155 VERR_INVALID_HANDLE);
156
157 mutex_enter(&pEventInt->Mtx);
158
159 if (pEventInt->fSignaled)
160 {
161 Assert(!pEventInt->cWaiters);
162 ASMAtomicXchgU8(&pEventInt->fSignaled, false);
163 rc = VINF_SUCCESS;
164 }
165 else
166 {
167 ASMAtomicIncU32(&pEventInt->cWaiters);
168
169 /*
170 * Translate milliseconds into ticks and go to sleep.
171 */
172 if (cMillies != RT_INDEFINITE_WAIT)
173 {
174 clock_t cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
175 clock_t timeout = ddi_get_lbolt();
176 timeout += cTicks;
177 if (fInterruptible)
178 rc = cv_timedwait_sig(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
179 else
180 rc = cv_timedwait(&pEventInt->Cnd, &pEventInt->Mtx, timeout);
181 }
182 else
183 {
184 if (fInterruptible)
185 rc = cv_wait_sig(&pEventInt->Cnd, &pEventInt->Mtx);
186 else
187 {
188 cv_wait(&pEventInt->Cnd, &pEventInt->Mtx);
189 rc = 1;
190 }
191 }
192
193 if (rc > 0)
194 {
195 /* Retured due to call to cv_signal() or cv_broadcast() */
196 if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
197 {
198 rc = VERR_SEM_DESTROYED;
199 if (!ASMAtomicDecU32(&pEventInt->cWaking))
200 {
201 mutex_exit(&pEventInt->Mtx);
202 cv_destroy(&pEventInt->Cnd);
203 mutex_destroy(&pEventInt->Mtx);
204 RTMemFree(pEventInt);
205 return rc;
206 }
207 }
208
209 ASMAtomicDecU32(&pEventInt->cWaking);
210 rc = VINF_SUCCESS;
211 }
212 else if (rc == -1)
213 {
214 /* Returned due to timeout being reached */
215 if (pEventInt->cWaiters > 0)
216 ASMAtomicDecU32(&pEventInt->cWaiters);
217 rc = VERR_TIMEOUT;
218 }
219 else
220 {
221 /* Returned due to pending signal */
222 if (pEventInt->cWaiters > 0)
223 ASMAtomicDecU32(&pEventInt->cWaiters);
224 rc = VERR_INTERRUPTED;
225 }
226 }
227
228 mutex_exit(&pEventInt->Mtx);
229 return rc;
230}
231
232
233RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
234{
235 return rtSemEventWait(EventSem, cMillies, false /* not interruptible */);
236}
237
238
239RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
240{
241 return rtSemEventWait(EventSem, cMillies, true /* interruptible */);
242}
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