VirtualBox

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

Last change on this file since 52553 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1/* $Id: semevent-r0drv-solaris.c 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event Semaphores, Ring-0 Driver, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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#ifdef DEBUG_ramshankar
237 /** See @bugref{6318} comment#11 */
238 return VINF_SUCCESS;
239#endif
240 RT_ASSERT_PREEMPT_CPUID();
241 return VINF_SUCCESS;
242}
243
244
245/**
246 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
247 *
248 * @returns VBox status code.
249 * @param pThis The event semaphore.
250 * @param fFlags See RTSemEventWaitEx.
251 * @param uTimeout See RTSemEventWaitEx.
252 * @param pSrcPos The source code position of the wait.
253 */
254static int rtR0SemEventSolWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
255 PCRTLOCKVALSRCPOS pSrcPos)
256{
257 /*
258 * Validate the input.
259 */
260 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
261 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
262 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
263
264 rtR0SemEventSolRetain(pThis);
265 mutex_enter(&pThis->Mtx);
266
267 /*
268 * In the signaled state?
269 */
270 int rc;
271 if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
272 rc = VINF_SUCCESS;
273 else
274 {
275 /*
276 * We have to wait.
277 */
278 RTR0SEMSOLWAIT Wait;
279 rc = rtR0SemSolWaitInit(&Wait, fFlags, uTimeout);
280 if (RT_SUCCESS(rc))
281 {
282 RTSEMEVENTSOLENTRY Waiter; /* ASSUMES we won't get swapped out while waiting (TS_DONT_SWAP). */
283 Waiter.pThread = curthread;
284 Waiter.fWokenUp = false;
285 RTListAppend(&pThis->WaitList, &Waiter.Node);
286
287 for (;;)
288 {
289 /* The destruction test. */
290 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
291 rc = VERR_SEM_DESTROYED;
292 else
293 {
294 /* Check the exit conditions. */
295 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
296 rc = VERR_SEM_DESTROYED;
297 else if (Waiter.fWokenUp)
298 rc = VINF_SUCCESS;
299 else if (rtR0SemSolWaitHasTimedOut(&Wait))
300 rc = VERR_TIMEOUT;
301 else if (rtR0SemSolWaitWasInterrupted(&Wait))
302 rc = VERR_INTERRUPTED;
303 else
304 {
305 /* Do the wait and then recheck the conditions. */
306 rtR0SemSolWaitDoIt(&Wait, &pThis->Cnd, &pThis->Mtx, &Waiter.fWokenUp, false);
307 continue;
308 }
309 }
310 break;
311 }
312
313 rtR0SemSolWaitDelete(&Wait);
314 RTListNodeRemove(&Waiter.Node);
315 }
316 }
317
318 mutex_exit(&pThis->Mtx);
319 rtR0SemEventSolRelease(pThis);
320 return rc;
321}
322
323
324RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
325{
326#ifndef RTSEMEVENT_STRICT
327 return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, NULL);
328#else
329 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
330 return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, &SrcPos);
331#endif
332}
333
334
335RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
336 RTHCUINTPTR uId, RT_SRC_POS_DECL)
337{
338 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
339 return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, &SrcPos);
340}
341
342
343RTDECL(uint32_t) RTSemEventGetResolution(void)
344{
345 return rtR0SemSolWaitGetResolution();
346}
347
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