1 | /* $Id: MediumLock.h 27120 2010-03-05 18:03:49Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VirtualBox medium object lock collections
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2010 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 | #ifndef ____H_MEDIUMLOCK
|
---|
25 | #define ____H_MEDIUMLOCK
|
---|
26 |
|
---|
27 | /* interface definitions */
|
---|
28 | #include "VBox/com/VirtualBox.h"
|
---|
29 |
|
---|
30 | #include <iprt/types.h>
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Single entry for medium lock lists. Has a medium object reference,
|
---|
34 | * information about what kind of lock should be taken, and if it is
|
---|
35 | * locked right now.
|
---|
36 | */
|
---|
37 | class MediumLock
|
---|
38 | {
|
---|
39 | public:
|
---|
40 | /**
|
---|
41 | * Default medium lock constructor.
|
---|
42 | */
|
---|
43 | MediumLock();
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Default medium lock destructor.
|
---|
47 | */
|
---|
48 | ~MediumLock();
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Create a new medium lock description
|
---|
52 | *
|
---|
53 | * @param aMedium Reference to medium object
|
---|
54 | * @param aLockWrite @c true means a write lock should be taken
|
---|
55 | */
|
---|
56 | MediumLock(ComObjPtr<Medium> aMedium, bool aLockWrite);
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Acquire a medium lock.
|
---|
60 | */
|
---|
61 | int Lock();
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Release a medium lock.
|
---|
65 | */
|
---|
66 | int Unlock();
|
---|
67 |
|
---|
68 | private:
|
---|
69 | ComObjPtr<Medium> mMedium;
|
---|
70 | bool mLockWrite;
|
---|
71 | };
|
---|
72 |
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Medium lock list. Meant for storing the ordered locking information
|
---|
76 | * for a single medium chain.
|
---|
77 | */
|
---|
78 | class MediumLockList
|
---|
79 | {
|
---|
80 | public:
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Default medium lock list constructor.
|
---|
84 | */
|
---|
85 | MediumLockList();
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Default medium lock list destructor.
|
---|
89 | */
|
---|
90 | ~MediumLockList();
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Add a new medium lock declaration to the end of the list.
|
---|
94 | *
|
---|
95 | * @note May be only used in unlocked state.
|
---|
96 | *
|
---|
97 | * @return VBox status code.
|
---|
98 | * @param aMedium Reference to medium object
|
---|
99 | * @param aLockWrite @c true means a write lock should be taken
|
---|
100 | */
|
---|
101 | int Append(ComObjPtr<Medium> aMedium, bool aLockWrite);
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Add a new medium lock declaration to the beginning of the list.
|
---|
105 | *
|
---|
106 | * @note May be only used in unlocked state.
|
---|
107 | *
|
---|
108 | * @return VBox status code.
|
---|
109 | * @param aMedium Reference to medium object
|
---|
110 | * @param aLockWrite @c true means a write lock should be taken
|
---|
111 | */
|
---|
112 | int Prepend(ComObjPtr<Medium> aMedium, bool aLockWrite);
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Update a medium lock declaration.
|
---|
116 | *
|
---|
117 | * @note May be only used in unlocked state.
|
---|
118 | *
|
---|
119 | * @return VBox status code.
|
---|
120 | * @param aMedium Reference to medium object
|
---|
121 | * @param aLockWrite @c true means a write lock should be taken
|
---|
122 | */
|
---|
123 | int Update(ComObjPtr<Medium> aMedium, bool aLockWrite);
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Remove a medium lock declaration.
|
---|
127 | *
|
---|
128 | * @note May be only used in unlocked state.
|
---|
129 | *
|
---|
130 | * @return VBox status code.
|
---|
131 | * @param aMedium Reference to medium object
|
---|
132 | */
|
---|
133 | int Remove(ComObjPtr<Medium> aMedium);
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Acquire all medium locks "atomically", i.e. all or nothing.
|
---|
137 | */
|
---|
138 | int Lock();
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Release all medium locks.
|
---|
142 | */
|
---|
143 | int Unlock();
|
---|
144 |
|
---|
145 | private:
|
---|
146 | std::list<MediumLock> mMediumLocks;
|
---|
147 | bool mIsLocked;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Medium lock list map. Meant for storing a collection of lock lists.
|
---|
152 | * The usual use case is creating such a map when locking all medium chains
|
---|
153 | * belonging to one VM, however that's not the limit. Be creative.
|
---|
154 | */
|
---|
155 | class MediumLockListMap
|
---|
156 | {
|
---|
157 | public:
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Default medium lock list map constructor.
|
---|
161 | */
|
---|
162 | MediumLockListMap();
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Default medium lock list destructor.
|
---|
166 | */
|
---|
167 | ~MediumLockListMap();
|
---|
168 |
|
---|
169 | private:
|
---|
170 | std::map<ComObjPtr<Medium>, MediumLockList *> mMediumLocks;
|
---|
171 | }
|
---|
172 |
|
---|
173 | #endif /* !____H_MEDIUMLOCK */
|
---|
174 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|