1 | /* $Id: semeventmulti-r0drv-os2.cpp 90488 2021-08-03 09:17:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, OS/2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Contributed by knut st. osmundsen.
|
---|
8 | *
|
---|
9 | * Copyright (C) 2007-2020 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * The contents of this file may alternatively be used under the terms
|
---|
20 | * of the Common Development and Distribution License Version 1.0
|
---|
21 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
22 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
23 | * CDDL are applicable instead of those of the GPL.
|
---|
24 | *
|
---|
25 | * You may elect to license modified versions of this file under the
|
---|
26 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
27 | * --------------------------------------------------------------------
|
---|
28 | *
|
---|
29 | * This code is based on:
|
---|
30 | *
|
---|
31 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
32 | *
|
---|
33 | * Permission is hereby granted, free of charge, to any person
|
---|
34 | * obtaining a copy of this software and associated documentation
|
---|
35 | * files (the "Software"), to deal in the Software without
|
---|
36 | * restriction, including without limitation the rights to use,
|
---|
37 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
38 | * copies of the Software, and to permit persons to whom the
|
---|
39 | * Software is furnished to do so, subject to the following
|
---|
40 | * conditions:
|
---|
41 | *
|
---|
42 | * The above copyright notice and this permission notice shall be
|
---|
43 | * included in all copies or substantial portions of the Software.
|
---|
44 | *
|
---|
45 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
46 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
47 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
48 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
49 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
50 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
51 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
52 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
53 | */
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * Header Files *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 | #include "the-os2-kernel.h"
|
---|
60 | #include "internal/iprt.h"
|
---|
61 |
|
---|
62 | #include <iprt/semaphore.h>
|
---|
63 | #include <iprt/asm.h>
|
---|
64 | #include <iprt/assert.h>
|
---|
65 | #include <iprt/err.h>
|
---|
66 | #include <iprt/lockvalidator.h>
|
---|
67 | #include <iprt/mem.h>
|
---|
68 | #include "internal/magics.h"
|
---|
69 |
|
---|
70 |
|
---|
71 | /*********************************************************************************************************************************
|
---|
72 | * Structures and Typedefs *
|
---|
73 | *********************************************************************************************************************************/
|
---|
74 | /**
|
---|
75 | * OS/2 multiple release event semaphore.
|
---|
76 | */
|
---|
77 | typedef struct RTSEMEVENTMULTIINTERNAL
|
---|
78 | {
|
---|
79 | /** Magic value (RTSEMEVENTMULTI_MAGIC). */
|
---|
80 | uint32_t volatile u32Magic;
|
---|
81 | /** The number of waiting threads. */
|
---|
82 | uint32_t volatile cWaiters;
|
---|
83 | /** Set if the event object is signaled. */
|
---|
84 | uint8_t volatile fSignaled;
|
---|
85 | /** The number of threads in the process of waking up. */
|
---|
86 | uint32_t volatile cWaking;
|
---|
87 | /** The OS/2 spinlock protecting this structure. */
|
---|
88 | SpinLock_t Spinlock;
|
---|
89 | } RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
|
---|
93 | {
|
---|
94 | return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
|
---|
99 | const char *pszNameFmt, ...)
|
---|
100 | {
|
---|
101 | AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
|
---|
102 | AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
|
---|
103 |
|
---|
104 | AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
|
---|
105 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
106 | if (pThis)
|
---|
107 | {
|
---|
108 | pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
|
---|
109 | pThis->cWaiters = 0;
|
---|
110 | pThis->cWaking = 0;
|
---|
111 | pThis->fSignaled = 0;
|
---|
112 | KernAllocSpinLock(&pThis->Spinlock);
|
---|
113 |
|
---|
114 | *phEventMultiSem = pThis;
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 | RT_NOREF(hClass, pszNameFmt);
|
---|
118 | return VERR_NO_MEMORY;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
|
---|
123 | {
|
---|
124 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
125 | if (pThis == NIL_RTSEMEVENTMULTI)
|
---|
126 | return VINF_SUCCESS;
|
---|
127 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
128 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
129 |
|
---|
130 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
131 | ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
|
---|
132 | if (pThis->cWaiters > 0)
|
---|
133 | {
|
---|
134 | /* abort waiting thread, last man cleans up. */
|
---|
135 | ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
|
---|
136 | ULONG cThreads;
|
---|
137 | KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
|
---|
138 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
139 | }
|
---|
140 | else if (pThis->cWaking)
|
---|
141 | /* the last waking thread is gonna do the cleanup */
|
---|
142 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
143 | else
|
---|
144 | {
|
---|
145 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
146 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
147 | RTMemFree(pThis);
|
---|
148 | }
|
---|
149 |
|
---|
150 | return VINF_SUCCESS;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
|
---|
155 | {
|
---|
156 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
157 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
158 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
159 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
160 | VERR_INVALID_HANDLE);
|
---|
161 |
|
---|
162 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
163 |
|
---|
164 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
165 | if (pThis->cWaiters > 0)
|
---|
166 | {
|
---|
167 | ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
|
---|
168 | ASMAtomicXchgU32(&pThis->cWaiters, 0);
|
---|
169 | ULONG cThreads;
|
---|
170 | KernWakeup((ULONG)pThis, WAKEUP_DATA, &cThreads, VINF_SUCCESS);
|
---|
171 | }
|
---|
172 |
|
---|
173 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
174 | return VINF_SUCCESS;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
|
---|
179 | {
|
---|
180 | PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
|
---|
181 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
182 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
183 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
184 | VERR_INVALID_HANDLE);
|
---|
185 |
|
---|
186 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
187 | ASMAtomicXchgU8(&pThis->fSignaled, false);
|
---|
188 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
189 | return VINF_SUCCESS;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
|
---|
195 | *
|
---|
196 | * @returns VBox status code.
|
---|
197 | * @param pThis The event semaphore.
|
---|
198 | * @param fFlags See RTSemEventWaitEx.
|
---|
199 | * @param uTimeout See RTSemEventWaitEx.
|
---|
200 | * @param pSrcPos The source code position of the wait.
|
---|
201 | */
|
---|
202 | static int rtR0SemEventMultiOs2Wait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
203 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
204 | {
|
---|
205 | RT_NOREF(pSrcPos);
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Validate and convert the input.
|
---|
209 | */
|
---|
210 | if (!pThis)
|
---|
211 | return VERR_INVALID_HANDLE;
|
---|
212 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
213 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
|
---|
214 | ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
|
---|
215 | VERR_INVALID_HANDLE);
|
---|
216 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
217 |
|
---|
218 | ULONG cMsTimeout = rtR0SemWaitOs2ConvertTimeout(fFlags, uTimeout);
|
---|
219 | ULONG fBlock = BLOCK_SPINLOCK;
|
---|
220 | if (!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE))
|
---|
221 | fBlock |= BLOCK_UNINTERRUPTABLE;
|
---|
222 |
|
---|
223 | /*
|
---|
224 | * Do the job.
|
---|
225 | */
|
---|
226 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
227 |
|
---|
228 | int rc;
|
---|
229 | if (pThis->fSignaled)
|
---|
230 | rc = VINF_SUCCESS;
|
---|
231 | else
|
---|
232 | {
|
---|
233 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
234 |
|
---|
235 | ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
|
---|
236 | rc = KernBlock((ULONG)pThis, cMsTimeout, fBlock,
|
---|
237 | &pThis->Spinlock,
|
---|
238 | &ulData);
|
---|
239 | switch (rc)
|
---|
240 | {
|
---|
241 | case NO_ERROR:
|
---|
242 | rc = (int)ulData;
|
---|
243 | Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
|
---|
244 | Assert(pThis->cWaking > 0);
|
---|
245 | if ( !ASMAtomicDecU32(&pThis->cWaking)
|
---|
246 | && pThis->u32Magic != RTSEMEVENTMULTI_MAGIC)
|
---|
247 | {
|
---|
248 | /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
|
---|
249 | the last thread do the cleanup. */
|
---|
250 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
251 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
252 | RTMemFree(pThis);
|
---|
253 | return VINF_SUCCESS;
|
---|
254 | }
|
---|
255 | rc = VINF_SUCCESS;
|
---|
256 | break;
|
---|
257 |
|
---|
258 | case ERROR_TIMEOUT:
|
---|
259 | Assert(cMsTimeout != SEM_INDEFINITE_WAIT);
|
---|
260 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
261 | rc = VERR_TIMEOUT;
|
---|
262 | break;
|
---|
263 |
|
---|
264 | case ERROR_INTERRUPT:
|
---|
265 | Assert(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
|
---|
266 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
267 | rc = VERR_INTERRUPTED;
|
---|
268 | break;
|
---|
269 |
|
---|
270 | default:
|
---|
271 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
272 | rc = VERR_GENERAL_FAILURE;
|
---|
273 | break;
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
278 | return rc;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
283 | {
|
---|
284 | #ifndef RTSEMEVENT_STRICT
|
---|
285 | return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, NULL);
|
---|
286 | #else
|
---|
287 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
288 | return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
|
---|
289 | #endif
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
294 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
295 | {
|
---|
296 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
297 | return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
|
---|
302 | {
|
---|
303 | return 32000000; /* 32ms */
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 | RTR0DECL(bool) RTSemEventMultiIsSignalSafe(void)
|
---|
308 | {
|
---|
309 | return true;
|
---|
310 | }
|
---|
311 |
|
---|