1 | /* $Id: semeventmulti-r0drv-freebsd.c 19565 2009-05-10 22:32:03Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include "the-freebsd-kernel.h"
|
---|
35 |
|
---|
36 | #include <iprt/semaphore.h>
|
---|
37 | #include <iprt/alloc.h>
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 |
|
---|
42 | #include "internal/magics.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Structures and Typedefs *
|
---|
47 | *******************************************************************************/
|
---|
48 | /**
|
---|
49 | * FreeBSD multiple release event semaphore.
|
---|
50 | */
|
---|
51 | typedef 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 FreeBSD spinlock protecting this structure. */
|
---|
62 | struct mtx Mtx;
|
---|
63 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
64 |
|
---|
65 |
|
---|
66 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
|
---|
67 | {
|
---|
68 | Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
69 | AssertPtrReturn(pEventMultiSem, VERR_INVALID_POINTER);
|
---|
70 |
|
---|
71 | PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)RTMemAllocZ(sizeof(*pEventMultiInt));
|
---|
72 | if (pEventMultiInt)
|
---|
73 | {
|
---|
74 | pEventMultiInt->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
75 | pEventMultiInt->cWaiters = 0;
|
---|
76 | pEventMultiInt->cWaking = 0;
|
---|
77 | pEventMultiInt->fSignaled = 0;
|
---|
78 | mtx_init(&pEventMultiInt->Mtx, "IPRT Multiple Release Event Semaphore", NULL, MTX_SPIN);
|
---|
79 | *pEventMultiSem = pEventMultiInt;
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | }
|
---|
82 | return VERR_NO_MEMORY;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
|
---|
87 | {
|
---|
88 | if (EventMultiSem == NIL_RTSEMEVENTMULTI) /* don't bitch */
|
---|
89 | return VERR_INVALID_HANDLE;
|
---|
90 | PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
91 | AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
|
---|
92 | AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
93 | ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
|
---|
94 | VERR_INVALID_HANDLE);
|
---|
95 |
|
---|
96 | mtx_lock_spin(&pEventMultiInt->Mtx);
|
---|
97 | ASMAtomicIncU32(&pEventMultiInt->u32Magic); /* make the handle invalid */
|
---|
98 | if (pEventMultiInt->cWaiters > 0)
|
---|
99 | {
|
---|
100 | /* abort waiting thread, last man cleans up. */
|
---|
101 | ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
|
---|
102 | wakeup(pEventMultiInt);
|
---|
103 | mtx_unlock_spin(&pEventMultiInt->Mtx);
|
---|
104 | }
|
---|
105 | else if (pEventMultiInt->cWaking)
|
---|
106 | /* the last waking thread is gonna do the cleanup */
|
---|
107 | mtx_unlock_spin(&pEventMultiInt->Mtx);
|
---|
108 | else
|
---|
109 | {
|
---|
110 | mtx_unlock_spin(&pEventMultiInt->Mtx);
|
---|
111 | mtx_destroy(&pEventMultiInt->Mtx);
|
---|
112 | RTMemFree(pEventMultiInt);
|
---|
113 | }
|
---|
114 |
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
|
---|
120 | {
|
---|
121 | PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
122 | AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
|
---|
123 | AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
124 | ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
|
---|
125 | VERR_INVALID_HANDLE);
|
---|
126 |
|
---|
127 | mtx_lock_spin(&pEventMultiInt->Mtx);
|
---|
128 |
|
---|
129 | ASMAtomicXchgU8(&pEventMultiInt->fSignaled, true);
|
---|
130 | if (pEventMultiInt->cWaiters > 0)
|
---|
131 | {
|
---|
132 | ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
|
---|
133 | ASMAtomicXchgU32(&pEventMultiInt->cWaiters, 0);
|
---|
134 | wakeup(pEventMultiInt);
|
---|
135 | }
|
---|
136 |
|
---|
137 | mtx_unlock_spin(&pEventMultiInt->Mtx);
|
---|
138 | return VINF_SUCCESS;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
|
---|
143 | {
|
---|
144 | PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
145 | AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
|
---|
146 | AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
147 | ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
|
---|
148 | VERR_INVALID_HANDLE);
|
---|
149 |
|
---|
150 | mtx_lock_spin(&pEventMultiInt->Mtx);
|
---|
151 | ASMAtomicXchgU8(&pEventMultiInt->fSignaled, false);
|
---|
152 | mtx_unlock_spin(&pEventMultiInt->Mtx);
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fInterruptible)
|
---|
158 | {
|
---|
159 | int rc;
|
---|
160 | PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
161 | AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
|
---|
162 | AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
163 | ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
|
---|
164 | VERR_INVALID_HANDLE);
|
---|
165 |
|
---|
166 | mtx_lock_spin(&pEventMultiInt->Mtx);
|
---|
167 |
|
---|
168 | if (pEventMultiInt->fSignaled)
|
---|
169 | rc = VINF_SUCCESS;
|
---|
170 | else
|
---|
171 | {
|
---|
172 | /*
|
---|
173 | * Translate milliseconds into ticks and go to sleep.
|
---|
174 | */
|
---|
175 | int cTicks;
|
---|
176 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
177 | {
|
---|
178 | if (hz == 1000)
|
---|
179 | cTicks = cMillies;
|
---|
180 | else if (hz == 100)
|
---|
181 | cTicks = cMillies / 10;
|
---|
182 | else
|
---|
183 | {
|
---|
184 | int64_t cTicks64 = ((uint64_t)cMillies * hz) / 1000;
|
---|
185 | cTicks = (int)cTicks64;
|
---|
186 | if (cTicks != cTicks64)
|
---|
187 | cTicks = INT_MAX;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | else
|
---|
191 | cTicks = 0;
|
---|
192 |
|
---|
193 | ASMAtomicIncU32(&pEventMultiInt->cWaiters);
|
---|
194 |
|
---|
195 | rc = msleep_spin(pEventMultiInt, /* block id */
|
---|
196 | &pEventMultiInt->Mtx,
|
---|
197 | //fInterruptible ? PZERO | PCATCH : PZERO,
|
---|
198 | "iprte", /* max 6 chars */
|
---|
199 | cTicks);
|
---|
200 | switch (rc)
|
---|
201 | {
|
---|
202 | case 0:
|
---|
203 | if (pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC)
|
---|
204 | {
|
---|
205 | ASMAtomicDecU32(&pEventMultiInt->cWaking);
|
---|
206 | rc = VINF_SUCCESS;
|
---|
207 | }
|
---|
208 | else
|
---|
209 | {
|
---|
210 | rc = VERR_SEM_DESTROYED; /** @todo this isn't necessarily correct, we've
|
---|
211 | * could've woken up just before destruction... */
|
---|
212 | if (!ASMAtomicDecU32(&pEventMultiInt->cWaking))
|
---|
213 | {
|
---|
214 | /* The event was destroyed, as the last thread do the cleanup.
|
---|
215 | we don't actually know whether */
|
---|
216 | mtx_unlock_spin(&pEventMultiInt->Mtx);
|
---|
217 | mtx_destroy(&pEventMultiInt->Mtx);
|
---|
218 | RTMemFree(pEventMultiInt);
|
---|
219 | return rc;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | break;
|
---|
223 |
|
---|
224 | case EWOULDBLOCK:
|
---|
225 | Assert(cMillies != RT_INDEFINITE_WAIT);
|
---|
226 | if (pEventMultiInt->cWaiters > 0)
|
---|
227 | ASMAtomicDecU32(&pEventMultiInt->cWaiters);
|
---|
228 | rc = VERR_TIMEOUT;
|
---|
229 | break;
|
---|
230 |
|
---|
231 | case EINTR:
|
---|
232 | case ERESTART:
|
---|
233 | Assert(fInterruptible);
|
---|
234 | if (pEventMultiInt->cWaiters > 0)
|
---|
235 | ASMAtomicDecU32(&pEventMultiInt->cWaiters);
|
---|
236 | rc = VERR_INTERRUPTED;
|
---|
237 | break;
|
---|
238 |
|
---|
239 | default:
|
---|
240 | AssertMsgFailed(("msleep -> %d\n", rc));
|
---|
241 | rc = VERR_GENERAL_FAILURE;
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | mtx_unlock_spin(&pEventMultiInt->Mtx);
|
---|
247 | return rc;
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
252 | {
|
---|
253 | return rtSemEventMultiWait(EventMultiSem, cMillies, false /* not interruptible */);
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
258 | {
|
---|
259 | return rtSemEventMultiWait(EventMultiSem, cMillies, true /* interruptible */);
|
---|
260 | }
|
---|
261 |
|
---|