VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/os2/semeventmulti-r0drv-os2.cpp@ 13857

最後變更 在這個檔案從13857是 8245,由 vboxsync 提交於 17 年 前

rebranding: IPRT files again.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 8.7 KB
 
1/* $Id: semeventmulti-r0drv-os2.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
2/** @file
3 * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, OS/2.
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/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-os2-kernel.h"
36
37#include <iprt/semaphore.h>
38#include <iprt/alloc.h>
39#include <iprt/asm.h>
40#include <iprt/assert.h>
41#include <iprt/err.h>
42#include "internal/magics.h"
43
44
45/*******************************************************************************
46* Structures and Typedefs *
47*******************************************************************************/
48/**
49 * OS/2 multiple release event semaphore.
50 */
51typedef 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 OS/2 spinlock protecting this structure. */
62 SpinLock_t Spinlock;
63} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
64
65
66RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
67{
68 Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
69 AssertPtrReturn(pEventMultiSem, VERR_INVALID_POINTER);
70
71 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pEventMultiInt));
72 if (pEventMultiInt)
73 {
74 pEventMultiInt->u32Magic = RTSEMEVENTMULTI_MAGIC;
75 pEventMultiInt->cWaiters = 0;
76 pEventMultiInt->cWaking = 0;
77 pEventMultiInt->fSignaled = 0;
78 KernAllocSpinLock(&pEventMultiInt->Spinlock);
79 *pEventMultiSem = pEventMultiInt;
80 return VINF_SUCCESS;
81 }
82 return VERR_NO_MEMORY;
83}
84
85
86RTDECL(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 KernAcquireSpinLock(&pEventMultiInt->Spinlock);
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 ULONG cThreads;
103 KernWakeup((ULONG)pEventMultiInt, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
104 KernReleaseSpinLock(&pEventMultiInt->Spinlock);
105 }
106 else if (pEventMultiInt->cWaking)
107 /* the last waking thread is gonna do the cleanup */
108 KernReleaseSpinLock(&pEventMultiInt->Spinlock);
109 else
110 {
111 KernReleaseSpinLock(&pEventMultiInt->Spinlock);
112 KernFreeSpinLock(&pEventMultiInt->Spinlock);
113 RTMemFree(pEventMultiInt);
114 }
115
116 return VINF_SUCCESS;
117}
118
119
120RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
121{
122 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
123 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
124 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
125 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
126 VERR_INVALID_HANDLE);
127
128 KernAcquireSpinLock(&pEventMultiInt->Spinlock);
129
130 ASMAtomicXchgU8(&pEventMultiInt->fSignaled, true);
131 if (pEventMultiInt->cWaiters > 0)
132 {
133 ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
134 ASMAtomicXchgU32(&pEventMultiInt->cWaiters, 0);
135 ULONG cThreads;
136 KernWakeup((ULONG)pEventMultiInt, WAKEUP_DATA, &cThreads, VINF_SUCCESS);
137 }
138
139 KernReleaseSpinLock(&pEventMultiInt->Spinlock);
140 return VINF_SUCCESS;
141}
142
143
144RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
145{
146 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
147 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
148 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
149 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
150 VERR_INVALID_HANDLE);
151
152 KernAcquireSpinLock(&pEventMultiInt->Spinlock);
153 ASMAtomicXchgU8(&pEventMultiInt->fSignaled, false);
154 KernReleaseSpinLock(&pEventMultiInt->Spinlock);
155 return VINF_SUCCESS;
156}
157
158
159static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fInterruptible)
160{
161 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
162 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
163 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
164 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
165 VERR_INVALID_HANDLE);
166
167 KernAcquireSpinLock(&pEventMultiInt->Spinlock);
168
169 int rc;
170 if (pEventMultiInt->fSignaled)
171 rc = VINF_SUCCESS;
172 else
173 {
174 ASMAtomicIncU32(&pEventMultiInt->cWaiters);
175
176 ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
177 rc = KernBlock((ULONG)pEventMultiInt,
178 cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
179 BLOCK_SPINLOCK | (!fInterruptible ? BLOCK_UNINTERRUPTABLE : 0),
180 &pEventMultiInt->Spinlock,
181 &ulData);
182 switch (rc)
183 {
184 case NO_ERROR:
185 rc = (int)ulData;
186 Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
187 Assert(pEventMultiInt->cWaking > 0);
188 if ( !ASMAtomicDecU32(&pEventMultiInt->cWaking)
189 && pEventMultiInt->u32Magic != RTSEMEVENTMULTI_MAGIC)
190 {
191 /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
192 the last thread do the cleanup. */
193 KernReleaseSpinLock(&pEventMultiInt->Spinlock);
194 KernFreeSpinLock(&pEventMultiInt->Spinlock);
195 RTMemFree(pEventMultiInt);
196 return VINF_SUCCESS;
197 }
198 rc = VINF_SUCCESS;
199 break;
200
201 case ERROR_TIMEOUT:
202 Assert(cMillies != RT_INDEFINITE_WAIT);
203 ASMAtomicDecU32(&pEventMultiInt->cWaiters);
204 rc = VERR_TIMEOUT;
205 break;
206
207 case ERROR_INTERRUPT:
208 Assert(fInterruptible);
209 ASMAtomicDecU32(&pEventMultiInt->cWaiters);
210 rc = VERR_INTERRUPTED;
211 break;
212
213 default:
214 AssertMsgFailed(("rc=%d\n", rc));
215 rc = VERR_GENERAL_FAILURE;
216 break;
217 }
218 }
219
220 KernReleaseSpinLock(&pEventMultiInt->Spinlock);
221 return rc;
222}
223
224
225RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
226{
227 return rtSemEventMultiWait(EventMultiSem, cMillies, false /* not interruptible */);
228}
229
230
231RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
232{
233 return rtSemEventMultiWait(EventMultiSem, cMillies, true /* interruptible */);
234}
235
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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