VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/semevent-r0drv-solaris.c@ 40227

Last change on this file since 40227 was 39515, checked in by vboxsync, 13 years ago

*: Use RTLISTANCHOR.

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