VirtualBox

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

Last change on this file since 33973 was 33269, checked in by vboxsync, 14 years ago

IPRT: A quick replacement of the RTMemPage* and RTMemExec* APIs on posix. (Turned out to be a bit more work than expected because of the electric fence heap and init dependencies.)

  • 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 33269 2010-10-20 15:42:28Z 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 | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
76 Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
77 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
78 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
79
80 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
81 if (!pThis)
82 return VERR_NO_MEMORY;
83
84 pThis->u32Magic = RTSEMEVENT_MAGIC;
85 pThis->cWaiters = 0;
86 pThis->cWaking = 0;
87 pThis->fSignaled = 0;
88 KernAllocSpinLock(&pThis->Spinlock);
89
90 *phEventSem = pThis;
91 return VINF_SUCCESS;
92}
93
94
95RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
96{
97 PRTSEMEVENTINTERNAL pThis = hEventSem;
98 if (pThis == NIL_RTSEMEVENT)
99 return VINF_SUCCESS;
100 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
101 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
102
103 KernAcquireSpinLock(&pThis->Spinlock);
104 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
105 if (pThis->cWaiters > 0)
106 {
107 /* abort waiting thread, last man cleans up. */
108 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
109 ULONG cThreads;
110 KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
111 KernReleaseSpinLock(&pThis->Spinlock);
112 }
113 else if (pThis->cWaking)
114 /* the last waking thread is gonna do the cleanup */
115 KernReleaseSpinLock(&pThis->Spinlock);
116 else
117 {
118 KernReleaseSpinLock(&pThis->Spinlock);
119 KernFreeSpinLock(&pThis->Spinlock);
120 RTMemFree(pThis);
121 }
122
123 return VINF_SUCCESS;
124}
125
126
127RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
128{
129 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
130 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
131 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
132
133 KernAcquireSpinLock(&pThis->Spinlock);
134
135 if (pThis->cWaiters > 0)
136 {
137 ASMAtomicDecU32(&pThis->cWaiters);
138 ASMAtomicIncU32(&pThis->cWaking);
139 ULONG cThreads;
140 KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_ONE, &cThreads, VINF_SUCCESS);
141 if (RT_UNLIKELY(!cThreads))
142 {
143 /* shouldn't ever happen on OS/2 */
144 ASMAtomicXchgU8(&pThis->fSignaled, true);
145 ASMAtomicDecU32(&pThis->cWaking);
146 ASMAtomicIncU32(&pThis->cWaiters);
147 }
148 }
149 else
150 ASMAtomicXchgU8(&pThis->fSignaled, true);
151
152 KernReleaseSpinLock(&pThis->Spinlock);
153 return VINF_SUCCESS;
154}
155
156
157static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fInterruptible)
158{
159 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
160 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
161 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
162
163 KernAcquireSpinLock(&pThis->Spinlock);
164
165 int rc;
166 if (pThis->fSignaled)
167 {
168 Assert(!pThis->cWaiters);
169 ASMAtomicXchgU8(&pThis->fSignaled, false);
170 rc = VINF_SUCCESS;
171 }
172 else
173 {
174 ASMAtomicIncU32(&pThis->cWaiters);
175
176 ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
177 rc = KernBlock((ULONG)pThis,
178 cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
179 BLOCK_SPINLOCK | (!fInterruptible ? BLOCK_UNINTERRUPTABLE : 0),
180 &pThis->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(pThis->cWaking > 0);
188 if ( !ASMAtomicDecU32(&pThis->cWaking)
189 && pThis->u32Magic != RTSEMEVENT_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(&pThis->Spinlock);
194 KernFreeSpinLock(&pThis->Spinlock);
195 RTMemFree(pThis);
196 return rc;
197 }
198 break;
199
200 case ERROR_TIMEOUT:
201 Assert(cMillies != RT_INDEFINITE_WAIT);
202 ASMAtomicDecU32(&pThis->cWaiters);
203 rc = VERR_TIMEOUT;
204 break;
205
206 case ERROR_INTERRUPT:
207 Assert(fInterruptible);
208 ASMAtomicDecU32(&pThis->cWaiters);
209 rc = VERR_INTERRUPTED;
210 break;
211
212 default:
213 AssertMsgFailed(("rc=%d\n", rc));
214 rc = VERR_GENERAL_FAILURE;
215 break;
216 }
217 }
218
219 KernReleaseSpinLock(&pThis->Spinlock);
220 return rc;
221}
222
223
224RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
225{
226 return rtSemEventWait(hEventSem, cMillies, false /* not interruptible */);
227}
228
229
230RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
231{
232 return rtSemEventWait(hEventSem, cMillies, true /* interruptible */);
233}
234
235
236RTDECL(uint32_t) RTSemEventGetResolution(void)
237{
238 return 32000000; /* 32ms */
239}
240
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