1 | /* $Id: semevent-r0drv-os2.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Single 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/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 |
|
---|
82 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
83 | if (!pThis)
|
---|
84 | return VERR_NO_MEMORY;
|
---|
85 |
|
---|
86 | pThis->u32Magic = RTSEMEVENT_MAGIC;
|
---|
87 | pThis->cWaiters = 0;
|
---|
88 | pThis->cWaking = 0;
|
---|
89 | pThis->fSignaled = 0;
|
---|
90 | KernAllocSpinLock(&pThis->Spinlock);
|
---|
91 |
|
---|
92 | *phEventSem = pThis;
|
---|
93 | return VINF_SUCCESS;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
|
---|
98 | {
|
---|
99 | PRTSEMEVENTINTERNAL pThis = hEventSem;
|
---|
100 | if (pThis == NIL_RTSEMEVENT)
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
103 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
104 |
|
---|
105 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
106 | ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
|
---|
107 | if (pThis->cWaiters > 0)
|
---|
108 | {
|
---|
109 | /* abort waiting thread, last man cleans up. */
|
---|
110 | ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
|
---|
111 | ULONG cThreads;
|
---|
112 | KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_BOOST, &cThreads, (ULONG)VERR_SEM_DESTROYED);
|
---|
113 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
114 | }
|
---|
115 | else if (pThis->cWaking)
|
---|
116 | /* the last waking thread is gonna do the cleanup */
|
---|
117 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
118 | else
|
---|
119 | {
|
---|
120 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
121 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
122 | RTMemFree(pThis);
|
---|
123 | }
|
---|
124 |
|
---|
125 | return VINF_SUCCESS;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
|
---|
130 | {
|
---|
131 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
|
---|
132 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
133 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
134 |
|
---|
135 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
136 |
|
---|
137 | if (pThis->cWaiters > 0)
|
---|
138 | {
|
---|
139 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
140 | ASMAtomicIncU32(&pThis->cWaking);
|
---|
141 | ULONG cThreads;
|
---|
142 | KernWakeup((ULONG)pThis, WAKEUP_DATA | WAKEUP_ONE, &cThreads, VINF_SUCCESS);
|
---|
143 | if (RT_UNLIKELY(!cThreads))
|
---|
144 | {
|
---|
145 | /* shouldn't ever happen on OS/2 */
|
---|
146 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
147 | ASMAtomicDecU32(&pThis->cWaking);
|
---|
148 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | else
|
---|
152 | ASMAtomicXchgU8(&pThis->fSignaled, true);
|
---|
153 |
|
---|
154 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
155 | return VINF_SUCCESS;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
|
---|
161 | *
|
---|
162 | * @returns VBox status code.
|
---|
163 | * @param pThis The event semaphore.
|
---|
164 | * @param fFlags See RTSemEventWaitEx.
|
---|
165 | * @param uTimeout See RTSemEventWaitEx.
|
---|
166 | * @param pSrcPos The source code position of the wait.
|
---|
167 | */
|
---|
168 | static int rtR0SemEventOs2Wait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
169 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
170 | {
|
---|
171 | /*
|
---|
172 | * Validate and convert input.
|
---|
173 | */
|
---|
174 | if (!pThis)
|
---|
175 | return VERR_INVALID_HANDLE;
|
---|
176 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
177 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
178 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
179 |
|
---|
180 | ULONG cMsTimeout = rtR0SemWaitOs2ConvertTimeout(fFlags, uTimeout);
|
---|
181 | ULONG fBlock = BLOCK_SPINLOCK;
|
---|
182 | if (!(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE))
|
---|
183 | fBlock |= BLOCK_UNINTERRUPTABLE;
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * Do the job.
|
---|
187 | */
|
---|
188 | KernAcquireSpinLock(&pThis->Spinlock);
|
---|
189 |
|
---|
190 | int rc;
|
---|
191 | if (pThis->fSignaled)
|
---|
192 | {
|
---|
193 | Assert(!pThis->cWaiters);
|
---|
194 | ASMAtomicXchgU8(&pThis->fSignaled, false);
|
---|
195 | rc = VINF_SUCCESS;
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | ASMAtomicIncU32(&pThis->cWaiters);
|
---|
200 |
|
---|
201 | ULONG ulData = (ULONG)VERR_INTERNAL_ERROR;
|
---|
202 | rc = KernBlock((ULONG)pThis, cMsTimeout, fBlock,
|
---|
203 | &pThis->Spinlock,
|
---|
204 | &ulData);
|
---|
205 | switch (rc)
|
---|
206 | {
|
---|
207 | case NO_ERROR:
|
---|
208 | rc = (int)ulData;
|
---|
209 | Assert(rc == VINF_SUCCESS || rc == VERR_SEM_DESTROYED);
|
---|
210 | Assert(pThis->cWaking > 0);
|
---|
211 | if ( !ASMAtomicDecU32(&pThis->cWaking)
|
---|
212 | && pThis->u32Magic != RTSEMEVENT_MAGIC)
|
---|
213 | {
|
---|
214 | /* The event was destroyed (ulData == VINF_SUCCESS if it was after we awoke), as
|
---|
215 | the last thread do the cleanup. */
|
---|
216 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
217 | KernFreeSpinLock(&pThis->Spinlock);
|
---|
218 | RTMemFree(pThis);
|
---|
219 | return rc;
|
---|
220 | }
|
---|
221 | break;
|
---|
222 |
|
---|
223 | case ERROR_TIMEOUT:
|
---|
224 | Assert(cMsTimeout != SEM_INDEFINITE_WAIT);
|
---|
225 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
226 | rc = VERR_TIMEOUT;
|
---|
227 | break;
|
---|
228 |
|
---|
229 | case ERROR_INTERRUPT:
|
---|
230 | Assert(fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE);
|
---|
231 | ASMAtomicDecU32(&pThis->cWaiters);
|
---|
232 | rc = VERR_INTERRUPTED;
|
---|
233 | break;
|
---|
234 |
|
---|
235 | default:
|
---|
236 | AssertMsgFailed(("rc=%d\n", rc));
|
---|
237 | rc = VERR_GENERAL_FAILURE;
|
---|
238 | break;
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | KernReleaseSpinLock(&pThis->Spinlock);
|
---|
243 | return rc;
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
248 | {
|
---|
249 | #ifndef RTSEMEVENT_STRICT
|
---|
250 | return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, NULL);
|
---|
251 | #else
|
---|
252 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
253 | return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
254 | #endif
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
259 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
260 | {
|
---|
261 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
262 | return rtR0SemEventOs2Wait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | RTDECL(uint32_t) RTSemEventGetResolution(void)
|
---|
267 | {
|
---|
268 | return 32000000; /* 32ms */
|
---|
269 | }
|
---|
270 |
|
---|