VirtualBox

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

Last change on this file since 1507 was 23, checked in by vboxsync, 18 years ago

string.h & stdio.h + header cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/* $Id: PDMAllCritSect.cpp 23 2007-01-15 14:08:28Z vboxsync $ */
2/** @file
3 * PDM Critical Sections
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
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
33#include <VBox/log.h>
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36
37
38/**
39 * Leaves a critical section entered with PDMCritSectEnter().
40 *
41 * @returns VINF_SUCCESS if entered successfully.
42 * @returns rcBusy when encountering a busy critical section in GC/R0.
43 * @returns VERR_SEM_DESTROYED if the critical section is dead.
44 *
45 * @param pCritSect The PDM critical section to enter.
46 * @param rcBusy The status code to return when we're in GC or R0
47 * and the section is busy.
48 */
49PDMDECL(int) PDMCritSectEnter(PPDMCRITSECT pCritSect, int rcBusy)
50{
51 Assert(pCritSect->s.Core.cNestings < 8); /* useful to catch incorrect locking */
52#ifdef IN_RING3
53 NOREF(rcBusy);
54
55 STAM_STATS({ if (pCritSect->s.Core.cLockers >= 0 && !RTCritSectIsOwner(&pCritSect->s.Core)) STAM_COUNTER_INC(&pCritSect->s.StatContentionR3); });
56 int rc = RTCritSectEnter(&pCritSect->s.Core);
57 STAM_STATS({ if (pCritSect->s.Core.cNestings == 1) STAM_PROFILE_ADV_START(&pCritSect->s.StatLocked, l); });
58 return rc;
59
60#else
61 AssertMsgReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, ("%RX32\n", pCritSect->s.Core.u32Magic),
62 VERR_SEM_DESTROYED);
63 PVM pVM = pCritSect->s.CTXALLSUFF(pVM);
64 Assert(pVM);
65
66 /*
67 * Try take the lock.
68 */
69 if (ASMAtomicCmpXchgS32(&pCritSect->s.Core.cLockers, 0, -1))
70 {
71 pCritSect->s.Core.cNestings = 1;
72 ASMAtomicXchgSize(&pCritSect->s.Core.NativeThreadOwner, pVM->NativeThreadEMT);
73 STAM_PROFILE_ADV_START(&pCritSect->s.StatLocked, l);
74 return VINF_SUCCESS;
75 }
76
77 /*
78 * Nested?
79 */
80 if (pCritSect->s.Core.NativeThreadOwner == pVM->NativeThreadEMT)
81 {
82 pCritSect->s.Core.cNestings++;
83 ASMAtomicIncS32(&pCritSect->s.Core.cLockers);
84 return VINF_SUCCESS;
85 }
86
87 /*
88 * Failed.
89 */
90 LogFlow(("pcnetLock: locked => HC (%Vrc)\n", rcBusy));
91 STAM_COUNTER_INC(&pCritSect->s.StatContentionR0GCLock);
92 return rcBusy;
93#endif
94}
95
96
97/**
98 * Leaves a critical section entered with PDMCritSectEnter().
99 *
100 * @param pCritSect The PDM critical section to leave.
101 */
102PDMDECL(void) PDMCritSectLeave(PPDMCRITSECT pCritSect)
103{
104#ifdef IN_RING3
105# ifdef VBOX_WITH_STATISTICS
106 if (pCritSect->s.Core.cNestings == 1)
107 STAM_PROFILE_ADV_STOP(&pCritSect->s.StatLocked, l);
108# endif
109 int rc = RTCritSectLeave(&pCritSect->s.Core);
110 AssertRC(rc);
111
112#else
113 Assert(VALID_PTR(pCritSect));
114 Assert(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC);
115 Assert(pCritSect->s.Core.cNestings > 0);
116 Assert(pCritSect->s.Core.cLockers >= 0);
117 PVM pVM = pCritSect->s.CTXALLSUFF(pVM);
118 Assert(pVM);
119 Assert(pCritSect->s.Core.NativeThreadOwner == pVM->NativeThreadEMT);
120
121 /*
122 * Deal with nested attempts first.
123 * (We're exploiting nesting to avoid queuing multiple R3 leaves for the same section.)
124 */
125 pCritSect->s.Core.cNestings--;
126 if (pCritSect->s.Core.cNestings > 0)
127 {
128 ASMAtomicDecS32(&pCritSect->s.Core.cLockers);
129 return;
130 }
131
132 /*
133 * Try leave it.
134 */
135 if (pCritSect->s.Core.cLockers == 0)
136 {
137 STAM_PROFILE_ADV_STOP(&pCritSect->s.StatLocked, l);
138 ASMAtomicXchgSize(&pCritSect->s.Core.NativeThreadOwner, NIL_RTNATIVETHREAD);
139 if (ASMAtomicCmpXchgS32(&pCritSect->s.Core.cLockers, -1, 0))
140 return;
141
142 /* darn, someone raced in on us. */
143 ASMAtomicXchgSize(&pCritSect->s.Core.NativeThreadOwner, pVM->NativeThreadEMT);
144 STAM_PROFILE_ADV_START(&pCritSect->s.StatLocked, l);
145 }
146 pCritSect->s.Core.cNestings = 1;
147
148 /*
149 * Queue the request.
150 */
151 RTUINT i = pVM->pdm.s.cQueuedCritSectLeaves++;
152 LogFlow(("PDMCritSectLeave: [%d]=%p => R3\n", i, pCritSect));
153 AssertFatal(i < RT_ELEMENTS(pVM->pdm.s.apQueuedCritSectsLeaves));
154 pVM->pdm.s.apQueuedCritSectsLeaves[i] = MMHyperCCToR3(pVM, pCritSect);
155 VM_FF_SET(pVM, VM_FF_PDM_CRITSECT);
156 VM_FF_SET(pVM, VM_FF_TO_R3);
157 STAM_COUNTER_INC(&pVM->pdm.s.StatQueuedCritSectLeaves);
158 STAM_COUNTER_INC(&pCritSect->s.StatContentionR0GCUnlock);
159#endif
160}
161
162
163/**
164 * Checks the caller is the owner of the critical section.
165 *
166 * @returns true if owner.
167 * @returns false if not owner.
168 * @param pCritSect The critical section.
169 */
170PDMDECL(bool) PDMCritSectIsOwner(PCPDMCRITSECT pCritSect)
171{
172#ifdef IN_RING3
173 return RTCritSectIsOwner(&pCritSect->s.Core);
174#else
175 PVM pVM = pCritSect->s.CTXALLSUFF(pVM);
176 Assert(pVM);
177 return pCritSect->s.Core.NativeThreadOwner == pVM->NativeThreadEMT;
178#endif
179}
180
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