VirtualBox

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

Last change on this file since 25414 was 25310, checked in by vboxsync, 14 years ago

Main: lock validator, first batch: implement per-thread stack to trace locking (disabled by default, use VBOX_WITH_LOCK_VALIDATOR, but that WILL FAIL presently)

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

© 2023 Oracle
ContactPrivacy policyTerms of Use