1 | /* $Id: semevent-r0drv-freebsd.c 8245 2008-04-21 17:24:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Single 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 event semaphore.
|
---|
50 | */
|
---|
51 | typedef 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 FreeBSD spinlock protecting this structure. */
|
---|
62 | struct mtx Mtx;
|
---|
63 | } RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
|
---|
64 |
|
---|
65 |
|
---|
66 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
|
---|
67 | {
|
---|
68 | Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
|
---|
69 | AssertPtrReturn(pEventSem, VERR_INVALID_POINTER);
|
---|
70 |
|
---|
71 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
|
---|
72 | if (pEventInt)
|
---|
73 | {
|
---|
74 | pEventInt->u32Magic = RTSEMEVENT_MAGIC;
|
---|
75 | pEventInt->cWaiters = 0;
|
---|
76 | pEventInt->cWaking = 0;
|
---|
77 | pEventInt->fSignaled = 0;
|
---|
78 | mtx_init(&pEventInt->Mtx, "IPRT Event Semaphore", NULL, MTX_SPIN);
|
---|
79 | *pEventSem = pEventInt;
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | }
|
---|
82 | return VERR_NO_MEMORY;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
|
---|
87 | {
|
---|
88 | if (EventSem == NIL_RTSEMEVENT) /* don't bitch */
|
---|
89 | return VERR_INVALID_HANDLE;
|
---|
90 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
|
---|
91 | AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
|
---|
92 | AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
|
---|
93 | ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
|
---|
94 | VERR_INVALID_HANDLE);
|
---|
95 |
|
---|
96 | mtx_lock_spin(&pEventInt->Mtx);
|
---|
97 | ASMAtomicIncU32(&pEventInt->u32Magic); /* make the handle invalid */
|
---|
98 | if (pEventInt->cWaiters > 0)
|
---|
99 | {
|
---|
100 | /* abort waiting thread, last man cleans up. */
|
---|
101 | ASMAtomicXchgU32(&pEventInt->cWaking, pEventInt->cWaking + pEventInt->cWaiters);
|
---|
102 | wakeup(pEventInt);
|
---|
103 | mtx_unlock_spin(&pEventInt->Mtx);
|
---|
104 | }
|
---|
105 | else if (pEventInt->cWaking)
|
---|
106 | /* the last waking thread is gonna do the cleanup */
|
---|
107 | mtx_unlock_spin(&pEventInt->Mtx);
|
---|
108 | else
|
---|
109 | {
|
---|
110 | mtx_unlock_spin(&pEventInt->Mtx);
|
---|
111 | mtx_destroy(&pEventInt->Mtx);
|
---|
112 | RTMemFree(pEventInt);
|
---|
113 | }
|
---|
114 |
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
|
---|
120 | {
|
---|
121 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
|
---|
122 | AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
|
---|
123 | AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
|
---|
124 | ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
|
---|
125 | VERR_INVALID_HANDLE);
|
---|
126 |
|
---|
127 | mtx_lock_spin(&pEventInt->Mtx);
|
---|
128 |
|
---|
129 | if (pEventInt->cWaiters > 0)
|
---|
130 | {
|
---|
131 | ASMAtomicDecU32(&pEventInt->cWaiters);
|
---|
132 | ASMAtomicIncU32(&pEventInt->cWaking);
|
---|
133 | wakeup_one(pEventInt);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | ASMAtomicXchgU8(&pEventInt->fSignaled, true);
|
---|
137 |
|
---|
138 | mtx_unlock_spin(&pEventInt->Mtx);
|
---|
139 | return VINF_SUCCESS;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)
|
---|
144 | {
|
---|
145 | int rc;
|
---|
146 | PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
|
---|
147 | AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
|
---|
148 | AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
|
---|
149 | ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
|
---|
150 | VERR_INVALID_HANDLE);
|
---|
151 |
|
---|
152 | mtx_lock_spin(&pEventInt->Mtx);
|
---|
153 |
|
---|
154 | if (pEventInt->fSignaled)
|
---|
155 | {
|
---|
156 | Assert(!pEventInt->cWaiters);
|
---|
157 | ASMAtomicXchgU8(&pEventInt->fSignaled, false);
|
---|
158 | rc = VINF_SUCCESS;
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | /*
|
---|
163 | * Translate milliseconds into ticks and go to sleep.
|
---|
164 | */
|
---|
165 | int cTicks;
|
---|
166 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
167 | {
|
---|
168 | if (hz == 1000)
|
---|
169 | cTicks = cMillies;
|
---|
170 | else if (hz == 100)
|
---|
171 | cTicks = cMillies / 10;
|
---|
172 | else
|
---|
173 | {
|
---|
174 | int64_t cTicks64 = ((uint64_t)cMillies * hz) / 1000;
|
---|
175 | cTicks = (int)cTicks64;
|
---|
176 | if (cTicks != cTicks64)
|
---|
177 | cTicks = INT_MAX;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | else
|
---|
181 | cTicks = 0;
|
---|
182 |
|
---|
183 | ASMAtomicIncU32(&pEventInt->cWaiters);
|
---|
184 |
|
---|
185 | rc = msleep(pEventInt, /* block id */
|
---|
186 | &pEventInt->Mtx,
|
---|
187 | fInterruptible ? PZERO | PCATCH : PZERO,
|
---|
188 | "iprtev", /* max 6 chars */
|
---|
189 | cTicks);
|
---|
190 | switch (rc)
|
---|
191 | {
|
---|
192 | case 0:
|
---|
193 | if (pEventInt->u32Magic == RTSEMEVENT_MAGIC)
|
---|
194 | {
|
---|
195 | ASMAtomicDecU32(&pEventInt->cWaking);
|
---|
196 | rc = VINF_SUCCESS;
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | rc = VERR_SEM_DESTROYED; /** @todo this isn't necessarily correct, we've
|
---|
201 | * could've woken up just before destruction... */
|
---|
202 | if (!ASMAtomicDecU32(&pEventInt->cWaking))
|
---|
203 | {
|
---|
204 | /* The event was destroyed, as the last thread do the cleanup.
|
---|
205 | we don't actually know whether */
|
---|
206 | mtx_unlock_spin(&pEventInt->Mtx);
|
---|
207 | mtx_destroy(&pEventInt->Mtx);
|
---|
208 | RTMemFree(pEventInt);
|
---|
209 | return rc;
|
---|
210 | }
|
---|
211 | }
|
---|
212 | break;
|
---|
213 |
|
---|
214 | case EWOULDBLOCK:
|
---|
215 | Assert(cMillies != RT_INDEFINITE_WAIT);
|
---|
216 | if (pEventInt->cWaiters > 0)
|
---|
217 | ASMAtomicDecU32(&pEventInt->cWaiters);
|
---|
218 | rc = VERR_TIMEOUT;
|
---|
219 | break;
|
---|
220 |
|
---|
221 | case EINTR:
|
---|
222 | case ERESTART:
|
---|
223 | Assert(fInterruptible);
|
---|
224 | if (pEventInt->cWaiters > 0)
|
---|
225 | ASMAtomicDecU32(&pEventInt->cWaiters);
|
---|
226 | rc = VERR_INTERRUPTED;
|
---|
227 | break;
|
---|
228 |
|
---|
229 | default:
|
---|
230 | AssertMsgFailed(("msleep -> %d\n", rc));
|
---|
231 | rc = VERR_GENERAL_FAILURE;
|
---|
232 | break;
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | mtx_unlock_spin(&pEventInt->Mtx);
|
---|
237 | return rc;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
242 | {
|
---|
243 | return rtSemEventWait(EventSem, cMillies, false /* not interruptible */);
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
248 | {
|
---|
249 | return rtSemEventWait(EventSem, cMillies, true /* interruptible */);
|
---|
250 | }
|
---|
251 |
|
---|