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