VirtualBox

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

Last change on this file since 77923 was 77120, checked in by vboxsync, 6 years ago

IPRT: Some license header cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.9 KB
Line 
1/* $Id: semeventmulti-r0drv-os2.cpp 77120 2019-02-01 15:08:46Z 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-2019 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 *
30 * This code is based on:
31 *
32 * Copyright (c) 2007 knut st. osmundsen <[email protected]>
33 *
34 * Permission is hereby granted, free of charge, to any person
35 * obtaining a copy of this software and associated documentation
36 * files (the "Software"), to deal in the Software without
37 * restriction, including without limitation the rights to use,
38 * copy, modify, merge, publish, distribute, sublicense, and/or sell
39 * copies of the Software, and to permit persons to whom the
40 * Software is furnished to do so, subject to the following
41 * conditions:
42 *
43 * The above copyright notice and this permission notice shall be
44 * included in all copies or substantial portions of the Software.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
48 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
50 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
51 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
52 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
53 * OTHER DEALINGS IN THE SOFTWARE.
54 */
55
56
57/*********************************************************************************************************************************
58* Header Files *
59*********************************************************************************************************************************/
60#include "the-os2-kernel.h"
61#include "internal/iprt.h"
62
63#include <iprt/semaphore.h>
64#include <iprt/asm.h>
65#include <iprt/assert.h>
66#include <iprt/err.h>
67#include <iprt/lockvalidator.h>
68#include <iprt/mem.h>
69#include "internal/magics.h"
70
71
72/*********************************************************************************************************************************
73* Structures and Typedefs *
74*********************************************************************************************************************************/
75/**
76 * OS/2 multiple release event semaphore.
77 */
78typedef struct RTSEMEVENTMULTIINTERNAL
79{
80 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
81 uint32_t volatile u32Magic;
82 /** The number of waiting threads. */
83 uint32_t volatile cWaiters;
84 /** Set if the event object is signaled. */
85 uint8_t volatile fSignaled;
86 /** The number of threads in the process of waking up. */
87 uint32_t volatile cWaking;
88 /** The OS/2 spinlock protecting this structure. */
89 SpinLock_t Spinlock;
90} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
91
92
93RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
94{
95 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
96}
97
98
99RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
100 const char *pszNameFmt, ...)
101{
102 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
103 AssertPtrReturn(phEventMultiSem, VERR_INVALID_POINTER);
104
105 AssertCompile(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
106 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
107 if (pThis)
108 {
109 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
110 pThis->cWaiters = 0;
111 pThis->cWaking = 0;
112 pThis->fSignaled = 0;
113 KernAllocSpinLock(&pThis->Spinlock);
114
115 *phEventMultiSem = pThis;
116 return VINF_SUCCESS;
117 }
118 RT_NOREF(hClass, pszNameFmt);
119 return VERR_NO_MEMORY;
120}
121
122
123RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
124{
125 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
126 if (pThis == NIL_RTSEMEVENTMULTI)
127 return VINF_SUCCESS;
128 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
129 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
130
131 KernAcquireSpinLock(&pThis->Spinlock);
132 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
133 if (pThis->cWaiters > 0)
134 {
135 /* abort waiting thread, last man cleans up. */
136 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
137 ULONG cThreads;
138 KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
139 KernReleaseSpinLock(&pThis->Spinlock);
140 }
141 else if (pThis->cWaking)
142 /* the last waking thread is gonna do the cleanup */
143 KernReleaseSpinLock(&pThis->Spinlock);
144 else
145 {
146 KernReleaseSpinLock(&pThis->Spinlock);
147 KernFreeSpinLock(&pThis->Spinlock);
148 RTMemFree(pThis);
149 }
150
151 return VINF_SUCCESS;
152}
153
154
155RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
156{
157 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
158 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
159 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
160 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
161 VERR_INVALID_HANDLE);
162
163 KernAcquireSpinLock(&pThis->Spinlock);
164
165 ASMAtomicXchgU8(&pThis->fSignaled, true);
166 if (pThis->cWaiters > 0)
167 {
168 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
169 ASMAtomicXchgU32(&pThis->cWaiters, 0);
170 ULONG cThreads;
171 KernWakeup((ULONG)pThis, WAKEUP_DATA, &cThreads, VINF_SUCCESS);
172 }
173
174 KernReleaseSpinLock(&pThis->Spinlock);
175 return VINF_SUCCESS;
176}
177
178
179RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
180{
181 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
182 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
183 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
184 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
185 VERR_INVALID_HANDLE);
186
187 KernAcquireSpinLock(&pThis->Spinlock);
188 ASMAtomicXchgU8(&pThis->fSignaled, false);
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 */
203static int rtR0SemEventMultiOs2Wait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
204 PCRTLOCKVALSRCPOS pSrcPos)
205{
206 RT_NOREF(pSrcPos);
207
208 /*
209 * Validate and convert the input.
210 */
211 if (!pThis)
212 return VERR_INVALID_HANDLE;
213 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
214 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC,
215 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
216 VERR_INVALID_HANDLE);
217 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
218
219 ULONG cMsTimeout = rtR0SemWaitOs2ConvertTimeout(fFlags, uTimeout);
220 ULONG fBlock = BLOCK_SPINLOCK;
221 if (!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE))
222 fBlock |= BLOCK_UNINTERRUPTABLE;
223
224 /*
225 * Do the job.
226 */
227 KernAcquireSpinLock(&pThis->Spinlock);
228
229 int rc;
230 if (pThis->fSignaled)
231 rc = VINF_SUCCESS;
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 != RTSEMEVENTMULTI_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 VINF_SUCCESS;
255 }
256 rc = VINF_SUCCESS;
257 break;
258
259 case ERROR_TIMEOUT:
260 Assert(cMsTimeout != SEM_INDEFINITE_WAIT);
261 ASMAtomicDecU32(&pThis->cWaiters);
262 rc = VERR_TIMEOUT;
263 break;
264
265 case ERROR_INTERRUPT:
266 Assert(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
267 ASMAtomicDecU32(&pThis->cWaiters);
268 rc = VERR_INTERRUPTED;
269 break;
270
271 default:
272 AssertMsgFailed(("rc=%d\n", rc));
273 rc = VERR_GENERAL_FAILURE;
274 break;
275 }
276 }
277
278 KernReleaseSpinLock(&pThis->Spinlock);
279 return rc;
280}
281
282
283RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
284{
285#ifndef RTSEMEVENT_STRICT
286 return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, NULL);
287#else
288 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
289 return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
290#endif
291}
292
293
294RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
295 RTHCUINTPTR uId, RT_SRC_POS_DECL)
296{
297 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
298 return rtR0SemEventMultiOs2Wait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
299}
300
301
302RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
303{
304 return 32000000; /* 32ms */
305}
306
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