VirtualBox

source: vbox/trunk/src/VBox/Main/VirtualBoxBase.cpp@ 33876

Last change on this file since 33876 was 33708, checked in by vboxsync, 14 years ago

Main: Eliminate the last bits of VirtualBoxBaseWithChildrenNEXT. It won't be missed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.5 KB
Line 
1/* $Id: VirtualBoxBase.cpp 33708 2010-11-02 18:46:46Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM base classes implementation
6 */
7
8/*
9 * Copyright (C) 2006-2010 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#include <iprt/semaphore.h>
21#include <iprt/asm.h>
22
23#if !defined (VBOX_WITH_XPCOM)
24#include <windows.h>
25#include <dbghelp.h>
26#else /* !defined (VBOX_WITH_XPCOM) */
27/// @todo remove when VirtualBoxErrorInfo goes away from here
28#include <nsIServiceManager.h>
29#include <nsIExceptionService.h>
30#endif /* !defined (VBOX_WITH_XPCOM) */
31
32#include "VirtualBoxBase.h"
33#include "AutoCaller.h"
34#include "VirtualBoxErrorInfoImpl.h"
35#include "Logging.h"
36
37#include "VBox/com/ErrorInfo.h"
38#include "VBox/com/MultiResult.h"
39
40////////////////////////////////////////////////////////////////////////////////
41//
42// VirtualBoxBase
43//
44////////////////////////////////////////////////////////////////////////////////
45
46VirtualBoxBase::VirtualBoxBase()
47 : mStateLock(LOCKCLASS_OBJECTSTATE)
48{
49 mState = NotReady;
50 mStateChangeThread = NIL_RTTHREAD;
51 mCallers = 0;
52 mZeroCallersSem = NIL_RTSEMEVENT;
53 mInitUninitSem = NIL_RTSEMEVENTMULTI;
54 mInitUninitWaiters = 0;
55 mObjectLock = NULL;
56}
57
58VirtualBoxBase::~VirtualBoxBase()
59{
60 if (mObjectLock)
61 delete mObjectLock;
62 Assert(mInitUninitWaiters == 0);
63 Assert(mInitUninitSem == NIL_RTSEMEVENTMULTI);
64 if (mZeroCallersSem != NIL_RTSEMEVENT)
65 RTSemEventDestroy (mZeroCallersSem);
66 mCallers = 0;
67 mStateChangeThread = NIL_RTTHREAD;
68 mState = NotReady;
69}
70
71/**
72 * This virtual method returns an RWLockHandle that can be used to
73 * protect instance data. This RWLockHandle is generally referred to
74 * as the "object lock"; its locking class (for lock order validation)
75 * must be returned by another virtual method, getLockingClass(), which
76 * by default returns LOCKCLASS_OTHEROBJECT but is overridden by several
77 * subclasses such as VirtualBox, Host, Machine and others.
78 *
79 * On the first call this method lazily creates the RWLockHandle.
80 *
81 * @return
82 */
83/* virtual */
84RWLockHandle *VirtualBoxBase::lockHandle() const
85{
86 /* lazy initialization */
87 if (RT_UNLIKELY(!mObjectLock))
88 {
89 AssertCompile (sizeof (RWLockHandle *) == sizeof (void *));
90
91 // getLockingClass() is overridden by many subclasses to return
92 // one of the locking classes listed at the top of AutoLock.h
93 RWLockHandle *objLock = new RWLockHandle(getLockingClass());
94 if (!ASMAtomicCmpXchgPtr(&mObjectLock, objLock, NULL))
95 {
96 delete objLock;
97 objLock = ASMAtomicReadPtrT(&mObjectLock, RWLockHandle *);
98 }
99 return objLock;
100 }
101 return mObjectLock;
102}
103
104/**
105 * Increments the number of calls to this object by one.
106 *
107 * After this method succeeds, it is guaranteed that the object will remain
108 * in the Ready (or in the Limited) state at least until #releaseCaller() is
109 * called.
110 *
111 * This method is intended to mark the beginning of sections of code within
112 * methods of COM objects that depend on the readiness (Ready) state. The
113 * Ready state is a primary "ready to serve" state. Usually all code that
114 * works with component's data depends on it. On practice, this means that
115 * almost every public method, setter or getter of the object should add
116 * itself as an object's caller at the very beginning, to protect from an
117 * unexpected uninitialization that may happen on a different thread.
118 *
119 * Besides the Ready state denoting that the object is fully functional,
120 * there is a special Limited state. The Limited state means that the object
121 * is still functional, but its functionality is limited to some degree, so
122 * not all operations are possible. The @a aLimited argument to this method
123 * determines whether the caller represents this limited functionality or
124 * not.
125 *
126 * This method succeeds (and increments the number of callers) only if the
127 * current object's state is Ready. Otherwise, it will return E_ACCESSDENIED
128 * to indicate that the object is not operational. There are two exceptions
129 * from this rule:
130 * <ol>
131 * <li>If the @a aLimited argument is |true|, then this method will also
132 * succeed if the object's state is Limited (or Ready, of course).
133 * </li>
134 * <li>If this method is called from the same thread that placed
135 * the object to InInit or InUninit state (i.e. either from within the
136 * AutoInitSpan or AutoUninitSpan scope), it will succeed as well (but
137 * will not increase the number of callers).
138 * </li>
139 * </ol>
140 *
141 * Normally, calling addCaller() never blocks. However, if this method is
142 * called by a thread created from within the AutoInitSpan scope and this
143 * scope is still active (i.e. the object state is InInit), it will block
144 * until the AutoInitSpan destructor signals that it has finished
145 * initialization.
146 *
147 * When this method returns a failure, the caller must not use the object
148 * and should return the failed result code to its own caller.
149 *
150 * @param aState Where to store the current object's state (can be
151 * used in overridden methods to determine the cause of
152 * the failure).
153 * @param aLimited |true| to add a limited caller.
154 *
155 * @return S_OK on success or E_ACCESSDENIED on failure.
156 *
157 * @note It is preferable to use the #addLimitedCaller() rather than
158 * calling this method with @a aLimited = |true|, for better
159 * self-descriptiveness.
160 *
161 * @sa #addLimitedCaller()
162 * @sa #releaseCaller()
163 */
164HRESULT VirtualBoxBase::addCaller(State *aState /* = NULL */,
165 bool aLimited /* = false */)
166{
167 AutoWriteLock stateLock(mStateLock COMMA_LOCKVAL_SRC_POS);
168
169 HRESULT rc = E_ACCESSDENIED;
170
171 if (mState == Ready || (aLimited && mState == Limited))
172 {
173 /* if Ready or allows Limited, increase the number of callers */
174 ++ mCallers;
175 rc = S_OK;
176 }
177 else
178 if (mState == InInit || mState == InUninit)
179 {
180 if (mStateChangeThread == RTThreadSelf())
181 {
182 /* Called from the same thread that is doing AutoInitSpan or
183 * AutoUninitSpan, just succeed */
184 rc = S_OK;
185 }
186 else if (mState == InInit)
187 {
188 /* addCaller() is called by a "child" thread while the "parent"
189 * thread is still doing AutoInitSpan/AutoReinitSpan, so wait for
190 * the state to become either Ready/Limited or InitFailed (in
191 * case of init failure).
192 *
193 * Note that we increase the number of callers anyway -- to
194 * prevent AutoUninitSpan from early completion if we are
195 * still not scheduled to pick up the posted semaphore when
196 * uninit() is called.
197 */
198 ++ mCallers;
199
200 /* lazy semaphore creation */
201 if (mInitUninitSem == NIL_RTSEMEVENTMULTI)
202 {
203 RTSemEventMultiCreate (&mInitUninitSem);
204 Assert(mInitUninitWaiters == 0);
205 }
206
207 ++ mInitUninitWaiters;
208
209 LogFlowThisFunc(("Waiting for AutoInitSpan/AutoReinitSpan to finish...\n"));
210
211 stateLock.leave();
212 RTSemEventMultiWait (mInitUninitSem, RT_INDEFINITE_WAIT);
213 stateLock.enter();
214
215 if (-- mInitUninitWaiters == 0)
216 {
217 /* destroy the semaphore since no more necessary */
218 RTSemEventMultiDestroy (mInitUninitSem);
219 mInitUninitSem = NIL_RTSEMEVENTMULTI;
220 }
221
222 if (mState == Ready || (aLimited && mState == Limited))
223 rc = S_OK;
224 else
225 {
226 Assert(mCallers != 0);
227 -- mCallers;
228 if (mCallers == 0 && mState == InUninit)
229 {
230 /* inform AutoUninitSpan ctor there are no more callers */
231 RTSemEventSignal(mZeroCallersSem);
232 }
233 }
234 }
235 }
236
237 if (aState)
238 *aState = mState;
239
240 if (FAILED(rc))
241 {
242 if (mState == VirtualBoxBase::Limited)
243 rc = setError(rc, "The object functionality is limited");
244 else
245 rc = setError(rc, "The object is not ready");
246 }
247
248 return rc;
249}
250
251/**
252 * Decreases the number of calls to this object by one.
253 *
254 * Must be called after every #addCaller() or #addLimitedCaller() when
255 * protecting the object from uninitialization is no more necessary.
256 */
257void VirtualBoxBase::releaseCaller()
258{
259 AutoWriteLock stateLock(mStateLock COMMA_LOCKVAL_SRC_POS);
260
261 if (mState == Ready || mState == Limited)
262 {
263 /* if Ready or Limited, decrease the number of callers */
264 AssertMsgReturn(mCallers != 0, ("mCallers is ZERO!"), (void) 0);
265 --mCallers;
266
267 return;
268 }
269
270 if (mState == InInit || mState == InUninit)
271 {
272 if (mStateChangeThread == RTThreadSelf())
273 {
274 /* Called from the same thread that is doing AutoInitSpan or
275 * AutoUninitSpan: just succeed */
276 return;
277 }
278
279 if (mState == InUninit)
280 {
281 /* the caller is being released after AutoUninitSpan has begun */
282 AssertMsgReturn(mCallers != 0, ("mCallers is ZERO!"), (void) 0);
283 --mCallers;
284
285 if (mCallers == 0)
286 /* inform the Auto*UninitSpan ctor there are no more callers */
287 RTSemEventSignal(mZeroCallersSem);
288
289 return;
290 }
291 }
292
293 AssertMsgFailed (("mState = %d!", mState));
294}
295
296/**
297 * Sets error info for the current thread. This is an internal function that
298 * gets eventually called by all public variants. If @a aWarning is
299 * @c true, then the highest (31) bit in the @a aResultCode value which
300 * indicates the error severity is reset to zero to make sure the receiver will
301 * recognize that the created error info object represents a warning rather
302 * than an error.
303 */
304/* static */
305HRESULT VirtualBoxBase::setErrorInternal(HRESULT aResultCode,
306 const GUID &aIID,
307 const char *pcszComponent,
308 const Utf8Str &aText,
309 bool aWarning,
310 bool aLogIt)
311{
312 /* whether multi-error mode is turned on */
313 bool preserve = MultiResult::isMultiEnabled();
314
315 if (aLogIt)
316 LogRel(("ERROR [COM]: aRC=%Rhrc (%#08x) aIID={%RTuuid} aComponent={%s} aText={%s} aWarning=%RTbool, preserve=%RTbool\n",
317 aResultCode,
318 aResultCode,
319 &aIID,
320 pcszComponent,
321 aText.c_str(),
322 aWarning,
323 preserve));
324
325 /* these are mandatory, others -- not */
326 AssertReturn((!aWarning && FAILED(aResultCode)) ||
327 (aWarning && aResultCode != S_OK),
328 E_FAIL);
329 AssertReturn(!aText.isEmpty(), E_FAIL);
330
331 /* reset the error severity bit if it's a warning */
332 if (aWarning)
333 aResultCode &= ~0x80000000;
334
335 HRESULT rc = S_OK;
336
337 do
338 {
339 ComObjPtr<VirtualBoxErrorInfo> info;
340 rc = info.createObject();
341 if (FAILED(rc)) break;
342
343#if !defined (VBOX_WITH_XPCOM)
344
345 ComPtr<IVirtualBoxErrorInfo> curInfo;
346 if (preserve)
347 {
348 /* get the current error info if any */
349 ComPtr<IErrorInfo> err;
350 rc = ::GetErrorInfo (0, err.asOutParam());
351 if (FAILED(rc)) break;
352 rc = err.queryInterfaceTo(curInfo.asOutParam());
353 if (FAILED(rc))
354 {
355 /* create a IVirtualBoxErrorInfo wrapper for the native
356 * IErrorInfo object */
357 ComObjPtr<VirtualBoxErrorInfo> wrapper;
358 rc = wrapper.createObject();
359 if (SUCCEEDED(rc))
360 {
361 rc = wrapper->init (err);
362 if (SUCCEEDED(rc))
363 curInfo = wrapper;
364 }
365 }
366 }
367 /* On failure, curInfo will stay null */
368 Assert(SUCCEEDED(rc) || curInfo.isNull());
369
370 /* set the current error info and preserve the previous one if any */
371 rc = info->init(aResultCode, aIID, pcszComponent, aText, curInfo);
372 if (FAILED(rc)) break;
373
374 ComPtr<IErrorInfo> err;
375 rc = info.queryInterfaceTo(err.asOutParam());
376 if (SUCCEEDED(rc))
377 rc = ::SetErrorInfo (0, err);
378
379#else // !defined (VBOX_WITH_XPCOM)
380
381 nsCOMPtr <nsIExceptionService> es;
382 es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
383 if (NS_SUCCEEDED(rc))
384 {
385 nsCOMPtr <nsIExceptionManager> em;
386 rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
387 if (FAILED(rc)) break;
388
389 ComPtr<IVirtualBoxErrorInfo> curInfo;
390 if (preserve)
391 {
392 /* get the current error info if any */
393 ComPtr<nsIException> ex;
394 rc = em->GetCurrentException (ex.asOutParam());
395 if (FAILED(rc)) break;
396 rc = ex.queryInterfaceTo(curInfo.asOutParam());
397 if (FAILED(rc))
398 {
399 /* create a IVirtualBoxErrorInfo wrapper for the native
400 * nsIException object */
401 ComObjPtr<VirtualBoxErrorInfo> wrapper;
402 rc = wrapper.createObject();
403 if (SUCCEEDED(rc))
404 {
405 rc = wrapper->init (ex);
406 if (SUCCEEDED(rc))
407 curInfo = wrapper;
408 }
409 }
410 }
411 /* On failure, curInfo will stay null */
412 Assert(SUCCEEDED(rc) || curInfo.isNull());
413
414 /* set the current error info and preserve the previous one if any */
415 rc = info->init(aResultCode, aIID, pcszComponent, Bstr(aText), curInfo);
416 if (FAILED(rc)) break;
417
418 ComPtr<nsIException> ex;
419 rc = info.queryInterfaceTo(ex.asOutParam());
420 if (SUCCEEDED(rc))
421 rc = em->SetCurrentException (ex);
422 }
423 else if (rc == NS_ERROR_UNEXPECTED)
424 {
425 /*
426 * It is possible that setError() is being called by the object
427 * after the XPCOM shutdown sequence has been initiated
428 * (for example, when XPCOM releases all instances it internally
429 * references, which can cause object's FinalConstruct() and then
430 * uninit()). In this case, do_GetService() above will return
431 * NS_ERROR_UNEXPECTED and it doesn't actually make sense to
432 * set the exception (nobody will be able to read it).
433 */
434 LogWarningFunc(("Will not set an exception because nsIExceptionService is not available "
435 "(NS_ERROR_UNEXPECTED). XPCOM is being shutdown?\n"));
436 rc = NS_OK;
437 }
438
439#endif // !defined (VBOX_WITH_XPCOM)
440 }
441 while (0);
442
443 AssertComRC (rc);
444
445 return SUCCEEDED(rc) ? aResultCode : rc;
446}
447
448/**
449 * Shortcut instance method to calling the static setErrorInternal with the
450 * class interface ID and component name inserted correctly. This uses the
451 * virtual getClassIID() and getComponentName() methods which are automatically
452 * defined by the VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT macro.
453 * @param aResultCode
454 * @param pcsz
455 * @return
456 */
457HRESULT VirtualBoxBase::setError(HRESULT aResultCode, const char *pcsz, ...)
458{
459 va_list args;
460 va_start(args, pcsz);
461 HRESULT rc = setErrorInternal(aResultCode,
462 this->getClassIID(),
463 this->getComponentName(),
464 Utf8Str(pcsz, args),
465 false /* aWarning */,
466 true /* aLogIt */);
467 va_end(args);
468 return rc;
469}
470
471/**
472 * Like setError(), but sets the "warning" bit in the call to setErrorInternal().
473 * @param aResultCode
474 * @param pcsz
475 * @return
476 */
477HRESULT VirtualBoxBase::setWarning(HRESULT aResultCode, const char *pcsz, ...)
478{
479 va_list args;
480 va_start(args, pcsz);
481 HRESULT rc = setErrorInternal(aResultCode,
482 this->getClassIID(),
483 this->getComponentName(),
484 Utf8Str(pcsz, args),
485 true /* aWarning */,
486 true /* aLogIt */);
487 va_end(args);
488 return rc;
489}
490
491/**
492 * Like setError(), but disables the "log" flag in the call to setErrorInternal().
493 * @param aResultCode
494 * @param pcsz
495 * @return
496 */
497HRESULT VirtualBoxBase::setErrorNoLog(HRESULT aResultCode, const char *pcsz, ...)
498{
499 va_list args;
500 va_start(args, pcsz);
501 HRESULT rc = setErrorInternal(aResultCode,
502 this->getClassIID(),
503 this->getComponentName(),
504 Utf8Str(pcsz, args),
505 false /* aWarning */,
506 false /* aLogIt */);
507 va_end(args);
508 return rc;
509}
510
511////////////////////////////////////////////////////////////////////////////////
512//
513// AutoInitSpan methods
514//
515////////////////////////////////////////////////////////////////////////////////
516
517/**
518 * Creates a smart initialization span object that places the object to
519 * InInit state.
520 *
521 * Please see the AutoInitSpan class description for more info.
522 *
523 * @param aObj |this| pointer of the managed VirtualBoxBase object whose
524 * init() method is being called.
525 * @param aResult Default initialization result.
526 */
527AutoInitSpan::AutoInitSpan(VirtualBoxBase *aObj,
528 Result aResult /* = Failed */)
529 : mObj(aObj),
530 mResult(aResult),
531 mOk(false)
532{
533 Assert(aObj);
534
535 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
536
537 mOk = mObj->mState == VirtualBoxBase::NotReady;
538 AssertReturnVoid (mOk);
539
540 mObj->setState(VirtualBoxBase::InInit);
541}
542
543/**
544 * Places the managed VirtualBoxBase object to Ready/Limited state if the
545 * initialization succeeded or partly succeeded, or places it to InitFailed
546 * state and calls the object's uninit() method.
547 *
548 * Please see the AutoInitSpan class description for more info.
549 */
550AutoInitSpan::~AutoInitSpan()
551{
552 /* if the state was other than NotReady, do nothing */
553 if (!mOk)
554 return;
555
556 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
557
558 Assert(mObj->mState == VirtualBoxBase::InInit);
559
560 if (mObj->mCallers > 0)
561 {
562 Assert(mObj->mInitUninitWaiters > 0);
563
564 /* We have some pending addCaller() calls on other threads (created
565 * during InInit), signal that InInit is finished and they may go on. */
566 RTSemEventMultiSignal(mObj->mInitUninitSem);
567 }
568
569 if (mResult == Succeeded)
570 {
571 mObj->setState(VirtualBoxBase::Ready);
572 }
573 else
574 if (mResult == Limited)
575 {
576 mObj->setState(VirtualBoxBase::Limited);
577 }
578 else
579 {
580 mObj->setState(VirtualBoxBase::InitFailed);
581 /* leave the lock to prevent nesting when uninit() is called */
582 stateLock.leave();
583 /* call uninit() to let the object uninit itself after failed init() */
584 mObj->uninit();
585 /* Note: the object may no longer exist here (for example, it can call
586 * the destructor in uninit()) */
587 }
588}
589
590// AutoReinitSpan methods
591////////////////////////////////////////////////////////////////////////////////
592
593/**
594 * Creates a smart re-initialization span object and places the object to
595 * InInit state.
596 *
597 * Please see the AutoInitSpan class description for more info.
598 *
599 * @param aObj |this| pointer of the managed VirtualBoxBase object whose
600 * re-initialization method is being called.
601 */
602AutoReinitSpan::AutoReinitSpan(VirtualBoxBase *aObj)
603 : mObj(aObj),
604 mSucceeded(false),
605 mOk(false)
606{
607 Assert(aObj);
608
609 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
610
611 mOk = mObj->mState == VirtualBoxBase::Limited;
612 AssertReturnVoid (mOk);
613
614 mObj->setState(VirtualBoxBase::InInit);
615}
616
617/**
618 * Places the managed VirtualBoxBase object to Ready state if the
619 * re-initialization succeeded (i.e. #setSucceeded() has been called) or back to
620 * Limited state otherwise.
621 *
622 * Please see the AutoInitSpan class description for more info.
623 */
624AutoReinitSpan::~AutoReinitSpan()
625{
626 /* if the state was other than Limited, do nothing */
627 if (!mOk)
628 return;
629
630 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
631
632 Assert(mObj->mState == VirtualBoxBase::InInit);
633
634 if (mObj->mCallers > 0 && mObj->mInitUninitWaiters > 0)
635 {
636 /* We have some pending addCaller() calls on other threads (created
637 * during InInit), signal that InInit is finished and they may go on. */
638 RTSemEventMultiSignal(mObj->mInitUninitSem);
639 }
640
641 if (mSucceeded)
642 {
643 mObj->setState(VirtualBoxBase::Ready);
644 }
645 else
646 {
647 mObj->setState(VirtualBoxBase::Limited);
648 }
649}
650
651// AutoUninitSpan methods
652////////////////////////////////////////////////////////////////////////////////
653
654/**
655 * Creates a smart uninitialization span object and places this object to
656 * InUninit state.
657 *
658 * Please see the AutoInitSpan class description for more info.
659 *
660 * @note This method blocks the current thread execution until the number of
661 * callers of the managed VirtualBoxBase object drops to zero!
662 *
663 * @param aObj |this| pointer of the VirtualBoxBase object whose uninit()
664 * method is being called.
665 */
666AutoUninitSpan::AutoUninitSpan(VirtualBoxBase *aObj)
667 : mObj(aObj),
668 mInitFailed(false),
669 mUninitDone(false)
670{
671 Assert(aObj);
672
673 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
674
675 Assert(mObj->mState != VirtualBoxBase::InInit);
676
677 /* Set mUninitDone to |true| if this object is already uninitialized
678 * (NotReady) or if another AutoUninitSpan is currently active on some
679 * other thread (InUninit). */
680 mUninitDone = mObj->mState == VirtualBoxBase::NotReady
681 || mObj->mState == VirtualBoxBase::InUninit;
682
683 if (mObj->mState == VirtualBoxBase::InitFailed)
684 {
685 /* we've been called by init() on failure */
686 mInitFailed = true;
687 }
688 else
689 {
690 if (mUninitDone)
691 {
692 /* do nothing if already uninitialized */
693 if (mObj->mState == VirtualBoxBase::NotReady)
694 return;
695
696 /* otherwise, wait until another thread finishes uninitialization.
697 * This is necessary to make sure that when this method returns, the
698 * object is NotReady and therefore can be deleted (for example). */
699
700 /* lazy semaphore creation */
701 if (mObj->mInitUninitSem == NIL_RTSEMEVENTMULTI)
702 {
703 RTSemEventMultiCreate(&mObj->mInitUninitSem);
704 Assert(mObj->mInitUninitWaiters == 0);
705 }
706 ++mObj->mInitUninitWaiters;
707
708 LogFlowFunc(("{%p}: Waiting for AutoUninitSpan to finish...\n",
709 mObj));
710
711 stateLock.leave();
712 RTSemEventMultiWait(mObj->mInitUninitSem, RT_INDEFINITE_WAIT);
713 stateLock.enter();
714
715 if (--mObj->mInitUninitWaiters == 0)
716 {
717 /* destroy the semaphore since no more necessary */
718 RTSemEventMultiDestroy(mObj->mInitUninitSem);
719 mObj->mInitUninitSem = NIL_RTSEMEVENTMULTI;
720 }
721
722 return;
723 }
724 }
725
726 /* go to InUninit to prevent from adding new callers */
727 mObj->setState(VirtualBoxBase::InUninit);
728
729 /* wait for already existing callers to drop to zero */
730 if (mObj->mCallers > 0)
731 {
732 /* lazy creation */
733 Assert(mObj->mZeroCallersSem == NIL_RTSEMEVENT);
734 RTSemEventCreate(&mObj->mZeroCallersSem);
735
736 /* wait until remaining callers release the object */
737 LogFlowFunc(("{%p}: Waiting for callers (%d) to drop to zero...\n",
738 mObj, mObj->mCallers));
739
740 stateLock.leave();
741 RTSemEventWait(mObj->mZeroCallersSem, RT_INDEFINITE_WAIT);
742 }
743}
744
745/**
746 * Places the managed VirtualBoxBase object to the NotReady state.
747 */
748AutoUninitSpan::~AutoUninitSpan()
749{
750 /* do nothing if already uninitialized */
751 if (mUninitDone)
752 return;
753
754 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
755
756 Assert(mObj->mState == VirtualBoxBase::InUninit);
757
758 mObj->setState(VirtualBoxBase::NotReady);
759}
760
761////////////////////////////////////////////////////////////////////////////////
762//
763// MultiResult methods
764//
765////////////////////////////////////////////////////////////////////////////////
766
767RTTLS MultiResult::sCounter = NIL_RTTLS;
768
769/*static*/
770void MultiResult::incCounter()
771{
772 if (sCounter == NIL_RTTLS)
773 {
774 sCounter = RTTlsAlloc();
775 AssertReturnVoid(sCounter != NIL_RTTLS);
776 }
777
778 uintptr_t counter = (uintptr_t)RTTlsGet(sCounter);
779 ++counter;
780 RTTlsSet(sCounter, (void*)counter);
781}
782
783/*static*/
784void MultiResult::decCounter()
785{
786 uintptr_t counter = (uintptr_t)RTTlsGet(sCounter);
787 AssertReturnVoid(counter != 0);
788 --counter;
789 RTTlsSet(sCounter, (void*)counter);
790}
791
792/*static*/
793bool MultiResult::isMultiEnabled()
794{
795 return ((uintptr_t)RTTlsGet(MultiResult::sCounter)) > 0;
796}
797
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