1 | /* $Id: semeventmulti-r0drv-os2.cpp 1191 2007-03-04 20:46:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * InnoTek Portable Runtime - 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 |
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Structures and Typedefs *
|
---|
46 | *******************************************************************************/
|
---|
47 | /**
|
---|
48 | * OS/2 multiple release event semaphore.
|
---|
49 | */
|
---|
50 | typedef struct RTSEMEVENTMULTIINTERNAL
|
---|
51 | {
|
---|
52 | /** Magic value (RTSEMEVENTMULTI_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 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
63 |
|
---|
64 | /** Magic for the OS/2 multiple release event semaphore structure. (Isaac Asimov) */
|
---|
65 | #define RTSEMEVENTMULTI_MAGIC 0x19200102
|
---|
66 |
|
---|
67 |
|
---|
68 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
|
---|
69 | {
|
---|
70 | Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
71 | AssertPtrReturn(pEventMultiSem, VERR_INVALID_POINTER);
|
---|
72 |
|
---|
73 | PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pEventMultiInt));
|
---|
74 | if (pEventMultiInt)
|
---|
75 | {
|
---|
76 | pEventMultiInt->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
77 | pEventMultiInt->cWaiters = 0;
|
---|
78 | pEventMultiInt->cWaking = 0;
|
---|
79 | pEventMultiInt->fSignaled = 0;
|
---|
80 | KernAllocSpinLock(&pEventMultiInt->Spinlock);
|
---|
81 | *pEventMultiSem = pEventMultiInt;
|
---|
82 | return VINF_SUCCESS;
|
---|
83 | }
|
---|
84 | return VERR_NO_MEMORY;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
|
---|
89 | {
|
---|
90 | if (EventMultiSem == NIL_RTSEMEVENTMULTI) /* don't bitch */
|
---|
91 | return VERR_INVALID_HANDLE;
|
---|
92 | PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
93 | AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
|
---|
94 | AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
95 | ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
|
---|
96 | VERR_INVALID_HANDLE);
|
---|
97 |
|
---|
98 | KernAcquireSpinLock(&pEventMultiInt->Spinlock);
|
---|
99 | ASMAtomicIncU32(&pEventMultiInt->u32Magic); /* make the handle invalid */
|
---|
100 | if (pEventMultiInt->cWaiters > 0)
|
---|
101 | {
|
---|
102 | /* abort waiting thread, last man cleans up. */
|
---|
103 | ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
|
---|
104 | ULONG cThreads;
|
---|
105 | KernWakeup((ULONG)pEventMultiInt, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
|
---|
106 | KernReleaseSpinLock(&pEventMultiInt->Spinlock);
|
---|
107 | }
|
---|
108 | else if (pEventMultiInt->cWaking)
|
---|
109 | /* the last waking thread is gonna do the cleanup */
|
---|
110 | KernReleaseSpinLock(&pEventMultiInt->Spinlock);
|
---|
111 | else
|
---|
112 | {
|
---|
113 | KernReleaseSpinLock(&pEventMultiInt->Spinlock);
|
---|
114 | KernFreeSpinLock(&pEventMultiInt->Spinlock);
|
---|
115 | RTMemFree(pEventMultiInt);
|
---|
116 | }
|
---|
117 |
|
---|
118 | return VINF_SUCCESS;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
|
---|
123 | {
|
---|
124 | PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
125 | AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
|
---|
126 | AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
127 | ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
|
---|
128 | VERR_INVALID_HANDLE);
|
---|
129 |
|
---|
130 | KernAcquireSpinLock(&pEventMultiInt->Spinlock);
|
---|
131 |
|
---|
132 | ASMAtomicXchgU8(&pEventMultiInt->fSignaled, true);
|
---|
133 | if (pEventMultiInt->cWaiters > 0)
|
---|
134 | {
|
---|
135 | ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
|
---|
136 | ASMAtomicXchgU32(&pEventMultiInt->cWaiters, 0);
|
---|
137 | ULONG cThreads;
|
---|
138 | KernWakeup((ULONG)pEventMultiInt, WAKEUP_DATA, &cThreads, VINF_SUCCESS);
|
---|
139 | }
|
---|
140 |
|
---|
141 | KernReleaseSpinLock(&pEventMultiInt->Spinlock);
|
---|
142 | return VINF_SUCCESS;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
|
---|
147 | {
|
---|
148 | PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
149 | AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
|
---|
150 | AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
151 | ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
|
---|
152 | VERR_INVALID_HANDLE);
|
---|
153 |
|
---|
154 | KernAcquireSpinLock(&pEventMultiInt->Spinlock);
|
---|
155 | ASMAtomicXchgU8(&pEventMultiInt->fSignaled, false);
|
---|
156 | KernReleaseSpinLock(&pEventMultiInt->Spinlock);
|
---|
157 | return VINF_SUCCESS;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, bool fInterruptible)
|
---|
162 | {
|
---|
163 | PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
|
---|
164 | AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
|
---|
165 | AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
166 | ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
|
---|
167 | VERR_INVALID_HANDLE);
|
---|
168 |
|
---|
169 | KernAcquireSpinLock(&pEventMultiInt->Spinlock);
|
---|
170 |
|
---|
171 | int rc;
|
---|
172 | if (pEventMultiInt->fSignaled)
|
---|
173 | rc = VINF_SUCCESS;
|
---|
174 | else
|
---|
175 | {
|
---|
176 | ASMAtomicIncU32(&pEventMultiInt->cWaiters);
|
---|
177 |
|
---|
178 | ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
|
---|
179 | rc = KernBlock((ULONG)pEventMultiInt,
|
---|
180 | cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
|
---|
181 | BLOCK_SPINLOCK | (fInterruptible ? BLOCK_UNINTERRUPTABLE : 0),
|
---|
182 | &pEventMultiInt->Spinlock,
|
---|
183 | &ulData);
|
---|
184 | switch (rc)
|
---|
185 | {
|
---|
186 | case NO_ERROR:
|
---|
187 | rc = (int)ulData;
|
---|
188 | Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
|
---|
189 | Assert(pEventMultiInt->cWaking > 0);
|
---|
190 | if ( !ASMAtomicDecU32(&pEventMultiInt->cWaking)
|
---|
191 | && pEventMultiInt->u32Magic != RTSEMEVENTMULTI_MAGIC)
|
---|
192 | {
|
---|
193 | /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
|
---|
194 | the last thread do the cleanup. */
|
---|
195 | KernReleaseSpinLock(&pEventMultiInt->Spinlock);
|
---|
196 | KernFreeSpinLock(&pEventMultiInt->Spinlock);
|
---|
197 | RTMemFree(pEventMultiInt);
|
---|
198 | return VINF_SUCCESS;
|
---|
199 | }
|
---|
200 | rc = VINF_SUCCESS;
|
---|
201 | break;
|
---|
202 |
|
---|
203 | case ERROR_TIMEOUT:
|
---|
204 | Assert(cMillies != RT_INDEFINITE_WAIT);
|
---|
205 | ASMAtomicDecU32(&pEventMultiInt->cWaiters);
|
---|
206 | rc = VERR_TIMEOUT;
|
---|
207 | break;
|
---|
208 |
|
---|
209 | case ERROR_INTERRUPT:
|
---|
210 | Assert(fInterruptible);
|
---|
211 | ASMAtomicDecU32(&pEventMultiInt->cWaiters);
|
---|
212 | rc = VERR_INTERRUPTED;
|
---|
213 | break;
|
---|
214 |
|
---|
215 | default:
|
---|
216 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
217 | rc = VERR_GENERAL_FAILURE;
|
---|
218 | break;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | KernReleaseSpinLock(&pEventMultiInt->Spinlock);
|
---|
223 | return rc;
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
228 | {
|
---|
229 | return rtSemEventMultiWait(EventMultiSem, cMillies, false /* not interruptable */);
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
|
---|
234 | {
|
---|
235 | return rtSemEventMultiWait(EventMultiSem, cMillies, true /* interruptable */);
|
---|
236 | }
|
---|
237 |
|
---|