1 | /* $Id: PDMCritSect.cpp 33595 2010-10-29 10:35:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM - Critical Sections, Ring-3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_PDM//_CRITSECT
|
---|
23 | #include "PDMInternal.h"
|
---|
24 | #include <VBox/pdmcritsect.h>
|
---|
25 | #include <VBox/mm.h>
|
---|
26 | #include <VBox/vm.h>
|
---|
27 | #include <VBox/uvm.h>
|
---|
28 |
|
---|
29 | #include <VBox/err.h>
|
---|
30 | #include <VBox/log.h>
|
---|
31 | #include <VBox/sup.h>
|
---|
32 | #include <iprt/asm.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/lockvalidator.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/thread.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Internal Functions *
|
---|
41 | *******************************************************************************/
|
---|
42 | static int pdmR3CritSectDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTINT pCritSect, PPDMCRITSECTINT pPrev, bool fFinal);
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Register statistics related to the critical sections.
|
---|
48 | *
|
---|
49 | * @returns VBox status code.
|
---|
50 | * @param pVM The VM handle.
|
---|
51 | */
|
---|
52 | int pdmR3CritSectInitStats(PVM pVM)
|
---|
53 | {
|
---|
54 | STAM_REG(pVM, &pVM->pdm.s.StatQueuedCritSectLeaves, STAMTYPE_COUNTER, "/PDM/QueuedCritSectLeaves", STAMUNIT_OCCURENCES,
|
---|
55 | "Number of times a critical section leave request needed to be queued for ring-3 execution.");
|
---|
56 | return VINF_SUCCESS;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Relocates all the critical sections.
|
---|
62 | *
|
---|
63 | * @param pVM The VM handle.
|
---|
64 | */
|
---|
65 | void pdmR3CritSectRelocate(PVM pVM)
|
---|
66 | {
|
---|
67 | PUVM pUVM = pVM->pUVM;
|
---|
68 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
69 |
|
---|
70 | for (PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
71 | pCur;
|
---|
72 | pCur = pCur->pNext)
|
---|
73 | pCur->pVMRC = pVM->pVMRC;
|
---|
74 |
|
---|
75 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Deletes all remaining critical sections.
|
---|
81 | *
|
---|
82 | * This is called at the very end of the termination process. It is also called
|
---|
83 | * at the end of vmR3CreateU failure cleanup, which may cause it to be called
|
---|
84 | * twice depending on where vmR3CreateU actually failed. We have to do the
|
---|
85 | * latter call because other components expect the critical sections to be
|
---|
86 | * automatically deleted.
|
---|
87 | *
|
---|
88 | * @returns VBox status.
|
---|
89 | * First error code, rest is lost.
|
---|
90 | * @param pVMU The user mode VM handle.
|
---|
91 | * @remark Don't confuse this with PDMR3CritSectDelete.
|
---|
92 | */
|
---|
93 | VMMDECL(int) PDMR3CritSectTerm(PVM pVM)
|
---|
94 | {
|
---|
95 | PUVM pUVM = pVM->pUVM;
|
---|
96 | int rc = VINF_SUCCESS;
|
---|
97 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
98 |
|
---|
99 | while (pUVM->pdm.s.pCritSects)
|
---|
100 | {
|
---|
101 | int rc2 = pdmR3CritSectDeleteOne(pVM, pUVM, pUVM->pdm.s.pCritSects, NULL, true /* final */);
|
---|
102 | AssertRC(rc2);
|
---|
103 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
104 | rc = rc2;
|
---|
105 | }
|
---|
106 |
|
---|
107 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Initializes a critical section and inserts it into the list.
|
---|
114 | *
|
---|
115 | * @returns VBox status code.
|
---|
116 | * @param pVM The Vm handle.
|
---|
117 | * @param pCritSect The critical section.
|
---|
118 | * @param pvKey The owner key.
|
---|
119 | * @param RT_SRC_POS_DECL The source position.
|
---|
120 | * @param pszName The name of the critical section (for statistics).
|
---|
121 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
122 | * statistics and lock validation.
|
---|
123 | * @param va Arguments for the format string.
|
---|
124 | */
|
---|
125 | static int pdmR3CritSectInitOne(PVM pVM, PPDMCRITSECTINT pCritSect, void *pvKey, RT_SRC_POS_DECL, const char *pszNameFmt, va_list va)
|
---|
126 | {
|
---|
127 | VM_ASSERT_EMT(pVM);
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Allocate the semaphore.
|
---|
131 | */
|
---|
132 | AssertCompile(sizeof(SUPSEMEVENT) == sizeof(pCritSect->Core.EventSem));
|
---|
133 | int rc = SUPSemEventCreate(pVM->pSession, (PSUPSEMEVENT)&pCritSect->Core.EventSem);
|
---|
134 | if (RT_SUCCESS(rc))
|
---|
135 | {
|
---|
136 | /* Only format the name once. */
|
---|
137 | char *pszName = RTStrAPrintf2V(pszNameFmt, va); /** @todo plug the "leak"... */
|
---|
138 | if (pszName)
|
---|
139 | {
|
---|
140 | #ifndef PDMCRITSECT_STRICT
|
---|
141 | pCritSect->Core.pValidatorRec = NULL;
|
---|
142 | #else
|
---|
143 | rc = RTLockValidatorRecExclCreate(&pCritSect->Core.pValidatorRec,
|
---|
144 | # ifdef RT_LOCK_STRICT_ORDER
|
---|
145 | RTLockValidatorClassForSrcPos(RT_SRC_POS_ARGS, "%s", pszName),
|
---|
146 | # else
|
---|
147 | NIL_RTLOCKVALCLASS,
|
---|
148 | # endif
|
---|
149 | RTLOCKVAL_SUB_CLASS_NONE,
|
---|
150 | pCritSect, true, "%s", pszName);
|
---|
151 | #endif
|
---|
152 | if (RT_SUCCESS(rc))
|
---|
153 | {
|
---|
154 | /*
|
---|
155 | * Initialize the structure (first bit is c&p from RTCritSectInitEx).
|
---|
156 | */
|
---|
157 | pCritSect->Core.u32Magic = RTCRITSECT_MAGIC;
|
---|
158 | pCritSect->Core.fFlags = 0;
|
---|
159 | pCritSect->Core.cNestings = 0;
|
---|
160 | pCritSect->Core.cLockers = -1;
|
---|
161 | pCritSect->Core.NativeThreadOwner = NIL_RTNATIVETHREAD;
|
---|
162 | pCritSect->pVMR3 = pVM;
|
---|
163 | pCritSect->pVMR0 = pVM->pVMR0;
|
---|
164 | pCritSect->pVMRC = pVM->pVMRC;
|
---|
165 | pCritSect->pvKey = pvKey;
|
---|
166 | pCritSect->EventToSignal = NIL_RTSEMEVENT;
|
---|
167 | pCritSect->pNext = pVM->pUVM->pdm.s.pCritSects;
|
---|
168 | pCritSect->pszName = pszName;
|
---|
169 | pVM->pUVM->pdm.s.pCritSects = pCritSect;
|
---|
170 | STAMR3RegisterF(pVM, &pCritSect->StatContentionRZLock, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionRZLock", pCritSect->pszName);
|
---|
171 | STAMR3RegisterF(pVM, &pCritSect->StatContentionRZUnlock,STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionRZUnlock", pCritSect->pszName);
|
---|
172 | STAMR3RegisterF(pVM, &pCritSect->StatContentionR3, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionR3", pCritSect->pszName);
|
---|
173 | #ifdef VBOX_WITH_STATISTICS
|
---|
174 | STAMR3RegisterF(pVM, &pCritSect->StatLocked, STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, NULL, "/PDM/CritSects/%s/Locked", pCritSect->pszName);
|
---|
175 | #endif
|
---|
176 | return VINF_SUCCESS;
|
---|
177 | }
|
---|
178 |
|
---|
179 | RTStrFree(pszName);
|
---|
180 | }
|
---|
181 | else
|
---|
182 | rc = VERR_NO_STR_MEMORY;
|
---|
183 | SUPSemEventClose(pVM->pSession, (SUPSEMEVENT)pCritSect->Core.EventSem);
|
---|
184 | }
|
---|
185 | return rc;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Initializes a PDM critical section for internal use.
|
---|
191 | *
|
---|
192 | * The PDM critical sections are derived from the IPRT critical sections, but
|
---|
193 | * works in GC as well.
|
---|
194 | *
|
---|
195 | * @returns VBox status code.
|
---|
196 | * @param pVM The VM handle.
|
---|
197 | * @param pDevIns Device instance.
|
---|
198 | * @param pCritSect Pointer to the critical section.
|
---|
199 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
200 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
201 | * statistics and lock validation.
|
---|
202 | * @param ... Arguments for the format string.
|
---|
203 | * @thread EMT(0)
|
---|
204 | */
|
---|
205 | VMMR3DECL(int) PDMR3CritSectInit(PVM pVM, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL, const char *pszNameFmt, ...)
|
---|
206 | {
|
---|
207 | #if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
|
---|
208 | AssertCompile(sizeof(pCritSect->padding) >= sizeof(pCritSect->s));
|
---|
209 | #endif
|
---|
210 | Assert(RT_ALIGN_P(pCritSect, sizeof(uintptr_t)) == pCritSect);
|
---|
211 | va_list va;
|
---|
212 | va_start(va, pszNameFmt);
|
---|
213 | int rc = pdmR3CritSectInitOne(pVM, &pCritSect->s, pCritSect, RT_SRC_POS_ARGS, pszNameFmt, va);
|
---|
214 | va_end(va);
|
---|
215 | return rc;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Initializes a PDM critical section for a device.
|
---|
221 | *
|
---|
222 | * The PDM critical sections are derived from the IPRT critical sections, but
|
---|
223 | * works in GC as well.
|
---|
224 | *
|
---|
225 | * @returns VBox status code.
|
---|
226 | * @param pVM The VM handle.
|
---|
227 | * @param pDevIns Device instance.
|
---|
228 | * @param pCritSect Pointer to the critical section.
|
---|
229 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
230 | * statistics and lock validation.
|
---|
231 | * @param va Arguments for the format string.
|
---|
232 | */
|
---|
233 | int pdmR3CritSectInitDevice(PVM pVM, PPDMDEVINS pDevIns, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL,
|
---|
234 | const char *pszNameFmt, va_list va)
|
---|
235 | {
|
---|
236 | return pdmR3CritSectInitOne(pVM, &pCritSect->s, pDevIns, RT_SRC_POS_ARGS, pszNameFmt, va);
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Initializes a PDM critical section for a driver.
|
---|
242 | *
|
---|
243 | * @returns VBox status code.
|
---|
244 | * @param pVM The VM handle.
|
---|
245 | * @param pDrvIns Driver instance.
|
---|
246 | * @param pCritSect Pointer to the critical section.
|
---|
247 | * @param pszNameFmt Format string for naming the critical section. For
|
---|
248 | * statistics and lock validation.
|
---|
249 | * @param ... Arguments for the format string.
|
---|
250 | */
|
---|
251 | int pdmR3CritSectInitDriver(PVM pVM, PPDMDRVINS pDrvIns, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL,
|
---|
252 | const char *pszNameFmt, ...)
|
---|
253 | {
|
---|
254 | va_list va;
|
---|
255 | va_start(va, pszNameFmt);
|
---|
256 | int rc = pdmR3CritSectInitOne(pVM, &pCritSect->s, pDrvIns, RT_SRC_POS_ARGS, pszNameFmt, va);
|
---|
257 | va_end(va);
|
---|
258 | return rc;
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Deletes one critical section.
|
---|
264 | *
|
---|
265 | * @returns Return code from RTCritSectDelete.
|
---|
266 | *
|
---|
267 | * @param pVM The VM handle.
|
---|
268 | * @param pCritSect The critical section.
|
---|
269 | * @param pPrev The previous critical section in the list.
|
---|
270 | * @param fFinal Set if this is the final call and statistics shouldn't be deregistered.
|
---|
271 | *
|
---|
272 | * @remarks Caller must have entered the ListCritSect.
|
---|
273 | */
|
---|
274 | static int pdmR3CritSectDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTINT pCritSect, PPDMCRITSECTINT pPrev, bool fFinal)
|
---|
275 | {
|
---|
276 | /*
|
---|
277 | * Assert free waiters and so on (c&p from RTCritSectDelete).
|
---|
278 | */
|
---|
279 | Assert(pCritSect->Core.u32Magic == RTCRITSECT_MAGIC);
|
---|
280 | Assert(pCritSect->Core.cNestings == 0);
|
---|
281 | Assert(pCritSect->Core.cLockers == -1);
|
---|
282 | Assert(pCritSect->Core.NativeThreadOwner == NIL_RTNATIVETHREAD);
|
---|
283 | Assert(RTCritSectIsOwner(&pUVM->pdm.s.ListCritSect));
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * Unlink it.
|
---|
287 | */
|
---|
288 | if (pPrev)
|
---|
289 | pPrev->pNext = pCritSect->pNext;
|
---|
290 | else
|
---|
291 | pUVM->pdm.s.pCritSects = pCritSect->pNext;
|
---|
292 |
|
---|
293 | /*
|
---|
294 | * Delete it (parts taken from RTCritSectDelete).
|
---|
295 | * In case someone is waiting we'll signal the semaphore cLockers + 1 times.
|
---|
296 | */
|
---|
297 | ASMAtomicWriteU32(&pCritSect->Core.u32Magic, 0);
|
---|
298 | SUPSEMEVENT hEvent = (SUPSEMEVENT)pCritSect->Core.EventSem;
|
---|
299 | pCritSect->Core.EventSem = NIL_RTSEMEVENT;
|
---|
300 | while (pCritSect->Core.cLockers-- >= 0)
|
---|
301 | SUPSemEventSignal(pVM->pSession, hEvent);
|
---|
302 | ASMAtomicWriteS32(&pCritSect->Core.cLockers, -1);
|
---|
303 | int rc = SUPSemEventClose(pVM->pSession, hEvent);
|
---|
304 | AssertRC(rc);
|
---|
305 | RTLockValidatorRecExclDestroy(&pCritSect->Core.pValidatorRec);
|
---|
306 | pCritSect->pNext = NULL;
|
---|
307 | pCritSect->pvKey = NULL;
|
---|
308 | pCritSect->pVMR3 = NULL;
|
---|
309 | pCritSect->pVMR0 = NIL_RTR0PTR;
|
---|
310 | pCritSect->pVMRC = NIL_RTRCPTR;
|
---|
311 | RTStrFree((char *)pCritSect->pszName);
|
---|
312 | pCritSect->pszName = NULL;
|
---|
313 | if (!fFinal)
|
---|
314 | {
|
---|
315 | STAMR3Deregister(pVM, &pCritSect->StatContentionRZLock);
|
---|
316 | STAMR3Deregister(pVM, &pCritSect->StatContentionRZUnlock);
|
---|
317 | STAMR3Deregister(pVM, &pCritSect->StatContentionR3);
|
---|
318 | #ifdef VBOX_WITH_STATISTICS
|
---|
319 | STAMR3Deregister(pVM, &pCritSect->StatLocked);
|
---|
320 | #endif
|
---|
321 | }
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Deletes all critical sections with a give initializer key.
|
---|
328 | *
|
---|
329 | * @returns VBox status code.
|
---|
330 | * The entire list is processed on failure, so we'll only
|
---|
331 | * return the first error code. This shouldn't be a problem
|
---|
332 | * since errors really shouldn't happen here.
|
---|
333 | * @param pVM The VM handle.
|
---|
334 | * @param pvKey The initializer key.
|
---|
335 | */
|
---|
336 | static int pdmR3CritSectDeleteByKey(PVM pVM, void *pvKey)
|
---|
337 | {
|
---|
338 | /*
|
---|
339 | * Iterate the list and match key.
|
---|
340 | */
|
---|
341 | PUVM pUVM = pVM->pUVM;
|
---|
342 | int rc = VINF_SUCCESS;
|
---|
343 | PPDMCRITSECTINT pPrev = NULL;
|
---|
344 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
345 | PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
346 | while (pCur)
|
---|
347 | {
|
---|
348 | if (pCur->pvKey == pvKey)
|
---|
349 | {
|
---|
350 | int rc2 = pdmR3CritSectDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
|
---|
351 | AssertRC(rc2);
|
---|
352 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
353 | rc = rc2;
|
---|
354 | }
|
---|
355 |
|
---|
356 | /* next */
|
---|
357 | pPrev = pCur;
|
---|
358 | pCur = pCur->pNext;
|
---|
359 | }
|
---|
360 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
361 | return rc;
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Deletes all undeleted critical sections initialized by a given device.
|
---|
367 | *
|
---|
368 | * @returns VBox status code.
|
---|
369 | * @param pVM The VM handle.
|
---|
370 | * @param pDevIns The device handle.
|
---|
371 | */
|
---|
372 | int pdmR3CritSectDeleteDevice(PVM pVM, PPDMDEVINS pDevIns)
|
---|
373 | {
|
---|
374 | return pdmR3CritSectDeleteByKey(pVM, pDevIns);
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Deletes all undeleted critical sections initialized by a given driver.
|
---|
380 | *
|
---|
381 | * @returns VBox status code.
|
---|
382 | * @param pVM The VM handle.
|
---|
383 | * @param pDrvIns The driver handle.
|
---|
384 | */
|
---|
385 | int pdmR3CritSectDeleteDriver(PVM pVM, PPDMDRVINS pDrvIns)
|
---|
386 | {
|
---|
387 | return pdmR3CritSectDeleteByKey(pVM, pDrvIns);
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | /**
|
---|
392 | * Deletes the critical section.
|
---|
393 | *
|
---|
394 | * @returns VBox status code.
|
---|
395 | * @param pCritSect The PDM critical section to destroy.
|
---|
396 | */
|
---|
397 | VMMR3DECL(int) PDMR3CritSectDelete(PPDMCRITSECT pCritSect)
|
---|
398 | {
|
---|
399 | if (!RTCritSectIsInitialized(&pCritSect->s.Core))
|
---|
400 | return VINF_SUCCESS;
|
---|
401 |
|
---|
402 | /*
|
---|
403 | * Find and unlink it.
|
---|
404 | */
|
---|
405 | PVM pVM = pCritSect->s.pVMR3;
|
---|
406 | PUVM pUVM = pVM->pUVM;
|
---|
407 | AssertReleaseReturn(pVM, VERR_INTERNAL_ERROR);
|
---|
408 | PPDMCRITSECTINT pPrev = NULL;
|
---|
409 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
410 | PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
411 | while (pCur)
|
---|
412 | {
|
---|
413 | if (pCur == &pCritSect->s)
|
---|
414 | {
|
---|
415 | int rc = pdmR3CritSectDeleteOne(pVM, pUVM, pCur, pPrev, false /* not final */);
|
---|
416 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
417 | return rc;
|
---|
418 | }
|
---|
419 |
|
---|
420 | /* next */
|
---|
421 | pPrev = pCur;
|
---|
422 | pCur = pCur->pNext;
|
---|
423 | }
|
---|
424 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
425 | AssertReleaseMsgFailed(("pCritSect=%p wasn't found!\n", pCritSect));
|
---|
426 | return VERR_INTERNAL_ERROR;
|
---|
427 | }
|
---|
428 |
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Gets the name of the critical section.
|
---|
432 | *
|
---|
433 | *
|
---|
434 | * @returns Pointer to the critical section name (read only) on success,
|
---|
435 | * NULL on failure (invalid critical section).
|
---|
436 | * @param pCritSect The critical section.
|
---|
437 | */
|
---|
438 | VMMR3DECL(const char *) PDMR3CritSectName(PCPDMCRITSECT pCritSect)
|
---|
439 | {
|
---|
440 | AssertPtrReturn(pCritSect, NULL);
|
---|
441 | AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, NULL);
|
---|
442 | return pCritSect->s.pszName;
|
---|
443 | }
|
---|
444 |
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * Yield the critical section if someone is waiting on it.
|
---|
448 | *
|
---|
449 | * When yielding, we'll leave the critical section and try to make sure the
|
---|
450 | * other waiting threads get a chance of entering before we reclaim it.
|
---|
451 | *
|
---|
452 | * @retval true if yielded.
|
---|
453 | * @retval false if not yielded.
|
---|
454 | * @param pCritSect The critical section.
|
---|
455 | */
|
---|
456 | VMMR3DECL(bool) PDMR3CritSectYield(PPDMCRITSECT pCritSect)
|
---|
457 | {
|
---|
458 | AssertPtrReturn(pCritSect, false);
|
---|
459 | AssertReturn(pCritSect->s.Core.u32Magic == RTCRITSECT_MAGIC, false);
|
---|
460 | Assert(pCritSect->s.Core.NativeThreadOwner == RTThreadNativeSelf());
|
---|
461 |
|
---|
462 | /* No recursion allowed here. */
|
---|
463 | int32_t const cNestings = pCritSect->s.Core.cNestings;
|
---|
464 | AssertReturn(cNestings == 1, false);
|
---|
465 |
|
---|
466 | int32_t const cLockers = ASMAtomicReadS32(&pCritSect->s.Core.cLockers);
|
---|
467 | if (cLockers < cNestings)
|
---|
468 | return false;
|
---|
469 |
|
---|
470 | #ifdef PDMCRITSECT_STRICT
|
---|
471 | RTLOCKVALSRCPOS const SrcPos = pCritSect->s.Core.pValidatorRec->SrcPos;
|
---|
472 | #endif
|
---|
473 | PDMCritSectLeave(pCritSect);
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * If we're lucky, then one of the waiters has entered the lock already.
|
---|
477 | * We spin a little bit in hope for this to happen so we can avoid the
|
---|
478 | * yield detour.
|
---|
479 | */
|
---|
480 | if (ASMAtomicUoReadS32(&pCritSect->s.Core.cNestings) == 0)
|
---|
481 | {
|
---|
482 | int cLoops = 20;
|
---|
483 | while ( cLoops > 0
|
---|
484 | && ASMAtomicUoReadS32(&pCritSect->s.Core.cNestings) == 0
|
---|
485 | && ASMAtomicUoReadS32(&pCritSect->s.Core.cLockers) >= 0)
|
---|
486 | {
|
---|
487 | ASMNopPause();
|
---|
488 | cLoops--;
|
---|
489 | }
|
---|
490 | if (cLoops == 0)
|
---|
491 | RTThreadYield();
|
---|
492 | }
|
---|
493 |
|
---|
494 | #ifdef PDMCRITSECT_STRICT
|
---|
495 | int rc = PDMCritSectEnterDebug(pCritSect, VERR_INTERNAL_ERROR,
|
---|
496 | SrcPos.uId, SrcPos.pszFile, SrcPos.uLine, SrcPos.pszFunction);
|
---|
497 | #else
|
---|
498 | int rc = PDMCritSectEnter(pCritSect, VERR_INTERNAL_ERROR);
|
---|
499 | #endif
|
---|
500 | AssertLogRelRC(rc);
|
---|
501 | return true;
|
---|
502 | }
|
---|
503 |
|
---|
504 |
|
---|
505 | /**
|
---|
506 | * Schedule a event semaphore for signalling upon critsect exit.
|
---|
507 | *
|
---|
508 | * @returns VINF_SUCCESS on success.
|
---|
509 | * @returns VERR_TOO_MANY_SEMAPHORES if an event was already scheduled.
|
---|
510 | * @returns VERR_NOT_OWNER if we're not the critsect owner.
|
---|
511 | * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
|
---|
512 | *
|
---|
513 | * @param pCritSect The critical section.
|
---|
514 | * @param EventToSignal The semaphore that should be signalled.
|
---|
515 | */
|
---|
516 | VMMR3DECL(int) PDMR3CritSectScheduleExitEvent(PPDMCRITSECT pCritSect, RTSEMEVENT EventToSignal)
|
---|
517 | {
|
---|
518 | Assert(EventToSignal != NIL_RTSEMEVENT);
|
---|
519 | if (RT_UNLIKELY(!RTCritSectIsOwner(&pCritSect->s.Core)))
|
---|
520 | return VERR_NOT_OWNER;
|
---|
521 | if (RT_LIKELY( pCritSect->s.EventToSignal == NIL_RTSEMEVENT
|
---|
522 | || pCritSect->s.EventToSignal == EventToSignal))
|
---|
523 | {
|
---|
524 | pCritSect->s.EventToSignal = EventToSignal;
|
---|
525 | return VINF_SUCCESS;
|
---|
526 | }
|
---|
527 | return VERR_TOO_MANY_SEMAPHORES;
|
---|
528 | }
|
---|
529 |
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Counts the critical sections owned by the calling thread, optionally
|
---|
533 | * returning a comma separated list naming them.
|
---|
534 | *
|
---|
535 | * This is for diagnostic purposes only.
|
---|
536 | *
|
---|
537 | * @returns Lock count.
|
---|
538 | *
|
---|
539 | * @param pVM The VM handle.
|
---|
540 | * @param pszNames Where to return the critical section names.
|
---|
541 | * @param cbNames The size of the buffer.
|
---|
542 | */
|
---|
543 | VMMR3DECL(uint32_t) PDMR3CritSectCountOwned(PVM pVM, char *pszNames, size_t cbNames)
|
---|
544 | {
|
---|
545 | /*
|
---|
546 | * Init the name buffer.
|
---|
547 | */
|
---|
548 | size_t cchLeft = cbNames;
|
---|
549 | if (cchLeft)
|
---|
550 | {
|
---|
551 | cchLeft--;
|
---|
552 | pszNames[0] = pszNames[cchLeft] = '\0';
|
---|
553 | }
|
---|
554 |
|
---|
555 | /*
|
---|
556 | * Iterate the critical sections.
|
---|
557 | */
|
---|
558 | /* This is unsafe, but wtf. */
|
---|
559 | RTNATIVETHREAD const hNativeThread = RTThreadNativeSelf();
|
---|
560 | uint32_t cCritSects = 0;
|
---|
561 | for (PPDMCRITSECTINT pCur = pVM->pUVM->pdm.s.pCritSects;
|
---|
562 | pCur;
|
---|
563 | pCur = pCur->pNext)
|
---|
564 | {
|
---|
565 | /* Same as RTCritSectIsOwner(). */
|
---|
566 | if (pCur->Core.NativeThreadOwner == hNativeThread)
|
---|
567 | {
|
---|
568 | cCritSects++;
|
---|
569 |
|
---|
570 | /*
|
---|
571 | * Copy the name if there is space. Fun stuff.
|
---|
572 | */
|
---|
573 | if (cchLeft)
|
---|
574 | {
|
---|
575 | /* try add comma. */
|
---|
576 | if (cCritSects != 1)
|
---|
577 | {
|
---|
578 | *pszNames++ = ',';
|
---|
579 | if (--cchLeft)
|
---|
580 | {
|
---|
581 | *pszNames++ = ' ';
|
---|
582 | cchLeft--;
|
---|
583 | }
|
---|
584 | }
|
---|
585 |
|
---|
586 | /* try copy the name. */
|
---|
587 | if (cchLeft)
|
---|
588 | {
|
---|
589 | size_t const cchName = strlen(pCur->pszName);
|
---|
590 | if (cchName < cchLeft)
|
---|
591 | {
|
---|
592 | memcpy(pszNames, pCur->pszName, cchName);
|
---|
593 | pszNames += cchName;
|
---|
594 | cchLeft -= cchName;
|
---|
595 | }
|
---|
596 | else
|
---|
597 | {
|
---|
598 | if (cchLeft > 2)
|
---|
599 | {
|
---|
600 | memcpy(pszNames, pCur->pszName, cchLeft - 2);
|
---|
601 | pszNames += cchLeft - 2;
|
---|
602 | cchLeft = 2;
|
---|
603 | }
|
---|
604 | while (cchLeft-- > 0)
|
---|
605 | *pszNames++ = '+';
|
---|
606 | }
|
---|
607 | }
|
---|
608 | *pszNames = '\0';
|
---|
609 | }
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | return cCritSects;
|
---|
614 | }
|
---|
615 |
|
---|
616 |
|
---|
617 | /**
|
---|
618 | * Leave all critical sections the calling thread owns.
|
---|
619 | *
|
---|
620 | * @param pVM The VM handle.
|
---|
621 | */
|
---|
622 | void PDMR3CritSectLeaveAll(PVM pVM)
|
---|
623 | {
|
---|
624 | RTNATIVETHREAD const hNativeSelf = RTThreadNativeSelf();
|
---|
625 | PUVM pUVM = pVM->pUVM;
|
---|
626 |
|
---|
627 | RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
|
---|
628 | for (PPDMCRITSECTINT pCur = pUVM->pdm.s.pCritSects;
|
---|
629 | pCur;
|
---|
630 | pCur = pCur->pNext)
|
---|
631 | {
|
---|
632 | while ( pCur->Core.NativeThreadOwner == hNativeSelf
|
---|
633 | && pCur->Core.cNestings > 0)
|
---|
634 | PDMCritSectLeave((PPDMCRITSECT)pCur);
|
---|
635 | }
|
---|
636 | RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
|
---|
637 | }
|
---|
638 |
|
---|