VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/os2/semevent-r0drv-os2.cpp@ 1321

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

svn properties.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.0 KB
Line 
1/* $Id: semevent-r0drv-os2.cpp 1191 2007-03-04 20:46:04Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - Single 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
43
44/*******************************************************************************
45* Structures and Typedefs *
46*******************************************************************************/
47/**
48 * OS/2 event semaphore.
49 */
50typedef struct RTSEMEVENTINTERNAL
51{
52 /** Magic value (RTSEMEVENT_MAGIC). */
53 uint32_t volatile u32Magic;
54 /** The number of waiting threads. */
55 uint32_t volatile cWaiters;
56 /** Set if the event object is signaled. */
57 uint8_t volatile fSignaled;
58 /** The number of threads in the process of waking up. */
59 uint32_t volatile cWaking;
60 /** The OS/2 spinlock protecting this structure. */
61 SpinLock_t Spinlock;
62} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
63
64/** Magic for the OS/2 event semaphore structure. (Neil Gaiman) */
65#define RTSEMEVENT_MAGIC 0x19601110
66
67
68RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
69{
70 Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
71 AssertPtrReturn(pEventSem, VERR_INVALID_POINTER);
72
73 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
74 if (pEventInt)
75 {
76 pEventInt->u32Magic = RTSEMEVENT_MAGIC;
77 pEventInt->cWaiters = 0;
78 pEventInt->cWaking = 0;
79 pEventInt->fSignaled = 0;
80 KernAllocSpinLock(&pEventInt->Spinlock);
81 *pEventSem = pEventInt;
82 return VINF_SUCCESS;
83 }
84 return VERR_NO_MEMORY;
85}
86
87
88RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
89{
90 if (EventSem == NIL_RTSEMEVENT) /* don't bitch */
91 return VERR_INVALID_HANDLE;
92 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
93 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
94 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
95 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
96 VERR_INVALID_HANDLE);
97
98 KernAcquireSpinLock(&pEventInt->Spinlock);
99 ASMAtomicIncU32(&pEventInt->u32Magic); /* make the handle invalid */
100 if (pEventInt->cWaiters > 0)
101 {
102 /* abort waiting thread, last man cleans up. */
103 ASMAtomicXchgU32(&pEventInt->cWaking, pEventInt->cWaking + pEventInt->cWaiters);
104 ULONG cThreads;
105 KernWakeup((ULONG)pEventInt, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
106 KernReleaseSpinLock(&pEventInt->Spinlock);
107 }
108 else if (pEventInt->cWaking)
109 /* the last waking thread is gonna do the cleanup */
110 KernReleaseSpinLock(&pEventInt->Spinlock);
111 else
112 {
113 KernReleaseSpinLock(&pEventInt->Spinlock);
114 KernFreeSpinLock(&pEventInt->Spinlock);
115 RTMemFree(pEventInt);
116 }
117
118 return VINF_SUCCESS;
119}
120
121
122RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
123{
124 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
125 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
126 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
127 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
128 VERR_INVALID_HANDLE);
129
130 KernAcquireSpinLock(&pEventInt->Spinlock);
131
132 if (pEventInt->cWaiters > 0)
133 {
134 ASMAtomicDecU32(&pEventInt->cWaiters);
135 ASMAtomicIncU32(&pEventInt->cWaking);
136 ULONG cThreads;
137 KernWakeup((ULONG)pEventInt, WAKEUP_DATA | WAKEUP_ONE, &cThreads, VINF_SUCCESS);
138 if (RT_UNLIKELY(!cThreads))
139 {
140 /* shouldn't ever happen on OS/2 */
141 ASMAtomicXchgU8(&pEventInt->fSignaled, true);
142 ASMAtomicDecU32(&pEventInt->cWaking);
143 ASMAtomicIncU32(&pEventInt->cWaiters);
144 }
145 }
146 else
147 ASMAtomicXchgU8(&pEventInt->fSignaled, true);
148
149 KernReleaseSpinLock(&pEventInt->Spinlock);
150 return VINF_SUCCESS;
151}
152
153
154static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)
155{
156 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
157 AssertPtrReturn(pEventInt, VERR_INVALID_HANDLE);
158 AssertMsgReturn(pEventInt->u32Magic == RTSEMEVENT_MAGIC,
159 ("pEventInt=%p u32Magic=%#x\n", pEventInt, pEventInt->u32Magic),
160 VERR_INVALID_HANDLE);
161
162 KernAcquireSpinLock(&pEventInt->Spinlock);
163
164 int rc;
165 if (pEventInt->fSignaled)
166 {
167 Assert(!pEventInt->cWaiters);
168 ASMAtomicXchgU8(&pEventInt->fSignaled, false);
169 rc = VINF_SUCCESS;
170 }
171 else
172 {
173 ASMAtomicIncU32(&pEventInt->cWaiters);
174
175 ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
176 rc = KernBlock((ULONG)pEventInt,
177 cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
178 BLOCK_SPINLOCK | (fInterruptible ? BLOCK_UNINTERRUPTABLE : 0),
179 &pEventInt->Spinlock,
180 &ulData);
181 switch (rc)
182 {
183 case NO_ERROR:
184 rc = (int)ulData;
185 Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
186 Assert(pEventInt->cWaking > 0);
187 if ( !ASMAtomicDecU32(&pEventInt->cWaking)
188 && pEventInt->u32Magic != RTSEMEVENT_MAGIC)
189 {
190 /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
191 the last thread do the cleanup. */
192 KernReleaseSpinLock(&pEventInt->Spinlock);
193 KernFreeSpinLock(&pEventInt->Spinlock);
194 RTMemFree(pEventInt);
195 return rc;
196 }
197 break;
198
199 case ERROR_TIMEOUT:
200 Assert(cMillies != RT_INDEFINITE_WAIT);
201 ASMAtomicDecU32(&pEventInt->cWaiters);
202 rc = VERR_TIMEOUT;
203 break;
204
205 case ERROR_INTERRUPT:
206 Assert(fInterruptible);
207 ASMAtomicDecU32(&pEventInt->cWaiters);
208 rc = VERR_INTERRUPTED;
209 break;
210
211 default:
212 AssertMsgFailed(("rc=%d\n", rc));
213 rc = VERR_GENERAL_FAILURE;
214 break;
215 }
216 }
217
218 KernReleaseSpinLock(&pEventInt->Spinlock);
219 return rc;
220}
221
222
223RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
224{
225 return rtSemEventWait(EventSem, cMillies, false /* not interruptable */);
226}
227
228
229RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
230{
231 return rtSemEventWait(EventSem, cMillies, true /* interruptable */);
232}
233
Note: See TracBrowser for help on using the repository browser.

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