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