1 | /* $Id: ObjectState.h 55401 2015-04-23 10:03:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VirtualBox object state handling definitions
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2014 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #ifndef ____H_OBJECTSTATE
|
---|
20 | #define ____H_OBJECTSTATE
|
---|
21 |
|
---|
22 | #include "VBox/com/defs.h"
|
---|
23 | #include "VBox/com/AutoLock.h"
|
---|
24 |
|
---|
25 | // Forward declaration needed, but nothing more.
|
---|
26 | class VirtualBoxBase;
|
---|
27 |
|
---|
28 | ////////////////////////////////////////////////////////////////////////////////
|
---|
29 | //
|
---|
30 | // ObjectState
|
---|
31 | //
|
---|
32 | ////////////////////////////////////////////////////////////////////////////////
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Thec functionality implemented by this class is the primary object state
|
---|
36 | * (used by VirtualBoxBase and thus part of all API classes) that indicates
|
---|
37 | * if the object is ready to serve the calls, and if not, what stage it is
|
---|
38 | * currently at. Here is the primary state diagram:
|
---|
39 | *
|
---|
40 | * +-------------------------------------------------------+
|
---|
41 | * | |
|
---|
42 | * | (InitFailed) -----------------------+ |
|
---|
43 | * | ^ | |
|
---|
44 | * v | v |
|
---|
45 | * [*] ---> NotReady ----> (InInit) -----> Ready -----> (InUninit) ----+
|
---|
46 | * ^ |
|
---|
47 | * | v
|
---|
48 | * | Limited
|
---|
49 | * | |
|
---|
50 | * +-------+
|
---|
51 | *
|
---|
52 | * The object is fully operational only when its state is Ready. The Limited
|
---|
53 | * state means that only some vital part of the object is operational, and it
|
---|
54 | * requires some sort of reinitialization to become fully operational. The
|
---|
55 | * NotReady state means the object is basically dead: it either was not yet
|
---|
56 | * initialized after creation at all, or was uninitialized and is waiting to be
|
---|
57 | * destroyed when the last reference to it is released. All other states are
|
---|
58 | * transitional.
|
---|
59 | *
|
---|
60 | * The NotReady->InInit->Ready, NotReady->InInit->Limited and
|
---|
61 | * NotReady->InInit->InitFailed transition is done by the AutoInitSpan smart
|
---|
62 | * class.
|
---|
63 | *
|
---|
64 | * The Limited->InInit->Ready, Limited->InInit->Limited and
|
---|
65 | * Limited->InInit->InitFailed transition is done by the AutoReinitSpan smart
|
---|
66 | * class.
|
---|
67 | *
|
---|
68 | * The Ready->InUninit->NotReady and InitFailed->InUninit->NotReady
|
---|
69 | * transitions are done by the AutoUninitSpan smart class.
|
---|
70 | *
|
---|
71 | * In order to maintain the primary state integrity and declared functionality
|
---|
72 | * the following rules apply everywhere:
|
---|
73 | *
|
---|
74 | * 1) Use the above Auto*Span classes to perform state transitions. See the
|
---|
75 | * individual class descriptions for details.
|
---|
76 | *
|
---|
77 | * 2) All public methods of subclasses (i.e. all methods that can be called
|
---|
78 | * directly, not only from within other methods of the subclass) must have a
|
---|
79 | * standard prolog as described in the AutoCaller and AutoLimitedCaller
|
---|
80 | * documentation. Alternatively, they must use #addCaller() and
|
---|
81 | * #releaseCaller() directly (and therefore have both the prolog and the
|
---|
82 | * epilog), but this is not recommended because it is easy to forget the
|
---|
83 | * matching release, e.g. returning before reaching the call.
|
---|
84 | */
|
---|
85 | class ObjectState
|
---|
86 | {
|
---|
87 | public:
|
---|
88 | enum State { NotReady, Ready, InInit, InUninit, InitFailed, Limited };
|
---|
89 |
|
---|
90 | ObjectState(VirtualBoxBase *aObj);
|
---|
91 | ~ObjectState();
|
---|
92 |
|
---|
93 | State getState();
|
---|
94 |
|
---|
95 | HRESULT addCaller(bool aLimited = false);
|
---|
96 | void releaseCaller();
|
---|
97 |
|
---|
98 | bool autoInitSpanConstructor(State aExpectedState);
|
---|
99 | void autoInitSpanDestructor(State aNewState);
|
---|
100 | State autoUninitSpanConstructor();
|
---|
101 | void autoUninitSpanDestructor();
|
---|
102 |
|
---|
103 | private:
|
---|
104 | ObjectState();
|
---|
105 |
|
---|
106 | void setState(State aState);
|
---|
107 |
|
---|
108 | /** Pointer to the managed object, mostly for error signalling or debugging
|
---|
109 | * purposes, not used much. Guaranteed to be valid during the lifetime of
|
---|
110 | * this object, no need to mess with refcount. */
|
---|
111 | VirtualBoxBase *mObj;
|
---|
112 | /** Primary state of this object */
|
---|
113 | State mState;
|
---|
114 | /** Thread that caused the last state change */
|
---|
115 | RTTHREAD mStateChangeThread;
|
---|
116 | /** Total number of active calls to this object */
|
---|
117 | unsigned mCallers;
|
---|
118 | /** Posted when the number of callers drops to zero */
|
---|
119 | RTSEMEVENT mZeroCallersSem;
|
---|
120 | /** Posted when the object goes from InInit/InUninit to some other state */
|
---|
121 | RTSEMEVENTMULTI mInitUninitSem;
|
---|
122 | /** Number of threads waiting for mInitUninitDoneSem */
|
---|
123 | unsigned mInitUninitWaiters;
|
---|
124 |
|
---|
125 | /** Protects access to state related data members */
|
---|
126 | util::RWLockHandle mStateLock;
|
---|
127 | };
|
---|
128 |
|
---|
129 | #endif // !____H_OBJECTSTATE
|
---|