VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PDMAllCritSect.cpp@ 18078

Last change on this file since 18078 was 13898, checked in by vboxsync, 16 years ago

Moved more data to VMCPU.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1/* $Id: PDMAllCritSect.cpp 13898 2008-11-06 09:44:29Z vboxsync $ */
2/** @file
3 * PDM - Critical Sections, All Contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_PDM//_CRITSECT
27#include "PDMInternal.h"
28#include <VBox/pdm.h>
29#include <VBox/mm.h>
30#include <VBox/vm.h>
31#include <VBox/err.h>
32#include <VBox/hwaccm.h>
33
34#include <VBox/log.h>
35#include <iprt/asm.h>
36#include <iprt/assert.h>
37#ifdef IN_RING3
38# include <iprt/semaphore.h>
39#endif
40
41
42/**
43 * Enters a PDM critical section.
44 *
45 * @returns VINF_SUCCESS if entered successfully.
46 * @returns rcBusy when encountering a busy critical section in GC/R0.
47 * @returns VERR_SEM_DESTROYED if the critical section is dead.
48 *
49 * @param pCritSect The PDM critical section to enter.
50 * @param rcBusy The status code to return when we're in GC or R0
51 * and the section is busy.
52 */
53VMMDECL(int) PDMCritSectEnter(PPDMCRITSECT pCritSect, int rcBusy)
54{
55 Assert(pCritSect->s.Core.cNestings < 8); /* useful to catch incorrect locking */
56#ifdef IN_RING3
57 NOREF(rcBusy);
58
59 STAM_STATS({ if (pCritSect->s.Core.cLockers >= 0 && !RTCritSectIsOwner(&pCritSect->s.Core)) STAM_COUNTER_INC(&pCritSect->s.StatContentionR3); });
60 int rc = RTCritSectEnter(&pCritSect->s.Core);
61 STAM_STATS({ if (pCritSect->s.Core.cNestings == 1) STAM_PROFILE_ADV_START(&pCritSect->s.StatLocked, l); });
62 return rc;
63
64#else /* !IN_RING3 */
65 AssertMsgReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, ("%RX32\n", pCritSect->s.Core.u32Magic),
66 VERR_SEM_DESTROYED);
67 PVM pVM = pCritSect->s.CTX_SUFF(pVM);
68 Assert(pVM);
69 PVMCPU pVCpu = VMMGetCpu(pVM);
70
71 /*
72 * Try to take the lock.
73 */
74 if (ASMAtomicCmpXchgS32(&pCritSect->s.Core.cLockers, 0, -1))
75 {
76 pCritSect->s.Core.cNestings = 1;
77 Assert(pVCpu->hNativeThread);
78 ASMAtomicXchgSize(&pCritSect->s.Core.NativeThreadOwner, pVCpu->hNativeThread);
79 STAM_PROFILE_ADV_START(&pCritSect->s.StatLocked, l);
80 return VINF_SUCCESS;
81 }
82
83 /*
84 * Nested?
85 */
86 if (pCritSect->s.Core.NativeThreadOwner == pVCpu->hNativeThread)
87 {
88 pCritSect->s.Core.cNestings++;
89 ASMAtomicIncS32(&pCritSect->s.Core.cLockers);
90 return VINF_SUCCESS;
91 }
92
93 /*
94 * Failed.
95 */
96 LogFlow(("PDMCritSectEnter: locked => R3 (%Rrc)\n", rcBusy));
97 STAM_COUNTER_INC(&pCritSect->s.StatContentionRZLock);
98 return rcBusy;
99#endif /* !IN_RING3 */
100}
101
102
103#ifdef IN_RING3
104/**
105 * Enters a PDM critical section.
106 *
107 * @returns VINF_SUCCESS if entered successfully.
108 * @returns rcBusy when encountering a busy critical section in GC/R0.
109 * @returns VERR_SEM_DESTROYED if the critical section is dead.
110 *
111 * @param pCritSect The PDM critical section to enter.
112 * @param fCallHost Whether this is a VMMGCCallHost() or VMMR0CallHost() request.
113 */
114VMMR3DECL(int) PDMR3CritSectEnterEx(PPDMCRITSECT pCritSect, bool fCallHost)
115{
116 int rc = PDMCritSectEnter(pCritSect, VERR_INTERNAL_ERROR);
117 if ( rc == VINF_SUCCESS
118 && fCallHost
119 && pCritSect->s.Core.Strict.ThreadOwner != NIL_RTTHREAD)
120 {
121 RTThreadWriteLockDec(pCritSect->s.Core.Strict.ThreadOwner);
122 ASMAtomicUoWriteSize(&pCritSect->s.Core.Strict.ThreadOwner, NIL_RTTHREAD);
123 }
124 return rc;
125}
126#endif /* IN_RING3 */
127
128
129/**
130 * Leaves a critical section entered with PDMCritSectEnter().
131 *
132 * @param pCritSect The PDM critical section to leave.
133 */
134VMMDECL(void) PDMCritSectLeave(PPDMCRITSECT pCritSect)
135{
136#ifdef IN_RING3
137# ifdef VBOX_WITH_STATISTICS
138 if (pCritSect->s.Core.cNestings == 1)
139 STAM_PROFILE_ADV_STOP(&pCritSect->s.StatLocked, l);
140# endif
141 RTSEMEVENT EventToSignal = pCritSect->s.EventToSignal;
142 if (RT_LIKELY(EventToSignal == NIL_RTSEMEVENT))
143 {
144 int rc = RTCritSectLeave(&pCritSect->s.Core);
145 AssertRC(rc);
146 }
147 else
148 {
149 pCritSect->s.EventToSignal = NIL_RTSEMEVENT;
150 int rc = RTCritSectLeave(&pCritSect->s.Core);
151 AssertRC(rc);
152 LogBird(("signalling %#x\n", EventToSignal));
153 rc = RTSemEventSignal(EventToSignal);
154 AssertRC(rc);
155 }
156
157#else /* !IN_RING3 */
158 Assert(VALID_PTR(pCritSect));
159 Assert(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC);
160 Assert(pCritSect->s.Core.cNestings > 0);
161 Assert(pCritSect->s.Core.cLockers >= 0);
162 PVM pVM = pCritSect->s.CTX_SUFF(pVM);
163 Assert(pVM);
164 PVMCPU pVCpu = VMMGetCpu(pVM);
165 Assert(pVCpu);
166 AssertMsg(pCritSect->s.Core.NativeThreadOwner == pVCpu->hNativeThread, ("Owner %RX64 emt=%RX64\n", pCritSect->s.Core.NativeThreadOwner, pVCpu->hNativeThread));
167
168 /*
169 * Deal with nested attempts first.
170 * (We're exploiting nesting to avoid queuing multiple R3 leaves for the same section.)
171 */
172 pCritSect->s.Core.cNestings--;
173 if (pCritSect->s.Core.cNestings > 0)
174 {
175 ASMAtomicDecS32(&pCritSect->s.Core.cLockers);
176 return;
177 }
178
179 /*
180 * Try leave it.
181 */
182 if (pCritSect->s.Core.cLockers == 0)
183 {
184 STAM_PROFILE_ADV_STOP(&pCritSect->s.StatLocked, l);
185 ASMAtomicXchgSize(&pCritSect->s.Core.NativeThreadOwner, NIL_RTNATIVETHREAD);
186 if (ASMAtomicCmpXchgS32(&pCritSect->s.Core.cLockers, -1, 0))
187 return;
188
189 /* darn, someone raced in on us. */
190 Assert(pVCpu->hNativeThread);
191 ASMAtomicXchgSize(&pCritSect->s.Core.NativeThreadOwner, pVCpu->hNativeThread);
192 STAM_PROFILE_ADV_START(&pCritSect->s.StatLocked, l);
193 }
194 pCritSect->s.Core.cNestings = 1;
195
196 /*
197 * Queue the request.
198 */
199 RTUINT i = pVM->pdm.s.cQueuedCritSectLeaves++;
200 LogFlow(("PDMCritSectLeave: [%d]=%p => R3\n", i, pCritSect));
201 AssertFatal(i < RT_ELEMENTS(pVM->pdm.s.apQueuedCritSectsLeaves));
202 pVM->pdm.s.apQueuedCritSectsLeaves[i] = MMHyperCCToR3(pVM, pCritSect);
203 VM_FF_SET(pVM, VM_FF_PDM_CRITSECT);
204 VM_FF_SET(pVM, VM_FF_TO_R3);
205 STAM_COUNTER_INC(&pVM->pdm.s.StatQueuedCritSectLeaves);
206 STAM_COUNTER_INC(&pCritSect->s.StatContentionRZUnlock);
207#endif /* !IN_RING3 */
208}
209
210
211/**
212 * Checks the caller is the owner of the critical section.
213 *
214 * @returns true if owner.
215 * @returns false if not owner.
216 * @param pCritSect The critical section.
217 */
218VMMDECL(bool) PDMCritSectIsOwner(PCPDMCRITSECT pCritSect)
219{
220#ifdef IN_RING3
221 return RTCritSectIsOwner(&pCritSect->s.Core);
222#else
223 PVM pVM = pCritSect->s.CTX_SUFF(pVM);
224 Assert(pVM);
225 return pCritSect->s.Core.NativeThreadOwner == VMMGetCpu(pVM)->hNativeThread;
226#endif
227}
228
229
230/**
231 * Checks if a critical section is initialized or not.
232 *
233 * @returns true if initialized.
234 * @returns false if not initialized.
235 * @param pCritSect The critical section.
236 */
237VMMDECL(bool) PDMCritSectIsInitialized(PCPDMCRITSECT pCritSect)
238{
239 return pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC;
240}
241
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