VirtualBox

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

Last change on this file since 32736 was 25724, checked in by vboxsync, 15 years ago

iprt: Use RTMSINTERVAL for timeouts. Fixed missing timeout underflow checks in two RTFileAioCtxWait implementations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.7 KB
Line 
1/* $Id: semevent-r0drv-os2.cpp 25724 2010-01-11 14:45:34Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event Semaphores, Ring-0 Driver, OS/2.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
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#include "internal/magics.h"
44
45
46/*******************************************************************************
47* Structures and Typedefs *
48*******************************************************************************/
49/**
50 * OS/2 event semaphore.
51 */
52typedef struct RTSEMEVENTINTERNAL
53{
54 /** Magic value (RTSEMEVENT_MAGIC). */
55 uint32_t volatile u32Magic;
56 /** The number of waiting threads. */
57 uint32_t volatile cWaiters;
58 /** Set if the event object is signaled. */
59 uint8_t volatile fSignaled;
60 /** The number of threads in the process of waking up. */
61 uint32_t volatile cWaking;
62 /** The OS/2 spinlock protecting this structure. */
63 SpinLock_t Spinlock;
64} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
65
66
67RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
68{
69 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
70}
71
72
73RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
74{
75 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
76 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
77 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
78
79 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
80 if (!pThis)
81 return VERR_NO_MEMORY;
82
83 pThis->u32Magic = RTSEMEVENT_MAGIC;
84 pThis->cWaiters = 0;
85 pThis->cWaking = 0;
86 pThis->fSignaled = 0;
87 KernAllocSpinLock(&pThis->Spinlock);
88
89 *phEventSem = pThis;
90 return VINF_SUCCESS;
91}
92
93
94RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
95{
96 PRTSEMEVENTINTERNAL pThis = hEventSem;
97 if (pThis == NIL_RTSEMEVENT)
98 return VINF_SUCCESS;
99 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
100 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
101
102 KernAcquireSpinLock(&pThis->Spinlock);
103 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
104 if (pThis->cWaiters > 0)
105 {
106 /* abort waiting thread, last man cleans up. */
107 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
108 ULONG cThreads;
109 KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
110 KernReleaseSpinLock(&pThis->Spinlock);
111 }
112 else if (pThis->cWaking)
113 /* the last waking thread is gonna do the cleanup */
114 KernReleaseSpinLock(&pThis->Spinlock);
115 else
116 {
117 KernReleaseSpinLock(&pThis->Spinlock);
118 KernFreeSpinLock(&pThis->Spinlock);
119 RTMemFree(pThis);
120 }
121
122 return VINF_SUCCESS;
123}
124
125
126RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
127{
128 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
129 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
130 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
131
132 KernAcquireSpinLock(&pThis->Spinlock);
133
134 if (pThis->cWaiters > 0)
135 {
136 ASMAtomicDecU32(&pThis->cWaiters);
137 ASMAtomicIncU32(&pThis->cWaking);
138 ULONG cThreads;
139 KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_ONE, &cThreads, VINF_SUCCESS);
140 if (RT_UNLIKELY(!cThreads))
141 {
142 /* shouldn't ever happen on OS/2 */
143 ASMAtomicXchgU8(&pThis->fSignaled, true);
144 ASMAtomicDecU32(&pThis->cWaking);
145 ASMAtomicIncU32(&pThis->cWaiters);
146 }
147 }
148 else
149 ASMAtomicXchgU8(&pThis->fSignaled, true);
150
151 KernReleaseSpinLock(&pThis->Spinlock);
152 return VINF_SUCCESS;
153}
154
155
156static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fInterruptible)
157{
158 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
159 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
160 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
161
162 KernAcquireSpinLock(&pThis->Spinlock);
163
164 int rc;
165 if (pThis->fSignaled)
166 {
167 Assert(!pThis->cWaiters);
168 ASMAtomicXchgU8(&pThis->fSignaled, false);
169 rc = VINF_SUCCESS;
170 }
171 else
172 {
173 ASMAtomicIncU32(&pThis->cWaiters);
174
175 ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
176 rc = KernBlock((ULONG)pThis,
177 cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
178 BLOCK_SPINLOCK | (!fInterruptible ? BLOCK_UNINTERRUPTABLE : 0),
179 &pThis->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(pThis->cWaking > 0);
187 if ( !ASMAtomicDecU32(&pThis->cWaking)
188 && pThis->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(&pThis->Spinlock);
193 KernFreeSpinLock(&pThis->Spinlock);
194 RTMemFree(pThis);
195 return rc;
196 }
197 break;
198
199 case ERROR_TIMEOUT:
200 Assert(cMillies != RT_INDEFINITE_WAIT);
201 ASMAtomicDecU32(&pThis->cWaiters);
202 rc = VERR_TIMEOUT;
203 break;
204
205 case ERROR_INTERRUPT:
206 Assert(fInterruptible);
207 ASMAtomicDecU32(&pThis->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(&pThis->Spinlock);
219 return rc;
220}
221
222
223RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
224{
225 return rtSemEventWait(hEventSem, cMillies, false /* not interruptible */);
226}
227
228
229RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
230{
231 return rtSemEventWait(hEventSem, cMillies, true /* interruptible */);
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