VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/semevent-r0drv-freebsd.c@ 20374

最後變更 在這個檔案從20374是 20208,由 vboxsync 提交於 16 年 前

FreeBSD: typo

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 8.5 KB
 
1/* $Id: semevent-r0drv-freebsd.c 20208 2009-06-03 07:34:55Z 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 */
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 FreeBSD spinlock protecting this structure. */
62 struct mtx Mtx;
63} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
64
65
66RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
67{
68 Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
69 AssertPtrReturn(pEventSem, VERR_INVALID_POINTER);
70
71 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAllocZ(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
86RTDECL(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
119RTDECL(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
143static 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 struct timeval tv;
166
167 if (cMillies != RT_INDEFINITE_WAIT)
168 {
169 tv.tv_sec = cMillies / 1000;
170 tv.tv_usec = (cMillies % 1000) * 1000;
171 }
172
173 ASMAtomicIncU32(&pEventInt->cWaiters);
174
175 mtx_unlock_spin(&pEventInt->Mtx);
176/** @todo r=bird: This doesn't handle cMillies == 0 correctly, it will assert
177 * and or sleep for ever according to r47861. (That's probably an old bug
178 * of my making.)
179 *
180 * And it really looks like it's racing signalling on MP systems. (It
181 * *looks* like you leave the lock and then tries to go to sleep on a
182 * block id, someone spinning on the lock attempt to wake us up before we
183 * go to sleep. That's why the code was originally trying to use msleep
184 * here.). */
185 rc = tsleep(pEventInt, /* block id */
186 fInterruptible ? PZERO | PCATCH : PZERO,
187 "iprtev", /* max 6 chars */
188 cMillies == RT_INDEFINITE_WAIT
189 ? 0
190 : tvtohz(&tv));
191 mtx_lock_spin(&pEventInt->Mtx);
192
193 switch (rc)
194 {
195 case 0:
196 if (pEventInt->u32Magic == RTSEMEVENT_MAGIC)
197 {
198 ASMAtomicDecU32(&pEventInt->cWaking);
199 rc = VINF_SUCCESS;
200 }
201 else
202 {
203 rc = VERR_SEM_DESTROYED; /** @todo this isn't necessarily correct, we've
204 * could've woken up just before destruction... */
205 if (!ASMAtomicDecU32(&pEventInt->cWaking))
206 {
207 /* The event was destroyed, as the last thread do the cleanup.
208 we don't actually know whether */
209 mtx_unlock_spin(&pEventInt->Mtx);
210 mtx_destroy(&pEventInt->Mtx);
211 RTMemFree(pEventInt);
212 return rc;
213 }
214 }
215 break;
216
217 case EWOULDBLOCK:
218 Assert(cMillies != RT_INDEFINITE_WAIT);
219 if (pEventInt->cWaiters > 0)
220 ASMAtomicDecU32(&pEventInt->cWaiters);
221 rc = VERR_TIMEOUT;
222 break;
223
224 case EINTR:
225 case ERESTART:
226 Assert(fInterruptible);
227 if (pEventInt->cWaiters > 0)
228 ASMAtomicDecU32(&pEventInt->cWaiters);
229 rc = VERR_INTERRUPTED;
230 break;
231
232 default:
233 AssertMsgFailed(("tsleep -> %d\n", rc));
234 rc = VERR_GENERAL_FAILURE;
235 break;
236 }
237 }
238
239 mtx_unlock_spin(&pEventInt->Mtx);
240 return rc;
241}
242
243
244RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
245{
246 return rtSemEventWait(EventSem, cMillies, false /* not interruptible */);
247}
248
249
250RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
251{
252 return rtSemEventWait(EventSem, cMillies, true /* interruptible */);
253}
254
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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