VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/os2/semevent-r0drv-os2.cpp@ 82240

Last change on this file since 82240 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.3 KB
Line 
1/* $Id: semevent-r0drv-os2.cpp 77120 2019-02-01 15:08:46Z 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-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/mem.h>
68#include <iprt/lockvalidator.h>
69
70#include "internal/magics.h"
71
72
73/*********************************************************************************************************************************
74* Structures and Typedefs *
75*********************************************************************************************************************************/
76/**
77 * OS/2 event semaphore.
78 */
79typedef struct RTSEMEVENTINTERNAL
80{
81 /** Magic value (RTSEMEVENT_MAGIC). */
82 uint32_t volatile u32Magic;
83 /** The number of waiting threads. */
84 uint32_t volatile cWaiters;
85 /** Set if the event object is signaled. */
86 uint8_t volatile fSignaled;
87 /** The number of threads in the process of waking up. */
88 uint32_t volatile cWaking;
89 /** The OS/2 spinlock protecting this structure. */
90 SpinLock_t Spinlock;
91} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
92
93
94RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
95{
96 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
97}
98
99
100RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
101{
102 AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
103 Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
104 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
105 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
106 RT_NOREF(hClass, pszNameFmt);
107
108 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
109 if (!pThis)
110 return VERR_NO_MEMORY;
111
112 pThis->u32Magic = RTSEMEVENT_MAGIC;
113 pThis->cWaiters = 0;
114 pThis->cWaking = 0;
115 pThis->fSignaled = 0;
116 KernAllocSpinLock(&pThis->Spinlock);
117
118 *phEventSem = pThis;
119 return VINF_SUCCESS;
120}
121
122
123RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
124{
125 PRTSEMEVENTINTERNAL pThis = hEventSem;
126 if (pThis == NIL_RTSEMEVENT)
127 return VINF_SUCCESS;
128 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
129 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), 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) RTSemEventSignal(RTSEMEVENT hEventSem)
156{
157 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
158 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
159 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
160
161 KernAcquireSpinLock(&pThis->Spinlock);
162
163 if (pThis->cWaiters > 0)
164 {
165 ASMAtomicDecU32(&pThis->cWaiters);
166 ASMAtomicIncU32(&pThis->cWaking);
167 ULONG cThreads;
168 KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_ONE, &cThreads, VINF_SUCCESS);
169 if (RT_UNLIKELY(!cThreads))
170 {
171 /* shouldn't ever happen on OS/2 */
172 ASMAtomicXchgU8(&pThis->fSignaled, true);
173 ASMAtomicDecU32(&pThis->cWaking);
174 ASMAtomicIncU32(&pThis->cWaiters);
175 }
176 }
177 else
178 ASMAtomicXchgU8(&pThis->fSignaled, true);
179
180 KernReleaseSpinLock(&pThis->Spinlock);
181 return VINF_SUCCESS;
182}
183
184
185/**
186 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
187 *
188 * @returns VBox status code.
189 * @param pThis The event semaphore.
190 * @param fFlags See RTSemEventWaitEx.
191 * @param uTimeout See RTSemEventWaitEx.
192 * @param pSrcPos The source code position of the wait.
193 */
194static int rtR0SemEventOs2Wait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
195 PCRTLOCKVALSRCPOS pSrcPos)
196{
197 /*
198 * Validate and convert input.
199 */
200 if (!pThis)
201 return VERR_INVALID_HANDLE;
202 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
203 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
204 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
205
206 ULONG cMsTimeout = rtR0SemWaitOs2ConvertTimeout(fFlags, uTimeout);
207 ULONG fBlock = BLOCK_SPINLOCK;
208 if (!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE))
209 fBlock |= BLOCK_UNINTERRUPTABLE;
210
211 /*
212 * Do the job.
213 */
214 KernAcquireSpinLock(&pThis->Spinlock);
215
216 int rc;
217 if (pThis->fSignaled)
218 {
219 Assert(!pThis->cWaiters);
220 ASMAtomicXchgU8(&pThis->fSignaled, false);
221 rc = VINF_SUCCESS;
222 }
223 else
224 {
225 ASMAtomicIncU32(&pThis->cWaiters);
226
227 ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
228 rc = KernBlock((ULONG)pThis, cMsTimeout, fBlock,
229 &pThis->Spinlock,
230 &ulData);
231 switch (rc)
232 {
233 case NO_ERROR:
234 rc = (int)ulData;
235 Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
236 Assert(pThis->cWaking > 0);
237 if ( !ASMAtomicDecU32(&pThis->cWaking)
238 && pThis->u32Magic != RTSEMEVENT_MAGIC)
239 {
240 /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
241 the last thread do the cleanup. */
242 KernReleaseSpinLock(&pThis->Spinlock);
243 KernFreeSpinLock(&pThis->Spinlock);
244 RTMemFree(pThis);
245 return rc;
246 }
247 break;
248
249 case ERROR_TIMEOUT:
250 Assert(cMsTimeout != SEM_INDEFINITE_WAIT);
251 ASMAtomicDecU32(&pThis->cWaiters);
252 rc = VERR_TIMEOUT;
253 break;
254
255 case ERROR_INTERRUPT:
256 Assert(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
257 ASMAtomicDecU32(&pThis->cWaiters);
258 rc = VERR_INTERRUPTED;
259 break;
260
261 default:
262 AssertMsgFailed(("rc=%d\n", rc));
263 rc = VERR_GENERAL_FAILURE;
264 break;
265 }
266 }
267
268 KernReleaseSpinLock(&pThis->Spinlock);
269 return rc;
270}
271
272
273RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
274{
275#ifndef RTSEMEVENT_STRICT
276 return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, NULL);
277#else
278 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
279 return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, &SrcPos);
280#endif
281}
282
283
284RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
285 RTHCUINTPTR uId, RT_SRC_POS_DECL)
286{
287 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
288 return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, &SrcPos);
289}
290
291
292RTDECL(uint32_t) RTSemEventGetResolution(void)
293{
294 return 32000000; /* 32ms */
295}
296
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