1 | /** @file
|
---|
2 | *
|
---|
3 | * Automatic locks, implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 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 | #include <iprt/cdefs.h>
|
---|
23 | #include <iprt/critsect.h>
|
---|
24 | #include <iprt/thread.h>
|
---|
25 | #include <iprt/semaphore.h>
|
---|
26 |
|
---|
27 | #include <iprt/err.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 |
|
---|
30 | #if defined(DEBUG)
|
---|
31 | # include <iprt/asm.h> // for ASMReturnAddress
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <iprt/stream.h>
|
---|
37 |
|
---|
38 | #include "VBox/com/AutoLock.h"
|
---|
39 | #include <VBox/com/string.h>
|
---|
40 |
|
---|
41 | #include <vector>
|
---|
42 | #include <list>
|
---|
43 | #include <map>
|
---|
44 |
|
---|
45 | namespace util
|
---|
46 | {
|
---|
47 |
|
---|
48 | ////////////////////////////////////////////////////////////////////////////////
|
---|
49 | //
|
---|
50 | // RuntimeLockClass
|
---|
51 | //
|
---|
52 | ////////////////////////////////////////////////////////////////////////////////
|
---|
53 |
|
---|
54 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
55 | typedef std::map<VBoxLockingClass, RTLOCKVALCLASS> LockValidationClassesMap;
|
---|
56 | LockValidationClassesMap g_mapLockValidationClasses;
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Called from initterm.cpp on process initialization (on the main thread)
|
---|
61 | * to give us a chance to initialize lock validation runtime data.
|
---|
62 | */
|
---|
63 | void InitAutoLockSystem()
|
---|
64 | {
|
---|
65 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
66 | struct
|
---|
67 | {
|
---|
68 | VBoxLockingClass cls;
|
---|
69 | const char *pcszDescription;
|
---|
70 | } aClasses[] =
|
---|
71 | {
|
---|
72 | { LOCKCLASS_VIRTUALBOXOBJECT, "1-VIRTUALBOXOBJECT" },
|
---|
73 | { LOCKCLASS_USBPROXYSERVICE, "2-USBPROXYSERVICE" },
|
---|
74 | { LOCKCLASS_HOSTOBJECT, "3-HOSTOBJECT" },
|
---|
75 | { LOCKCLASS_LISTOFMACHINES, "4-LISTOFMACHINES" },
|
---|
76 | { LOCKCLASS_MACHINEOBJECT, "5-MACHINEOBJECT" },
|
---|
77 | { LOCKCLASS_LISTOFSNAPSHOTS, "6-LISTOFSNAPSHOTS" },
|
---|
78 | { LOCKCLASS_SNAPSHOTOBJECT, "7-SNAPSHOTOBJECT" },
|
---|
79 | { LOCKCLASS_LISTOFMEDIA, "8-LISTOFMEDIA" },
|
---|
80 | { LOCKCLASS_LISTOFOTHEROBJECTS, "9-LISTOFOTHEROBJECTS" },
|
---|
81 | { LOCKCLASS_OTHEROBJECT, "10-OTHEROBJECT" },
|
---|
82 | { LOCKCLASS_USBLIST, "11-USBLIST" },
|
---|
83 | { LOCKCLASS_PROGRESSLIST, "12-PROGRESSLIST" },
|
---|
84 | { LOCKCLASS_OBJECTSTATE, "13-OBJECTSTATE" }
|
---|
85 | };
|
---|
86 |
|
---|
87 | RTLOCKVALCLASS hClass;
|
---|
88 | int vrc;
|
---|
89 | for (unsigned i = 0; i < RT_ELEMENTS(aClasses); ++i)
|
---|
90 | {
|
---|
91 | vrc = RTLockValidatorClassCreate(&hClass,
|
---|
92 | true, /*fAutodidact*/
|
---|
93 | RT_SRC_POS,
|
---|
94 | aClasses[i].pcszDescription);
|
---|
95 | AssertRC(vrc);
|
---|
96 |
|
---|
97 | // teach the new class that the classes created previously can be held
|
---|
98 | // while the new class is being acquired
|
---|
99 | for (LockValidationClassesMap::iterator it = g_mapLockValidationClasses.begin();
|
---|
100 | it != g_mapLockValidationClasses.end();
|
---|
101 | ++it)
|
---|
102 | {
|
---|
103 | RTLOCKVALCLASS &canBeHeld = it->second;
|
---|
104 | vrc = RTLockValidatorClassAddPriorClass(hClass,
|
---|
105 | canBeHeld);
|
---|
106 | AssertRC(vrc);
|
---|
107 | }
|
---|
108 |
|
---|
109 | // and store the new class
|
---|
110 | g_mapLockValidationClasses[aClasses[i].cls] = hClass;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /* WriteLockHandle critsect1(LOCKCLASS_VIRTUALBOXOBJECT);
|
---|
114 | WriteLockHandle critsect2(LOCKCLASS_VIRTUALBOXLIST);
|
---|
115 |
|
---|
116 | AutoWriteLock lock1(critsect1 COMMA_LOCKVAL_SRC_POS);
|
---|
117 | AutoWriteLock lock2(critsect2 COMMA_LOCKVAL_SRC_POS);*/
|
---|
118 | #endif
|
---|
119 | }
|
---|
120 |
|
---|
121 | ////////////////////////////////////////////////////////////////////////////////
|
---|
122 | //
|
---|
123 | // RWLockHandle
|
---|
124 | //
|
---|
125 | ////////////////////////////////////////////////////////////////////////////////
|
---|
126 |
|
---|
127 | struct RWLockHandle::Data
|
---|
128 | {
|
---|
129 | Data()
|
---|
130 | { }
|
---|
131 |
|
---|
132 | RTSEMRW sem;
|
---|
133 | VBoxLockingClass lockClass;
|
---|
134 |
|
---|
135 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
136 | com::Utf8Str strDescription;
|
---|
137 | #endif
|
---|
138 | };
|
---|
139 |
|
---|
140 | RWLockHandle::RWLockHandle(VBoxLockingClass lockClass)
|
---|
141 | {
|
---|
142 | m = new Data();
|
---|
143 |
|
---|
144 | m->lockClass = lockClass;
|
---|
145 |
|
---|
146 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
147 | m->strDescription = com::Utf8StrFmt("r/w %RCv", this);
|
---|
148 | int vrc = RTSemRWCreateEx(&m->sem, 0 /*fFlags*/, g_mapLockValidationClasses[lockClass], RTLOCKVAL_SUB_CLASS_ANY, NULL);
|
---|
149 | #else
|
---|
150 | int vrc = RTSemRWCreateEx(&m->sem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_ANY, NULL);
|
---|
151 | #endif
|
---|
152 | AssertRC(vrc);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /*virtual*/ RWLockHandle::~RWLockHandle()
|
---|
156 | {
|
---|
157 | RTSemRWDestroy(m->sem);
|
---|
158 | delete m;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*virtual*/ bool RWLockHandle::isWriteLockOnCurrentThread() const
|
---|
162 | {
|
---|
163 | return RTSemRWIsWriteOwner(m->sem);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /*virtual*/ void RWLockHandle::lockWrite(LOCKVAL_SRC_POS_DECL)
|
---|
167 | {
|
---|
168 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
169 | int vrc = RTSemRWRequestWriteDebug(m->sem, RT_INDEFINITE_WAIT, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
170 | #else
|
---|
171 | int vrc = RTSemRWRequestWrite(m->sem, RT_INDEFINITE_WAIT);
|
---|
172 | #endif
|
---|
173 | AssertRC(vrc);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /*virtual*/ void RWLockHandle::unlockWrite()
|
---|
177 | {
|
---|
178 | int vrc = RTSemRWReleaseWrite(m->sem);
|
---|
179 | AssertRC(vrc);
|
---|
180 |
|
---|
181 | }
|
---|
182 |
|
---|
183 | /*virtual*/ void RWLockHandle::lockRead(LOCKVAL_SRC_POS_DECL)
|
---|
184 | {
|
---|
185 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
186 | int vrc = RTSemRWRequestReadDebug(m->sem, RT_INDEFINITE_WAIT, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
187 | #else
|
---|
188 | int vrc = RTSemRWRequestRead(m->sem, RT_INDEFINITE_WAIT);
|
---|
189 | #endif
|
---|
190 | AssertRC(vrc);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /*virtual*/ void RWLockHandle::unlockRead()
|
---|
194 | {
|
---|
195 | int vrc = RTSemRWReleaseRead(m->sem);
|
---|
196 | AssertRC(vrc);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*virtual*/ uint32_t RWLockHandle::writeLockLevel() const
|
---|
200 | {
|
---|
201 | /* Note! This does not include read recursions done by the writer! */
|
---|
202 | return RTSemRWGetWriteRecursion(m->sem);
|
---|
203 | }
|
---|
204 |
|
---|
205 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
206 | /*virtual*/ const char* RWLockHandle::describe() const
|
---|
207 | {
|
---|
208 | return m->strDescription.c_str();
|
---|
209 | }
|
---|
210 | #endif
|
---|
211 |
|
---|
212 | ////////////////////////////////////////////////////////////////////////////////
|
---|
213 | //
|
---|
214 | // WriteLockHandle
|
---|
215 | //
|
---|
216 | ////////////////////////////////////////////////////////////////////////////////
|
---|
217 |
|
---|
218 | struct WriteLockHandle::Data
|
---|
219 | {
|
---|
220 | Data()
|
---|
221 | { }
|
---|
222 |
|
---|
223 | mutable RTCRITSECT sem;
|
---|
224 | VBoxLockingClass lockClass;
|
---|
225 |
|
---|
226 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
227 | com::Utf8Str strDescription;
|
---|
228 | #endif
|
---|
229 | };
|
---|
230 |
|
---|
231 | WriteLockHandle::WriteLockHandle(VBoxLockingClass lockClass)
|
---|
232 | {
|
---|
233 | m = new Data;
|
---|
234 |
|
---|
235 | m->lockClass = lockClass;
|
---|
236 |
|
---|
237 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
238 | m->strDescription = com::Utf8StrFmt("crit %RCv", this);
|
---|
239 | int vrc = RTCritSectInitEx(&m->sem, 0/*fFlags*/, g_mapLockValidationClasses[lockClass], RTLOCKVAL_SUB_CLASS_ANY, NULL);
|
---|
240 | #else
|
---|
241 | int vrc = RTCritSectInitEx(&m->sem, 0/*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_ANY, NULL);
|
---|
242 | #endif
|
---|
243 | AssertRC(vrc);
|
---|
244 | }
|
---|
245 |
|
---|
246 | WriteLockHandle::~WriteLockHandle()
|
---|
247 | {
|
---|
248 | RTCritSectDelete(&m->sem);
|
---|
249 | delete m;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*virtual*/ bool WriteLockHandle::isWriteLockOnCurrentThread() const
|
---|
253 | {
|
---|
254 | return RTCritSectIsOwner(&m->sem);
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*virtual*/ void WriteLockHandle::lockWrite(LOCKVAL_SRC_POS_DECL)
|
---|
258 | {
|
---|
259 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
260 | RTCritSectEnterDebug(&m->sem, (uintptr_t)ASMReturnAddress(), RT_SRC_POS_ARGS);
|
---|
261 | #else
|
---|
262 | RTCritSectEnter(&m->sem);
|
---|
263 | #endif
|
---|
264 | }
|
---|
265 |
|
---|
266 | /*virtual*/ void WriteLockHandle::unlockWrite()
|
---|
267 | {
|
---|
268 | RTCritSectLeave(&m->sem);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /*virtual*/ void WriteLockHandle::lockRead(LOCKVAL_SRC_POS_DECL)
|
---|
272 | {
|
---|
273 | lockWrite(LOCKVAL_SRC_POS_ARGS);
|
---|
274 | }
|
---|
275 |
|
---|
276 | /*virtual*/ void WriteLockHandle::unlockRead()
|
---|
277 | {
|
---|
278 | unlockWrite();
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*virtual*/ uint32_t WriteLockHandle::writeLockLevel() const
|
---|
282 | {
|
---|
283 | return RTCritSectGetRecursion(&m->sem);
|
---|
284 | }
|
---|
285 |
|
---|
286 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
287 | /*virtual*/ const char* WriteLockHandle::describe() const
|
---|
288 | {
|
---|
289 | return m->strDescription.c_str();
|
---|
290 | }
|
---|
291 | #endif
|
---|
292 |
|
---|
293 | ////////////////////////////////////////////////////////////////////////////////
|
---|
294 | //
|
---|
295 | // AutoLockBase
|
---|
296 | //
|
---|
297 | ////////////////////////////////////////////////////////////////////////////////
|
---|
298 |
|
---|
299 | typedef std::vector<LockHandle*> HandlesVector;
|
---|
300 | typedef std::vector<uint32_t> CountsVector;
|
---|
301 |
|
---|
302 | struct AutoLockBase::Data
|
---|
303 | {
|
---|
304 | Data(size_t cHandles
|
---|
305 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
306 | , const char *pcszFile_,
|
---|
307 | unsigned uLine_,
|
---|
308 | const char *pcszFunction_
|
---|
309 | #endif
|
---|
310 | )
|
---|
311 | : fIsLocked(false),
|
---|
312 | aHandles(cHandles), // size of array
|
---|
313 | acUnlockedInLeave(cHandles)
|
---|
314 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
315 | , pcszFile(pcszFile_),
|
---|
316 | uLine(uLine_),
|
---|
317 | pcszFunction(pcszFunction_)
|
---|
318 | #endif
|
---|
319 | {
|
---|
320 | for (uint32_t i = 0; i < cHandles; ++i)
|
---|
321 | {
|
---|
322 | acUnlockedInLeave[i] = 0;
|
---|
323 | aHandles[i] = NULL;
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | bool fIsLocked; // if true, then all items in aHandles are locked by this AutoLock and
|
---|
328 | // need to be unlocked in the destructor
|
---|
329 | HandlesVector aHandles; // array (vector) of LockHandle instances; in the case of AutoWriteLock
|
---|
330 | // and AutoReadLock, there will only be one item on the list; with the
|
---|
331 | // AutoMulti* derivatives, there will be multiple
|
---|
332 | CountsVector acUnlockedInLeave; // for each lock handle, how many times the handle was unlocked in leave(); otherwise 0
|
---|
333 |
|
---|
334 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
335 | // information about where the lock occured (passed down from the AutoLock classes)
|
---|
336 | const char *pcszFile;
|
---|
337 | unsigned uLine;
|
---|
338 | const char *pcszFunction;
|
---|
339 | #endif
|
---|
340 | };
|
---|
341 |
|
---|
342 | AutoLockBase::AutoLockBase(uint32_t cHandles
|
---|
343 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
344 | {
|
---|
345 | m = new Data(cHandles
|
---|
346 | COMMA_LOCKVAL_SRC_POS_ARGS);
|
---|
347 | }
|
---|
348 |
|
---|
349 | AutoLockBase::AutoLockBase(uint32_t cHandles,
|
---|
350 | LockHandle *pHandle
|
---|
351 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
352 | {
|
---|
353 | Assert(cHandles == 1);
|
---|
354 | m = new Data(1
|
---|
355 | COMMA_LOCKVAL_SRC_POS_ARGS);
|
---|
356 | m->aHandles[0] = pHandle;
|
---|
357 | }
|
---|
358 |
|
---|
359 | AutoLockBase::~AutoLockBase()
|
---|
360 | {
|
---|
361 | delete m;
|
---|
362 | }
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * Requests ownership of all contained lock handles by calling
|
---|
366 | * the pure virtual callLockImpl() function on each of them,
|
---|
367 | * which must be implemented by the descendant class; in the
|
---|
368 | * implementation, AutoWriteLock will request a write lock
|
---|
369 | * whereas AutoReadLock will request a read lock.
|
---|
370 | *
|
---|
371 | * Does *not* modify the lock counts in the member variables.
|
---|
372 | */
|
---|
373 | void AutoLockBase::callLockOnAllHandles()
|
---|
374 | {
|
---|
375 | for (HandlesVector::iterator it = m->aHandles.begin();
|
---|
376 | it != m->aHandles.end();
|
---|
377 | ++it)
|
---|
378 | {
|
---|
379 | LockHandle *pHandle = *it;
|
---|
380 | if (pHandle)
|
---|
381 | // call virtual function implemented in AutoWriteLock or AutoReadLock
|
---|
382 | this->callLockImpl(*pHandle);
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Releases ownership of all contained lock handles by calling
|
---|
388 | * the pure virtual callUnlockImpl() function on each of them,
|
---|
389 | * which must be implemented by the descendant class; in the
|
---|
390 | * implementation, AutoWriteLock will release a write lock
|
---|
391 | * whereas AutoReadLock will release a read lock.
|
---|
392 | *
|
---|
393 | * Does *not* modify the lock counts in the member variables.
|
---|
394 | */
|
---|
395 | void AutoLockBase::callUnlockOnAllHandles()
|
---|
396 | {
|
---|
397 | // unlock in reverse order!
|
---|
398 | for (HandlesVector::reverse_iterator it = m->aHandles.rbegin();
|
---|
399 | it != m->aHandles.rend();
|
---|
400 | ++it)
|
---|
401 | {
|
---|
402 | LockHandle *pHandle = *it;
|
---|
403 | if (pHandle)
|
---|
404 | // call virtual function implemented in AutoWriteLock or AutoReadLock
|
---|
405 | this->callUnlockImpl(*pHandle);
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * Destructor implementation that can also be called explicitly, if required.
|
---|
411 | * Restores the exact state before the AutoLock was created; that is, unlocks
|
---|
412 | * all contained semaphores and might actually lock them again if leave()
|
---|
413 | * was called during the AutoLock's lifetime.
|
---|
414 | */
|
---|
415 | void AutoLockBase::cleanup()
|
---|
416 | {
|
---|
417 | bool fAnyUnlockedInLeave = false;
|
---|
418 |
|
---|
419 | uint32_t i = 0;
|
---|
420 | for (HandlesVector::iterator it = m->aHandles.begin();
|
---|
421 | it != m->aHandles.end();
|
---|
422 | ++it)
|
---|
423 | {
|
---|
424 | LockHandle *pHandle = *it;
|
---|
425 | if (pHandle)
|
---|
426 | {
|
---|
427 | if (m->acUnlockedInLeave[i])
|
---|
428 | {
|
---|
429 | // there was a leave() before the destruction: then restore the
|
---|
430 | // lock level that might have been set by locks other than our own
|
---|
431 | if (m->fIsLocked)
|
---|
432 | {
|
---|
433 | --m->acUnlockedInLeave[i];
|
---|
434 | fAnyUnlockedInLeave = true;
|
---|
435 | }
|
---|
436 | for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
|
---|
437 | callLockImpl(*pHandle);
|
---|
438 | }
|
---|
439 | }
|
---|
440 | ++i;
|
---|
441 | }
|
---|
442 |
|
---|
443 | if (m->fIsLocked && !fAnyUnlockedInLeave)
|
---|
444 | callUnlockOnAllHandles();
|
---|
445 | }
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * Requests ownership of all contained semaphores. Public method that can
|
---|
449 | * only be called once and that also gets called by the AutoLock constructors.
|
---|
450 | */
|
---|
451 | void AutoLockBase::acquire()
|
---|
452 | {
|
---|
453 | AssertMsg(!m->fIsLocked, ("m->fIsLocked is true, attempting to lock twice!"));
|
---|
454 | callLockOnAllHandles();
|
---|
455 | m->fIsLocked = true;
|
---|
456 | }
|
---|
457 |
|
---|
458 | /**
|
---|
459 | * Releases ownership of all contained semaphores. Public method.
|
---|
460 | */
|
---|
461 | void AutoLockBase::release()
|
---|
462 | {
|
---|
463 | AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot release!"));
|
---|
464 | callUnlockOnAllHandles();
|
---|
465 | m->fIsLocked = false;
|
---|
466 | }
|
---|
467 |
|
---|
468 | ////////////////////////////////////////////////////////////////////////////////
|
---|
469 | //
|
---|
470 | // AutoReadLock
|
---|
471 | //
|
---|
472 | ////////////////////////////////////////////////////////////////////////////////
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * Release all read locks acquired by this instance through the #lock()
|
---|
476 | * call and destroys the instance.
|
---|
477 | *
|
---|
478 | * Note that if there there are nested #lock() calls without the
|
---|
479 | * corresponding number of #unlock() calls when the destructor is called, it
|
---|
480 | * will assert. This is because having an unbalanced number of nested locks
|
---|
481 | * is a program logic error which must be fixed.
|
---|
482 | */
|
---|
483 | /*virtual*/ AutoReadLock::~AutoReadLock()
|
---|
484 | {
|
---|
485 | LockHandle *pHandle = m->aHandles[0];
|
---|
486 |
|
---|
487 | if (pHandle)
|
---|
488 | {
|
---|
489 | if (m->fIsLocked)
|
---|
490 | callUnlockImpl(*pHandle);
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 | /**
|
---|
495 | * Implementation of the pure virtual declared in AutoLockBase.
|
---|
496 | * This gets called by AutoLockBase.acquire() to actually request
|
---|
497 | * the semaphore; in the AutoReadLock implementation, we request
|
---|
498 | * the semaphore in read mode.
|
---|
499 | */
|
---|
500 | /*virtual*/ void AutoReadLock::callLockImpl(LockHandle &l)
|
---|
501 | {
|
---|
502 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
503 | l.lockRead(m->pcszFile, m->uLine, m->pcszFunction);
|
---|
504 | #else
|
---|
505 | l.lockRead();
|
---|
506 | #endif
|
---|
507 | }
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Implementation of the pure virtual declared in AutoLockBase.
|
---|
511 | * This gets called by AutoLockBase.release() to actually release
|
---|
512 | * the semaphore; in the AutoReadLock implementation, we release
|
---|
513 | * the semaphore in read mode.
|
---|
514 | */
|
---|
515 | /*virtual*/ void AutoReadLock::callUnlockImpl(LockHandle &l)
|
---|
516 | {
|
---|
517 | l.unlockRead();
|
---|
518 | }
|
---|
519 |
|
---|
520 | ////////////////////////////////////////////////////////////////////////////////
|
---|
521 | //
|
---|
522 | // AutoWriteLockBase
|
---|
523 | //
|
---|
524 | ////////////////////////////////////////////////////////////////////////////////
|
---|
525 |
|
---|
526 | /**
|
---|
527 | * Implementation of the pure virtual declared in AutoLockBase.
|
---|
528 | * This gets called by AutoLockBase.acquire() to actually request
|
---|
529 | * the semaphore; in the AutoWriteLock implementation, we request
|
---|
530 | * the semaphore in write mode.
|
---|
531 | */
|
---|
532 | /*virtual*/ void AutoWriteLockBase::callLockImpl(LockHandle &l)
|
---|
533 | {
|
---|
534 | #ifdef VBOX_WITH_MAIN_LOCK_VALIDATION
|
---|
535 | l.lockWrite(m->pcszFile, m->uLine, m->pcszFunction);
|
---|
536 | #else
|
---|
537 | l.lockWrite();
|
---|
538 | #endif
|
---|
539 | }
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * Implementation of the pure virtual declared in AutoLockBase.
|
---|
543 | * This gets called by AutoLockBase.release() to actually release
|
---|
544 | * the semaphore; in the AutoWriteLock implementation, we release
|
---|
545 | * the semaphore in write mode.
|
---|
546 | */
|
---|
547 | /*virtual*/ void AutoWriteLockBase::callUnlockImpl(LockHandle &l)
|
---|
548 | {
|
---|
549 | l.unlockWrite();
|
---|
550 | }
|
---|
551 |
|
---|
552 | /**
|
---|
553 | * Causes the current thread to completely release the write lock to make
|
---|
554 | * the managed semaphore immediately available for locking by other threads.
|
---|
555 | *
|
---|
556 | * This implies that all nested write locks on the semaphore will be
|
---|
557 | * released, even those that were acquired through the calls to #lock()
|
---|
558 | * methods of all other AutoWriteLock/AutoReadLock instances managing the
|
---|
559 | * <b>same</b> read/write semaphore.
|
---|
560 | *
|
---|
561 | * After calling this method, the only method you are allowed to call is
|
---|
562 | * #enter(). It will acquire the write lock again and restore the same
|
---|
563 | * level of nesting as it had before calling #leave().
|
---|
564 | *
|
---|
565 | * If this instance is destroyed without calling #enter(), the destructor
|
---|
566 | * will try to restore the write lock level that existed when #leave() was
|
---|
567 | * called minus the number of nested #lock() calls made on this instance
|
---|
568 | * itself. This is done to preserve lock levels of other
|
---|
569 | * AutoWriteLock/AutoReadLock instances managing the same semaphore (if
|
---|
570 | * any). Tiis also means that the destructor may indefinitely block if a
|
---|
571 | * write or a read lock is owned by some other thread by that time.
|
---|
572 | */
|
---|
573 | void AutoWriteLockBase::leave()
|
---|
574 | {
|
---|
575 | AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot leave()!"));
|
---|
576 |
|
---|
577 | // unlock in reverse order!
|
---|
578 | uint32_t i = m->aHandles.size();
|
---|
579 | for (HandlesVector::reverse_iterator it = m->aHandles.rbegin();
|
---|
580 | it != m->aHandles.rend();
|
---|
581 | ++it)
|
---|
582 | {
|
---|
583 | --i; // array index is zero based, decrement with every loop since we iterate backwards
|
---|
584 | LockHandle *pHandle = *it;
|
---|
585 | if (pHandle)
|
---|
586 | {
|
---|
587 | AssertMsg(m->acUnlockedInLeave[i] == 0, ("m->cUnlockedInLeave[%d] is %d, must be 0! Called leave() twice?", i, m->acUnlockedInLeave[i]));
|
---|
588 | m->acUnlockedInLeave[i] = pHandle->writeLockLevel();
|
---|
589 | AssertMsg(m->acUnlockedInLeave[i] >= 1, ("m->cUnlockedInLeave[%d] is %d, must be >=1!", i, m->acUnlockedInLeave[i]));
|
---|
590 |
|
---|
591 | for (uint32_t left = m->acUnlockedInLeave[i];
|
---|
592 | left;
|
---|
593 | --left)
|
---|
594 | callUnlockImpl(*pHandle);
|
---|
595 | }
|
---|
596 | }
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Causes the current thread to restore the write lock level after the
|
---|
601 | * #leave() call. This call will indefinitely block if another thread has
|
---|
602 | * successfully acquired a write or a read lock on the same semaphore in
|
---|
603 | * between.
|
---|
604 | */
|
---|
605 | void AutoWriteLockBase::enter()
|
---|
606 | {
|
---|
607 | AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot enter()!"));
|
---|
608 |
|
---|
609 | uint32_t i = 0;
|
---|
610 | for (HandlesVector::iterator it = m->aHandles.begin();
|
---|
611 | it != m->aHandles.end();
|
---|
612 | ++it)
|
---|
613 | {
|
---|
614 | LockHandle *pHandle = *it;
|
---|
615 | if (pHandle)
|
---|
616 | {
|
---|
617 | AssertMsg(m->acUnlockedInLeave[i] != 0, ("m->cUnlockedInLeave[%d] is 0! enter() without leave()?", i));
|
---|
618 |
|
---|
619 | for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
|
---|
620 | callLockImpl(*pHandle);
|
---|
621 | }
|
---|
622 | ++i;
|
---|
623 | }
|
---|
624 | }
|
---|
625 |
|
---|
626 | /**
|
---|
627 | * Same as #enter() but checks if the current thread actally owns the lock
|
---|
628 | * and only proceeds if not. As a result, as opposed to #enter(), doesn't
|
---|
629 | * assert when called with the lock already being held.
|
---|
630 | */
|
---|
631 | void AutoWriteLockBase::maybeEnter()
|
---|
632 | {
|
---|
633 | uint32_t i = 0;
|
---|
634 | for (HandlesVector::iterator it = m->aHandles.begin();
|
---|
635 | it != m->aHandles.end();
|
---|
636 | ++it)
|
---|
637 | {
|
---|
638 | LockHandle *pHandle = *it;
|
---|
639 | if (pHandle)
|
---|
640 | {
|
---|
641 | if (!pHandle->isWriteLockOnCurrentThread())
|
---|
642 | {
|
---|
643 | for (; m->acUnlockedInLeave[i]; --m->acUnlockedInLeave[i])
|
---|
644 | callLockImpl(*pHandle);
|
---|
645 | }
|
---|
646 | }
|
---|
647 | ++i;
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | ////////////////////////////////////////////////////////////////////////////////
|
---|
652 | //
|
---|
653 | // AutoWriteLock
|
---|
654 | //
|
---|
655 | ////////////////////////////////////////////////////////////////////////////////
|
---|
656 |
|
---|
657 | /**
|
---|
658 | * Attaches another handle to this auto lock instance.
|
---|
659 | *
|
---|
660 | * The previous object's lock is completely released before the new one is
|
---|
661 | * acquired. The lock level of the new handle will be the same. This
|
---|
662 | * also means that if the lock was not acquired at all before #attach(), it
|
---|
663 | * will not be acquired on the new handle too.
|
---|
664 | *
|
---|
665 | * @param aHandle New handle to attach.
|
---|
666 | */
|
---|
667 | void AutoWriteLock::attach(LockHandle *aHandle)
|
---|
668 | {
|
---|
669 | LockHandle *pHandle = m->aHandles[0];
|
---|
670 |
|
---|
671 | /* detect simple self-reattachment */
|
---|
672 | if (pHandle != aHandle)
|
---|
673 | {
|
---|
674 | bool fWasLocked = m->fIsLocked;
|
---|
675 |
|
---|
676 | cleanup();
|
---|
677 |
|
---|
678 | m->aHandles[0] = aHandle;
|
---|
679 | m->fIsLocked = fWasLocked;
|
---|
680 |
|
---|
681 | if (aHandle)
|
---|
682 | if (fWasLocked)
|
---|
683 | callLockImpl(*aHandle);
|
---|
684 | }
|
---|
685 | }
|
---|
686 |
|
---|
687 | /**
|
---|
688 | * Returns @c true if the current thread holds a write lock on the managed
|
---|
689 | * read/write semaphore. Returns @c false if the managed semaphore is @c
|
---|
690 | * NULL.
|
---|
691 | *
|
---|
692 | * @note Intended for debugging only.
|
---|
693 | */
|
---|
694 | bool AutoWriteLock::isWriteLockOnCurrentThread() const
|
---|
695 | {
|
---|
696 | return m->aHandles[0] ? m->aHandles[0]->isWriteLockOnCurrentThread() : false;
|
---|
697 | }
|
---|
698 |
|
---|
699 | /**
|
---|
700 | * Returns the current write lock level of the managed smaphore. The lock
|
---|
701 | * level determines the number of nested #lock() calls on the given
|
---|
702 | * semaphore handle. Returns @c 0 if the managed semaphore is @c
|
---|
703 | * NULL.
|
---|
704 | *
|
---|
705 | * Note that this call is valid only when the current thread owns a write
|
---|
706 | * lock on the given semaphore handle and will assert otherwise.
|
---|
707 | *
|
---|
708 | * @note Intended for debugging only.
|
---|
709 | */
|
---|
710 | uint32_t AutoWriteLock::writeLockLevel() const
|
---|
711 | {
|
---|
712 | return m->aHandles[0] ? m->aHandles[0]->writeLockLevel() : 0;
|
---|
713 | }
|
---|
714 |
|
---|
715 | ////////////////////////////////////////////////////////////////////////////////
|
---|
716 | //
|
---|
717 | // AutoMultiWriteLock*
|
---|
718 | //
|
---|
719 | ////////////////////////////////////////////////////////////////////////////////
|
---|
720 |
|
---|
721 | AutoMultiWriteLock2::AutoMultiWriteLock2(Lockable *pl1,
|
---|
722 | Lockable *pl2
|
---|
723 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
724 | : AutoWriteLockBase(2
|
---|
725 | COMMA_LOCKVAL_SRC_POS_ARGS)
|
---|
726 | {
|
---|
727 | if (pl1)
|
---|
728 | m->aHandles[0] = pl1->lockHandle();
|
---|
729 | if (pl2)
|
---|
730 | m->aHandles[1] = pl2->lockHandle();
|
---|
731 | acquire();
|
---|
732 | }
|
---|
733 |
|
---|
734 | AutoMultiWriteLock2::AutoMultiWriteLock2(LockHandle *pl1,
|
---|
735 | LockHandle *pl2
|
---|
736 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
737 | : AutoWriteLockBase(2
|
---|
738 | COMMA_LOCKVAL_SRC_POS_ARGS)
|
---|
739 | {
|
---|
740 | m->aHandles[0] = pl1;
|
---|
741 | m->aHandles[1] = pl2;
|
---|
742 | acquire();
|
---|
743 | }
|
---|
744 |
|
---|
745 | AutoMultiWriteLock3::AutoMultiWriteLock3(Lockable *pl1,
|
---|
746 | Lockable *pl2,
|
---|
747 | Lockable *pl3
|
---|
748 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
749 | : AutoWriteLockBase(3
|
---|
750 | COMMA_LOCKVAL_SRC_POS_ARGS)
|
---|
751 | {
|
---|
752 | if (pl1)
|
---|
753 | m->aHandles[0] = pl1->lockHandle();
|
---|
754 | if (pl2)
|
---|
755 | m->aHandles[1] = pl2->lockHandle();
|
---|
756 | if (pl3)
|
---|
757 | m->aHandles[2] = pl3->lockHandle();
|
---|
758 | acquire();
|
---|
759 | }
|
---|
760 |
|
---|
761 | AutoMultiWriteLock3::AutoMultiWriteLock3(LockHandle *pl1,
|
---|
762 | LockHandle *pl2,
|
---|
763 | LockHandle *pl3
|
---|
764 | COMMA_LOCKVAL_SRC_POS_DECL)
|
---|
765 | : AutoWriteLockBase(3
|
---|
766 | COMMA_LOCKVAL_SRC_POS_ARGS)
|
---|
767 | {
|
---|
768 | m->aHandles[0] = pl1;
|
---|
769 | m->aHandles[1] = pl2;
|
---|
770 | m->aHandles[2] = pl3;
|
---|
771 | acquire();
|
---|
772 | }
|
---|
773 |
|
---|
774 | } /* namespace util */
|
---|
775 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|