1 | /* $Id: semevent-r0drv-os2.cpp 75129 2018-10-28 17:00:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Single Release Event Semaphores, Ring-0 Driver, OS/2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
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/mem.h>
|
---|
43 | #include <iprt/lockvalidator.h>
|
---|
44 |
|
---|
45 | #include "internal/magics.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 | /**
|
---|
52 | * OS/2 event semaphore.
|
---|
53 | */
|
---|
54 | typedef struct RTSEMEVENTINTERNAL
|
---|
55 | {
|
---|
56 | /** Magic value (RTSEMEVENT_MAGIC). */
|
---|
57 | uint32_t volatile u32Magic;
|
---|
58 | /** The number of waiting threads. */
|
---|
59 | uint32_t volatile cWaiters;
|
---|
60 | /** Set if the event object is signaled. */
|
---|
61 | uint8_t volatile fSignaled;
|
---|
62 | /** The number of threads in the process of waking up. */
|
---|
63 | uint32_t volatile cWaking;
|
---|
64 | /** The OS/2 spinlock protecting this structure. */
|
---|
65 | SpinLock_t Spinlock;
|
---|
66 | } RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
|
---|
67 |
|
---|
68 |
|
---|
69 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
|
---|
70 | {
|
---|
71 | return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
|
---|
76 | {
|
---|
77 | AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
|
---|
78 | Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
|
---|
79 | AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
|
---|
80 | AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
|
---|
81 | RT_NOREF(hClass, pszNameFmt);
|
---|
82 |
|
---|
83 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
84 | if (!pThis)
|
---|
85 | return VERR_NO_MEMORY;
|
---|
86 |
|
---|
87 | pThis->u32Magic = RTSEMEVENT_MAGIC;
|
---|
88 | pThis->cWaiters = 0;
|
---|
89 | pThis->cWaking = 0;
|
---|
90 | pThis->fSignaled = 0;
|
---|
91 | KernAllocSpinLock(&pThis->Spinlock);
|
---|
92 |
|
---|
93 | *phEventSem = pThis;
|
---|
94 | return VINF_SUCCESS;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
|
---|
99 | {
|
---|
100 | PRTSEMEVENTINTERNAL pThis = hEventSem;
|
---|
101 | if (pThis == NIL_RTSEMEVENT)
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
104 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), 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 |
|
---|
130 | RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
|
---|
131 | {
|
---|
132 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
|
---|
133 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
134 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
135 |
|
---|
136 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
137 |
|
---|
138 | if (pThis->cWaiters > 0)
|
---|
139 | {
|
---|
140 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
141 | ASMAtomicIncU32(&pThis->cWaking);
|
---|
142 | ULONG cThreads;
|
---|
143 | KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_ONE, &cThreads, VINF_SUCCESS);
|
---|
144 | if (RT_UNLIKELY(!cThreads))
|
---|
145 | {
|
---|
146 | /* shouldn't ever happen on OS/2 */
|
---|
147 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
148 | ASMAtomicDecU32(&pThis->cWaking);
|
---|
149 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | else
|
---|
153 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
154 |
|
---|
155 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
156 | return VINF_SUCCESS;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
|
---|
162 | *
|
---|
163 | * @returns VBox status code.
|
---|
164 | * @param pThis The event semaphore.
|
---|
165 | * @param fFlags See RTSemEventWaitEx.
|
---|
166 | * @param uTimeout See RTSemEventWaitEx.
|
---|
167 | * @param pSrcPos The source code position of the wait.
|
---|
168 | */
|
---|
169 | static int rtR0SemEventOs2Wait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
170 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
171 | {
|
---|
172 | /*
|
---|
173 | * Validate and convert input.
|
---|
174 | */
|
---|
175 | if (!pThis)
|
---|
176 | return VERR_INVALID_HANDLE;
|
---|
177 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
178 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
179 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
180 |
|
---|
181 | ULONG cMsTimeout = rtR0SemWaitOs2ConvertTimeout(fFlags, uTimeout);
|
---|
182 | ULONG fBlock = BLOCK_SPINLOCK;
|
---|
183 | if (!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE))
|
---|
184 | fBlock |= BLOCK_UNINTERRUPTABLE;
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Do the job.
|
---|
188 | */
|
---|
189 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
190 |
|
---|
191 | int rc;
|
---|
192 | if (pThis->fSignaled)
|
---|
193 | {
|
---|
194 | Assert(!pThis->cWaiters);
|
---|
195 | ASMAtomicXchgU8(&pThis->fSignaled, false);
|
---|
196 | rc = VINF_SUCCESS;
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
201 |
|
---|
202 | ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
|
---|
203 | rc = KernBlock((ULONG)pThis, cMsTimeout, fBlock,
|
---|
204 | &pThis->Spinlock,
|
---|
205 | &ulData);
|
---|
206 | switch (rc)
|
---|
207 | {
|
---|
208 | case NO_ERROR:
|
---|
209 | rc = (int)ulData;
|
---|
210 | Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
|
---|
211 | Assert(pThis->cWaking > 0);
|
---|
212 | if ( !ASMAtomicDecU32(&pThis->cWaking)
|
---|
213 | && pThis->u32Magic != RTSEMEVENT_MAGIC)
|
---|
214 | {
|
---|
215 | /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
|
---|
216 | the last thread do the cleanup. */
|
---|
217 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
218 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
219 | RTMemFree(pThis);
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 | break;
|
---|
223 |
|
---|
224 | case ERROR_TIMEOUT:
|
---|
225 | Assert(cMsTimeout != SEM_INDEFINITE_WAIT);
|
---|
226 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
227 | rc = VERR_TIMEOUT;
|
---|
228 | break;
|
---|
229 |
|
---|
230 | case ERROR_INTERRUPT:
|
---|
231 | Assert(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
|
---|
232 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
233 | rc = VERR_INTERRUPTED;
|
---|
234 | break;
|
---|
235 |
|
---|
236 | default:
|
---|
237 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
238 | rc = VERR_GENERAL_FAILURE;
|
---|
239 | break;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
244 | return rc;
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
249 | {
|
---|
250 | #ifndef RTSEMEVENT_STRICT
|
---|
251 | return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, NULL);
|
---|
252 | #else
|
---|
253 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
254 | return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
255 | #endif
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
260 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
261 | {
|
---|
262 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
263 | return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | RTDECL(uint32_t) RTSemEventGetResolution(void)
|
---|
268 | {
|
---|
269 | return 32000000; /* 32ms */
|
---|
270 | }
|
---|
271 |
|
---|