VirtualBox

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

Last change on this file since 25834 was 25834, checked in by vboxsync, 15 years ago

Main: finish integration of Main lock validation with IPRT; only enabled with VBOX_WITH_STRICT_LOCKS=1 (do NOT enable unless you want Main to stop working now)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.6 KB
Line 
1/* $Id: VirtualBoxBase.cpp 25834 2010-01-14 16:21:05Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM base classes implementation
6 */
7
8/*
9 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#include <iprt/semaphore.h>
25#include <iprt/asm.h>
26
27#if !defined (VBOX_WITH_XPCOM)
28#include <windows.h>
29#include <dbghelp.h>
30#else /* !defined (VBOX_WITH_XPCOM) */
31/// @todo remove when VirtualBoxErrorInfo goes away from here
32#include <nsIServiceManager.h>
33#include <nsIExceptionService.h>
34#endif /* !defined (VBOX_WITH_XPCOM) */
35
36#include "VirtualBoxBase.h"
37#include "VirtualBoxErrorInfoImpl.h"
38#include "Logging.h"
39
40#include "objectslist.h"
41
42////////////////////////////////////////////////////////////////////////////////
43//
44// VirtualBoxBaseProto
45//
46////////////////////////////////////////////////////////////////////////////////
47
48VirtualBoxBaseProto::VirtualBoxBaseProto()
49 : mStateLock(LOCKCLASS_OBJECTSTATE)
50{
51 mState = NotReady;
52 mStateChangeThread = NIL_RTTHREAD;
53 mCallers = 0;
54 mZeroCallersSem = NIL_RTSEMEVENT;
55 mInitUninitSem = NIL_RTSEMEVENTMULTI;
56 mInitUninitWaiters = 0;
57 mObjectLock = NULL;
58}
59
60VirtualBoxBaseProto::~VirtualBoxBaseProto()
61{
62 if (mObjectLock)
63 delete mObjectLock;
64 Assert (mInitUninitWaiters == 0);
65 Assert (mInitUninitSem == NIL_RTSEMEVENTMULTI);
66 if (mZeroCallersSem != NIL_RTSEMEVENT)
67 RTSemEventDestroy (mZeroCallersSem);
68 mCallers = 0;
69 mStateChangeThread = NIL_RTTHREAD;
70 mState = NotReady;
71}
72
73// util::Lockable interface
74
75RWLockHandle *VirtualBoxBaseProto::lockHandle() const
76{
77 /* lazy initialization */
78 if (RT_UNLIKELY(!mObjectLock))
79 {
80 AssertCompile (sizeof (RWLockHandle *) == sizeof (void *));
81
82 // locking class is LOCKCLASS_VIRTUALBOXOBJECT for the VirtualBox singleton,
83 // LOCKCLASS_HOSTOBJECT for the Host object and LOCKCLASS_OBJECT for any other object
84 RWLockHandle *objLock = new RWLockHandle(getLockingClass());
85 if (!ASMAtomicCmpXchgPtr ((void * volatile *) &mObjectLock, objLock, NULL))
86 {
87 delete objLock;
88 objLock = (RWLockHandle *) ASMAtomicReadPtr ((void * volatile *) &mObjectLock);
89 }
90 return objLock;
91 }
92 return mObjectLock;
93}
94
95/**
96 * Increments the number of calls to this object by one.
97 *
98 * After this method succeeds, it is guaranted that the object will remain
99 * in the Ready (or in the Limited) state at least until #releaseCaller() is
100 * called.
101 *
102 * This method is intended to mark the beginning of sections of code within
103 * methods of COM objects that depend on the readiness (Ready) state. The
104 * Ready state is a primary "ready to serve" state. Usually all code that
105 * works with component's data depends on it. On practice, this means that
106 * almost every public method, setter or getter of the object should add
107 * itself as an object's caller at the very beginning, to protect from an
108 * unexpected uninitialization that may happen on a different thread.
109 *
110 * Besides the Ready state denoting that the object is fully functional,
111 * there is a special Limited state. The Limited state means that the object
112 * is still functional, but its functionality is limited to some degree, so
113 * not all operations are possible. The @a aLimited argument to this method
114 * determines whether the caller represents this limited functionality or
115 * not.
116 *
117 * This method succeeeds (and increments the number of callers) only if the
118 * current object's state is Ready. Otherwise, it will return E_ACCESSDENIED
119 * to indicate that the object is not operational. There are two exceptions
120 * from this rule:
121 * <ol>
122 * <li>If the @a aLimited argument is |true|, then this method will also
123 * succeeed if the object's state is Limited (or Ready, of course).
124 * </li>
125 * <li>If this method is called from the same thread that placed
126 * the object to InInit or InUninit state (i.e. either from within the
127 * AutoInitSpan or AutoUninitSpan scope), it will succeed as well (but
128 * will not increase the number of callers).
129 * </li>
130 * </ol>
131 *
132 * Normally, calling addCaller() never blocks. However, if this method is
133 * called by a thread created from within the AutoInitSpan scope and this
134 * scope is still active (i.e. the object state is InInit), it will block
135 * until the AutoInitSpan destructor signals that it has finished
136 * initialization.
137 *
138 * Also, addCaller() will block if the object is probing uninitialization on
139 * another thread with AutoMayUninitSpan (i.e. the object state is MayUninit).
140 * And again, the block will last until the AutoMayUninitSpan destructor signals
141 * that it has finished probing and the object is either ready again or will
142 * uninitialize shortly (so that addCaller() will fail).
143 *
144 * When this method returns a failure, the caller must not use the object
145 * and should return the failed result code to its own caller.
146 *
147 * @param aState Where to store the current object's state (can be
148 * used in overriden methods to determine the cause of
149 * the failure).
150 * @param aLimited |true| to add a limited caller.
151 *
152 * @return S_OK on success or E_ACCESSDENIED on failure.
153 *
154 * @note It is preferrable to use the #addLimitedCaller() rather than
155 * calling this method with @a aLimited = |true|, for better
156 * self-descriptiveness.
157 *
158 * @sa #addLimitedCaller()
159 * @sa #releaseCaller()
160 */
161HRESULT VirtualBoxBaseProto::addCaller (State *aState /* = NULL */,
162 bool aLimited /* = false */)
163{
164 AutoWriteLock stateLock(mStateLock COMMA_LOCKVAL_SRC_POS);
165
166 HRESULT rc = E_ACCESSDENIED;
167
168 if (mState == Ready || (aLimited && mState == Limited))
169 {
170 /* if Ready or allows Limited, increase the number of callers */
171 ++ mCallers;
172 rc = S_OK;
173 }
174 else
175 if (mState == InInit || mState == MayUninit || mState == InUninit)
176 {
177 if (mStateChangeThread == RTThreadSelf())
178 {
179 /* Called from the same thread that is doing AutoInitSpan or
180 * AutoUninitSpan or AutoMayUninitSpan, just succeed */
181 rc = S_OK;
182 }
183 else if (mState == InInit || mState == MayUninit)
184 {
185 /* One of the two:
186 *
187 * 1) addCaller() is called by a "child" thread while the "parent"
188 * thread is still doing AutoInitSpan/AutoReinitSpan, so wait for
189 * the state to become either Ready/Limited or InitFailed (in
190 * case of init failure).
191 *
192 * 2) addCaller() is called while another thread is in
193 * AutoMayUninitSpan, so wait for the state to become either
194 * Ready or WillUninit.
195 *
196 * Note that in either case we increase the number of callers anyway
197 * -- to prevent AutoUninitSpan from early completion if we are
198 * still not scheduled to pick up the posted semaphore when uninit()
199 * is called.
200 */
201 ++ mCallers;
202
203 /* lazy semaphore creation */
204 if (mInitUninitSem == NIL_RTSEMEVENTMULTI)
205 {
206 RTSemEventMultiCreate (&mInitUninitSem);
207 Assert (mInitUninitWaiters == 0);
208 }
209
210 ++ mInitUninitWaiters;
211
212 LogFlowThisFunc((mState == InInit ?
213 "Waiting for AutoInitSpan/AutoReinitSpan to "
214 "finish...\n" :
215 "Waiting for AutoMayUninitSpan to finish...\n"));
216
217 stateLock.leave();
218 RTSemEventMultiWait (mInitUninitSem, RT_INDEFINITE_WAIT);
219 stateLock.enter();
220
221 if (-- mInitUninitWaiters == 0)
222 {
223 /* destroy the semaphore since no more necessary */
224 RTSemEventMultiDestroy (mInitUninitSem);
225 mInitUninitSem = NIL_RTSEMEVENTMULTI;
226 }
227
228 if (mState == Ready || (aLimited && mState == Limited))
229 rc = S_OK;
230 else
231 {
232 Assert (mCallers != 0);
233 -- mCallers;
234 if (mCallers == 0 && mState == InUninit)
235 {
236 /* inform AutoUninitSpan ctor there are no more callers */
237 RTSemEventSignal (mZeroCallersSem);
238 }
239 }
240 }
241 }
242
243 if (aState)
244 *aState = mState;
245
246 return rc;
247}
248
249/**
250 * Decreases the number of calls to this object by one.
251 *
252 * Must be called after every #addCaller() or #addLimitedCaller() when
253 * protecting the object from uninitialization is no more necessary.
254 */
255void VirtualBoxBaseProto::releaseCaller()
256{
257 AutoWriteLock stateLock(mStateLock COMMA_LOCKVAL_SRC_POS);
258
259 if (mState == Ready || mState == Limited)
260 {
261 /* if Ready or Limited, decrease the number of callers */
262 AssertMsgReturn (mCallers != 0, ("mCallers is ZERO!"), (void) 0);
263 -- mCallers;
264
265 return;
266 }
267
268 if (mState == InInit || mState == MayUninit || mState == InUninit)
269 {
270 if (mStateChangeThread == RTThreadSelf())
271 {
272 /* Called from the same thread that is doing AutoInitSpan,
273 * AutoMayUninitSpan or AutoUninitSpan: just succeed */
274 return;
275 }
276
277 if (mState == MayUninit || mState == InUninit)
278 {
279 /* the caller is being released after AutoUninitSpan or
280 * AutoMayUninitSpan has begun */
281 AssertMsgReturn (mCallers != 0, ("mCallers is ZERO!"), (void) 0);
282 -- mCallers;
283
284 if (mCallers == 0)
285 {
286 /* inform the Auto*UninitSpan ctor there are no more callers */
287 RTSemEventSignal (mZeroCallersSem);
288 }
289
290 return;
291 }
292 }
293
294 AssertMsgFailed (("mState = %d!", mState));
295}
296
297////////////////////////////////////////////////////////////////////////////////
298//
299// VirtualBoxBaseProto::AutoInitSpan methods
300//
301////////////////////////////////////////////////////////////////////////////////
302
303/**
304 * Creates a smart initialization span object that places the object to
305 * InInit state.
306 *
307 * Please see the AutoInitSpan class description for more info.
308 *
309 * @param aObj |this| pointer of the managed VirtualBoxBase object whose
310 * init() method is being called.
311 * @param aResult Default initialization result.
312 */
313VirtualBoxBaseProto::AutoInitSpan::
314AutoInitSpan (VirtualBoxBaseProto *aObj, Result aResult /* = Failed */)
315 : mObj (aObj), mResult (aResult), mOk (false)
316{
317 Assert (aObj);
318
319 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
320
321 mOk = mObj->mState == NotReady;
322 AssertReturnVoid (mOk);
323
324 mObj->setState (InInit);
325}
326
327/**
328 * Places the managed VirtualBoxBase object to Ready/Limited state if the
329 * initialization succeeded or partly succeeded, or places it to InitFailed
330 * state and calls the object's uninit() method.
331 *
332 * Please see the AutoInitSpan class description for more info.
333 */
334VirtualBoxBaseProto::AutoInitSpan::~AutoInitSpan()
335{
336 /* if the state was other than NotReady, do nothing */
337 if (!mOk)
338 return;
339
340 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
341
342 Assert (mObj->mState == InInit);
343
344 if (mObj->mCallers > 0)
345 {
346 Assert (mObj->mInitUninitWaiters > 0);
347
348 /* We have some pending addCaller() calls on other threads (created
349 * during InInit), signal that InInit is finished and they may go on. */
350 RTSemEventMultiSignal (mObj->mInitUninitSem);
351 }
352
353 if (mResult == Succeeded)
354 {
355 mObj->setState (Ready);
356 }
357 else
358 if (mResult == Limited)
359 {
360 mObj->setState (VirtualBoxBaseProto::Limited);
361 }
362 else
363 {
364 mObj->setState (InitFailed);
365 /* leave the lock to prevent nesting when uninit() is called */
366 stateLock.leave();
367 /* call uninit() to let the object uninit itself after failed init() */
368 mObj->uninit();
369 /* Note: the object may no longer exist here (for example, it can call
370 * the destructor in uninit()) */
371 }
372}
373
374// VirtualBoxBaseProto::AutoReinitSpan methods
375////////////////////////////////////////////////////////////////////////////////
376
377/**
378 * Creates a smart re-initialization span object and places the object to
379 * InInit state.
380 *
381 * Please see the AutoInitSpan class description for more info.
382 *
383 * @param aObj |this| pointer of the managed VirtualBoxBase object whose
384 * re-initialization method is being called.
385 */
386VirtualBoxBaseProto::AutoReinitSpan::
387AutoReinitSpan (VirtualBoxBaseProto *aObj)
388 : mObj (aObj), mSucceeded (false), mOk (false)
389{
390 Assert (aObj);
391
392 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
393
394 mOk = mObj->mState == Limited;
395 AssertReturnVoid (mOk);
396
397 mObj->setState (InInit);
398}
399
400/**
401 * Places the managed VirtualBoxBase object to Ready state if the
402 * re-initialization succeeded (i.e. #setSucceeded() has been called) or back to
403 * Limited state otherwise.
404 *
405 * Please see the AutoInitSpan class description for more info.
406 */
407VirtualBoxBaseProto::AutoReinitSpan::~AutoReinitSpan()
408{
409 /* if the state was other than Limited, do nothing */
410 if (!mOk)
411 return;
412
413 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
414
415 Assert (mObj->mState == InInit);
416
417 if (mObj->mCallers > 0 && mObj->mInitUninitWaiters > 0)
418 {
419 /* We have some pending addCaller() calls on other threads (created
420 * during InInit), signal that InInit is finished and they may go on. */
421 RTSemEventMultiSignal (mObj->mInitUninitSem);
422 }
423
424 if (mSucceeded)
425 {
426 mObj->setState (Ready);
427 }
428 else
429 {
430 mObj->setState (Limited);
431 }
432}
433
434// VirtualBoxBaseProto::AutoUninitSpan methods
435////////////////////////////////////////////////////////////////////////////////
436
437/**
438 * Creates a smart uninitialization span object and places this object to
439 * InUninit state.
440 *
441 * Please see the AutoInitSpan class description for more info.
442 *
443 * @note This method blocks the current thread execution until the number of
444 * callers of the managed VirtualBoxBase object drops to zero!
445 *
446 * @param aObj |this| pointer of the VirtualBoxBase object whose uninit()
447 * method is being called.
448 */
449VirtualBoxBaseProto::AutoUninitSpan::AutoUninitSpan (VirtualBoxBaseProto *aObj)
450 : mObj (aObj), mInitFailed (false), mUninitDone (false)
451{
452 Assert (aObj);
453
454 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
455
456 Assert (mObj->mState != InInit);
457
458 /* Set mUninitDone to |true| if this object is already uninitialized
459 * (NotReady) or if another AutoUninitSpan is currently active on some
460 * other thread (InUninit). */
461 mUninitDone = mObj->mState == NotReady ||
462 mObj->mState == InUninit;
463
464 if (mObj->mState == InitFailed)
465 {
466 /* we've been called by init() on failure */
467 mInitFailed = true;
468 }
469 else
470 {
471 if (mUninitDone)
472 {
473 /* do nothing if already uninitialized */
474 if (mObj->mState == NotReady)
475 return;
476
477 /* otherwise, wait until another thread finishes uninitialization.
478 * This is necessary to make sure that when this method returns, the
479 * object is NotReady and therefore can be deleted (for example).
480 * In particular, this is used by
481 * VirtualBoxBaseWithTypedChildrenNEXT::uninitDependentChildren(). */
482
483 /* lazy semaphore creation */
484 if (mObj->mInitUninitSem == NIL_RTSEMEVENTMULTI)
485 {
486 RTSemEventMultiCreate (&mObj->mInitUninitSem);
487 Assert (mObj->mInitUninitWaiters == 0);
488 }
489 ++ mObj->mInitUninitWaiters;
490
491 LogFlowFunc (("{%p}: Waiting for AutoUninitSpan to finish...\n",
492 mObj));
493
494 stateLock.leave();
495 RTSemEventMultiWait (mObj->mInitUninitSem, RT_INDEFINITE_WAIT);
496 stateLock.enter();
497
498 if (-- mObj->mInitUninitWaiters == 0)
499 {
500 /* destroy the semaphore since no more necessary */
501 RTSemEventMultiDestroy (mObj->mInitUninitSem);
502 mObj->mInitUninitSem = NIL_RTSEMEVENTMULTI;
503 }
504
505 return;
506 }
507 }
508
509 /* go to InUninit to prevent from adding new callers */
510 mObj->setState (InUninit);
511
512 /* wait for already existing callers to drop to zero */
513 if (mObj->mCallers > 0)
514 {
515 /* lazy creation */
516 Assert (mObj->mZeroCallersSem == NIL_RTSEMEVENT);
517 RTSemEventCreate (&mObj->mZeroCallersSem);
518
519 /* wait until remaining callers release the object */
520 LogFlowFunc (("{%p}: Waiting for callers (%d) to drop to zero...\n",
521 mObj, mObj->mCallers));
522
523 stateLock.leave();
524 RTSemEventWait (mObj->mZeroCallersSem, RT_INDEFINITE_WAIT);
525 }
526}
527
528/**
529 * Places the managed VirtualBoxBase object to the NotReady state.
530 */
531VirtualBoxBaseProto::AutoUninitSpan::~AutoUninitSpan()
532{
533 /* do nothing if already uninitialized */
534 if (mUninitDone)
535 return;
536
537 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
538
539 Assert (mObj->mState == InUninit);
540
541 mObj->setState (NotReady);
542}
543
544// VirtualBoxBaseProto::AutoMayUninitSpan methods
545////////////////////////////////////////////////////////////////////////////////
546
547/**
548 * Creates a smart initialization span object that places the object to
549 * MayUninit state.
550 *
551 * Please see the AutoMayUninitSpan class description for more info.
552 *
553 * @param aObj |this| pointer of the managed VirtualBoxBase object whose
554 * uninit() method to be probably called.
555 */
556VirtualBoxBaseProto::AutoMayUninitSpan::
557AutoMayUninitSpan (VirtualBoxBaseProto *aObj)
558 : mObj (aObj), mRC (E_FAIL), mAlreadyInProgress (false)
559 , mAcceptUninit (false)
560{
561 Assert (aObj);
562
563 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
564
565 AssertReturnVoid (mObj->mState != InInit &&
566 mObj->mState != InUninit);
567
568 switch (mObj->mState)
569 {
570 case Ready:
571 break;
572 case MayUninit:
573 /* Nothing to be done if already in MayUninit. */
574 mAlreadyInProgress = true;
575 mRC = S_OK;
576 return;
577 default:
578 /* Abuse mObj->addCaller() to get the extended error info possibly
579 * set by reimplementations of addCaller() and return it to the
580 * caller. Note that this abuse is supposed to be safe because we
581 * should've filtered out all states where addCaller() would do
582 * something else but set error info. */
583 mRC = mObj->addCaller();
584 Assert (FAILED (mRC));
585 return;
586 }
587
588 /* go to MayUninit to cause new callers to wait until we finish */
589 mObj->setState (MayUninit);
590 mRC = S_OK;
591
592 /* wait for already existing callers to drop to zero */
593 if (mObj->mCallers > 0)
594 {
595 /* lazy creation */
596 Assert (mObj->mZeroCallersSem == NIL_RTSEMEVENT);
597 RTSemEventCreate (&mObj->mZeroCallersSem);
598
599 /* wait until remaining callers release the object */
600 LogFlowFunc (("{%p}: Waiting for callers (%d) to drop to zero...\n",
601 mObj, mObj->mCallers));
602
603 stateLock.leave();
604 RTSemEventWait (mObj->mZeroCallersSem, RT_INDEFINITE_WAIT);
605 }
606}
607
608/**
609 * Places the managed VirtualBoxBase object back to Ready state if
610 * #acceptUninit() was not called, or places it to WillUninit state and calls
611 * the object's uninit() method.
612 *
613 * Please see the AutoMayUninitSpan class description for more info.
614 */
615VirtualBoxBaseProto::AutoMayUninitSpan::~AutoMayUninitSpan()
616{
617 /* if we did nothing in the constructor, do nothing here */
618 if (mAlreadyInProgress || FAILED (mRC))
619 return;
620
621 AutoWriteLock stateLock(mObj->mStateLock COMMA_LOCKVAL_SRC_POS);
622
623 Assert (mObj->mState == MayUninit);
624
625 if (mObj->mCallers > 0)
626 {
627 Assert (mObj->mInitUninitWaiters > 0);
628
629 /* We have some pending addCaller() calls on other threads made after
630 * going to during MayUnit, signal that MayUnit is finished and they may
631 * go on. */
632 RTSemEventMultiSignal (mObj->mInitUninitSem);
633 }
634
635 if (!mAcceptUninit)
636 {
637 mObj->setState (Ready);
638 }
639 else
640 {
641 mObj->setState (WillUninit);
642 /* leave the lock to prevent nesting when uninit() is called */
643 stateLock.leave();
644 /* call uninit() to let the object uninit itself */
645 mObj->uninit();
646 /* Note: the object may no longer exist here (for example, it can call
647 * the destructor in uninit()) */
648 }
649}
650
651////////////////////////////////////////////////////////////////////////////////
652//
653// VirtualBoxBase
654//
655////////////////////////////////////////////////////////////////////////////////
656
657/**
658 * Translates the given text string according to the currently installed
659 * translation table and current context. The current context is determined
660 * by the context parameter. Additionally, a comment to the source text
661 * string text can be given. This comment (which is NULL by default)
662 * is helpful in situations where it is necessary to distinguish between
663 * two or more semantically different roles of the same source text in the
664 * same context.
665 *
666 * @param context the context of the translation (can be NULL
667 * to indicate the global context)
668 * @param sourceText the string to translate
669 * @param comment the comment to the string (NULL means no comment)
670 *
671 * @return
672 * the translated version of the source string in UTF-8 encoding,
673 * or the source string itself if the translation is not found
674 * in the given context.
675 */
676// static
677const char *VirtualBoxBase::translate (const char * /* context */, const char *sourceText,
678 const char * /* comment */)
679{
680#if 0
681 Log(("VirtualBoxBase::translate:\n"
682 " context={%s}\n"
683 " sourceT={%s}\n"
684 " comment={%s}\n",
685 context, sourceText, comment));
686#endif
687
688 /// @todo (dmik) incorporate Qt translation file parsing and lookup
689 return sourceText;
690}
691
692////////////////////////////////////////////////////////////////////////////////
693//
694// VirtualBoxSupportTranslationBase
695//
696////////////////////////////////////////////////////////////////////////////////
697
698/**
699 * Modifies the given argument so that it will contain only a class name
700 * (null-terminated). The argument must point to a <b>non-constant</b>
701 * string containing a valid value, as it is generated by the
702 * __PRETTY_FUNCTION__ built-in macro of the GCC compiler, or by the
703 * __FUNCTION__ macro of any other compiler.
704 *
705 * The function assumes that the macro is used within the member of the
706 * class derived from the VirtualBoxSupportTranslation<> template.
707 *
708 * @param prettyFunctionName string to modify
709 * @return
710 * true on success and false otherwise
711 */
712bool VirtualBoxSupportTranslationBase::cutClassNameFrom__PRETTY_FUNCTION__ (char *fn)
713{
714 Assert (fn);
715 if (!fn)
716 return false;
717
718#if defined (__GNUC__)
719
720 // the format is like:
721 // VirtualBoxSupportTranslation<C>::VirtualBoxSupportTranslation() [with C = VirtualBox]
722
723 #define START " = "
724 #define END "]"
725
726#elif defined (_MSC_VER)
727
728 // the format is like:
729 // VirtualBoxSupportTranslation<class VirtualBox>::__ctor
730
731 #define START "<class "
732 #define END ">::"
733
734#endif
735
736 char *start = strstr (fn, START);
737 Assert (start);
738 if (start)
739 {
740 start += sizeof (START) - 1;
741 char *end = strstr (start, END);
742 Assert (end && (end > start));
743 if (end && (end > start))
744 {
745 size_t len = end - start;
746 memmove (fn, start, len);
747 fn [len] = 0;
748 return true;
749 }
750 }
751
752 #undef END
753 #undef START
754
755 return false;
756}
757
758////////////////////////////////////////////////////////////////////////////////
759//
760// VirtualBoxSupportErrorInfoImplBase
761//
762////////////////////////////////////////////////////////////////////////////////
763
764RTTLS VirtualBoxSupportErrorInfoImplBase::MultiResult::sCounter = NIL_RTTLS;
765
766void VirtualBoxSupportErrorInfoImplBase::MultiResult::init()
767{
768 if (sCounter == NIL_RTTLS)
769 {
770 sCounter = RTTlsAlloc();
771 AssertReturnVoid (sCounter != NIL_RTTLS);
772 }
773
774 uintptr_t counter = (uintptr_t) RTTlsGet (sCounter);
775 ++ counter;
776 RTTlsSet (sCounter, (void *) counter);
777}
778
779VirtualBoxSupportErrorInfoImplBase::MultiResult::~MultiResult()
780{
781 uintptr_t counter = (uintptr_t) RTTlsGet (sCounter);
782 AssertReturnVoid (counter != 0);
783 -- counter;
784 RTTlsSet (sCounter, (void *) counter);
785}
786
787/**
788 * Sets error info for the current thread. This is an internal function that
789 * gets eventually called by all public variants. If @a aWarning is
790 * @c true, then the highest (31) bit in the @a aResultCode value which
791 * indicates the error severity is reset to zero to make sure the receiver will
792 * recognize that the created error info object represents a warning rather
793 * than an error.
794 */
795/* static */
796HRESULT VirtualBoxSupportErrorInfoImplBase::setErrorInternal (
797 HRESULT aResultCode, const GUID &aIID,
798 const Bstr &aComponent, const Bstr &aText,
799 bool aWarning, bool aLogIt)
800{
801 /* whether multi-error mode is turned on */
802 bool preserve = ((uintptr_t) RTTlsGet (MultiResult::sCounter)) > 0;
803
804 if (aLogIt)
805 LogRel (("ERROR [COM]: aRC=%Rhrc (%#08x) aIID={%RTuuid} aComponent={%ls} aText={%ls} "
806 "aWarning=%RTbool, preserve=%RTbool\n",
807 aResultCode, aResultCode, &aIID, aComponent.raw(), aText.raw(), aWarning,
808 preserve));
809
810 /* these are mandatory, others -- not */
811 AssertReturn((!aWarning && FAILED (aResultCode)) ||
812 (aWarning && aResultCode != S_OK),
813 E_FAIL);
814 AssertReturn(!aText.isEmpty(), E_FAIL);
815
816 /* reset the error severity bit if it's a warning */
817 if (aWarning)
818 aResultCode &= ~0x80000000;
819
820 HRESULT rc = S_OK;
821
822 do
823 {
824 ComObjPtr<VirtualBoxErrorInfo> info;
825 rc = info.createObject();
826 if (FAILED(rc)) break;
827
828#if !defined (VBOX_WITH_XPCOM)
829
830 ComPtr<IVirtualBoxErrorInfo> curInfo;
831 if (preserve)
832 {
833 /* get the current error info if any */
834 ComPtr<IErrorInfo> err;
835 rc = ::GetErrorInfo (0, err.asOutParam());
836 if (FAILED(rc)) break;
837 rc = err.queryInterfaceTo(curInfo.asOutParam());
838 if (FAILED (rc))
839 {
840 /* create a IVirtualBoxErrorInfo wrapper for the native
841 * IErrorInfo object */
842 ComObjPtr<VirtualBoxErrorInfo> wrapper;
843 rc = wrapper.createObject();
844 if (SUCCEEDED(rc))
845 {
846 rc = wrapper->init (err);
847 if (SUCCEEDED(rc))
848 curInfo = wrapper;
849 }
850 }
851 }
852 /* On failure, curInfo will stay null */
853 Assert (SUCCEEDED(rc) || curInfo.isNull());
854
855 /* set the current error info and preserve the previous one if any */
856 rc = info->init (aResultCode, aIID, aComponent, aText, curInfo);
857 if (FAILED(rc)) break;
858
859 ComPtr<IErrorInfo> err;
860 rc = info.queryInterfaceTo(err.asOutParam());
861 if (SUCCEEDED(rc))
862 rc = ::SetErrorInfo (0, err);
863
864#else // !defined (VBOX_WITH_XPCOM)
865
866 nsCOMPtr <nsIExceptionService> es;
867 es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
868 if (NS_SUCCEEDED(rc))
869 {
870 nsCOMPtr <nsIExceptionManager> em;
871 rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
872 if (FAILED(rc)) break;
873
874 ComPtr<IVirtualBoxErrorInfo> curInfo;
875 if (preserve)
876 {
877 /* get the current error info if any */
878 ComPtr<nsIException> ex;
879 rc = em->GetCurrentException (ex.asOutParam());
880 if (FAILED(rc)) break;
881 rc = ex.queryInterfaceTo(curInfo.asOutParam());
882 if (FAILED(rc))
883 {
884 /* create a IVirtualBoxErrorInfo wrapper for the native
885 * nsIException object */
886 ComObjPtr<VirtualBoxErrorInfo> wrapper;
887 rc = wrapper.createObject();
888 if (SUCCEEDED(rc))
889 {
890 rc = wrapper->init (ex);
891 if (SUCCEEDED(rc))
892 curInfo = wrapper;
893 }
894 }
895 }
896 /* On failure, curInfo will stay null */
897 Assert (SUCCEEDED(rc) || curInfo.isNull());
898
899 /* set the current error info and preserve the previous one if any */
900 rc = info->init (aResultCode, aIID, aComponent, aText, curInfo);
901 if (FAILED(rc)) break;
902
903 ComPtr<nsIException> ex;
904 rc = info.queryInterfaceTo(ex.asOutParam());
905 if (SUCCEEDED(rc))
906 rc = em->SetCurrentException (ex);
907 }
908 else if (rc == NS_ERROR_UNEXPECTED)
909 {
910 /*
911 * It is possible that setError() is being called by the object
912 * after the XPCOM shutdown sequence has been initiated
913 * (for example, when XPCOM releases all instances it internally
914 * references, which can cause object's FinalConstruct() and then
915 * uninit()). In this case, do_GetService() above will return
916 * NS_ERROR_UNEXPECTED and it doesn't actually make sense to
917 * set the exception (nobody will be able to read it).
918 */
919 LogWarningFunc (("Will not set an exception because "
920 "nsIExceptionService is not available "
921 "(NS_ERROR_UNEXPECTED). "
922 "XPCOM is being shutdown?\n"));
923 rc = NS_OK;
924 }
925
926#endif // !defined (VBOX_WITH_XPCOM)
927 }
928 while (0);
929
930 AssertComRC (rc);
931
932 return SUCCEEDED(rc) ? aResultCode : rc;
933}
934
935
936/**
937 * Uninitializes all dependent children registered on this object with
938 * #addDependentChild().
939 *
940 * Must be called from within the VirtualBoxBaseProto::AutoUninitSpan (i.e.
941 * typically from this object's uninit() method) to uninitialize children
942 * before this object goes out of service and becomes unusable.
943 *
944 * Note that this method will call uninit() methods of child objects. If
945 * these methods need to call the parent object during uninitialization,
946 * #uninitDependentChildren() must be called before the relevant part of the
947 * parent is uninitialized: usually at the begnning of the parent
948 * uninitialization sequence.
949 *
950 * Keep in mind that the uninitialized child objects may be no longer available
951 * (i.e. may be deleted) after this method returns.
952 *
953 * @note Locks #childrenLock() for writing.
954 *
955 * @note May lock something else through the called children.
956 */
957void VirtualBoxBaseWithChildrenNEXT::uninitDependentChildren()
958{
959 AutoCaller autoCaller(this);
960
961 /* sanity */
962 AssertReturnVoid (autoCaller.state() == InUninit ||
963 autoCaller.state() == InInit);
964
965 AutoWriteLock chLock(childrenLock() COMMA_LOCKVAL_SRC_POS);
966
967 size_t count = mDependentChildren.size();
968
969 while (count != 0)
970 {
971 /* strongly reference the weak child from the map to make sure it won't
972 * be deleted while we've released the lock */
973 DependentChildren::iterator it = mDependentChildren.begin();
974 ComPtr<IUnknown> unk = it->first;
975 Assert (!unk.isNull());
976
977 VirtualBoxBase *child = it->second;
978
979 /* release the lock to let children stuck in removeDependentChild() go
980 * on (otherwise we'll deadlock in uninit() */
981 chLock.leave();
982
983 /* Note that if child->uninit() happens to be called on another
984 * thread right before us and is not yet finished, the second
985 * uninit() call will wait until the first one has done so
986 * (thanks to AutoUninitSpan). */
987 Assert (child);
988 if (child)
989 child->uninit();
990
991 chLock.enter();
992
993 /* uninit() is guaranteed to be done here so the child must be already
994 * deleted from the list by removeDependentChild() called from there.
995 * Do some checks to avoid endless loops when the user is forgetful */
996 -- count;
997 Assert (count == mDependentChildren.size());
998 if (count != mDependentChildren.size())
999 mDependentChildren.erase (it);
1000
1001 Assert (count == mDependentChildren.size());
1002 }
1003}
1004
1005/**
1006 * Returns a pointer to the dependent child (registered using
1007 * #addDependentChild()) corresponding to the given interface pointer or NULL if
1008 * the given pointer is unrelated.
1009 *
1010 * The relation is checked by using the given interface pointer as a key in the
1011 * map of dependent children.
1012 *
1013 * Note that ComPtr<IUnknown> is used as an argument instead of IUnknown * in
1014 * order to guarantee IUnknown identity and disambiguation by doing
1015 * QueryInterface (IUnknown) rather than a regular C cast.
1016 *
1017 * @param aUnk Pointer to map to the dependent child object.
1018 * @return Pointer to the dependent VirtualBoxBase child object.
1019 *
1020 * @note Locks #childrenLock() for reading.
1021 */
1022VirtualBoxBase* VirtualBoxBaseWithChildrenNEXT::getDependentChild(const ComPtr<IUnknown> &aUnk)
1023{
1024 AssertReturn(!aUnk.isNull(), NULL);
1025
1026 AutoCaller autoCaller(this);
1027
1028 /* return NULL if uninitDependentChildren() is in action */
1029 if (autoCaller.state() == InUninit)
1030 return NULL;
1031
1032 AutoReadLock alock(childrenLock() COMMA_LOCKVAL_SRC_POS);
1033
1034 DependentChildren::const_iterator it = mDependentChildren.find (aUnk);
1035 if (it == mDependentChildren.end())
1036 return NULL;
1037
1038 return (*it).second;
1039}
1040
1041/** Helper for addDependentChild(). */
1042void VirtualBoxBaseWithChildrenNEXT::doAddDependentChild(IUnknown *aUnk,
1043 VirtualBoxBase *aChild)
1044{
1045 AssertReturnVoid (aUnk != NULL);
1046 AssertReturnVoid (aChild != NULL);
1047
1048 AutoCaller autoCaller(this);
1049
1050 /* sanity */
1051 AssertReturnVoid (autoCaller.state() == InInit ||
1052 autoCaller.state() == Ready ||
1053 autoCaller.state() == Limited);
1054
1055 AutoWriteLock alock(childrenLock() COMMA_LOCKVAL_SRC_POS);
1056
1057 std::pair <DependentChildren::iterator, bool> result =
1058 mDependentChildren.insert (DependentChildren::value_type (aUnk, aChild));
1059 AssertMsg (result.second, ("Failed to insert child %p to the map\n", aUnk));
1060}
1061
1062/** Helper for removeDependentChild(). */
1063void VirtualBoxBaseWithChildrenNEXT::doRemoveDependentChild (IUnknown *aUnk)
1064{
1065 AssertReturnVoid (aUnk);
1066
1067 AutoCaller autoCaller(this);
1068
1069 /* sanity */
1070 AssertReturnVoid (autoCaller.state() == InUninit ||
1071 autoCaller.state() == InInit ||
1072 autoCaller.state() == Ready ||
1073 autoCaller.state() == Limited);
1074
1075 AutoWriteLock alock(childrenLock() COMMA_LOCKVAL_SRC_POS);
1076
1077 DependentChildren::size_type result = mDependentChildren.erase (aUnk);
1078 AssertMsg (result == 1, ("Failed to remove child %p from the map\n", aUnk));
1079 NOREF (result);
1080}
1081
1082/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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