VirtualBox

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

Last change on this file since 76734 was 75133, checked in by vboxsync, 6 years ago

IPRT: Shut up some warnings.

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