VirtualBox

source: vbox/trunk/src/VBox/Main/include/MediumLock.h@ 28398

Last change on this file since 28398 was 28398, checked in by vboxsync, 15 years ago

Medium/include: svn property cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: MediumLock.h 28398 2010-04-16 08:38:37Z 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
37class Medium;
38class 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 */
45class MediumLock
46{
47public:
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 only used in unlocked 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 * Acquire a medium lock.
92 *
93 * @return COM status code
94 */
95 HRESULT Lock();
96
97 /**
98 * Release a medium lock.
99 *
100 * @return COM status code
101 */
102 HRESULT Unlock();
103
104private:
105 ComObjPtr<Medium> mMedium;
106 AutoCaller mMediumCaller;
107 bool mLockWrite;
108 bool mIsLocked;
109 /** Flag whether the medium was skipped when taking the locks.
110 * Only existing and accessible media objects need to be locked. */
111 bool mLockSkipped;
112};
113
114
115/**
116 * Medium lock list. Meant for storing the ordered locking information
117 * for a single medium chain.
118 */
119class MediumLockList
120{
121public:
122
123 /* Base list data type. */
124 typedef std::list<MediumLock> Base;
125
126 /**
127 * Default medium lock list constructor.
128 */
129 MediumLockList();
130
131 /**
132 * Default medium lock list destructor.
133 */
134 ~MediumLockList();
135
136 /**
137 * Checks if medium lock declaration list is empty.
138 *
139 * @return true if list is empty.
140 */
141 bool IsEmpty();
142
143 /**
144 * Add a new medium lock declaration to the end of the list.
145 *
146 * @note May be only used in unlocked state.
147 *
148 * @return COM status code
149 * @param aMedium Reference to medium object
150 * @param aLockWrite @c true means a write lock should be taken
151 */
152 HRESULT Append(const ComObjPtr<Medium> &aMedium, bool aLockWrite);
153
154 /**
155 * Add a new medium lock declaration to the beginning of the list.
156 *
157 * @note May be only used in unlocked state.
158 *
159 * @return COM status code
160 * @param aMedium Reference to medium object
161 * @param aLockWrite @c true means a write lock should be taken
162 */
163 HRESULT Prepend(const ComObjPtr<Medium> &aMedium, bool aLockWrite);
164
165 /**
166 * Update a medium lock declaration.
167 *
168 * @note May be only used in unlocked state.
169 *
170 * @return COM status code
171 * @param aMedium Reference to medium object
172 * @param aLockWrite @c true means a write lock should be taken
173 */
174 HRESULT Update(const ComObjPtr<Medium> &aMedium, bool aLockWrite);
175
176 /**
177 * Remove a medium lock declaration and return an updated iterator.
178 *
179 * @note May be used in locked state.
180 *
181 * @return COM status code
182 * @param aIt Iterator for the element to remove
183 */
184 HRESULT RemoveByIterator(Base::iterator &aIt);
185
186 /**
187 * Clear all medium lock declaration.
188 *
189 * @note Implicitly unlocks all locks.
190 *
191 * @return COM status code
192 */
193 HRESULT Clear();
194
195 /**
196 * Get iterator begin() for base list.
197 */
198 Base::iterator GetBegin();
199
200 /**
201 * Get iterator end() for base list.
202 */
203 Base::iterator GetEnd();
204
205 /**
206 * Acquire all medium locks "atomically", i.e. all or nothing.
207 *
208 * @return COM status code
209 */
210 HRESULT Lock();
211
212 /**
213 * Release all medium locks.
214 *
215 * @return COM status code
216 */
217 HRESULT Unlock();
218
219private:
220 Base mMediumLocks;
221 bool mIsLocked;
222};
223
224/**
225 * Medium lock list map. Meant for storing a collection of lock lists.
226 * The usual use case is creating such a map when locking all medium chains
227 * belonging to one VM, however that's not the limit. Be creative.
228 */
229class MediumLockListMap
230{
231public:
232
233 /**
234 * Default medium lock list map constructor.
235 */
236 MediumLockListMap();
237
238 /**
239 * Default medium lock list map destructor.
240 */
241 ~MediumLockListMap();
242
243 /**
244 * Checks if medium lock list map is empty.
245 *
246 * @return true if list is empty.
247 */
248 bool IsEmpty();
249
250 /**
251 * Insert a new medium lock list into the map.
252 *
253 * @note May be only used in unlocked state.
254 *
255 * @return COM status code
256 * @param aMediumAttachment Reference to medium attachment object, the key.
257 * @param aMediumLockList Reference to medium lock list object
258 */
259 HRESULT Insert(const ComObjPtr<MediumAttachment> &aMediumAttachment, MediumLockList *aMediumLockList);
260
261 /**
262 * Replace the medium lock list key by a different one.
263 *
264 * @note May be used in locked state.
265 *
266 * @return COM status code
267 * @param aMediumAttachmentOld Reference to medium attachment object.
268 * @param aMediumAttachmentNew Reference to medium attachment object.
269 */
270 HRESULT ReplaceKey(const ComObjPtr<MediumAttachment> &aMediumAttachmentOld, const ComObjPtr<MediumAttachment> &aMediumAttachmentNew);
271
272 /**
273 * Remove a medium lock list from the map. The list will get deleted.
274 *
275 * @note May be only used in unlocked state.
276 *
277 * @return COM status code
278 * @param aMediumAttachment Reference to medium attachment object, the key.
279 */
280 HRESULT Remove(const ComObjPtr<MediumAttachment> &aMediumAttachment);
281
282 /**
283 * Clear all medium lock declarations in this map.
284 *
285 * @note Implicitly unlocks all locks.
286 *
287 * @return COM status code
288 */
289 HRESULT Clear();
290
291 /**
292 * Get the medium lock list identified by the given key.
293 *
294 * @note May be used in locked state.
295 *
296 * @return COM status code
297 * @param aMediumAttachment Key for medium attachment object.
298 * @param aMediumLockList Out: medium attachment object reference.
299 */
300 HRESULT Get(const ComObjPtr<MediumAttachment> &aMediumAttachment, MediumLockList * &aMediumLockList);
301
302 /**
303 * Acquire all medium locks "atomically", i.e. all or nothing.
304 *
305 * @return COM status code
306 */
307 HRESULT Lock();
308
309 /**
310 * Release all medium locks.
311 *
312 * @return COM status code
313 */
314 HRESULT Unlock();
315
316private:
317 typedef std::map<ComObjPtr<MediumAttachment>, MediumLockList *> Base;
318 Base mMediumLocks;
319 bool mIsLocked;
320};
321
322#endif /* !____H_MEDIUMLOCK */
323/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette