VirtualBox

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

Last change on this file since 5335 was 5335, checked in by vboxsync, 18 years ago

all threads must wakeup on destroy; check for the right macro (bug copied from the freebsd implementation); interruptible/non-interruptible waiting.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.4 KB
Line 
1/* $Id: semeventmulti-r0drv-solaris.c 5335 2007-10-16 17:59:15Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Multiple Release Event Semaphores, Ring-0 Driver, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include "the-solaris-kernel.h"
23
24#include <iprt/semaphore.h>
25#include <iprt/alloc.h>
26#include <iprt/asm.h>
27#include <iprt/assert.h>
28#include <iprt/err.h>
29
30#include "internal/magics.h"
31
32
33/*******************************************************************************
34* Structures and Typedefs *
35*******************************************************************************/
36/**
37 * FreeBSD multiple release event semaphore.
38 */
39typedef struct RTSEMEVENTMULTIINTERNAL
40{
41 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
42 uint32_t volatile u32Magic;
43 /** The number of waiting threads. */
44 uint32_t volatile cWaiters;
45 /** Set if the event object is signaled. */
46 uint8_t volatile fSignaled;
47 /** The number of threads in the process of waking up. */
48 uint32_t volatile cWaking;
49 /** The Solaris mutex protecting this structure and pairing up the with the cv. */
50 kmutex_t Mtx;
51 /** The Solaris condition variable. */
52 kcondvar_t Cnd;
53} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
54
55
56RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
57{
58 Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
59 AssertPtrReturn(pEventMultiSem, VERR_INVALID_POINTER);
60
61 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
62 if (pThis)
63 {
64 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
65 pThis->cWaiters = 0;
66 pThis->cWaking = 0;
67 pThis->fSignaled = 0;
68 mutex_init(&pThis->Mtx, "IPRT Multiple Release Event Semaphore", MUTEX_DRIVER, NULL);
69 cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
70 *pEventMultiSem = pThis;
71 return VINF_SUCCESS;
72 }
73 return VERR_NO_MEMORY;
74}
75
76
77RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
78{
79 if (EventMultiSem == NIL_RTSEMEVENTMULTI) /* don't bitch */
80 return VERR_INVALID_HANDLE;
81 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
82 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
83 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
84 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
85 VERR_INVALID_HANDLE);
86
87 mutex_enter(&pThis->Mtx);
88 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
89 if (pThis->cWaiters > 0)
90 {
91 /* abort waiting thread, last man cleans up. */
92 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
93 cv_broadcast(&pThis->Cnd);
94 mutex_exit(&pThis->Mtx);
95 }
96 else if (pThis->cWaking)
97 /* the last waking thread is gonna do the cleanup */
98 mutex_exit(&pThis->Mtx);
99 else
100 {
101 mutex_exit(&pThis->Mtx);
102 cv_destroy(&pThis->Cnd);
103 mutex_destroy(&pThis->Mtx);
104 RTMemFree(pThis);
105 }
106
107 return VINF_SUCCESS;
108}
109
110
111RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
112{
113 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
114 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
115 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
116 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
117 VERR_INVALID_HANDLE);
118
119 mutex_enter(&pThis->Mtx);
120
121 ASMAtomicXchgU8(&pThis->fSignaled, true);
122 if (pThis->cWaiters > 0)
123 {
124 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
125 ASMAtomicXchgU32(&pThis->cWaiters, 0);
126 cv_signal(&pThis->Cnd);
127 }
128
129 mutex_exit(&pThis->Mtx);
130 return VINF_SUCCESS;
131}
132
133
134RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
135{
136 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
137 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
138 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
139 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
140 VERR_INVALID_HANDLE);
141
142 mutex_enter(&pThis->Mtx);
143 ASMAtomicXchgU8(&pThis->fSignaled, false);
144 mutex_exit(&pThis->Mtx);
145 return VINF_SUCCESS;
146}
147
148
149static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fInterruptible)
150{
151 int rc;
152 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
153 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
154 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
155 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
156 VERR_INVALID_HANDLE);
157
158 mutex_enter(&pThis->Mtx);
159
160 if (pThis->fSignaled)
161 rc = VINF_SUCCESS;
162 else
163 {
164 /*
165 * Translate milliseconds into ticks and go to sleep.
166 */
167 int cTicks;
168 clock_t timeout;
169 if (cMillies != RT_INDEFINITE_WAIT)
170 cTicks = drv_usectohz((clock_t)(cMillies * 1000L));
171 else
172 cTicks = 0;
173
174 timeout = ddi_get_lbolt();
175 timeout += cTicks;
176
177 ASMAtomicIncU32(&pThis->cWaiters);
178
179 if (fInterruptible)
180 rc = cv_timedwait_sig(&pThis->Cnd, &pThis->Mtx, timeout);
181 else
182 rc = cv_timedwait(&pThis->Cnd, &pThis->Mtx, timeout);
183 if (rc > 0)
184 {
185 /* Retured due to call to cv_signal() or cv_broadcast() */
186 if (RT_LIKELY(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC))
187 rc = VINF_SUCCESS;
188 else
189 {
190 rc = VERR_SEM_DESTROYED;
191 if (!ASMAtomicDecU32(&pThis->cWaking))
192 {
193 mutex_exit(&pThis->Mtx);
194 cv_destroy(&pThis->Cnd);
195 mutex_destroy(&pThis->Mtx);
196 RTMemFree(pThis);
197 return rc;
198 }
199 }
200 ASMAtomicDecU32(&pThis->cWaking);
201 }
202 else if (rc == -1)
203 {
204 /* Returned due to timeout being reached */
205 if (pThis->cWaiters > 0)
206 ASMAtomicDecU32(&pThis->cWaiters);
207 rc = VERR_TIMEOUT;
208 }
209 else
210 {
211 /* Returned due to pending signal */
212 if (pThis->cWaiters > 0)
213 ASMAtomicDecU32(&pThis->cWaiters);
214 rc = VERR_INTERRUPTED;
215 }
216 }
217
218 mutex_exit(&pThis->Mtx);
219 return rc;
220}
221
222
223RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
224{
225 return rtSemEventMultiWait(EventMultiSem, cMillies, false /* not interruptible */);
226}
227
228
229RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
230{
231 return rtSemEventMultiWait(EventMultiSem, cMillies, true /* interruptible */);
232}
233
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette