1 | /* $Id: semevent-r0drv-os2.cpp 82968 2020-02-04 10:35:17Z 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-2020 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 | * This code is based on:
|
---|
30 | *
|
---|
31 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
32 | *
|
---|
33 | * Permission is hereby granted, free of charge, to any person
|
---|
34 | * obtaining a copy of this software and associated documentation
|
---|
35 | * files (the "Software"), to deal in the Software without
|
---|
36 | * restriction, including without limitation the rights to use,
|
---|
37 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
38 | * copies of the Software, and to permit persons to whom the
|
---|
39 | * Software is furnished to do so, subject to the following
|
---|
40 | * conditions:
|
---|
41 | *
|
---|
42 | * The above copyright notice and this permission notice shall be
|
---|
43 | * included in all copies or substantial portions of the Software.
|
---|
44 | *
|
---|
45 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
46 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
47 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
48 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
49 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
50 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
51 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
52 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
53 | */
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * Header Files *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 | #include "the-os2-kernel.h"
|
---|
60 | #include "internal/iprt.h"
|
---|
61 |
|
---|
62 | #include <iprt/semaphore.h>
|
---|
63 | #include <iprt/asm.h>
|
---|
64 | #include <iprt/assert.h>
|
---|
65 | #include <iprt/err.h>
|
---|
66 | #include <iprt/mem.h>
|
---|
67 | #include <iprt/lockvalidator.h>
|
---|
68 |
|
---|
69 | #include "internal/magics.h"
|
---|
70 |
|
---|
71 |
|
---|
72 | /*********************************************************************************************************************************
|
---|
73 | * Structures and Typedefs *
|
---|
74 | *********************************************************************************************************************************/
|
---|
75 | /**
|
---|
76 | * OS/2 event semaphore.
|
---|
77 | */
|
---|
78 | typedef struct RTSEMEVENTINTERNAL
|
---|
79 | {
|
---|
80 | /** Magic value (RTSEMEVENT_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 | } RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
|
---|
91 |
|
---|
92 |
|
---|
93 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
|
---|
94 | {
|
---|
95 | return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
|
---|
100 | {
|
---|
101 | AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
|
---|
102 | Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
|
---|
103 | AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
|
---|
104 | AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
|
---|
105 | RT_NOREF(hClass, pszNameFmt);
|
---|
106 |
|
---|
107 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
108 | if (!pThis)
|
---|
109 | return VERR_NO_MEMORY;
|
---|
110 |
|
---|
111 | pThis->u32Magic = RTSEMEVENT_MAGIC;
|
---|
112 | pThis->cWaiters = 0;
|
---|
113 | pThis->cWaking = 0;
|
---|
114 | pThis->fSignaled = 0;
|
---|
115 | KernAllocSpinLock(&pThis->Spinlock);
|
---|
116 |
|
---|
117 | *phEventSem = pThis;
|
---|
118 | return VINF_SUCCESS;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
|
---|
123 | {
|
---|
124 | PRTSEMEVENTINTERNAL pThis = hEventSem;
|
---|
125 | if (pThis == NIL_RTSEMEVENT)
|
---|
126 | return VINF_SUCCESS;
|
---|
127 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
128 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
129 |
|
---|
130 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
131 | ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
|
---|
132 | if (pThis->cWaiters > 0)
|
---|
133 | {
|
---|
134 | /* abort waiting thread, last man cleans up. */
|
---|
135 | ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
|
---|
136 | ULONG cThreads;
|
---|
137 | KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
|
---|
138 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
139 | }
|
---|
140 | else if (pThis->cWaking)
|
---|
141 | /* the last waking thread is gonna do the cleanup */
|
---|
142 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
143 | else
|
---|
144 | {
|
---|
145 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
146 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
147 | RTMemFree(pThis);
|
---|
148 | }
|
---|
149 |
|
---|
150 | return VINF_SUCCESS;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
|
---|
155 | {
|
---|
156 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
|
---|
157 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
158 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
159 |
|
---|
160 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
161 |
|
---|
162 | if (pThis->cWaiters > 0)
|
---|
163 | {
|
---|
164 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
165 | ASMAtomicIncU32(&pThis->cWaking);
|
---|
166 | ULONG cThreads;
|
---|
167 | KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_ONE, &cThreads, VINF_SUCCESS);
|
---|
168 | if (RT_UNLIKELY(!cThreads))
|
---|
169 | {
|
---|
170 | /* shouldn't ever happen on OS/2 */
|
---|
171 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
172 | ASMAtomicDecU32(&pThis->cWaking);
|
---|
173 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | else
|
---|
177 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
178 |
|
---|
179 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
180 | return VINF_SUCCESS;
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
|
---|
186 | *
|
---|
187 | * @returns VBox status code.
|
---|
188 | * @param pThis The event semaphore.
|
---|
189 | * @param fFlags See RTSemEventWaitEx.
|
---|
190 | * @param uTimeout See RTSemEventWaitEx.
|
---|
191 | * @param pSrcPos The source code position of the wait.
|
---|
192 | */
|
---|
193 | static int rtR0SemEventOs2Wait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
194 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
195 | {
|
---|
196 | /*
|
---|
197 | * Validate and convert input.
|
---|
198 | */
|
---|
199 | if (!pThis)
|
---|
200 | return VERR_INVALID_HANDLE;
|
---|
201 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
202 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
203 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
204 |
|
---|
205 | ULONG cMsTimeout = rtR0SemWaitOs2ConvertTimeout(fFlags, uTimeout);
|
---|
206 | ULONG fBlock = BLOCK_SPINLOCK;
|
---|
207 | if (!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE))
|
---|
208 | fBlock |= BLOCK_UNINTERRUPTABLE;
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * Do the job.
|
---|
212 | */
|
---|
213 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
214 |
|
---|
215 | int rc;
|
---|
216 | if (pThis->fSignaled)
|
---|
217 | {
|
---|
218 | Assert(!pThis->cWaiters);
|
---|
219 | ASMAtomicXchgU8(&pThis->fSignaled, false);
|
---|
220 | rc = VINF_SUCCESS;
|
---|
221 | }
|
---|
222 | else
|
---|
223 | {
|
---|
224 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
225 |
|
---|
226 | ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
|
---|
227 | rc = KernBlock((ULONG)pThis, cMsTimeout, fBlock,
|
---|
228 | &pThis->Spinlock,
|
---|
229 | &ulData);
|
---|
230 | switch (rc)
|
---|
231 | {
|
---|
232 | case NO_ERROR:
|
---|
233 | rc = (int)ulData;
|
---|
234 | Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
|
---|
235 | Assert(pThis->cWaking > 0);
|
---|
236 | if ( !ASMAtomicDecU32(&pThis->cWaking)
|
---|
237 | && pThis->u32Magic != RTSEMEVENT_MAGIC)
|
---|
238 | {
|
---|
239 | /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
|
---|
240 | the last thread do the cleanup. */
|
---|
241 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
242 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
243 | RTMemFree(pThis);
|
---|
244 | return rc;
|
---|
245 | }
|
---|
246 | break;
|
---|
247 |
|
---|
248 | case ERROR_TIMEOUT:
|
---|
249 | Assert(cMsTimeout != SEM_INDEFINITE_WAIT);
|
---|
250 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
251 | rc = VERR_TIMEOUT;
|
---|
252 | break;
|
---|
253 |
|
---|
254 | case ERROR_INTERRUPT:
|
---|
255 | Assert(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
|
---|
256 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
257 | rc = VERR_INTERRUPTED;
|
---|
258 | break;
|
---|
259 |
|
---|
260 | default:
|
---|
261 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
262 | rc = VERR_GENERAL_FAILURE;
|
---|
263 | break;
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
273 | {
|
---|
274 | #ifndef RTSEMEVENT_STRICT
|
---|
275 | return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, NULL);
|
---|
276 | #else
|
---|
277 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
278 | return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
279 | #endif
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|
283 | RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
284 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
285 | {
|
---|
286 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
287 | return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | RTDECL(uint32_t) RTSemEventGetResolution(void)
|
---|
292 | {
|
---|
293 | return 32000000; /* 32ms */
|
---|
294 | }
|
---|
295 |
|
---|