1 | /* $Id: semeventmulti-r0drv-os2.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - 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 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/semaphore.h>
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include <iprt/lockvalidator.h>
|
---|
43 | #include <iprt/mem.h>
|
---|
44 | #include "internal/magics.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Structures and Typedefs *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | /**
|
---|
51 | * OS/2 multiple release event semaphore.
|
---|
52 | */
|
---|
53 | typedef struct RTSEMEVENTMULTIINTERNAL
|
---|
54 | {
|
---|
55 | /** Magic value (RTSEMEVENTMULTI_MAGIC). */
|
---|
56 | uint32_t volatile u32Magic;
|
---|
57 | /** The number of waiting threads. */
|
---|
58 | uint32_t volatile cWaiters;
|
---|
59 | /** Set if the event object is signaled. */
|
---|
60 | uint8_t volatile fSignaled;
|
---|
61 | /** The number of threads in the process of waking up. */
|
---|
62 | uint32_t volatile cWaking;
|
---|
63 | /** The OS/2 spinlock protecting this structure. */
|
---|
64 | SpinLock_t Spinlock;
|
---|
65 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
66 |
|
---|
67 |
|
---|
68 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
|
---|
69 | {
|
---|
70 | return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
|
---|
75 | const char *pszNameFmt, ...)
|
---|
76 | {
|
---|
77 | AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
78 | AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
|
---|
79 |
|
---|
80 | AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
81 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
82 | if (pThis)
|
---|
83 | {
|
---|
84 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
85 | pThis->cWaiters = 0;
|
---|
86 | pThis->cWaking = 0;
|
---|
87 | pThis->fSignaled = 0;
|
---|
88 | KernAllocSpinLock(&pThis->Spinlock);
|
---|
89 |
|
---|
90 | *phEventMultiSem = pThis;
|
---|
91 | return VINF_SUCCESS;
|
---|
92 | }
|
---|
93 | return VERR_NO_MEMORY;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
|
---|
98 | {
|
---|
99 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
100 | if (pThis == NIL_RTSEMEVENTMULTI)
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
103 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
104 |
|
---|
105 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
106 | ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
|
---|
107 | if (pThis->cWaiters > 0)
|
---|
108 | {
|
---|
109 | /* abort waiting thread, last man cleans up. */
|
---|
110 | ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
|
---|
111 | ULONG cThreads;
|
---|
112 | KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
|
---|
113 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
114 | }
|
---|
115 | else if (pThis->cWaking)
|
---|
116 | /* the last waking thread is gonna do the cleanup */
|
---|
117 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
118 | else
|
---|
119 | {
|
---|
120 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
121 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
122 | RTMemFree(pThis);
|
---|
123 | }
|
---|
124 |
|
---|
125 | return VINF_SUCCESS;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
|
---|
130 | {
|
---|
131 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
132 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
133 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
134 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
135 | VERR_INVALID_HANDLE);
|
---|
136 |
|
---|
137 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
138 |
|
---|
139 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
140 | if (pThis->cWaiters > 0)
|
---|
141 | {
|
---|
142 | ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
|
---|
143 | ASMAtomicXchgU32(&pThis->cWaiters, 0);
|
---|
144 | ULONG cThreads;
|
---|
145 | KernWakeup((ULONG)pThis, WAKEUP_DATA, &cThreads, VINF_SUCCESS);
|
---|
146 | }
|
---|
147 |
|
---|
148 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
149 | return VINF_SUCCESS;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
|
---|
154 | {
|
---|
155 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
156 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
157 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
158 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
159 | VERR_INVALID_HANDLE);
|
---|
160 |
|
---|
161 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
162 | ASMAtomicXchgU8(&pThis->fSignaled, false);
|
---|
163 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
164 | return VINF_SUCCESS;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
|
---|
170 | *
|
---|
171 | * @returns VBox status code.
|
---|
172 | * @param pThis The event semaphore.
|
---|
173 | * @param fFlags See RTSemEventWaitEx.
|
---|
174 | * @param uTimeout See RTSemEventWaitEx.
|
---|
175 | * @param pSrcPos The source code position of the wait.
|
---|
176 | */
|
---|
177 | static int rtR0SemEventMultiOs2Wait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
178 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
179 | {
|
---|
180 | /*
|
---|
181 | * Validate and convert the input.
|
---|
182 | */
|
---|
183 | if (!pThis)
|
---|
184 | return VERR_INVALID_HANDLE;
|
---|
185 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
186 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
187 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
188 | VERR_INVALID_HANDLE);
|
---|
189 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
190 |
|
---|
191 | ULONG cMsTimeout = rtR0SemWaitOs2ConvertTimeout(fFlags, uTimeout);
|
---|
192 | ULONG fBlock = BLOCK_SPINLOCK;
|
---|
193 | if (!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE))
|
---|
194 | fBlock |= BLOCK_UNINTERRUPTABLE;
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * Do the job.
|
---|
198 | */
|
---|
199 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
200 |
|
---|
201 | int rc;
|
---|
202 | if (pThis->fSignaled)
|
---|
203 | rc = VINF_SUCCESS;
|
---|
204 | else
|
---|
205 | {
|
---|
206 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
207 |
|
---|
208 | ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
|
---|
209 | rc = KernBlock((ULONG)pThis, cMsTimeout, fBlock,
|
---|
210 | &pThis->Spinlock,
|
---|
211 | &ulData);
|
---|
212 | switch (rc)
|
---|
213 | {
|
---|
214 | case NO_ERROR:
|
---|
215 | rc = (int)ulData;
|
---|
216 | Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
|
---|
217 | Assert(pThis->cWaking > 0);
|
---|
218 | if ( !ASMAtomicDecU32(&pThis->cWaking)
|
---|
219 | && pThis->u32Magic != RTSEMEVENTMULTI_MAGIC)
|
---|
220 | {
|
---|
221 | /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
|
---|
222 | the last thread do the cleanup. */
|
---|
223 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
224 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
225 | RTMemFree(pThis);
|
---|
226 | return VINF_SUCCESS;
|
---|
227 | }
|
---|
228 | rc = VINF_SUCCESS;
|
---|
229 | break;
|
---|
230 |
|
---|
231 | case ERROR_TIMEOUT:
|
---|
232 | Assert(cMsTimeout != SEM_INDEFINITE_WAIT);
|
---|
233 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
234 | rc = VERR_TIMEOUT;
|
---|
235 | break;
|
---|
236 |
|
---|
237 | case ERROR_INTERRUPT:
|
---|
238 | Assert(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
|
---|
239 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
240 | rc = VERR_INTERRUPTED;
|
---|
241 | break;
|
---|
242 |
|
---|
243 | default:
|
---|
244 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
245 | rc = VERR_GENERAL_FAILURE;
|
---|
246 | break;
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
251 | return rc;
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
256 | {
|
---|
257 | #ifndef RTSEMEVENT_STRICT
|
---|
258 | return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, NULL);
|
---|
259 | #else
|
---|
260 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
261 | return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
|
---|
262 | #endif
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
267 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
268 | {
|
---|
269 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
270 | return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
|
---|
275 | {
|
---|
276 | return 32000000; /* 32ms */
|
---|
277 | }
|
---|
278 |
|
---|