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