1 | /* $Id: semeventmulti-r0drv-os2.cpp 25724 2010-01-11 14:45:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple 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 | #include "internal/magics.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Structures and Typedefs *
|
---|
47 | *******************************************************************************/
|
---|
48 | /**
|
---|
49 | * OS/2 multiple release event semaphore.
|
---|
50 | */
|
---|
51 | typedef struct RTSEMEVENTMULTIINTERNAL
|
---|
52 | {
|
---|
53 | /** Magic value (RTSEMEVENTMULTI_MAGIC). */
|
---|
54 | uint32_t volatile u32Magic;
|
---|
55 | /** The number of waiting threads. */
|
---|
56 | uint32_t volatile cWaiters;
|
---|
57 | /** Set if the event object is signaled. */
|
---|
58 | uint8_t volatile fSignaled;
|
---|
59 | /** The number of threads in the process of waking up. */
|
---|
60 | uint32_t volatile cWaking;
|
---|
61 | /** The OS/2 spinlock protecting this structure. */
|
---|
62 | SpinLock_t Spinlock;
|
---|
63 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
64 |
|
---|
65 |
|
---|
66 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
|
---|
67 | {
|
---|
68 | return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
|
---|
73 | const char *pszNameFmt, ...)
|
---|
74 | {
|
---|
75 | AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
76 | AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
|
---|
77 |
|
---|
78 | AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
79 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
80 | if (pThis)
|
---|
81 | {
|
---|
82 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
83 | pThis->cWaiters = 0;
|
---|
84 | pThis->cWaking = 0;
|
---|
85 | pThis->fSignaled = 0;
|
---|
86 | KernAllocSpinLock(&pThis->Spinlock);
|
---|
87 |
|
---|
88 | *phEventMultiSem = pThis;
|
---|
89 | return VINF_SUCCESS;
|
---|
90 | }
|
---|
91 | return VERR_NO_MEMORY;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
|
---|
96 | {
|
---|
97 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
98 | if (pThis == NIL_RTSEMEVENTMULTI)
|
---|
99 | return VINF_SUCCESS;
|
---|
100 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
101 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), 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 |
|
---|
127 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
|
---|
128 | {
|
---|
129 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
130 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
131 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
132 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
133 | VERR_INVALID_HANDLE);
|
---|
134 |
|
---|
135 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
136 |
|
---|
137 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
138 | if (pThis->cWaiters > 0)
|
---|
139 | {
|
---|
140 | ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
|
---|
141 | ASMAtomicXchgU32(&pThis->cWaiters, 0);
|
---|
142 | ULONG cThreads;
|
---|
143 | KernWakeup((ULONG)pThis, WAKEUP_DATA, &cThreads, VINF_SUCCESS);
|
---|
144 | }
|
---|
145 |
|
---|
146 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
147 | return VINF_SUCCESS;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
|
---|
152 | {
|
---|
153 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
154 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
155 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
156 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
157 | VERR_INVALID_HANDLE);
|
---|
158 |
|
---|
159 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
160 | ASMAtomicXchgU8(&pThis->fSignaled, false);
|
---|
161 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
162 | return VINF_SUCCESS;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | static int rtSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies, bool fInterruptible)
|
---|
167 | {
|
---|
168 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
169 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
170 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
171 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
172 | VERR_INVALID_HANDLE);
|
---|
173 |
|
---|
174 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
175 |
|
---|
176 | int rc;
|
---|
177 | if (pThis->fSignaled)
|
---|
178 | rc = VINF_SUCCESS;
|
---|
179 | else
|
---|
180 | {
|
---|
181 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
182 |
|
---|
183 | ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
|
---|
184 | rc = KernBlock((ULONG)pThis,
|
---|
185 | cMillies == RT_INDEFINITE_WAIT ? SEM_INDEFINITE_WAIT : cMillies,
|
---|
186 | BLOCK_SPINLOCK | (!fInterruptible ? BLOCK_UNINTERRUPTABLE : 0),
|
---|
187 | &pThis->Spinlock,
|
---|
188 | &ulData);
|
---|
189 | switch (rc)
|
---|
190 | {
|
---|
191 | case NO_ERROR:
|
---|
192 | rc = (int)ulData;
|
---|
193 | Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
|
---|
194 | Assert(pThis->cWaking > 0);
|
---|
195 | if ( !ASMAtomicDecU32(&pThis->cWaking)
|
---|
196 | && pThis->u32Magic != RTSEMEVENTMULTI_MAGIC)
|
---|
197 | {
|
---|
198 | /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
|
---|
199 | the last thread do the cleanup. */
|
---|
200 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
201 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
202 | RTMemFree(pThis);
|
---|
203 | return VINF_SUCCESS;
|
---|
204 | }
|
---|
205 | rc = VINF_SUCCESS;
|
---|
206 | break;
|
---|
207 |
|
---|
208 | case ERROR_TIMEOUT:
|
---|
209 | Assert(cMillies != RT_INDEFINITE_WAIT);
|
---|
210 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
211 | rc = VERR_TIMEOUT;
|
---|
212 | break;
|
---|
213 |
|
---|
214 | case ERROR_INTERRUPT:
|
---|
215 | Assert(fInterruptible);
|
---|
216 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
217 | rc = VERR_INTERRUPTED;
|
---|
218 | break;
|
---|
219 |
|
---|
220 | default:
|
---|
221 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
222 | rc = VERR_GENERAL_FAILURE;
|
---|
223 | break;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
228 | return rc;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
|
---|
233 | {
|
---|
234 | return rtSemEventMultiWait(hEventMultiSem, cMillies, false /* not interruptible */);
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
|
---|
239 | {
|
---|
240 | return rtSemEventMultiWait(hEventMultiSem, cMillies, true /* interruptible */);
|
---|
241 | }
|
---|
242 |
|
---|