VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/os2/semeventmulti-r0drv-os2.cpp@ 96763

Last change on this file since 96763 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 11.3 KB
Line 
1/* $Id: semeventmulti-r0drv-os2.cpp 96407 2022-08-22 17:43:14Z 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-2022 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 <bird-src-spam@anduin.net>
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/lockvalidator.h>
77#include <iprt/mem.h>
78#include "internal/magics.h"
79
80
81/*********************************************************************************************************************************
82* Structures and Typedefs *
83*********************************************************************************************************************************/
84/**
85 * OS/2 multiple release event semaphore.
86 */
87typedef struct RTSEMEVENTMULTIINTERNAL
88{
89 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
90 uint32_t volatile u32Magic;
91 /** The number of waiting threads. */
92 uint32_t volatile cWaiters;
93 /** Set if the event object is signaled. */
94 uint8_t volatile fSignaled;
95 /** The number of threads in the process of waking up. */
96 uint32_t volatile cWaking;
97 /** The OS/2 spinlock protecting this structure. */
98 SpinLock_t Spinlock;
99} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
100
101
102RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
103{
104 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
105}
106
107
108RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
109 const char *pszNameFmt, ...)
110{
111 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
112 AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
113
114 AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
115 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
116 if (pThis)
117 {
118 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
119 pThis->cWaiters = 0;
120 pThis->cWaking = 0;
121 pThis->fSignaled = 0;
122 KernAllocSpinLock(&pThis->Spinlock);
123
124 *phEventMultiSem = pThis;
125 return VINF_SUCCESS;
126 }
127 RT_NOREF(hClass, pszNameFmt);
128 return VERR_NO_MEMORY;
129}
130
131
132RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
133{
134 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
135 if (pThis == NIL_RTSEMEVENTMULTI)
136 return VINF_SUCCESS;
137 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
138 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), 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
164RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
165{
166 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
167 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
168 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
169 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
170 VERR_INVALID_HANDLE);
171
172 KernAcquireSpinLock(&pThis->Spinlock);
173
174 ASMAtomicXchgU8(&pThis->fSignaled, true);
175 if (pThis->cWaiters > 0)
176 {
177 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
178 ASMAtomicXchgU32(&pThis->cWaiters, 0);
179 ULONG cThreads;
180 KernWakeup((ULONG)pThis, WAKEUP_DATA, &cThreads, VINF_SUCCESS);
181 }
182
183 KernReleaseSpinLock(&pThis->Spinlock);
184 return VINF_SUCCESS;
185}
186
187
188RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
189{
190 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
191 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
192 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
193 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
194 VERR_INVALID_HANDLE);
195
196 KernAcquireSpinLock(&pThis->Spinlock);
197 ASMAtomicXchgU8(&pThis->fSignaled, false);
198 KernReleaseSpinLock(&pThis->Spinlock);
199 return VINF_SUCCESS;
200}
201
202
203/**
204 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
205 *
206 * @returns VBox status code.
207 * @param pThis The event semaphore.
208 * @param fFlags See RTSemEventWaitEx.
209 * @param uTimeout See RTSemEventWaitEx.
210 * @param pSrcPos The source code position of the wait.
211 */
212static int rtR0SemEventMultiOs2Wait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
213 PCRTLOCKVALSRCPOS pSrcPos)
214{
215 RT_NOREF(pSrcPos);
216
217 /*
218 * Validate and convert the input.
219 */
220 if (!pThis)
221 return VERR_INVALID_HANDLE;
222 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
223 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
224 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
225 VERR_INVALID_HANDLE);
226 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
227
228 ULONG cMsTimeout = rtR0SemWaitOs2ConvertTimeout(fFlags, uTimeout);
229 ULONG fBlock = BLOCK_SPINLOCK;
230 if (!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE))
231 fBlock |= BLOCK_UNINTERRUPTABLE;
232
233 /*
234 * Do the job.
235 */
236 KernAcquireSpinLock(&pThis->Spinlock);
237
238 int rc;
239 if (pThis->fSignaled)
240 rc = VINF_SUCCESS;
241 else
242 {
243 ASMAtomicIncU32(&pThis->cWaiters);
244
245 ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
246 rc = KernBlock((ULONG)pThis, cMsTimeout, fBlock,
247 &pThis->Spinlock,
248 &ulData);
249 switch (rc)
250 {
251 case NO_ERROR:
252 rc = (int)ulData;
253 Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
254 Assert(pThis->cWaking > 0);
255 if ( !ASMAtomicDecU32(&pThis->cWaking)
256 && pThis->u32Magic != RTSEMEVENTMULTI_MAGIC)
257 {
258 /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
259 the last thread do the cleanup. */
260 KernReleaseSpinLock(&pThis->Spinlock);
261 KernFreeSpinLock(&pThis->Spinlock);
262 RTMemFree(pThis);
263 return VINF_SUCCESS;
264 }
265 rc = VINF_SUCCESS;
266 break;
267
268 case ERROR_TIMEOUT:
269 Assert(cMsTimeout != SEM_INDEFINITE_WAIT);
270 ASMAtomicDecU32(&pThis->cWaiters);
271 rc = VERR_TIMEOUT;
272 break;
273
274 case ERROR_INTERRUPT:
275 Assert(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
276 ASMAtomicDecU32(&pThis->cWaiters);
277 rc = VERR_INTERRUPTED;
278 break;
279
280 default:
281 AssertMsgFailed(("rc=%d\n", rc));
282 rc = VERR_GENERAL_FAILURE;
283 break;
284 }
285 }
286
287 KernReleaseSpinLock(&pThis->Spinlock);
288 return rc;
289}
290
291
292RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
293{
294#ifndef RTSEMEVENT_STRICT
295 return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, NULL);
296#else
297 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
298 return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
299#endif
300}
301
302
303RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
304 RTHCUINTPTR uId, RT_SRC_POS_DECL)
305{
306 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
307 return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
308}
309
310
311RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
312{
313 return 32000000; /* 32ms */
314}
315
316
317RTR0DECL(bool) RTSemEventMultiIsSignalSafe(void)
318{
319 return true;
320}
321
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette