1 | /* $Id: MediumLock.h 28582 2010-04-22 08:09:21Z 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 | #include "VirtualBoxBase.h"
|
---|
30 | #include "AutoCaller.h"
|
---|
31 |
|
---|
32 | #include <iprt/types.h>
|
---|
33 |
|
---|
34 | #include <list>
|
---|
35 | #include <map>
|
---|
36 |
|
---|
37 | class Medium;
|
---|
38 | class MediumAttachment;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Single entry for medium lock lists. Has a medium object reference,
|
---|
42 | * information about what kind of lock should be taken, and if it is
|
---|
43 | * locked right now.
|
---|
44 | */
|
---|
45 | class MediumLock
|
---|
46 | {
|
---|
47 | public:
|
---|
48 | /**
|
---|
49 | * Default medium lock constructor.
|
---|
50 | */
|
---|
51 | MediumLock();
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Default medium lock destructor.
|
---|
55 | */
|
---|
56 | ~MediumLock();
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Create a new medium lock description
|
---|
60 | *
|
---|
61 | * @param aMedium Reference to medium object
|
---|
62 | * @param aLockWrite @c true means a write lock should be taken
|
---|
63 | */
|
---|
64 | MediumLock(const ComObjPtr<Medium> &aMedium, bool aLockWrite);
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Copy constructor. Needed because we contain an AutoCaller
|
---|
68 | * instance which is deliberately not copyable. The copy is not
|
---|
69 | * marked as locked, so be careful.
|
---|
70 | *
|
---|
71 | * @param aMediumLock Reference to source object.
|
---|
72 | */
|
---|
73 | MediumLock(const MediumLock &aMediumLock);
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Update a medium lock description.
|
---|
77 | *
|
---|
78 | * @note May be used in locked state.
|
---|
79 | *
|
---|
80 | * @return COM status code
|
---|
81 | * @param aLockWrite @c true means a write lock should be taken
|
---|
82 | */
|
---|
83 | HRESULT UpdateLock(bool aLockWrite);
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Get medium object reference.
|
---|
87 | */
|
---|
88 | const ComObjPtr<Medium> &GetMedium() const;
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Get medium object lock request type.
|
---|
92 | */
|
---|
93 | bool GetLockRequest() const;
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Acquire a medium lock.
|
---|
97 | *
|
---|
98 | * @return COM status code
|
---|
99 | */
|
---|
100 | HRESULT Lock();
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Release a medium lock.
|
---|
104 | *
|
---|
105 | * @return COM status code
|
---|
106 | */
|
---|
107 | HRESULT Unlock();
|
---|
108 |
|
---|
109 | private:
|
---|
110 | ComObjPtr<Medium> mMedium;
|
---|
111 | AutoCaller mMediumCaller;
|
---|
112 | bool mLockWrite;
|
---|
113 | bool mIsLocked;
|
---|
114 | /** Flag whether the medium was skipped when taking the locks.
|
---|
115 | * Only existing and accessible media objects need to be locked. */
|
---|
116 | bool mLockSkipped;
|
---|
117 | };
|
---|
118 |
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Medium lock list. Meant for storing the ordered locking information
|
---|
122 | * for a single medium chain.
|
---|
123 | */
|
---|
124 | class MediumLockList
|
---|
125 | {
|
---|
126 | public:
|
---|
127 |
|
---|
128 | /* Base list data type. */
|
---|
129 | typedef std::list<MediumLock> Base;
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Default medium lock list constructor.
|
---|
133 | */
|
---|
134 | MediumLockList();
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Default medium lock list destructor.
|
---|
138 | */
|
---|
139 | ~MediumLockList();
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Checks if medium lock declaration list is empty.
|
---|
143 | *
|
---|
144 | * @return true if list is empty.
|
---|
145 | */
|
---|
146 | bool IsEmpty();
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Add a new medium lock declaration to the end of the list.
|
---|
150 | *
|
---|
151 | * @note May be only used in unlocked state.
|
---|
152 | *
|
---|
153 | * @return COM status code
|
---|
154 | * @param aMedium Reference to medium object
|
---|
155 | * @param aLockWrite @c true means a write lock should be taken
|
---|
156 | */
|
---|
157 | HRESULT Append(const ComObjPtr<Medium> &aMedium, bool aLockWrite);
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Add a new medium lock declaration to the beginning of the list.
|
---|
161 | *
|
---|
162 | * @note May be only used in unlocked state.
|
---|
163 | *
|
---|
164 | * @return COM status code
|
---|
165 | * @param aMedium Reference to medium object
|
---|
166 | * @param aLockWrite @c true means a write lock should be taken
|
---|
167 | */
|
---|
168 | HRESULT Prepend(const ComObjPtr<Medium> &aMedium, bool aLockWrite);
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Update a medium lock declaration.
|
---|
172 | *
|
---|
173 | * @note May be only used in unlocked state.
|
---|
174 | *
|
---|
175 | * @return COM status code
|
---|
176 | * @param aMedium Reference to medium object
|
---|
177 | * @param aLockWrite @c true means a write lock should be taken
|
---|
178 | */
|
---|
179 | HRESULT Update(const ComObjPtr<Medium> &aMedium, bool aLockWrite);
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Remove a medium lock declaration and return an updated iterator.
|
---|
183 | *
|
---|
184 | * @note May be used in locked state.
|
---|
185 | *
|
---|
186 | * @return COM status code
|
---|
187 | * @param aIt Iterator for the element to remove
|
---|
188 | */
|
---|
189 | HRESULT RemoveByIterator(Base::iterator &aIt);
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Clear all medium lock declarations.
|
---|
193 | *
|
---|
194 | * @note Implicitly unlocks all locks.
|
---|
195 | *
|
---|
196 | * @return COM status code
|
---|
197 | */
|
---|
198 | HRESULT Clear();
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Get iterator begin() for base list.
|
---|
202 | */
|
---|
203 | Base::iterator GetBegin();
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Get iterator end() for base list.
|
---|
207 | */
|
---|
208 | Base::iterator GetEnd();
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Acquire all medium locks "atomically", i.e. all or nothing.
|
---|
212 | *
|
---|
213 | * @return COM status code
|
---|
214 | */
|
---|
215 | HRESULT Lock();
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Release all medium locks.
|
---|
219 | *
|
---|
220 | * @return COM status code
|
---|
221 | */
|
---|
222 | HRESULT Unlock();
|
---|
223 |
|
---|
224 | private:
|
---|
225 | Base mMediumLocks;
|
---|
226 | bool mIsLocked;
|
---|
227 | };
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Medium lock list map. Meant for storing a collection of lock lists.
|
---|
231 | * The usual use case is creating such a map when locking all medium chains
|
---|
232 | * belonging to one VM, however that's not the limit. Be creative.
|
---|
233 | */
|
---|
234 | class MediumLockListMap
|
---|
235 | {
|
---|
236 | public:
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Default medium lock list map constructor.
|
---|
240 | */
|
---|
241 | MediumLockListMap();
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Default medium lock list map destructor.
|
---|
245 | */
|
---|
246 | ~MediumLockListMap();
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Checks if medium lock list map is empty.
|
---|
250 | *
|
---|
251 | * @return true if list is empty.
|
---|
252 | */
|
---|
253 | bool IsEmpty();
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Insert a new medium lock list into the map.
|
---|
257 | *
|
---|
258 | * @note May be only used in unlocked state.
|
---|
259 | *
|
---|
260 | * @return COM status code
|
---|
261 | * @param aMediumAttachment Reference to medium attachment object, the key.
|
---|
262 | * @param aMediumLockList Reference to medium lock list object
|
---|
263 | */
|
---|
264 | HRESULT Insert(const ComObjPtr<MediumAttachment> &aMediumAttachment, MediumLockList *aMediumLockList);
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Replace the medium lock list key by a different one.
|
---|
268 | *
|
---|
269 | * @note May be used in locked state.
|
---|
270 | *
|
---|
271 | * @return COM status code
|
---|
272 | * @param aMediumAttachmentOld Reference to medium attachment object.
|
---|
273 | * @param aMediumAttachmentNew Reference to medium attachment object.
|
---|
274 | */
|
---|
275 | HRESULT ReplaceKey(const ComObjPtr<MediumAttachment> &aMediumAttachmentOld, const ComObjPtr<MediumAttachment> &aMediumAttachmentNew);
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Remove a medium lock list from the map. The list will get deleted.
|
---|
279 | *
|
---|
280 | * @note May be only used in unlocked state.
|
---|
281 | *
|
---|
282 | * @return COM status code
|
---|
283 | * @param aMediumAttachment Reference to medium attachment object, the key.
|
---|
284 | */
|
---|
285 | HRESULT Remove(const ComObjPtr<MediumAttachment> &aMediumAttachment);
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Clear all medium lock declarations in this map.
|
---|
289 | *
|
---|
290 | * @note Implicitly unlocks all locks.
|
---|
291 | *
|
---|
292 | * @return COM status code
|
---|
293 | */
|
---|
294 | HRESULT Clear();
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * Get the medium lock list identified by the given key.
|
---|
298 | *
|
---|
299 | * @note May be used in locked state.
|
---|
300 | *
|
---|
301 | * @return COM status code
|
---|
302 | * @param aMediumAttachment Key for medium attachment object.
|
---|
303 | * @param aMediumLockList Out: medium attachment object reference.
|
---|
304 | */
|
---|
305 | HRESULT Get(const ComObjPtr<MediumAttachment> &aMediumAttachment, MediumLockList * &aMediumLockList);
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Acquire all medium locks "atomically", i.e. all or nothing.
|
---|
309 | *
|
---|
310 | * @return COM status code
|
---|
311 | */
|
---|
312 | HRESULT Lock();
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Release all medium locks.
|
---|
316 | *
|
---|
317 | * @return COM status code
|
---|
318 | */
|
---|
319 | HRESULT Unlock();
|
---|
320 |
|
---|
321 | private:
|
---|
322 | typedef std::map<ComObjPtr<MediumAttachment>, MediumLockList *> Base;
|
---|
323 | Base mMediumLocks;
|
---|
324 | bool mIsLocked;
|
---|
325 | };
|
---|
326 |
|
---|
327 | #endif /* !____H_MEDIUMLOCK */
|
---|
328 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|