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