VirtualBox

Changeset 3330 in vbox


Ignore:
Timestamp:
Jun 29, 2007 4:35:37 PM (18 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
22472
Message:

Main: Converted DVDDrive and FloppyDrive to the new locking scheme.

Location:
trunk/src/VBox/Main
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/DVDDriveImpl.cpp

    r2981 r3330  
    2222#include "DVDDriveImpl.h"
    2323#include "MachineImpl.h"
     24#include "VirtualBoxImpl.h"
    2425#include "Logging.h"
    2526
    2627#include <iprt/string.h>
    27 
    28 // defines
    29 ////////////////////////////////////////////////////////////////////////////////
     28#include <iprt/cpputils.h>
    3029
    3130// constructor / destructor
    3231////////////////////////////////////////////////////////////////////////////////
    3332
     33DEFINE_EMPTY_CTOR_DTOR (DVDDrive)
     34
    3435HRESULT DVDDrive::FinalConstruct()
    3536{
     
    3940void DVDDrive::FinalRelease()
    4041{
    41     if (isReady())
    42         uninit ();
     42    uninit();
    4343}
    4444
     
    4949 * Initializes the DVD drive object.
    5050 *
    51  * @param   parent  handle of our parent object
     51 * @param   aParent  Handle of our parent object.
    5252 * @return  COM result indicator
    5353 */
    54 HRESULT DVDDrive::init (Machine *parent)
    55 {
    56     LogFlowMember (("DVDDrive::init (%p)\n", parent));
    57 
    58     ComAssertRet (parent, E_INVALIDARG);
    59 
    60     AutoLock alock (this);
    61     ComAssertRet (!isReady(), E_UNEXPECTED);
    62 
    63     mParent = parent;
    64     // mPeer is left null
     54HRESULT DVDDrive::init (Machine *aParent)
     55{
     56    LogFlowThisFunc (("aParent=%p\n", aParent));
     57
     58    ComAssertRet (aParent, E_INVALIDARG);
     59
     60    /* Enclose the state transition NotReady->InInit->Ready */
     61    AutoInitSpan autoInitSpan (this);
     62    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     63
     64    unconst (mParent) = aParent;
     65    /* mPeer is left null */
    6566
    6667    mData.allocate();
    6768
    68     setReady (true);
     69    /* Confirm a successful initialization */
     70    autoInitSpan.setSucceeded();
     71
    6972    return S_OK;
    7073}
     
    7780 *  @note This object must be destroyed before the original object
    7881 *  it shares data with is destroyed.
    79  */
    80 HRESULT DVDDrive::init (Machine *parent, DVDDrive *that)
    81 {
    82     LogFlowMember (("DVDDrive::init (%p, %p)\n", parent, that));
    83 
    84     ComAssertRet (parent && that, E_INVALIDARG);
    85 
    86     AutoLock alock (this);
    87     ComAssertRet (!isReady(), E_UNEXPECTED);
    88 
    89     mParent = parent;
    90     mPeer = that;
    91 
    92     AutoLock thatlock (that);
    93     mData.share (that->mData);
    94 
    95     setReady (true);
     82 *
     83 *  @note Locks @a aThat object for reading.
     84 */
     85HRESULT DVDDrive::init (Machine *aParent, DVDDrive *aThat)
     86{
     87    LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
     88
     89    ComAssertRet (aParent && aThat, E_INVALIDARG);
     90
     91    /* Enclose the state transition NotReady->InInit->Ready */
     92    AutoInitSpan autoInitSpan (this);
     93    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     94
     95    unconst (mParent) = aParent;
     96    unconst (mPeer) = aThat;
     97
     98    AutoCaller thatCaller (aThat);
     99    AssertComRCReturnRC (thatCaller.rc());
     100
     101    AutoReaderLock thatLock (aThat);
     102    mData.share (aThat->mData);
     103
     104    /* Confirm a successful initialization */
     105    autoInitSpan.setSucceeded();
     106
    96107    return S_OK;
    97108}
    98109
    99110/**
    100  *  Initializes the guest object given another guest object
     111 *  Initializes the DVD drive object given another DVD drive object
    101112 *  (a kind of copy constructor). This object makes a private copy of data
    102113 *  of the original object passed as an argument.
    103  */
    104 HRESULT DVDDrive::initCopy (Machine *parent, DVDDrive *that)
    105 {
    106     LogFlowMember (("DVDDrive::initCopy (%p, %p)\n", parent, that));
    107 
    108     ComAssertRet (parent && that, E_INVALIDARG);
    109 
    110     AutoLock alock (this);
    111     ComAssertRet (!isReady(), E_UNEXPECTED);
    112 
    113     mParent = parent;
    114     // mPeer is left null
    115 
    116     AutoLock thatlock (that);
    117     mData.attachCopy (that->mData);
    118 
    119     setReady (true);
     114 *
     115 *  @note Locks @a aThat object for reading.
     116 */
     117HRESULT DVDDrive::initCopy (Machine *aParent, DVDDrive *aThat)
     118{
     119    LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
     120
     121    ComAssertRet (aParent && aThat, E_INVALIDARG);
     122
     123    /* Enclose the state transition NotReady->InInit->Ready */
     124    AutoInitSpan autoInitSpan (this);
     125    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     126
     127    unconst (mParent) = aParent;
     128    /* mPeer is left null */
     129
     130    AutoCaller thatCaller (aThat);
     131    AssertComRCReturnRC (thatCaller.rc());
     132
     133    AutoReaderLock thatLock (aThat);
     134    mData.attachCopy (aThat->mData);
     135
     136    /* Confirm a successful initialization */
     137    autoInitSpan.setSucceeded();
     138
    120139    return S_OK;
    121140}
     
    127146void DVDDrive::uninit()
    128147{
    129     LogFlowMember (("DVDDrive::uninit()\n"));
     148    LogFlowThisFunc (("\n"));
     149
     150    /* Enclose the state transition Ready->InUninit->NotReady */
     151    AutoUninitSpan autoUninitSpan (this);
     152    if (autoUninitSpan.uninitDone())
     153        return;
     154
     155    mData.free();
     156
     157    unconst (mParent).setNull();
     158    unconst (mParent).setNull();
     159}
     160
     161// IDVDDrive properties
     162////////////////////////////////////////////////////////////////////////////////
     163
     164STDMETHODIMP DVDDrive::COMGETTER(State) (DriveState_T *aDriveState)
     165{
     166    if (!aDriveState)
     167        return E_POINTER;
     168
     169    AutoCaller autoCaller (this);
     170    CheckComRCReturnRC (autoCaller.rc());
     171
     172    AutoReaderLock alock (this);
     173
     174    *aDriveState = mData->mDriveState;
     175
     176    return S_OK;
     177}
     178
     179STDMETHODIMP DVDDrive::COMGETTER(Passthrough) (BOOL *aPassthrough)
     180{
     181    if (!aPassthrough)
     182        return E_POINTER;
     183
     184    AutoCaller autoCaller (this);
     185    CheckComRCReturnRC (autoCaller.rc());
     186
     187    AutoReaderLock alock (this);
     188
     189    *aPassthrough = mData->mPassthrough;
     190
     191    return S_OK;
     192}
     193
     194STDMETHODIMP DVDDrive::COMSETTER(Passthrough) (BOOL aPassthrough)
     195{
     196    AutoCaller autoCaller (this);
     197    CheckComRCReturnRC (autoCaller.rc());
     198
     199    /* the machine needs to be mutable */
     200    Machine::AutoMutableStateDependency adep (mParent);
     201    CheckComRCReturnRC (adep.rc());
    130202
    131203    AutoLock alock (this);
    132204
    133     AssertReturn (isReady(), (void) 0);
    134 
    135     mData.free();
    136 
    137     mPeer.setNull();
    138     mParent.setNull();
    139 
    140     setReady (false);
    141 }
    142 
    143 // IDVDDrive properties
    144 ////////////////////////////////////////////////////////////////////////////////
    145 
    146 STDMETHODIMP DVDDrive::COMGETTER(State) (DriveState_T *driveState)
    147 {
    148     if (!driveState)
    149         return E_POINTER;
     205    if (mData->mPassthrough != aPassthrough)
     206    {
     207        mData.backup();
     208        mData->mPassthrough = aPassthrough;
     209    }
     210
     211    return S_OK;
     212}
     213
     214// IDVDDrive methods
     215////////////////////////////////////////////////////////////////////////////////
     216
     217STDMETHODIMP DVDDrive::MountImage (INPTR GUIDPARAM aImageId)
     218{
     219    if (Guid::isEmpty (aImageId))
     220        return E_INVALIDARG;
     221
     222    AutoCaller autoCaller (this);
     223    CheckComRCReturnRC (autoCaller.rc());
     224
     225    /* the machine needs to be mutable */
     226    Machine::AutoMutableStateDependency adep (mParent);
     227    CheckComRCReturnRC (adep.rc());
    150228
    151229    AutoLock alock (this);
    152     CHECK_READY();
    153 
    154     *driveState = mData->mDriveState;
    155     return S_OK;
    156 }
    157 
    158 STDMETHODIMP DVDDrive::COMGETTER(Passthrough) (BOOL *passthrough)
    159 {
    160     if (!passthrough)
    161         return E_POINTER;
    162 
    163     AutoLock alock(this);
    164     CHECK_READY();
    165 
    166     *passthrough = mData->mPassthrough;
    167     return S_OK;
    168 }
    169 
    170 STDMETHODIMP DVDDrive::COMSETTER(Passthrough) (BOOL passthrough)
    171 {
    172     AutoLock alock(this);
    173     CHECK_READY();
    174     CHECK_MACHINE_MUTABILITY (mParent);
    175 
    176     if (mData->mPassthrough != passthrough)
    177     {
    178         mData.backup();
    179         mData->mPassthrough = passthrough;
    180     }
    181     return S_OK;
    182 }
    183 
    184 // IDVDDrive methods
    185 ////////////////////////////////////////////////////////////////////////////////
    186 
    187 STDMETHODIMP DVDDrive::MountImage(INPTR GUIDPARAM imageId)
    188 {
    189     if (Guid::isEmpty(imageId))
    190         return E_INVALIDARG;
    191 
    192     AutoLock alock (this);
    193     CHECK_READY();
    194 
    195     CHECK_MACHINE_MUTABILITY (mParent);
    196230
    197231    HRESULT rc = E_FAIL;
    198232
    199     // find the image in the collection
    200     ComPtr <IVirtualBox> vbox;
    201     rc = mParent->COMGETTER(Parent) (vbox.asOutParam());
    202     AssertComRCReturn (rc, rc);
     233    /* Our lifetime is bound to mParent's lifetime, so we don't add caller.
     234     * We also don't lock mParent since its mParent field is const. */
    203235
    204236    ComPtr <IDVDImage> image;
    205     rc = vbox->GetDVDImage (imageId, image.asOutParam());
     237    rc = mParent->virtualBox()->GetDVDImage (aImageId, image.asOutParam());
     238
    206239    if (SUCCEEDED (rc))
    207240    {
     
    210243        {
    211244            mData.backup();
     245
    212246            unmount();
     247
    213248            mData->mDVDImage = image;
    214249            mData->mDriveState = DriveState_ImageMounted;
    215250
     251            /* leave the lock before informing callbacks*/
    216252            alock.unlock();
     253
    217254            mParent->onDVDDriveChange();
    218255        }
     
    222259}
    223260
    224 STDMETHODIMP DVDDrive::CaptureHostDrive(IHostDVDDrive *hostDVDDrive)
    225 {
    226     if (!hostDVDDrive)
     261STDMETHODIMP DVDDrive::CaptureHostDrive (IHostDVDDrive *aHostDVDDrive)
     262{
     263    if (!aHostDVDDrive)
    227264        return E_INVALIDARG;
    228265
     266    AutoCaller autoCaller (this);
     267    CheckComRCReturnRC (autoCaller.rc());
     268
     269    /* the machine needs to be mutable */
     270    Machine::AutoMutableStateDependency adep (mParent);
     271    CheckComRCReturnRC (adep.rc());
     272
    229273    AutoLock alock (this);
    230     CHECK_READY();
    231 
    232     CHECK_MACHINE_MUTABILITY (mParent);
    233274
    234275    if (mData->mDriveState != DriveState_HostDriveCaptured ||
    235         !mData->mHostDrive.equalsTo (hostDVDDrive))
     276        !mData->mHostDrive.equalsTo (aHostDVDDrive))
    236277    {
    237278        mData.backup();
     279
    238280        unmount();
    239         mData->mHostDrive = hostDVDDrive;
     281
     282        mData->mHostDrive = aHostDVDDrive;
    240283        mData->mDriveState = DriveState_HostDriveCaptured;
    241284
     285        /* leave the lock before informing callbacks*/
    242286        alock.unlock();
     287
    243288        mParent->onDVDDriveChange();
    244289    }
     
    249294STDMETHODIMP DVDDrive::Unmount()
    250295{
     296    AutoCaller autoCaller (this);
     297    CheckComRCReturnRC (autoCaller.rc());
     298
     299    /* the machine needs to be mutable */
     300    Machine::AutoMutableStateDependency adep (mParent);
     301    CheckComRCReturnRC (adep.rc());
     302
    251303    AutoLock alock (this);
    252     CHECK_READY();
    253 
    254     CHECK_MACHINE_MUTABILITY (mParent);
    255304
    256305    if (mData->mDriveState != DriveState_NotMounted)
    257306    {
    258307        mData.backup();
     308
    259309        unmount();
     310
    260311        mData->mDriveState = DriveState_NotMounted;
    261312
     313        /* leave the lock before informing callbacks*/
    262314        alock.unlock();
     315
    263316        mParent->onDVDDriveChange();
    264317    }
     
    267320}
    268321
    269 STDMETHODIMP DVDDrive::GetImage(IDVDImage **dvdImage)
    270 {
    271     if (!dvdImage)
     322STDMETHODIMP DVDDrive::GetImage (IDVDImage **aDVDImage)
     323{
     324    if (!aDVDImage)
    272325        return E_POINTER;
    273326
     327    AutoCaller autoCaller (this);
     328    CheckComRCReturnRC (autoCaller.rc());
     329
     330    AutoReaderLock alock (this);
     331
     332    mData->mDVDImage.queryInterfaceTo (aDVDImage);
     333
     334    return S_OK;
     335}
     336
     337STDMETHODIMP DVDDrive::GetHostDrive(IHostDVDDrive **aHostDrive)
     338{
     339    if (!aHostDrive)
     340        return E_POINTER;
     341
     342    AutoCaller autoCaller (this);
     343    CheckComRCReturnRC (autoCaller.rc());
     344
     345    AutoReaderLock alock (this);
     346
     347    mData->mHostDrive.queryInterfaceTo (aHostDrive);
     348
     349    return S_OK;
     350}
     351
     352// public methods only for internal purposes
     353////////////////////////////////////////////////////////////////////////////////
     354
     355/**
     356 *  @note Locks this object for writing.
     357 */
     358bool DVDDrive::rollback()
     359{
     360    /* sanity */
     361    AutoCaller autoCaller (this);
     362    AssertComRCReturn (autoCaller.rc(), false);
     363
    274364    AutoLock alock (this);
    275     CHECK_READY();
    276 
    277     mData->mDVDImage.queryInterfaceTo (dvdImage);
    278     return S_OK;
    279 }
    280 
    281 STDMETHODIMP DVDDrive::GetHostDrive(IHostDVDDrive **hostDrive)
    282 {
    283     if (!hostDrive)
    284         return E_POINTER;
    285 
    286     AutoLock alock (this);
    287     CHECK_READY();
    288 
    289     mData->mHostDrive.queryInterfaceTo (hostDrive);
    290     return S_OK;
    291 }
    292 
    293 // public methods only for internal purposes
    294 ////////////////////////////////////////////////////////////////////////////////
    295 
    296 bool DVDDrive::rollback()
    297 {
    298     AutoLock alock (this);
    299365
    300366    bool changed = false;
     
    302368    if (mData.isBackedUp())
    303369    {
    304         // we need to check all data to see whether anything will be changed
    305         // after rollback
     370        /* we need to check all data to see whether anything will be changed
     371         * after rollback */
    306372        changed = mData.hasActualChanges();
    307373        mData.rollback();
     
    311377}
    312378
     379/**
     380 *  @note Locks this object for writing, together with the peer object (also
     381 *  for writing) if there is one.
     382 */
    313383void DVDDrive::commit()
    314384{
    315     AutoLock alock (this);
     385    /* sanity */
     386    AutoCaller autoCaller (this);
     387    AssertComRCReturnVoid (autoCaller.rc());
     388
     389    /* sanity too */
     390    AutoCaller thatCaller (mPeer);
     391    AssertComRCReturnVoid (thatCaller.rc());
     392
     393    /* lock both for writing since we modify both */
     394    AutoMultiLock <2> alock (this->wlock(), AutoLock::maybeWlock (mPeer));
     395
    316396    if (mData.isBackedUp())
    317397    {
     
    319399        if (mPeer)
    320400        {
    321             // attach new data to the peer and reshare it
    322             AutoLock peerlock (mPeer);
     401            /* attach new data to the peer and reshare it */
    323402            mPeer->mData.attach (mData);
    324403        }
     
    326405}
    327406
     407/**
     408 *  @note Locks this object for writing, together with the peer object (locked
     409 *  for reading) if there is one.
     410 */
    328411void DVDDrive::copyFrom (DVDDrive *aThat)
    329412{
    330     AutoLock alock (this);
    331 
    332     // this will back up current data
     413    /* sanity */
     414    AutoCaller autoCaller (this);
     415    AssertComRCReturnVoid (autoCaller.rc());
     416
     417    /* sanity too */
     418    AutoCaller thatCaller (mPeer);
     419    AssertComRCReturnVoid (thatCaller.rc());
     420
     421    /* peer is not modified, lock it for reading */
     422    AutoMultiLock <2> alock (this->wlock(), AutoLock::maybeRlock (mPeer));
     423
     424    /* this will back up current data */
    333425    mData.assignCopy (aThat->mData);
    334426}
     
    338430
    339431/**
    340  * Helper to unmount a drive
    341  *
    342  * @returns COM status code
     432 *  Helper to unmount a drive.
     433 *
     434 *  @return COM status code
    343435 *
    344436 */
    345437HRESULT DVDDrive::unmount()
    346438{
     439    AssertReturn (isLockedOnCurrentThread(), E_FAIL);
     440
    347441    if (mData->mDVDImage)
    348442        mData->mDVDImage.setNull();
    349443    if (mData->mHostDrive)
    350444        mData->mHostDrive.setNull();
    351     return S_OK;
    352 }
     445
     446    return S_OK;
     447}
  • trunk/src/VBox/Main/DVDImageImpl.cpp

    r3007 r3330  
    2727#include <iprt/path.h>
    2828#include <iprt/cpputils.h>
     29
    2930#include <VBox/err.h>
    3031#include <VBox/param.h>
  • trunk/src/VBox/Main/FloppyDriveImpl.cpp

    r2981 r3330  
    2222#include "FloppyDriveImpl.h"
    2323#include "MachineImpl.h"
     24#include "VirtualBoxImpl.h"
    2425#include "Logging.h"
    2526
    2627#include <iprt/string.h>
    27 
    28 // defines
    29 /////////////////////////////////////////////////////////////////////////////
     28#include <iprt/cpputils.h>
    3029
    3130// constructor / destructor
    3231/////////////////////////////////////////////////////////////////////////////
    3332
     33DEFINE_EMPTY_CTOR_DTOR (FloppyDrive)
     34
    3435HRESULT FloppyDrive::FinalConstruct()
    3536{
     
    3940void FloppyDrive::FinalRelease()
    4041{
    41     if (isReady())
    42         uninit ();
     42    uninit();
    4343}
    4444
     
    4949 * Initializes the Floppy drive object.
    5050 *
    51  * @returns COM result indicator
    52  * @param parent handle of our parent object
    53  */
    54 HRESULT FloppyDrive::init (Machine *parent)
    55 {
    56     LogFlowMember (("FloppyDrive::init (%p)\n", parent));
    57 
    58     ComAssertRet (parent, E_INVALIDARG);
    59 
    60     AutoLock alock (this);
    61     ComAssertRet (!isReady(), E_UNEXPECTED);
    62 
    63     mParent = parent;
    64     // mPeer is left null
     51 * @param   aParent  Handle of our parent object.
     52 * @return  COM result indicator
     53 */
     54HRESULT FloppyDrive::init (Machine *aParent)
     55{
     56    LogFlowThisFunc (("aParent=%p\n", aParent));
     57
     58    ComAssertRet (aParent, E_INVALIDARG);
     59
     60    /* Enclose the state transition NotReady->InInit->Ready */
     61    AutoInitSpan autoInitSpan (this);
     62    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     63
     64    unconst (mParent) = aParent;
     65    /* mPeer is left null */
    6566
    6667    mData.allocate();
    6768
    68     setReady (true);
     69    /* Confirm a successful initialization */
     70    autoInitSpan.setSucceeded();
     71
    6972    return S_OK;
    7073}
     
    7780 *  @note This object must be destroyed before the original object
    7881 *  it shares data with is destroyed.
    79  */
    80 HRESULT FloppyDrive::init (Machine *parent, FloppyDrive *that)
    81 {
    82     LogFlowMember (("FloppyDrive::init (%p, %p)\n", parent, that));
    83 
    84     ComAssertRet (parent && that, E_INVALIDARG);
    85 
    86     AutoLock alock (this);
    87     ComAssertRet (!isReady(), E_UNEXPECTED);
    88 
    89     mParent = parent;
    90     mPeer = that;
    91 
    92     AutoLock thatlock (that);
    93     mData.share (that->mData);
    94 
    95     setReady (true);
     82 *
     83 *  @note Locks @a aThat object for reading.
     84 */
     85HRESULT FloppyDrive::init (Machine *aParent, FloppyDrive *aThat)
     86{
     87    LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
     88
     89    ComAssertRet (aParent && aThat, E_INVALIDARG);
     90
     91    /* Enclose the state transition NotReady->InInit->Ready */
     92    AutoInitSpan autoInitSpan (this);
     93    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     94
     95    unconst (mParent) = aParent;
     96    unconst (mPeer) = aThat;
     97
     98    AutoCaller thatCaller (aThat);
     99    AssertComRCReturnRC (thatCaller.rc());
     100
     101    AutoReaderLock thatLock (aThat);
     102    mData.share (aThat->mData);
     103
     104    /* Confirm a successful initialization */
     105    autoInitSpan.setSucceeded();
     106
    96107    return S_OK;
    97108}
     
    101112 *  (a kind of copy constructor). This object makes a private copy of data
    102113 *  of the original object passed as an argument.
    103  */
    104 HRESULT FloppyDrive::initCopy (Machine *parent, FloppyDrive *that)
    105 {
    106     LogFlowMember (("FloppyDrive::initCopy (%p, %p)\n", parent, that));
    107 
    108     ComAssertRet (parent && that, E_INVALIDARG);
    109 
    110     AutoLock alock (this);
    111     ComAssertRet (!isReady(), E_UNEXPECTED);
    112 
    113     mParent = parent;
    114     // mPeer is left null
    115 
    116     AutoLock thatlock (that);
    117     mData.attachCopy (that->mData);
    118 
    119     setReady (true);
     114 *
     115 *  @note Locks @a aThat object for reading.
     116 */
     117HRESULT FloppyDrive::initCopy (Machine *aParent, FloppyDrive *aThat)
     118{
     119    LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
     120
     121    ComAssertRet (aParent && aThat, E_INVALIDARG);
     122
     123    /* Enclose the state transition NotReady->InInit->Ready */
     124    AutoInitSpan autoInitSpan (this);
     125    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     126
     127    unconst (mParent) = aParent;
     128    /* mPeer is left null */
     129
     130    AutoCaller thatCaller (aThat);
     131    AssertComRCReturnRC (thatCaller.rc());
     132
     133    AutoReaderLock thatLock (aThat);
     134    mData.attachCopy (aThat->mData);
     135
     136    /* Confirm a successful initialization */
     137    autoInitSpan.setSucceeded();
     138
    120139    return S_OK;
    121140}
     
    127146void FloppyDrive::uninit()
    128147{
    129     LogFlowMember (("FloppyDrive::uninit()\n"));
     148    LogFlowThisFunc (("\n"));
     149
     150    /* Enclose the state transition Ready->InUninit->NotReady */
     151    AutoUninitSpan autoUninitSpan (this);
     152    if (autoUninitSpan.uninitDone())
     153        return;
     154
     155    mData.free();
     156
     157    unconst (mParent).setNull();
     158    unconst (mParent).setNull();
     159}
     160
     161// IFloppyDrive properties
     162/////////////////////////////////////////////////////////////////////////////
     163
     164STDMETHODIMP FloppyDrive::COMGETTER(Enabled) (BOOL *aEnabled)
     165{
     166    if (!aEnabled)
     167        return E_POINTER;
     168
     169    AutoCaller autoCaller (this);
     170    CheckComRCReturnRC (autoCaller.rc());
     171
     172    AutoReaderLock alock (this);
     173
     174    *aEnabled = mData->mEnabled;
     175
     176    return S_OK;
     177}
     178
     179STDMETHODIMP FloppyDrive::COMSETTER(Enabled) (BOOL aEnabled)
     180{
     181    LogFlowThisFunc (("aEnabled=%RTbool\n", aEnabled));
     182
     183    AutoCaller autoCaller (this);
     184    CheckComRCReturnRC (autoCaller.rc());
     185
     186    /* the machine needs to be mutable */
     187    Machine::AutoMutableStateDependency adep (mParent);
     188    CheckComRCReturnRC (adep.rc());
    130189
    131190    AutoLock alock (this);
    132     AssertReturn (isReady(), (void) 0);
    133 
    134     mData.free();
    135 
    136     mPeer.setNull();
    137     mParent.setNull();
    138 
    139     setReady (false);
    140 }
    141 
    142 // IFloppyDrive properties
    143 /////////////////////////////////////////////////////////////////////////////
    144 
    145 STDMETHODIMP FloppyDrive::COMGETTER(Enabled) (BOOL *enabled)
    146 {
    147     if (!enabled)
     191
     192    if (mData->mEnabled != aEnabled)
     193    {
     194        mData.backup();
     195        mData->mEnabled = aEnabled;
     196
     197        /* leave the lock before informing callbacks*/
     198        alock.unlock();
     199
     200        mParent->onFloppyDriveChange();
     201    }
     202
     203    return S_OK;
     204}
     205
     206STDMETHODIMP FloppyDrive::COMGETTER(State) (DriveState_T *aDriveState)
     207{
     208    if (!aDriveState)
    148209        return E_POINTER;
    149210
    150     AutoLock alock(this);
    151     CHECK_READY();
    152 
    153     *enabled = mData->mEnabled;
    154     return S_OK;
    155 }
    156 
    157 STDMETHODIMP FloppyDrive::COMSETTER(Enabled) (BOOL enabled)
    158 {
    159     LogFlowMember(("FloppyDrive::SetEnabled: %s\n", enabled ? "enable" : "disable"));
    160 
    161     AutoLock alock(this);
    162     CHECK_READY();
    163 
    164     CHECK_MACHINE_MUTABILITY (mParent);
    165 
    166     if (mData->mEnabled != enabled)
    167     {
    168         mData.backup();
    169         mData->mEnabled = enabled;
    170 
    171         alock.unlock();
    172         mParent->onFloppyDriveChange();
    173     }
    174 
    175     return S_OK;
    176 }
    177 
    178 STDMETHODIMP FloppyDrive::COMGETTER(State) (DriveState_T *driveState)
    179 {
    180     if (!driveState)
    181         return E_POINTER;
     211    AutoCaller autoCaller (this);
     212    CheckComRCReturnRC (autoCaller.rc());
     213
     214    AutoReaderLock alock (this);
     215
     216    *aDriveState = mData->mDriveState;
     217
     218    return S_OK;
     219}
     220
     221// IFloppyDrive methods
     222/////////////////////////////////////////////////////////////////////////////
     223
     224STDMETHODIMP FloppyDrive::MountImage (INPTR GUIDPARAM aImageId)
     225{
     226    if (Guid::isEmpty (aImageId))
     227        return E_INVALIDARG;
     228
     229    AutoCaller autoCaller (this);
     230    CheckComRCReturnRC (autoCaller.rc());
     231
     232    /* the machine needs to be mutable */
     233    Machine::AutoMutableStateDependency adep (mParent);
     234    CheckComRCReturnRC (adep.rc());
    182235
    183236    AutoLock alock (this);
    184     CHECK_READY();
    185 
    186     *driveState = mData->mDriveState;
    187     return S_OK;
    188 }
    189 
    190 // IFloppyDrive methods
    191 /////////////////////////////////////////////////////////////////////////////
    192 
    193 STDMETHODIMP FloppyDrive::MountImage(INPTR GUIDPARAM imageId)
    194 {
    195     if (Guid::isEmpty(imageId))
    196         return E_INVALIDARG;
    197 
    198     AutoLock alock (this);
    199     CHECK_READY();
    200 
    201     CHECK_MACHINE_MUTABILITY (mParent);
    202237
    203238    HRESULT rc = E_FAIL;
    204239
    205     // find the image in the collection
    206     ComPtr <IVirtualBox> vbox;
    207     rc = mParent->COMGETTER(Parent) (vbox.asOutParam());
    208     AssertComRCReturn (rc, rc);
     240    /* Our lifetime is bound to mParent's lifetime, so we don't add caller.
     241     * We also don't lock mParent since its mParent field is const. */
    209242
    210243    ComPtr <IFloppyImage> image;
    211     rc = vbox->GetFloppyImage (imageId, image.asOutParam());
     244    rc = mParent->virtualBox()->GetFloppyImage (aImageId, image.asOutParam());
     245
    212246    if (SUCCEEDED (rc))
    213247    {
     
    216250        {
    217251            mData.backup();
     252
    218253            unmount();
     254
    219255            mData->mFloppyImage = image;
    220256            mData->mDriveState = DriveState_ImageMounted;
    221257
     258            /* leave the lock before informing callbacks*/
    222259            alock.unlock();
     260
    223261            mParent->onFloppyDriveChange();
    224262        }
     
    228266}
    229267
    230 STDMETHODIMP FloppyDrive::CaptureHostDrive(IHostFloppyDrive *hostFloppyDrive)
    231 {
    232     if (!hostFloppyDrive)
     268STDMETHODIMP FloppyDrive::CaptureHostDrive (IHostFloppyDrive *aHostFloppyDrive)
     269{
     270    if (!aHostFloppyDrive)
    233271        return E_INVALIDARG;
    234272
     273    AutoCaller autoCaller (this);
     274    CheckComRCReturnRC (autoCaller.rc());
     275
     276    /* the machine needs to be mutable */
     277    Machine::AutoMutableStateDependency adep (mParent);
     278    CheckComRCReturnRC (adep.rc());
     279
    235280    AutoLock alock (this);
    236     CHECK_READY();
    237 
    238     CHECK_MACHINE_MUTABILITY (mParent);
    239281
    240282    if (mData->mDriveState != DriveState_HostDriveCaptured ||
    241         !mData->mHostDrive.equalsTo (hostFloppyDrive))
     283        !mData->mHostDrive.equalsTo (aHostFloppyDrive))
    242284    {
    243285        mData.backup();
     286
    244287        unmount();
    245         mData->mHostDrive = hostFloppyDrive;
     288
     289        mData->mHostDrive = aHostFloppyDrive;
    246290        mData->mDriveState = DriveState_HostDriveCaptured;
    247291
     292        /* leave the lock before informing callbacks*/
    248293        alock.unlock();
     294
    249295        mParent->onFloppyDriveChange();
    250296    }
     
    255301STDMETHODIMP FloppyDrive::Unmount()
    256302{
     303    AutoCaller autoCaller (this);
     304    CheckComRCReturnRC (autoCaller.rc());
     305
     306    /* the machine needs to be mutable */
     307    Machine::AutoMutableStateDependency adep (mParent);
     308    CheckComRCReturnRC (adep.rc());
     309
    257310    AutoLock alock (this);
    258     CHECK_READY();
    259 
    260     CHECK_MACHINE_MUTABILITY (mParent);
    261311
    262312    if (mData->mDriveState != DriveState_NotMounted)
    263313    {
    264314        mData.backup();
     315
    265316        unmount();
     317
    266318        mData->mDriveState = DriveState_NotMounted;
    267319
     320        /* leave the lock before informing callbacks*/
    268321        alock.unlock();
     322
    269323        mParent->onFloppyDriveChange();
    270324    }
     
    273327}
    274328
    275 STDMETHODIMP FloppyDrive::GetImage(IFloppyImage **floppyImage)
    276 {
    277     if (!floppyImage)
     329STDMETHODIMP FloppyDrive::GetImage (IFloppyImage **aFloppyImage)
     330{
     331    if (!aFloppyImage)
    278332        return E_POINTER;
    279333
     334    AutoCaller autoCaller (this);
     335    CheckComRCReturnRC (autoCaller.rc());
     336
     337    AutoReaderLock alock (this);
     338
     339    mData->mFloppyImage.queryInterfaceTo (aFloppyImage);
     340
     341    return S_OK;
     342}
     343
     344STDMETHODIMP FloppyDrive::GetHostDrive (IHostFloppyDrive **aHostDrive)
     345{
     346    if (!aHostDrive)
     347        return E_POINTER;
     348
     349    AutoCaller autoCaller (this);
     350    CheckComRCReturnRC (autoCaller.rc());
     351
     352    AutoReaderLock alock (this);
     353
     354    mData->mHostDrive.queryInterfaceTo (aHostDrive);
     355
     356    return S_OK;
     357}
     358
     359// public methods only for internal purposes
     360/////////////////////////////////////////////////////////////////////////////
     361
     362/**
     363 *  @note Locks this object for writing.
     364 */
     365bool FloppyDrive::rollback()
     366{
     367    /* sanity */
     368    AutoCaller autoCaller (this);
     369    AssertComRCReturn (autoCaller.rc(), false);
     370
    280371    AutoLock alock (this);
    281     CHECK_READY();
    282 
    283     mData->mFloppyImage.queryInterfaceTo (floppyImage);
    284     return S_OK;
    285 }
    286 
    287 STDMETHODIMP FloppyDrive::GetHostDrive(IHostFloppyDrive **hostDrive)
    288 {
    289     if (!hostDrive)
    290         return E_POINTER;
    291 
    292     AutoLock alock (this);
    293     CHECK_READY();
    294 
    295     mData->mHostDrive.queryInterfaceTo (hostDrive);
    296     return S_OK;
    297 }
    298 
    299 // public methods only for internal purposes
    300 /////////////////////////////////////////////////////////////////////////////
    301 
    302 bool FloppyDrive::rollback()
    303 {
    304     AutoLock alock (this);
    305372
    306373    bool changed = false;
     
    308375    if (mData.isBackedUp())
    309376    {
    310         // we need to check all data to see whether anything will be changed
    311         // after rollback
     377        /* we need to check all data to see whether anything will be changed
     378         * after rollback */
    312379        changed = mData.hasActualChanges();
    313380        mData.rollback();
     
    317384}
    318385
     386/**
     387 *  @note Locks this object for writing, together with the peer object (also
     388 *  for writing) if there is one.
     389 */
    319390void FloppyDrive::commit()
    320391{
    321     AutoLock alock (this);
     392    /* sanity */
     393    AutoCaller autoCaller (this);
     394    AssertComRCReturnVoid (autoCaller.rc());
     395
     396    /* sanity too */
     397    AutoCaller thatCaller (mPeer);
     398    AssertComRCReturnVoid (thatCaller.rc());
     399
     400    /* lock both for writing since we modify both */
     401    AutoMultiLock <2> alock (this->wlock(), AutoLock::maybeWlock (mPeer));
     402
    322403    if (mData.isBackedUp())
    323404    {
     
    325406        if (mPeer)
    326407        {
    327             // attach new data to the peer and reshare it
    328             AutoLock peerlock (mPeer);
     408            /* attach new data to the peer and reshare it */
    329409            mPeer->mData.attach (mData);
    330410        }
     
    332412}
    333413
     414/**
     415 *  @note Locks this object for writing, together with the peer object (locked
     416 *  for reading) if there is one.
     417 */
    334418void FloppyDrive::copyFrom (FloppyDrive *aThat)
    335419{
    336     AutoLock alock (this);
    337 
    338     // this will back up current data
     420    /* sanity */
     421    AutoCaller autoCaller (this);
     422    AssertComRCReturnVoid (autoCaller.rc());
     423
     424    /* sanity too */
     425    AutoCaller thatCaller (mPeer);
     426    AssertComRCReturnVoid (thatCaller.rc());
     427
     428    /* peer is not modified, lock it for reading */
     429    AutoMultiLock <2> alock (this->wlock(), AutoLock::maybeRlock (mPeer));
     430
     431    /* this will back up current data */
    339432    mData.assignCopy (aThat->mData);
    340433}
     
    344437
    345438/**
    346  * Helper to unmount a drive
    347  *
    348  * @returns COM status code
    349  *
     439 *  Helper to unmount a drive.
     440 *
     441 *  @return COM status code
    350442 */
    351443HRESULT FloppyDrive::unmount()
    352444{
     445    AssertReturn (isLockedOnCurrentThread(), E_FAIL);
     446
    353447    if (mData->mFloppyImage)
    354448        mData->mFloppyImage.setNull();
    355449    if (mData->mHostDrive)
    356450        mData->mHostDrive.setNull();
    357     return S_OK;
    358 }
     451
     452    return S_OK;
     453}
  • trunk/src/VBox/Main/VirtualBoxImpl.cpp

    r3191 r3330  
    31643164 *  is returned.
    31653165 *
    3166  *  @param id
     3166 *  @param aId
    31673167 *      ID of the DVD image (unused when NULL)
    3168  *  @param filePathFull
     3168 *  @param aFilePathFull
    31693169 *      full path to the image file (unused when NULL)
    31703170 *  @param aSetError
    31713171 *      if TRUE, the appropriate error info is set in case when the image is not
    31723172 *      found and only one search criteria (ID or file name) is specified.
    3173  *  @param dvdImage
     3173 *  @param aImage
    31743174 *      where to store the found DVD image object (can be NULL)
    31753175 *
  • trunk/src/VBox/Main/include/DVDDriveImpl.h

    r2981 r3330  
    2828
    2929class ATL_NO_VTABLE DVDDrive :
     30    public VirtualBoxBaseNEXT,
    3031    public VirtualBoxSupportErrorInfoImpl <DVDDrive, IDVDDrive>,
    3132    public VirtualBoxSupportTranslation <DVDDrive>,
    32     public VirtualBoxBase,
    3333    public IDVDDrive
    3434{
     
    5656    };
    5757
     58    VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (DVDDrive)
     59
    5860    DECLARE_NOT_AGGREGATABLE(DVDDrive)
    5961
     
    6769    NS_DECL_ISUPPORTS
    6870
     71    DECLARE_EMPTY_CTOR_DTOR (DVDDrive)
     72
    6973    HRESULT FinalConstruct();
    7074    void FinalRelease();
    7175
    7276    // public initializer/uninitializer for internal purposes only
    73     HRESULT init (Machine *parent);
    74     HRESULT init (Machine *parent, DVDDrive *that);
    75     HRESULT initCopy (Machine *parent, DVDDrive *that);
     77    HRESULT init (Machine *aParent);
     78    HRESULT init (Machine *aParent, DVDDrive *aThat);
     79    HRESULT initCopy (Machine *aParent, DVDDrive *aThat);
    7680    void uninit();
    7781
    7882    // IDVDDrive properties
    79     STDMETHOD(COMGETTER(State)) (DriveState_T *driveState);
    80     STDMETHOD(COMGETTER(Passthrough)) (BOOL *passthrough);
    81     STDMETHOD(COMSETTER(Passthrough)) (BOOL passthrough);
     83    STDMETHOD(COMGETTER(State)) (DriveState_T *aDriveState);
     84    STDMETHOD(COMGETTER(Passthrough)) (BOOL *aPassthrough);
     85    STDMETHOD(COMSETTER(Passthrough)) (BOOL aPassthrough);
    8286
    8387    // IDVDDrive methods
    84     STDMETHOD(MountImage)(INPTR GUIDPARAM imageId);
    85     STDMETHOD(CaptureHostDrive)(IHostDVDDrive *hostDVDDrive);
     88    STDMETHOD(MountImage) (INPTR GUIDPARAM aImageId);
     89    STDMETHOD(CaptureHostDrive) (IHostDVDDrive *aHostDVDDrive);
    8690    STDMETHOD(Unmount)();
    87     STDMETHOD(GetImage)(IDVDImage **dvdImage);
    88     STDMETHOD(GetHostDrive)(IHostDVDDrive **hostDVDDrive);
     91    STDMETHOD(GetImage) (IDVDImage **aDVDImage);
     92    STDMETHOD(GetHostDrive) (IHostDVDDrive **aHostDVDDrive);
    8993
    9094    // public methods only for internal purposes
     
    9599    void commit();
    96100    void copyFrom (DVDDrive *aThat);
     101
     102    // public methods for internal purposes only
     103    // (ensure there is a caller and a read lock before calling them!)
    97104
    98105    Backupable <Data> &data() { return mData; }
     
    105112    HRESULT unmount();
    106113
    107     ComObjPtr <Machine, ComWeakRef> mParent;
    108     ComObjPtr <DVDDrive> mPeer;
     114    const ComObjPtr <Machine, ComWeakRef> mParent;
     115    const ComObjPtr <DVDDrive> mPeer;
     116
    109117    Backupable <Data> mData;
    110118};
  • trunk/src/VBox/Main/include/FloppyDriveImpl.h

    r2981 r3330  
    2828
    2929class ATL_NO_VTABLE FloppyDrive :
     30    public VirtualBoxBaseNEXT,
    3031    public VirtualBoxSupportErrorInfoImpl <FloppyDrive, IFloppyDrive>,
    3132    public VirtualBoxSupportTranslation <FloppyDrive>,
    32     public VirtualBoxBase,
    3333    public IFloppyDrive
    3434{
     
    5757    };
    5858
     59    VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (FloppyDrive)
     60
    5961    DECLARE_NOT_AGGREGATABLE(FloppyDrive)
    6062
     
    6870    NS_DECL_ISUPPORTS
    6971
     72    DECLARE_EMPTY_CTOR_DTOR (FloppyDrive)
     73
    7074    HRESULT FinalConstruct();
    7175    void FinalRelease();
    7276
    7377    // public initializer/uninitializer for internal purposes only
    74     HRESULT init (Machine *parent);
    75     HRESULT init (Machine *parent, FloppyDrive *that);
    76     HRESULT initCopy (Machine *parent, FloppyDrive *that);
     78    HRESULT init (Machine *aParent);
     79    HRESULT init (Machine *aParent, FloppyDrive *aThat);
     80    HRESULT initCopy (Machine *parent, FloppyDrive *aThat);
    7781    void uninit();
    7882
    7983    // IFloppyDrive properties
    80     STDMETHOD(COMGETTER(Enabled)) (BOOL *enabled);
    81     STDMETHOD(COMSETTER(Enabled)) (BOOL enabled);
    82     STDMETHOD(COMGETTER(State)) (DriveState_T *driveState);
     84    STDMETHOD(COMGETTER(Enabled)) (BOOL *aEnabled);
     85    STDMETHOD(COMSETTER(Enabled)) (BOOL aEnabled);
     86    STDMETHOD(COMGETTER(State)) (DriveState_T *aDriveState);
    8387
    8488    // IFloppyDrive methods
    85     STDMETHOD(MountImage)(INPTR GUIDPARAM imageId);
    86     STDMETHOD(CaptureHostDrive)(IHostFloppyDrive *hostFloppyDrive);
     89    STDMETHOD(MountImage) (INPTR GUIDPARAM aImageId);
     90    STDMETHOD(CaptureHostDrive) (IHostFloppyDrive *aHostFloppyDrive);
    8791    STDMETHOD(Unmount)();
    88     STDMETHOD(GetImage)(IFloppyImage **floppyImage);
    89     STDMETHOD(GetHostDrive)(IHostFloppyDrive **hostFloppyDrive);
     92    STDMETHOD(GetImage) (IFloppyImage **aFloppyImage);
     93    STDMETHOD(GetHostDrive) (IHostFloppyDrive **aHostFloppyDrive);
    9094
    9195    // public methods only for internal purposes
     
    96100    void commit();
    97101    void copyFrom (FloppyDrive *aThat);
     102
     103    // public methods for internal purposes only
     104    // (ensure there is a caller and a read lock before calling them!)
    98105
    99106    Backupable <Data> &data() { return mData; }
     
    106113    HRESULT unmount();
    107114
    108     ComObjPtr <Machine, ComWeakRef> mParent;
    109     ComObjPtr <FloppyDrive> mPeer;
     115    const ComObjPtr <Machine, ComWeakRef> mParent;
     116    const ComObjPtr <FloppyDrive> mPeer;
     117
    110118    Backupable <Data> mData;
    111119};
Note: See TracChangeset for help on using the changeset viewer.

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