VirtualBox

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

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

Main: bring back r55600 with fix for broken machine creation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.1 KB
RevLine 
[6076]1/* $Id: VirtualBoxBase.cpp 25184 2009-12-04 11:37:03Z vboxsync $ */
2
[1]3/** @file
4 *
5 * VirtualBox COM base classes implementation
6 */
7
8/*
[17689]9 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
[1]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
[5999]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.
[8155]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.
[1]22 */
23
[16538]24#include <iprt/semaphore.h>
25#include <iprt/asm.h>
26
[3191]27#if !defined (VBOX_WITH_XPCOM)
[1]28#include <windows.h>
29#include <dbghelp.h>
[8083]30#else /* !defined (VBOX_WITH_XPCOM) */
[13580]31/// @todo remove when VirtualBoxErrorInfo goes away from here
[1]32#include <nsIServiceManager.h>
33#include <nsIExceptionService.h>
[8083]34#endif /* !defined (VBOX_WITH_XPCOM) */
[1]35
36#include "VirtualBoxBase.h"
37#include "VirtualBoxErrorInfoImpl.h"
38#include "Logging.h"
39
[25184]40#include "objectslist.h"
41
[1]42////////////////////////////////////////////////////////////////////////////////
[25184]43//
44// VirtualBoxBaseProto
45//
46////////////////////////////////////////////////////////////////////////////////
[1]47
[13580]48VirtualBoxBaseProto::VirtualBoxBaseProto()
[1]49{
50 mState = NotReady;
51 mStateChangeThread = NIL_RTTHREAD;
52 mCallers = 0;
53 mZeroCallersSem = NIL_RTSEMEVENT;
[13580]54 mInitUninitSem = NIL_RTSEMEVENTMULTI;
55 mInitUninitWaiters = 0;
[1]56 mObjectLock = NULL;
57}
58
[13580]59VirtualBoxBaseProto::~VirtualBoxBaseProto()
[1]60{
61 if (mObjectLock)
62 delete mObjectLock;
[13580]63 Assert (mInitUninitWaiters == 0);
64 Assert (mInitUninitSem == NIL_RTSEMEVENTMULTI);
[1]65 if (mZeroCallersSem != NIL_RTSEMEVENT)
66 RTSemEventDestroy (mZeroCallersSem);
67 mCallers = 0;
68 mStateChangeThread = NIL_RTTHREAD;
69 mState = NotReady;
70}
71
[8083]72// util::Lockable interface
[13580]73
74RWLockHandle *VirtualBoxBaseProto::lockHandle() const
[1]75{
[8665]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 }
[1]88 return mObjectLock;
89}
90
91/**
[13580]92 * Increments the number of calls to this object by one.
[1]93 *
[13580]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.
[1]97 *
[13580]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.
[1]105 *
[13580]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.
[1]112 *
[13580]113 * This method succeeeds (and increments the number of callers) only if the
[14579]114 * current object's state is Ready. Otherwise, it will return E_ACCESSDENIED
[13580]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>
[1]127 *
[13580]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.
[1]133 *
[13580]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).
[1]139 *
[13580]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.
[1]142 *
[13580]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.
[1]147 *
[14579]148 * @return S_OK on success or E_ACCESSDENIED on failure.
[13580]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()
[1]156 */
[13580]157HRESULT VirtualBoxBaseProto::addCaller (State *aState /* = NULL */,
158 bool aLimited /* = false */)
[1]159{
[8083]160 AutoWriteLock stateLock (mStateLock);
[1]161
[14579]162 HRESULT rc = E_ACCESSDENIED;
[1]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
[13580]171 if (mState == InInit || mState == MayUninit || mState == InUninit)
[1]172 {
173 if (mStateChangeThread == RTThreadSelf())
174 {
[13580]175 /* Called from the same thread that is doing AutoInitSpan or
176 * AutoUninitSpan or AutoMayUninitSpan, just succeed */
[1]177 rc = S_OK;
178 }
[13580]179 else if (mState == InInit || mState == MayUninit)
[1]180 {
[13580]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.
[1]196 */
197 ++ mCallers;
198
[13580]199 /* lazy semaphore creation */
200 if (mInitUninitSem == NIL_RTSEMEVENTMULTI)
201 {
202 RTSemEventMultiCreate (&mInitUninitSem);
203 Assert (mInitUninitWaiters == 0);
204 }
[1]205
[13580]206 ++ mInitUninitWaiters;
[1]207
[21878]208 LogFlowThisFunc((mState == InInit ?
[13580]209 "Waiting for AutoInitSpan/AutoReinitSpan to "
210 "finish...\n" :
211 "Waiting for AutoMayUninitSpan to finish...\n"));
212
[1]213 stateLock.leave();
[13580]214 RTSemEventMultiWait (mInitUninitSem, RT_INDEFINITE_WAIT);
[1]215 stateLock.enter();
216
[13580]217 if (-- mInitUninitWaiters == 0)
[1]218 {
219 /* destroy the semaphore since no more necessary */
[13580]220 RTSemEventMultiDestroy (mInitUninitSem);
221 mInitUninitSem = NIL_RTSEMEVENTMULTI;
[1]222 }
223
[13874]224 if (mState == Ready || (aLimited && mState == Limited))
[1]225 rc = S_OK;
226 else
227 {
[13580]228 Assert (mCallers != 0);
[1]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/**
[13580]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.
[1]250 */
[13580]251void VirtualBoxBaseProto::releaseCaller()
[1]252{
[8083]253 AutoWriteLock stateLock (mStateLock);
[1]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
[13580]264 if (mState == InInit || mState == MayUninit || mState == InUninit)
[1]265 {
266 if (mStateChangeThread == RTThreadSelf())
267 {
[16586]268 /* Called from the same thread that is doing AutoInitSpan,
269 * AutoMayUninitSpan or AutoUninitSpan: just succeed */
[1]270 return;
271 }
272
[16586]273 if (mState == MayUninit || mState == InUninit)
[1]274 {
[16586]275 /* the caller is being released after AutoUninitSpan or
276 * AutoMayUninitSpan has begun */
[1]277 AssertMsgReturn (mCallers != 0, ("mCallers is ZERO!"), (void) 0);
278 -- mCallers;
279
280 if (mCallers == 0)
281 {
[16586]282 /* inform the Auto*UninitSpan ctor there are no more callers */
[1]283 RTSemEventSignal (mZeroCallersSem);
284 }
285
286 return;
287 }
288 }
289
290 AssertMsgFailed (("mState = %d!", mState));
291}
292
[25184]293////////////////////////////////////////////////////////////////////////////////
294//
[13580]295// VirtualBoxBaseProto::AutoInitSpan methods
[25184]296//
[1]297////////////////////////////////////////////////////////////////////////////////
298
299/**
[13580]300 * Creates a smart initialization span object that places the object to
301 * InInit state.
[1]302 *
[13580]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.
[1]308 */
[13580]309VirtualBoxBaseProto::AutoInitSpan::
310AutoInitSpan (VirtualBoxBaseProto *aObj, Result aResult /* = Failed */)
311 : mObj (aObj), mResult (aResult), mOk (false)
[1]312{
313 Assert (aObj);
314
[8083]315 AutoWriteLock stateLock (mObj->mStateLock);
[1]316
317 mOk = mObj->mState == NotReady;
[13580]318 AssertReturnVoid (mOk);
[1]319
320 mObj->setState (InInit);
321}
322
323/**
[13580]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.
[1]329 */
[13580]330VirtualBoxBaseProto::AutoInitSpan::~AutoInitSpan()
[1]331{
332 /* if the state was other than NotReady, do nothing */
333 if (!mOk)
334 return;
335
[8083]336 AutoWriteLock stateLock (mObj->mStateLock);
[1]337
338 Assert (mObj->mState == InInit);
339
340 if (mObj->mCallers > 0)
341 {
[13580]342 Assert (mObj->mInitUninitWaiters > 0);
[5251]343
[1]344 /* We have some pending addCaller() calls on other threads (created
[13580]345 * during InInit), signal that InInit is finished and they may go on. */
346 RTSemEventMultiSignal (mObj->mInitUninitSem);
[1]347 }
348
[13580]349 if (mResult == Succeeded)
[1]350 {
351 mObj->setState (Ready);
352 }
353 else
[13580]354 if (mResult == Limited)
[1]355 {
[13580]356 mObj->setState (VirtualBoxBaseProto::Limited);
[1]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
[13580]370// VirtualBoxBaseProto::AutoReinitSpan methods
[1]371////////////////////////////////////////////////////////////////////////////////
372
373/**
[13580]374 * Creates a smart re-initialization span object and places the object to
375 * InInit state.
[1]376 *
[13580]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.
[1]381 */
[13580]382VirtualBoxBaseProto::AutoReinitSpan::
383AutoReinitSpan (VirtualBoxBaseProto *aObj)
[1]384 : mObj (aObj), mSucceeded (false), mOk (false)
385{
386 Assert (aObj);
387
[8083]388 AutoWriteLock stateLock (mObj->mStateLock);
[1]389
390 mOk = mObj->mState == Limited;
[13580]391 AssertReturnVoid (mOk);
[1]392
393 mObj->setState (InInit);
394}
395
396/**
[13580]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.
[1]402 */
[13580]403VirtualBoxBaseProto::AutoReinitSpan::~AutoReinitSpan()
[1]404{
405 /* if the state was other than Limited, do nothing */
406 if (!mOk)
407 return;
408
[8083]409 AutoWriteLock stateLock (mObj->mStateLock);
[1]410
411 Assert (mObj->mState == InInit);
412
[13580]413 if (mObj->mCallers > 0 && mObj->mInitUninitWaiters > 0)
[1]414 {
[13580]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);
[1]418 }
419
420 if (mSucceeded)
421 {
422 mObj->setState (Ready);
423 }
424 else
425 {
426 mObj->setState (Limited);
427 }
428}
429
[13580]430// VirtualBoxBaseProto::AutoUninitSpan methods
[1]431////////////////////////////////////////////////////////////////////////////////
432
433/**
[13580]434 * Creates a smart uninitialization span object and places this object to
435 * InUninit state.
[1]436 *
[13580]437 * Please see the AutoInitSpan class description for more info.
[1]438 *
[13580]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.
[1]444 */
[13580]445VirtualBoxBaseProto::AutoUninitSpan::AutoUninitSpan (VirtualBoxBaseProto *aObj)
[1]446 : mObj (aObj), mInitFailed (false), mUninitDone (false)
447{
448 Assert (aObj);
449
[8083]450 AutoWriteLock stateLock (mObj->mStateLock);
[1]451
452 Assert (mObj->mState != InInit);
453
[13580]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). */
[1]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)
[13580]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
[1]501 return;
[13580]502 }
[1]503 }
504
505 /* go to InUninit to prevent from adding new callers */
506 mObj->setState (InUninit);
507
[13580]508 /* wait for already existing callers to drop to zero */
[1]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 */
[13580]516 LogFlowFunc (("{%p}: Waiting for callers (%d) to drop to zero...\n",
517 mObj, mObj->mCallers));
[1]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 */
[13580]527VirtualBoxBaseProto::AutoUninitSpan::~AutoUninitSpan()
[1]528{
529 /* do nothing if already uninitialized */
530 if (mUninitDone)
531 return;
532
[8083]533 AutoWriteLock stateLock (mObj->mStateLock);
[1]534
535 Assert (mObj->mState == InUninit);
536
537 mObj->setState (NotReady);
538}
539
[13580]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)
[14579]554 : mObj (aObj), mRC (E_FAIL), mAlreadyInProgress (false)
[13580]555 , mAcceptUninit (false)
556{
557 Assert (aObj);
558
559 AutoWriteLock stateLock (mObj->mStateLock);
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);
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
[1]647////////////////////////////////////////////////////////////////////////////////
[25184]648//
649// VirtualBoxBase
650//
651////////////////////////////////////////////////////////////////////////////////
[1]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)
[17911]658 * is helpful in situations where it is necessary to distinguish between
[1]659 * two or more semantically different roles of the same source text in the
660 * same context.
661 *
[14294]662 * @param context the context of the translation (can be NULL
[1]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
[17911]673const char *VirtualBoxBase::translate (const char * /* context */, const char *sourceText,
674 const char * /* comment */)
[1]675{
[8083]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
[1]683
684 /// @todo (dmik) incorporate Qt translation file parsing and lookup
685 return sourceText;
686}
687
688////////////////////////////////////////////////////////////////////////////////
[25184]689//
690// VirtualBoxSupportTranslationBase
691//
692////////////////////////////////////////////////////////////////////////////////
[1]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////////////////////////////////////////////////////////////////////////////////
[25184]755//
756// VirtualBoxSupportErrorInfoImplBase
757//
758////////////////////////////////////////////////////////////////////////////////
[1]759
[6964]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
[2672]783/**
784 * Sets error info for the current thread. This is an internal function that
[6964]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.
[2672]790 */
[6964]791/* static */
[2672]792HRESULT VirtualBoxSupportErrorInfoImplBase::setErrorInternal (
793 HRESULT aResultCode, const GUID &aIID,
794 const Bstr &aComponent, const Bstr &aText,
[8709]795 bool aWarning, bool aLogIt)
[1]796{
[6964]797 /* whether multi-error mode is turned on */
798 bool preserve = ((uintptr_t) RTTlsGet (MultiResult::sCounter)) > 0;
799
[8709]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));
[1]805
806 /* these are mandatory, others -- not */
[21878]807 AssertReturn((!aWarning && FAILED (aResultCode)) ||
[6964]808 (aWarning && aResultCode != S_OK),
809 E_FAIL);
[21878]810 AssertReturn(!aText.isEmpty(), E_FAIL);
[1]811
[6964]812 /* reset the error severity bit if it's a warning */
813 if (aWarning)
814 aResultCode &= ~0x80000000;
815
[2672]816 HRESULT rc = S_OK;
817
818 do
[1]819 {
[21878]820 ComObjPtr<VirtualBoxErrorInfo> info;
[2672]821 rc = info.createObject();
[25149]822 if (FAILED(rc)) break;
[1]823
[3191]824#if !defined (VBOX_WITH_XPCOM)
[1]825
[21878]826 ComPtr<IVirtualBoxErrorInfo> curInfo;
[6964]827 if (preserve)
[2672]828 {
829 /* get the current error info if any */
[21878]830 ComPtr<IErrorInfo> err;
[2672]831 rc = ::GetErrorInfo (0, err.asOutParam());
[25149]832 if (FAILED(rc)) break;
[21878]833 rc = err.queryInterfaceTo(curInfo.asOutParam());
[2672]834 if (FAILED (rc))
835 {
836 /* create a IVirtualBoxErrorInfo wrapper for the native
837 * IErrorInfo object */
[21878]838 ComObjPtr<VirtualBoxErrorInfo> wrapper;
[2672]839 rc = wrapper.createObject();
[21878]840 if (SUCCEEDED(rc))
[2672]841 {
842 rc = wrapper->init (err);
[21878]843 if (SUCCEEDED(rc))
[2672]844 curInfo = wrapper;
845 }
846 }
847 }
848 /* On failure, curInfo will stay null */
[21878]849 Assert (SUCCEEDED(rc) || curInfo.isNull());
[2672]850
851 /* set the current error info and preserve the previous one if any */
852 rc = info->init (aResultCode, aIID, aComponent, aText, curInfo);
[25149]853 if (FAILED(rc)) break;
[2672]854
[21878]855 ComPtr<IErrorInfo> err;
856 rc = info.queryInterfaceTo(err.asOutParam());
857 if (SUCCEEDED(rc))
[1]858 rc = ::SetErrorInfo (0, err);
859
[3191]860#else // !defined (VBOX_WITH_XPCOM)
[1]861
862 nsCOMPtr <nsIExceptionService> es;
863 es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
[21878]864 if (NS_SUCCEEDED(rc))
[1]865 {
866 nsCOMPtr <nsIExceptionManager> em;
867 rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
[25149]868 if (FAILED(rc)) break;
[2672]869
[21878]870 ComPtr<IVirtualBoxErrorInfo> curInfo;
[6964]871 if (preserve)
[1]872 {
[2672]873 /* get the current error info if any */
[21878]874 ComPtr<nsIException> ex;
[2672]875 rc = em->GetCurrentException (ex.asOutParam());
[25149]876 if (FAILED(rc)) break;
[21878]877 rc = ex.queryInterfaceTo(curInfo.asOutParam());
[25149]878 if (FAILED(rc))
[2672]879 {
880 /* create a IVirtualBoxErrorInfo wrapper for the native
881 * nsIException object */
[21878]882 ComObjPtr<VirtualBoxErrorInfo> wrapper;
[2672]883 rc = wrapper.createObject();
[21878]884 if (SUCCEEDED(rc))
[2672]885 {
886 rc = wrapper->init (ex);
[21878]887 if (SUCCEEDED(rc))
[2672]888 curInfo = wrapper;
889 }
890 }
[1]891 }
[2672]892 /* On failure, curInfo will stay null */
[21878]893 Assert (SUCCEEDED(rc) || curInfo.isNull());
[2672]894
895 /* set the current error info and preserve the previous one if any */
896 rc = info->init (aResultCode, aIID, aComponent, aText, curInfo);
[25149]897 if (FAILED(rc)) break;
[2672]898
[21878]899 ComPtr<nsIException> ex;
900 rc = info.queryInterfaceTo(ex.asOutParam());
901 if (SUCCEEDED(rc))
[2672]902 rc = em->SetCurrentException (ex);
[1]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
[3191]922#endif // !defined (VBOX_WITH_XPCOM)
[1]923 }
[2672]924 while (0);
[1]925
926 AssertComRC (rc);
927
[21878]928 return SUCCEEDED(rc) ? aResultCode : rc;
[1]929}
930
[6076]931
[6851]932/**
[13729]933 * Uninitializes all dependent children registered on this object with
934 * #addDependentChild().
[6935]935 *
[13729]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 *
[13887]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.
[6851]952 */
953void VirtualBoxBaseWithChildrenNEXT::uninitDependentChildren()
954{
[21878]955 AutoCaller autoCaller(this);
[6851]956
[13887]957 /* sanity */
[13857]958 AssertReturnVoid (autoCaller.state() == InUninit ||
959 autoCaller.state() == InInit);
[6851]960
[13887]961 AutoWriteLock chLock (childrenLock());
962
963 size_t count = mDependentChildren.size();
964
965 while (count != 0)
[6851]966 {
[13887]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();
[21878]970 ComPtr<IUnknown> unk = it->first;
[13887]971 Assert (!unk.isNull());
[13729]972
[13887]973 VirtualBoxBase *child = it->second;
[6851]974
[13887]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());
[6851]998 }
999}
1000
1001/**
[13729]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.
[6851]1005 *
[13729]1006 * The relation is checked by using the given interface pointer as a key in the
1007 * map of dependent children.
1008 *
[21878]1009 * Note that ComPtr<IUnknown> is used as an argument instead of IUnknown * in
[6935]1010 * order to guarantee IUnknown identity and disambiguation by doing
1011 * QueryInterface (IUnknown) rather than a regular C cast.
1012 *
[6851]1013 * @param aUnk Pointer to map to the dependent child object.
[13729]1014 * @return Pointer to the dependent VirtualBoxBase child object.
1015 *
1016 * @note Locks #childrenLock() for reading.
[6851]1017 */
[21823]1018VirtualBoxBase* VirtualBoxBaseWithChildrenNEXT::getDependentChild(const ComPtr<IUnknown> &aUnk)
[6851]1019{
[21878]1020 AssertReturn(!aUnk.isNull(), NULL);
[6851]1021
[21878]1022 AutoCaller autoCaller(this);
[6851]1023
1024 /* return NULL if uninitDependentChildren() is in action */
[13729]1025 if (autoCaller.state() == InUninit)
[6851]1026 return NULL;
1027
[21878]1028 AutoReadLock alock(childrenLock());
[13729]1029
[6851]1030 DependentChildren::const_iterator it = mDependentChildren.find (aUnk);
1031 if (it == mDependentChildren.end())
1032 return NULL;
[13729]1033
[6851]1034 return (*it).second;
1035}
1036
[13729]1037/** Helper for addDependentChild(). */
[21823]1038void VirtualBoxBaseWithChildrenNEXT::doAddDependentChild(IUnknown *aUnk,
1039 VirtualBoxBase *aChild)
[6851]1040{
[13729]1041 AssertReturnVoid (aUnk != NULL);
1042 AssertReturnVoid (aChild != NULL);
[6851]1043
[21878]1044 AutoCaller autoCaller(this);
[6851]1045
[13729]1046 /* sanity */
1047 AssertReturnVoid (autoCaller.state() == InInit ||
1048 autoCaller.state() == Ready ||
1049 autoCaller.state() == Limited);
[6851]1050
[21878]1051 AutoWriteLock alock(childrenLock());
[13729]1052
[6851]1053 std::pair <DependentChildren::iterator, bool> result =
1054 mDependentChildren.insert (DependentChildren::value_type (aUnk, aChild));
[13729]1055 AssertMsg (result.second, ("Failed to insert child %p to the map\n", aUnk));
[6851]1056}
1057
[13729]1058/** Helper for removeDependentChild(). */
[6851]1059void VirtualBoxBaseWithChildrenNEXT::doRemoveDependentChild (IUnknown *aUnk)
1060{
1061 AssertReturnVoid (aUnk);
1062
[21878]1063 AutoCaller autoCaller(this);
[6851]1064
[13887]1065 /* sanity */
1066 AssertReturnVoid (autoCaller.state() == InUninit ||
1067 autoCaller.state() == InInit ||
1068 autoCaller.state() == Ready ||
1069 autoCaller.state() == Limited);
[6851]1070
[21878]1071 AutoWriteLock alock(childrenLock());
[13729]1072
[6851]1073 DependentChildren::size_type result = mDependentChildren.erase (aUnk);
[13729]1074 AssertMsg (result == 1, ("Failed to remove child %p from the map\n", aUnk));
[6851]1075 NOREF (result);
1076}
1077
[17689]1078/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use