VirtualBox

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

最後變更 在這個檔案從33144是 33144,由 vboxsync 提交於 14 年 前

IPRT: Reimplemented ring-0 event semaphores for solaris.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 10.1 KB
 
1/* $Id: semevent-r0drv-solaris.c 33144 2010-10-14 22:11:36Z vboxsync $ */
2/** @file
3 * IPRT - 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.alldomusa.eu.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 */
60typedef 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. */
70typedef RTSEMEVENTSOLENTRY *PRTSEMEVENTSOLENTRY;
71
72
73/**
74 * Solaris event semaphore.
75 */
76typedef 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
94RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
95{
96 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
97}
98
99
100RTDECL(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), VERR_INVALID_PARAMETER);
104 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
105 RT_ASSERT_PREEMPTIBLE();
106
107 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
108 if (!pThis)
109 return VERR_NO_MEMORY;
110
111 pThis->u32Magic = RTSEMEVENT_MAGIC;
112 pThis->cRefs = 1;
113 pThis->fSignaled = false;
114 RTListInit(&pThis->WaitList);
115 mutex_init(&pThis->Mtx, "IPRT Event Semaphore", MUTEX_DRIVER, (void *)ipltospl(DISP_LEVEL));
116 cv_init(&pThis->Cnd, "IPRT CV", CV_DRIVER, NULL);
117
118 *phEventSem = pThis;
119 return VINF_SUCCESS;
120}
121
122
123/**
124 * Retain a reference to the semaphore.
125 *
126 * @param pThis The semaphore.
127 */
128DECLINLINE(void) rtR0SemEventSolRetain(PRTSEMEVENTINTERNAL pThis)
129{
130 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
131 Assert(cRefs && cRefs < 100000);
132}
133
134
135/**
136 * The destruct.
137 *
138 * @param pThis The semaphore.
139 */
140static void rtR0SemEventSolDtor(PRTSEMEVENTINTERNAL pThis)
141{
142 Assert(pThis->u32Magic != RTSEMEVENT_MAGIC);
143 cv_destroy(&pThis->Cnd);
144 mutex_destroy(&pThis->Mtx);
145 RTMemFree(pThis);
146}
147
148
149/**
150 * Release a reference, destroy the thing if necessary.
151 *
152 * @param pThis The semaphore.
153 */
154DECLINLINE(void) rtR0SemEventSolRelease(PRTSEMEVENTINTERNAL pThis)
155{
156 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
157 rtR0SemEventSolDtor(pThis);
158}
159
160
161RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
162{
163 /*
164 * Validate input.
165 */
166 PRTSEMEVENTINTERNAL pThis = hEventSem;
167 if (pThis == NIL_RTSEMEVENT)
168 return VINF_SUCCESS;
169 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
170 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
171 Assert(pThis->cRefs > 0);
172 RT_ASSERT_INTS_ON();
173
174 mutex_enter(&pThis->Mtx);
175
176 /*
177 * Invalidate the semaphore.
178 */
179 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
180 ASMAtomicWriteBool(&pThis->fSignaled, false);
181
182 /*
183 * Abort and wake up all threads.
184 */
185 PRTSEMEVENTSOLENTRY pWaiter;
186 RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTSOLENTRY, Node)
187 {
188 pWaiter->fWokenUp = true;
189 }
190 cv_broadcast(&pThis->Cnd);
191
192 /*
193 * Release the reference from RTSemEventCreateEx.
194 */
195 mutex_exit(&pThis->Mtx);
196 rtR0SemEventSolRelease(pThis);
197
198 return VINF_SUCCESS;
199}
200
201
202RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
203{
204 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
205 RT_ASSERT_PREEMPT_CPUID_VAR();
206 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
207 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
208 RT_ASSERT_INTS_ON();
209
210 rtR0SemEventSolRetain(pThis);
211 rtR0SemSolWaitEnterMutexWithUnpinningHack(&pThis->Mtx);
212
213 /*
214 * Wake up one thread.
215 */
216 ASMAtomicWriteBool(&pThis->fSignaled, true);
217
218 PRTSEMEVENTSOLENTRY pWaiter;
219 RTListForEach(&pThis->WaitList, pWaiter, RTSEMEVENTSOLENTRY, Node)
220 {
221 if (!pWaiter->fWokenUp)
222 {
223 pWaiter->fWokenUp = true;
224 setrun(pWaiter->pThread);
225 ASMAtomicWriteBool(&pThis->fSignaled, false);
226 break;
227 }
228 }
229
230 mutex_exit(&pThis->Mtx);
231 rtR0SemEventSolRelease(pThis);
232
233 RT_ASSERT_PREEMPT_CPUID();
234 return VINF_SUCCESS;
235}
236
237
238/**
239 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
240 *
241 * @returns VBox status code.
242 * @param pThis The event semaphore.
243 * @param fFlags See RTSemEventWaitEx.
244 * @param uTimeout See RTSemEventWaitEx.
245 * @param pSrcPos The source code position of the wait.
246 */
247static int rtR0SemEventSolWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
248 PCRTLOCKVALSRCPOS pSrcPos)
249{
250 /*
251 * Validate the input.
252 */
253 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
254 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
255 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
256
257 rtR0SemEventSolRetain(pThis);
258 mutex_enter(&pThis->Mtx);
259
260 /*
261 * In the signaled state?
262 */
263 int rc;
264 if (ASMAtomicCmpXchgBool(&pThis->fSignaled, false, true))
265 rc = VINF_SUCCESS;
266 else
267 {
268 /*
269 * We have to wait.
270 */
271 RTR0SEMSOLWAIT Wait;
272 rc = rtR0SemSolWaitInit(&Wait, fFlags, uTimeout);
273 if (RT_SUCCESS(rc))
274 {
275 RTSEMEVENTSOLENTRY Waiter;
276 Waiter.pThread = curthread;
277 Waiter.fWokenUp = false;
278 RTListAppend(&pThis->WaitList, &Waiter.Node);
279
280 for (;;)
281 {
282 /* The destruction test. */
283 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
284 rc = VERR_SEM_DESTROYED;
285 else
286 {
287 /* Check the exit conditions. */
288 if (RT_UNLIKELY(pThis->u32Magic != RTSEMEVENT_MAGIC))
289 rc = VERR_SEM_DESTROYED;
290 else if (Waiter.fWokenUp)
291 rc = VINF_SUCCESS;
292 else if (rtR0SemSolWaitHasTimedOut(&Wait))
293 rc = VERR_TIMEOUT;
294 else if (rtR0SemSolWaitWasInterrupted(&Wait))
295 rc = VERR_INTERRUPTED;
296 else
297 {
298 /* Do the wait and then recheck the conditions. */
299 rtR0SemSolWaitDoIt(&Wait, &pThis->Cnd, &pThis->Mtx);
300 continue;
301 }
302 }
303 break;
304 }
305
306 rtR0SemSolWaitDelete(&Wait);
307 RTListNodeRemove(&Waiter.Node);
308 }
309 }
310
311 mutex_exit(&pThis->Mtx);
312 rtR0SemEventSolRelease(pThis);
313 return rc;
314}
315
316
317#undef RTSemEventWaitEx
318RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
319{
320#ifndef RTSEMEVENT_STRICT
321 return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, NULL);
322#else
323 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
324 return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, &SrcPos);
325#endif
326}
327
328
329RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
330 RTHCUINTPTR uId, RT_SRC_POS_DECL)
331{
332 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
333 return rtR0SemEventSolWait(hEventSem, fFlags, uTimeout, &SrcPos);
334}
335
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette