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