1 | /* $Id: semevent-r0drv-solaris.c 33676 2010-11-02 09:48:24Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Single Release Event Semaphores, Ring-0 Driver, Solaris.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include "the-solaris-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 | #include <iprt/semaphore.h>
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/asm.h>
|
---|
37 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
38 | # include <iprt/asm-amd64-x86.h>
|
---|
39 | #endif
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/list.h>
|
---|
42 | #include <iprt/lockvalidator.h>
|
---|
43 | #include <iprt/mem.h>
|
---|
44 | #include <iprt/mp.h>
|
---|
45 | #include <iprt/thread.h>
|
---|
46 | #include "internal/magics.h"
|
---|
47 | #include "semeventwait-r0drv-solaris.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Structures and Typedefs *
|
---|
52 | *******************************************************************************/
|
---|
53 | /**
|
---|
54 | * Waiter entry. Lives on the stack.
|
---|
55 | *
|
---|
56 | * @remarks Unfortunately, we cannot easily use cv_signal because we cannot
|
---|
57 | * distinguish between it and the spurious wakeups we get after fork.
|
---|
58 | * So, we keep an unprioritized FIFO with the sleeping threads.
|
---|
59 | */
|
---|
60 | typedef struct RTSEMEVENTSOLENTRY
|
---|
61 | {
|
---|
62 | /** The list node. */
|
---|
63 | RTLISTNODE Node;
|
---|
64 | /** The thread. */
|
---|
65 | kthread_t *pThread;
|
---|
66 | /** Flag set when waking up the thread by signal or destroy. */
|
---|
67 | bool volatile fWokenUp;
|
---|
68 | } RTSEMEVENTSOLENTRY;
|
---|
69 | /** Pointer to waiter entry. */
|
---|
70 | typedef RTSEMEVENTSOLENTRY *PRTSEMEVENTSOLENTRY;
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Solaris event semaphore.
|
---|
75 | */
|
---|
76 | typedef struct RTSEMEVENTINTERNAL
|
---|
77 | {
|
---|
78 | /** Magic value (RTSEMEVENT_MAGIC). */
|
---|
79 | uint32_t volatile u32Magic;
|
---|
80 | /** The number of threads referencing this object. */
|
---|
81 | uint32_t volatile cRefs;
|
---|
82 | /** Set if the object is signalled when there are no waiters. */
|
---|
83 | bool fSignaled;
|
---|
84 | /** List of waiting and woken up threads. */
|
---|
85 | RTLISTNODE WaitList;
|
---|
86 | /** The Solaris mutex protecting this structure and pairing up the with the cv. */
|
---|
87 | kmutex_t Mtx;
|
---|
88 | /** The Solaris condition variable. */
|
---|
89 | kcondvar_t Cnd;
|
---|
90 | } RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
|
---|
95 | {
|
---|
96 | return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
|
---|
101 | {
|
---|
102 | AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
|
---|
103 | AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
|
---|
104 | Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
|
---|
105 | AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
|
---|
106 | RT_ASSERT_PREEMPTIBLE();
|
---|
107 |
|
---|
108 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
|
---|
109 | if (!pThis)
|
---|
110 | return VERR_NO_MEMORY;
|
---|
111 |
|
---|
112 | pThis->u32Magic = RTSEMEVENT_MAGIC;
|
---|
113 | pThis->cRefs = 1;
|
---|
114 | pThis->fSignaled = false;
|
---|
115 | RTListInit(&pThis->WaitList);
|
---|
116 | mutex_init(&pThis->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
|
---|
117 | cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
|
---|
118 |
|
---|
119 | *phEventSem = pThis;
|
---|
120 | return VINF_SUCCESS;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Retain a reference to the semaphore.
|
---|
126 | *
|
---|
127 | * @param pThis The semaphore.
|
---|
128 | */
|
---|
129 | DECLINLINE(void) rtR0SemEventSolRetain(PRTSEMEVENTINTERNAL pThis)
|
---|
130 | {
|
---|
131 | uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
|
---|
132 | Assert(cRefs && cRefs < 100000);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * The destruct.
|
---|
138 | *
|
---|
139 | * @param pThis The semaphore.
|
---|
140 | */
|
---|
141 | static void rtR0SemEventSolDtor(PRTSEMEVENTINTERNAL pThis)
|
---|
142 | {
|
---|
143 | Assert(pThis->u32Magic != RTSEMEVENT_MAGIC);
|
---|
144 | cv_destroy(&pThis->Cnd);
|
---|
145 | mutex_destroy(&pThis->Mtx);
|
---|
146 | RTMemFree(pThis);
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Release a reference, destroy the thing if necessary.
|
---|
152 | *
|
---|
153 | * @param pThis The semaphore.
|
---|
154 | */
|
---|
155 | DECLINLINE(void) rtR0SemEventSolRelease(PRTSEMEVENTINTERNAL pThis)
|
---|
156 | {
|
---|
157 | if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
|
---|
158 | rtR0SemEventSolDtor(pThis);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
|
---|
163 | {
|
---|
164 | /*
|
---|
165 | * Validate input.
|
---|
166 | */
|
---|
167 | PRTSEMEVENTINTERNAL pThis = hEventSem;
|
---|
168 | if (pThis == NIL_RTSEMEVENT)
|
---|
169 | return VINF_SUCCESS;
|
---|
170 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
171 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
172 | Assert(pThis->cRefs > 0);
|
---|
173 | RT_ASSERT_INTS_ON();
|
---|
174 |
|
---|
175 | mutex_enter(&pThis->Mtx);
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * Invalidate the semaphore.
|
---|
179 | */
|
---|
180 | ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
|
---|
181 | ASMAtomicWriteBool(&pThis->fSignaled, false);
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * Abort and wake up all threads.
|
---|
185 | */
|
---|
186 | PRTSEMEVENTSOLENTRY pWaiter;
|
---|
187 | RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTSOLENTRY, Node)
|
---|
188 | {
|
---|
189 | pWaiter->fWokenUp = true;
|
---|
190 | }
|
---|
191 | cv_broadcast(&pThis->Cnd);
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * Release the reference from RTSemEventCreateEx.
|
---|
195 | */
|
---|
196 | mutex_exit(&pThis->Mtx);
|
---|
197 | rtR0SemEventSolRelease(pThis);
|
---|
198 |
|
---|
199 | return VINF_SUCCESS;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
|
---|
204 | {
|
---|
205 | PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
|
---|
206 | RT_ASSERT_PREEMPT_CPUID_VAR();
|
---|
207 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
208 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
|
---|
209 | RT_ASSERT_INTS_ON();
|
---|
210 |
|
---|
211 | rtR0SemEventSolRetain(pThis);
|
---|
212 | rtR0SemSolWaitEnterMutexWithUnpinningHack(&pThis->Mtx);
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * Wake up one thread.
|
---|
216 | */
|
---|
217 | ASMAtomicWriteBool(&pThis->fSignaled, true);
|
---|
218 |
|
---|
219 | PRTSEMEVENTSOLENTRY pWaiter;
|
---|
220 | RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTSOLENTRY, Node)
|
---|
221 | {
|
---|
222 | if (!pWaiter->fWokenUp)
|
---|
223 | {
|
---|
224 | pWaiter->fWokenUp = true;
|
---|
225 | setrun(pWaiter->pThread);
|
---|
226 | ASMAtomicWriteBool(&pThis->fSignaled, false);
|
---|
227 | break;
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | mutex_exit(&pThis->Mtx);
|
---|
232 | rtR0SemEventSolRelease(pThis);
|
---|
233 |
|
---|
234 | RT_ASSERT_PREEMPT_CPUID();
|
---|
235 | return VINF_SUCCESS;
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
|
---|
241 | *
|
---|
242 | * @returns VBox status code.
|
---|
243 | * @param pThis The event semaphore.
|
---|
244 | * @param fFlags See RTSemEventWaitEx.
|
---|
245 | * @param uTimeout See RTSemEventWaitEx.
|
---|
246 | * @param pSrcPos The source code position of the wait.
|
---|
247 | */
|
---|
248 | static int rtR0SemEventSolWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
|
---|
249 | PCRTLOCKVALSRCPOS pSrcPos)
|
---|
250 | {
|
---|
251 | /*
|
---|
252 | * Validate the input.
|
---|
253 | */
|
---|
254 | AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
|
---|
255 | AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
|
---|
256 | AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
|
---|
257 |
|
---|
258 | rtR0SemEventSolRetain(pThis);
|
---|
259 | mutex_enter(&pThis->Mtx);
|
---|
260 |
|
---|
261 | /*
|
---|
262 | * In the signaled state?
|
---|
263 | */
|
---|
264 | int rc;
|
---|
265 | if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
|
---|
266 | rc = VINF_SUCCESS;
|
---|
267 | else
|
---|
268 | {
|
---|
269 | /*
|
---|
270 | * We have to wait.
|
---|
271 | */
|
---|
272 | RTR0SEMSOLWAIT Wait;
|
---|
273 | rc = rtR0SemSolWaitInit(&Wait, fFlags, uTimeout);
|
---|
274 | if (RT_SUCCESS(rc))
|
---|
275 | {
|
---|
276 | RTSEMEVENTSOLENTRY Waiter; /* ASSUMES we won't get swapped out while waiting (TS_DONT_SWAP). */
|
---|
277 | Waiter.pThread = curthread;
|
---|
278 | Waiter.fWokenUp = false;
|
---|
279 | RTListAppend(&pThis->WaitList, &Waiter.Node);
|
---|
280 |
|
---|
281 | for (;;)
|
---|
282 | {
|
---|
283 | /* The destruction test. */
|
---|
284 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
|
---|
285 | rc = VERR_SEM_DESTROYED;
|
---|
286 | else
|
---|
287 | {
|
---|
288 | /* Check the exit conditions. */
|
---|
289 | if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
|
---|
290 | rc = VERR_SEM_DESTROYED;
|
---|
291 | else if (Waiter.fWokenUp)
|
---|
292 | rc = VINF_SUCCESS;
|
---|
293 | else if (rtR0SemSolWaitHasTimedOut(&Wait))
|
---|
294 | rc = VERR_TIMEOUT;
|
---|
295 | else if (rtR0SemSolWaitWasInterrupted(&Wait))
|
---|
296 | rc = VERR_INTERRUPTED;
|
---|
297 | else
|
---|
298 | {
|
---|
299 | /* Do the wait and then recheck the conditions. */
|
---|
300 | rtR0SemSolWaitDoIt(&Wait, &pThis->Cnd, &pThis->Mtx);
|
---|
301 | continue;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | break;
|
---|
305 | }
|
---|
306 |
|
---|
307 | rtR0SemSolWaitDelete(&Wait);
|
---|
308 | RTListNodeRemove(&Waiter.Node);
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | mutex_exit(&pThis->Mtx);
|
---|
313 | rtR0SemEventSolRelease(pThis);
|
---|
314 | return rc;
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | #undef RTSemEventWaitEx
|
---|
319 | RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
320 | {
|
---|
321 | #ifndef RTSEMEVENT_STRICT
|
---|
322 | return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, NULL);
|
---|
323 | #else
|
---|
324 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
|
---|
325 | return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
326 | #endif
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
|
---|
331 | RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
332 | {
|
---|
333 | RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
|
---|
334 | return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, &SrcPos);
|
---|
335 | }
|
---|
336 |
|
---|
337 |
|
---|
338 | RTDECL(uint32_t) RTSemEventGetResolution(void)
|
---|
339 | {
|
---|
340 | return rtR0SemSolWaitGetResolution();
|
---|
341 | }
|
---|
342 |
|
---|