VirtualBox

Changeset 2845 in vbox for trunk/src/VBox


Ignore:
Timestamp:
May 24, 2007 4:16:41 PM (18 years ago)
Author:
vboxsync
Message:

Main: Switched USBController to the new locking model.

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

Legend:

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

    r2805 r2845  
    4343/////////////////////////////////////////////////////////////////////////////
    4444
     45DEFINE_EMPTY_CTOR_DTOR (USBController)
     46
    4547HRESULT USBController::FinalConstruct()
    4648{
     
    5052void USBController::FinalRelease()
    5153{
    52     if (isReady())
    53         uninit();
     54    uninit();
    5455}
    5556
     
    6162 *
    6263 * @returns COM result indicator.
    63  * @param a_pParent     Pointer to our parent object.
    64  */
    65 HRESULT USBController::init(Machine *a_pParent)
    66 {
    67     LogFlowMember(("USBController::init (%p)\n", a_pParent));
    68 
    69     ComAssertRet (a_pParent, E_INVALIDARG);
    70 
    71     AutoLock alock(this);
    72     ComAssertRet (!isReady(), E_UNEXPECTED);
    73 
    74     m_Parent = a_pParent;
    75     // m_Peer is left null
    76 
    77     m_Data.allocate();
    78     m_DeviceFilters.allocate();
    79 
    80     setReady(true);
     64 * @param aParent       Pointer to our parent object.
     65 */
     66HRESULT USBController::init (Machine *aParent)
     67{
     68    LogFlowThisFunc (("aParent=%p\n", aParent));
     69
     70    ComAssertRet (aParent, E_INVALIDARG);
     71
     72    /* Enclose the state transition NotReady->InInit->Ready */
     73    AutoInitSpan autoInitSpan (this);
     74    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     75
     76    unconst (mParent) = aParent;
     77    /* mPeer is left null */
     78
     79    mData.allocate();
     80    mDeviceFilters.allocate();
     81
     82    /* Confirm a successful initialization */
     83    autoInitSpan.setSucceeded();
     84
    8185    return S_OK;
    8286}
     
    8993 *
    9094 * @returns COM result indicator.
    91  * @param a_pParent     Pointer to our parent object.
    92  * @param a_pPeer       The object to share.
    93  *
    94  * @remark This object must be destroyed before the original object
     95 * @param aParent       Pointer to our parent object.
     96 * @param aPeer         The object to share.
     97 *
     98 * @note This object must be destroyed before the original object
    9599 * it shares data with is destroyed.
    96100 */
    97 HRESULT USBController::init(Machine *a_pParent, USBController *a_pPeer)
    98 {
    99     LogFlowMember(("USBController::init (%p, %p)\n", a_pParent, a_pPeer));
    100 
    101     ComAssertRet (a_pParent && a_pPeer, E_INVALIDARG);
    102 
    103     AutoLock alock(this);
    104     ComAssertRet (!isReady(), E_UNEXPECTED);
    105 
    106     m_Parent = a_pParent;
    107     m_Peer = a_pPeer;
    108 
    109     AutoLock thatlock(a_pPeer);
    110     m_Data.share(a_pPeer->m_Data);
    111 
    112     // create copies of all filters
    113     m_DeviceFilters.allocate();
    114     DeviceFilterList::const_iterator it = a_pPeer->m_DeviceFilters->begin();
    115     while (it != a_pPeer->m_DeviceFilters->end())
     101HRESULT USBController::init (Machine *aParent, USBController *aPeer)
     102{
     103    LogFlowThisFunc (("aParent=%p, aPeer=%p\n", aParent, aPeer));
     104
     105    ComAssertRet (aParent && aPeer, E_INVALIDARG);
     106
     107    /* Enclose the state transition NotReady->InInit->Ready */
     108    AutoInitSpan autoInitSpan (this);
     109    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     110
     111    unconst (mParent) = aParent;
     112    unconst (mPeer) = aPeer;
     113
     114    AutoLock thatlock (aPeer);
     115    mData.share (aPeer->mData);
     116
     117    /* create copies of all filters */
     118    mDeviceFilters.allocate();
     119    DeviceFilterList::const_iterator it = aPeer->mDeviceFilters->begin();
     120    while (it != aPeer->mDeviceFilters->end())
    116121    {
    117122        ComObjPtr <USBDeviceFilter> filter;
    118123        filter.createObject();
    119124        filter->init (this, *it);
    120         m_DeviceFilters->push_back (filter);
     125        mDeviceFilters->push_back (filter);
    121126        ++ it;
    122127    }
    123128
    124     setReady(true);
     129    /* Confirm a successful initialization */
     130    autoInitSpan.setSucceeded();
     131
    125132    return S_OK;
    126133}
     
    132139 *  of the original object passed as an argument.
    133140 */
    134 HRESULT USBController::initCopy (Machine *a_pParent, USBController *a_pPeer)
    135 {
    136     LogFlowMember(("USBController::initCopy (%p, %p)\n", a_pParent, a_pPeer));
    137 
    138     ComAssertRet (a_pParent && a_pPeer, E_INVALIDARG);
    139 
    140     AutoLock alock(this);
    141     ComAssertRet (!isReady(), E_UNEXPECTED);
    142 
    143     m_Parent = a_pParent;
    144     // m_Peer is left null
    145 
    146     AutoLock thatlock(a_pPeer);
    147     m_Data.attachCopy (a_pPeer->m_Data);
    148 
    149     // create private copies of all filters
    150     m_DeviceFilters.allocate();
    151     DeviceFilterList::const_iterator it = a_pPeer->m_DeviceFilters->begin();
    152     while (it != a_pPeer->m_DeviceFilters->end())
     141HRESULT USBController::initCopy (Machine *aParent, USBController *aPeer)
     142{
     143    LogFlowThisFunc (("aParent=%p, aPeer=%p\n", aParent, aPeer));
     144
     145    ComAssertRet (aParent && aPeer, E_INVALIDARG);
     146
     147    /* Enclose the state transition NotReady->InInit->Ready */
     148    AutoInitSpan autoInitSpan (this);
     149    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     150
     151    unconst (mParent) = aParent;
     152    /* mPeer is left null */
     153
     154    AutoLock thatlock (aPeer);
     155    mData.attachCopy (aPeer->mData);
     156
     157    /* create private copies of all filters */
     158    mDeviceFilters.allocate();
     159    DeviceFilterList::const_iterator it = aPeer->mDeviceFilters->begin();
     160    while (it != aPeer->mDeviceFilters->end())
    153161    {
    154162        ComObjPtr <USBDeviceFilter> filter;
    155163        filter.createObject();
    156164        filter->initCopy (this, *it);
    157         m_DeviceFilters->push_back (filter);
     165        mDeviceFilters->push_back (filter);
    158166        ++ it;
    159167    }
    160168
    161     setReady(true);
     169    /* Confirm a successful initialization */
     170    autoInitSpan.setSucceeded();
     171
    162172    return S_OK;
    163173}
     
    170180void USBController::uninit()
    171181{
    172     LogFlowMember(("USBController::uninit()\n"));
    173 
    174     AutoLock alock(this);
    175     AssertReturn (isReady(), (void) 0);
    176 
    177     // uninit all filters (including those still referenced by clients)
     182    LogFlowThisFunc (("\n"));
     183
     184    /* Enclose the state transition Ready->InUninit->NotReady */
     185    AutoUninitSpan autoUninitSpan (this);
     186    if (autoUninitSpan.uninitDone())
     187        return;
     188
     189    /* uninit all filters (including those still referenced by clients) */
    178190    uninitDependentChildren();
    179191
    180     m_DeviceFilters.free();
    181     m_Data.free();
    182 
    183     m_Peer.setNull();
    184     m_Parent.setNull();
    185 
    186     setReady(false);
    187 }
    188 
     192    mDeviceFilters.free();
     193    mData.free();
     194
     195    unconst (mPeer).setNull();
     196    unconst (mParent).setNull();
     197}
    189198
    190199
     
    192201/////////////////////////////////////////////////////////////////////////////
    193202
    194 STDMETHODIMP USBController::COMGETTER(Enabled)(BOOL *a_pfEnabled)
    195 {
    196     if (!a_pfEnabled)
     203STDMETHODIMP USBController::COMGETTER(Enabled) (BOOL *aEnabled)
     204{
     205    if (!aEnabled)
    197206        return E_POINTER;
    198207
    199     AutoLock alock(this);
    200     CHECK_READY();
    201 
    202     *a_pfEnabled = m_Data->m_fEnabled;
    203     LogFlowMember(("USBController::GetEnabled: returns %p\n", *a_pfEnabled));
    204     return S_OK;
    205 }
    206 
    207 
    208 STDMETHODIMP USBController::COMSETTER(Enabled)(BOOL a_fEnabled)
    209 {
    210     LogFlowMember(("USBController::SetEnabled: %s\n", a_fEnabled ? "enable" : "disable"));
    211 
    212     AutoLock alock(this);
    213     CHECK_READY();
    214 
    215     CHECK_MACHINE_MUTABILITY (m_Parent);
    216 
    217     if (m_Data->m_fEnabled != a_fEnabled)
    218     {
    219         m_Data.backup();
    220         m_Data->m_fEnabled = a_fEnabled;
    221 
    222         alock.unlock();
    223         m_Parent->onUSBControllerChange();
    224     }
    225 
    226     return S_OK;
    227 }
    228 
    229 /**
    230  * Get the USB standard version.
    231  *
    232  * @param   a_pusUSBStandard    Where to store the BCD version word.
    233  */
    234 STDMETHODIMP USBController::COMGETTER(USBStandard)(USHORT *a_pusUSBStandard)
    235 {
    236     if (!a_pusUSBStandard)
     208    AutoCaller autoCaller (this);
     209    CheckComRCReturnRC (autoCaller.rc());
     210
     211    AutoReaderLock alock (this);
     212
     213    *aEnabled = mData->mEnabled;
     214
     215    LogFlowThisFunc (("returns %RTbool\n", *aEnabled));
     216
     217    return S_OK;
     218}
     219
     220
     221STDMETHODIMP USBController::COMSETTER(Enabled) (BOOL aEnabled)
     222{
     223    LogFlowThisFunc (("aEnabled=%RTbool\n", aEnabled));
     224
     225    AutoCaller autoCaller (this);
     226    CheckComRCReturnRC (autoCaller.rc());
     227
     228    AutoLock alock (this);
     229
     230    CHECK_MACHINE_MUTABILITY (mParent);
     231
     232    if (mData->mEnabled != aEnabled)
     233    {
     234        mData.backup();
     235        mData->mEnabled = aEnabled;
     236
     237        /* leave the lock for safety */
     238        alock.leave();
     239
     240        mParent->onUSBControllerChange();
     241    }
     242
     243    return S_OK;
     244}
     245
     246STDMETHODIMP USBController::COMGETTER(USBStandard) (USHORT *aUSBStandard)
     247{
     248    if (!aUSBStandard)
    237249        return E_POINTER;
    238250
    239     AutoLock alock(this);
    240     CHECK_READY();
    241 
    242     *a_pusUSBStandard = 0x0101;
    243     LogFlowMember(("USBController::GetUSBStandard: returns 0x0101\n"));
     251    AutoCaller autoCaller (this);
     252    CheckComRCReturnRC (autoCaller.rc());
     253
     254    /* not accessing data -- no need to lock */
     255
     256    *aUSBStandard = 0x0101;
     257
     258    LogFlowThisFunc (("returns %04hX\n", *aUSBStandard));
     259
    244260    return S_OK;
    245261}
     
    250266        return E_POINTER;
    251267
    252     AutoLock lock(this);
    253     CHECK_READY();
     268    AutoCaller autoCaller (this);
     269    CheckComRCReturnRC (autoCaller.rc());
     270
     271    AutoReaderLock alock (this);
    254272
    255273    ComObjPtr <USBDeviceFilterCollection> collection;
    256274    collection.createObject();
    257     collection->init (*m_DeviceFilters.data());
     275    collection->init (*mDeviceFilters.data());
    258276    collection.queryInterfaceTo (aDevicesFilters);
     277
    259278    return S_OK;
    260279}
     
    272291        return E_INVALIDARG;
    273292
    274     AutoLock lock (this);
    275     CHECK_READY();
    276 
    277     CHECK_MACHINE_MUTABILITY (m_Parent);
     293    AutoCaller autoCaller (this);
     294    CheckComRCReturnRC (autoCaller.rc());
     295
     296    AutoLock alock (this);
     297
     298    CHECK_MACHINE_MUTABILITY (mParent);
    278299
    279300    ComObjPtr <USBDeviceFilter> filter;
    280301    filter.createObject();
    281302    HRESULT rc = filter->init (this, aName);
    282     ComAssertComRCRet (rc, rc);
     303    ComAssertComRCRetRC (rc);
    283304    rc = filter.queryInterfaceTo (aFilter);
    284     AssertComRCReturn (rc, rc);
     305    AssertComRCReturnRC (rc);
     306
    285307    return S_OK;
    286308}
     
    292314        return E_INVALIDARG;
    293315
    294     AutoLock lock (this);
    295     CHECK_READY();
     316    AutoCaller autoCaller (this);
     317    CheckComRCReturnRC (autoCaller.rc());
    296318
    297319    /* the machine needs to be mutable */
    298     Machine::AutoStateDependency <Machine::MutableStateDep> adep (m_Parent);
     320    Machine::AutoStateDependency <Machine::MutableStateDep> adep (mParent);
    299321    CheckComRCReturnRC (adep.rc());
     322
     323    AutoLock alock (this);
    300324
    301325    ComObjPtr <USBDeviceFilter> filter = getDependentChild (aFilter);
     
    310334
    311335    /* backup the list before modification */
    312     m_DeviceFilters.backup();
     336    mDeviceFilters.backup();
    313337
    314338    /* iterate to the position... */
    315339    DeviceFilterList::iterator it;
    316     if (aPosition < m_DeviceFilters->size())
    317     {
    318         it = m_DeviceFilters->begin();
     340    if (aPosition < mDeviceFilters->size())
     341    {
     342        it = mDeviceFilters->begin();
    319343        std::advance (it, aPosition);
    320344    }
    321345    else
    322         it = m_DeviceFilters->end();
     346        it = mDeviceFilters->end();
    323347    /* ...and insert */
    324     m_DeviceFilters->insert (it, filter);
     348    mDeviceFilters->insert (it, filter);
    325349    filter->mInList = true;
    326350
     
    335359#endif
    336360    {
    337         USBProxyService *service = m_Parent->virtualBox()->host()->usbProxyService();
     361        USBProxyService *service = mParent->virtualBox()->host()->usbProxyService();
    338362        ComAssertRet (service, E_FAIL);
    339363
     
    351375        return E_POINTER;
    352376
    353     AutoLock lock (this);
    354     CHECK_READY();
     377    AutoCaller autoCaller (this);
     378    CheckComRCReturnRC (autoCaller.rc());
    355379
    356380    /* the machine needs to be mutable */
    357     Machine::AutoStateDependency <Machine::MutableStateDep> adep (m_Parent);
     381    Machine::AutoStateDependency <Machine::MutableStateDep> adep (mParent);
    358382    CheckComRCReturnRC (adep.rc());
    359383
    360     if (!m_DeviceFilters->size())
     384    AutoLock alock (this);
     385
     386    if (!mDeviceFilters->size())
    361387        return setError (E_INVALIDARG,
    362388            tr ("The USB device filter list is empty"));
    363389
    364     if (aPosition >= m_DeviceFilters->size())
     390    if (aPosition >= mDeviceFilters->size())
    365391        return setError (E_INVALIDARG,
    366392            tr ("Invalid position: %lu (must be in range [0, %lu])"),
    367             aPosition, m_DeviceFilters->size() - 1);
     393            aPosition, mDeviceFilters->size() - 1);
    368394
    369395    /* backup the list before modification */
    370     m_DeviceFilters.backup();
     396    mDeviceFilters.backup();
    371397
    372398    ComObjPtr <USBDeviceFilter> filter;
    373399    {
    374400        /* iterate to the position... */
    375         DeviceFilterList::iterator it = m_DeviceFilters->begin();
     401        DeviceFilterList::iterator it = mDeviceFilters->begin();
    376402        std::advance (it, aPosition);
    377403        /* ...get an element from there... */
     
    379405        /* ...and remove */
    380406        filter->mInList = false;
    381         m_DeviceFilters->erase (it);
     407        mDeviceFilters->erase (it);
    382408    }
    383409
     
    397423#endif
    398424    {
    399         USBProxyService *service = m_Parent->virtualBox()->host()->usbProxyService();
     425        USBProxyService *service = mParent->virtualBox()->host()->usbProxyService();
    400426        ComAssertRet (service, E_FAIL);
    401427
     
    411437/////////////////////////////////////////////////////////////////////////////
    412438
    413 /** Loads settings from the configuration node */
     439/**
     440 *  Loads settings from the configuration node.
     441 *
     442 *  @note Locks objects for reading!
     443 */
    414444HRESULT USBController::loadSettings (CFGNODE aMachine)
    415445{
    416     AutoLock lock (this);
    417     CHECK_READY();
    418 
    419446    ComAssertRet (aMachine, E_FAIL);
     447
     448    AutoCaller autoCaller (this);
     449    CheckComRCReturnRC (autoCaller.rc());
     450
     451    AutoLock alock (this);
    420452
    421453    CFGNODE controller = NULL;
     
    423455    Assert (controller);
    424456
    425     // enabled
     457    /* enabled */
    426458    bool enabled;
    427459    CFGLDRQueryBool (controller, "enabled", &enabled);
    428     m_Data->m_fEnabled = enabled;
     460    mData->mEnabled = enabled;
    429461
    430462    HRESULT rc = S_OK;
     
    466498                              manufacturer, product, serialNumber,
    467499                              port, remote);
    468         // error info is set by init() when appropriate
     500        /* error info is set by init() when appropriate */
    469501        if (SUCCEEDED (rc))
    470502        {
    471             m_DeviceFilters->push_back (filterObj);
     503            mDeviceFilters->push_back (filterObj);
    472504            filterObj->mInList = true;
    473505        }
     
    481513}
    482514
    483 /** Saves settings to the configuration node */
     515/**
     516 *  Saves settings to the configuration node.
     517 *
     518 *  @note Locks objects for reading!
     519 */
    484520HRESULT USBController::saveSettings (CFGNODE aMachine)
    485521{
    486     AutoLock lock (this);
    487     CHECK_READY();
    488 
    489522    ComAssertRet (aMachine, E_FAIL);
    490523
    491     // first, delete the entry
     524    AutoCaller autoCaller (this);
     525    CheckComRCReturnRC (autoCaller.rc());
     526
     527    AutoReaderLock alock (this);
     528
     529    /* first, delete the entry */
    492530    CFGNODE controller = NULL;
    493531    int vrc = CFGLDRGetChildNode (aMachine, "USBController", 0, &controller);
     
    497535        ComAssertRCRet (vrc, E_FAIL);
    498536    }
    499     // then, recreate it
     537    /* then, recreate it */
    500538    vrc = CFGLDRCreateChildNode (aMachine, "USBController", &controller);
    501539    ComAssertRCRet (vrc, E_FAIL);
    502540
    503     // enabled
    504     CFGLDRSetBool (controller, "enabled", !!m_Data->m_fEnabled);
    505 
    506     DeviceFilterList::const_iterator it = m_DeviceFilters->begin();
    507     while (it != m_DeviceFilters->end())
     541    /* enabled */
     542    CFGLDRSetBool (controller, "enabled", !!mData->mEnabled);
     543
     544    DeviceFilterList::const_iterator it = mDeviceFilters->begin();
     545    while (it != mDeviceFilters->end())
    508546    {
    509547        AutoLock filterLock (*it);
     
    516554        CFGLDRSetBool (filter, "active", !!data.mActive);
    517555
    518         // all are optional
     556        /* all are optional */
    519557        if (data.mVendorId.string())
    520558            CFGLDRSetBSTR (filter, "vendorid", data.mVendorId.string());
     
    544582}
    545583
     584/** @note Locks objects for reading! */
    546585bool USBController::isModified()
    547586{
    548     AutoLock alock(this);
    549 
    550     if (m_Data.isBackedUp() || m_DeviceFilters.isBackedUp())
     587    AutoCaller autoCaller (this);
     588    AssertComRCReturn (autoCaller.rc(), false);
     589
     590    AutoReaderLock alock (this);
     591
     592    if (mData.isBackedUp() || mDeviceFilters.isBackedUp())
    551593        return true;
    552594
    553595    /* see whether any of filters has changed its data */
    554596    for (DeviceFilterList::const_iterator
    555          it = m_DeviceFilters->begin();
    556          it != m_DeviceFilters->end();
     597         it = mDeviceFilters->begin();
     598         it != mDeviceFilters->end();
    557599         ++ it)
    558600    {
     
    564606}
    565607
     608/** @note Locks objects for reading! */
    566609bool USBController::isReallyModified()
    567610{
    568     AutoLock alock(this);
    569 
    570     if (m_Data.hasActualChanges())
     611    AutoCaller autoCaller (this);
     612    AssertComRCReturn (autoCaller.rc(), false);
     613
     614    AutoReaderLock alock (this);
     615
     616    if (mData.hasActualChanges())
    571617        return true;
    572618
    573     if (!m_DeviceFilters.isBackedUp())
     619    if (!mDeviceFilters.isBackedUp())
    574620    {
    575621        /* see whether any of filters has changed its data */
    576622        for (DeviceFilterList::const_iterator
    577              it = m_DeviceFilters->begin();
    578              it != m_DeviceFilters->end();
     623             it = mDeviceFilters->begin();
     624             it != mDeviceFilters->end();
    579625             ++ it)
    580626        {
     
    586632    }
    587633
    588     if (m_DeviceFilters->size() != m_DeviceFilters.backedUpData()->size())
     634    if (mDeviceFilters->size() != mDeviceFilters.backedUpData()->size())
    589635        return true;
    590636
    591     if (m_DeviceFilters->size() == 0)
     637    if (mDeviceFilters->size() == 0)
    592638        return false;
    593639
    594640    /* Make copies to speed up comparison */
    595     DeviceFilterList devices = *m_DeviceFilters.data();
    596     DeviceFilterList backDevices = *m_DeviceFilters.backedUpData();
     641    DeviceFilterList devices = *mDeviceFilters.data();
     642    DeviceFilterList backDevices = *mDeviceFilters.backedUpData();
    597643
    598644    DeviceFilterList::iterator it = devices.begin();
     
    623669}
    624670
     671/** @note Locks objects for writing! */
    625672bool USBController::rollback()
    626673{
     674    AutoCaller autoCaller (this);
     675    AssertComRCReturn (autoCaller.rc(), false);
     676
     677    /* we need the machine state */
     678    Machine::AutoStateDependency <Machine::MutableStateDep> adep (mParent);
     679    AssertComRCReturn (adep.rc(), false);
     680
    627681    AutoLock alock (this);
    628682
    629     /* we need the machine state */
    630     Machine::AutoStateDependency <Machine::MutableStateDep> adep (m_Parent);
    631     AssertComRCReturn (adep.rc(), false);
    632 
    633683    bool dataChanged = false;
    634684
    635     if (m_Data.isBackedUp())
     685    if (mData.isBackedUp())
    636686    {
    637687        /* we need to check all data to see whether anything will be changed
    638688         * after rollback */
    639         dataChanged = m_Data.hasActualChanges();
    640         m_Data.rollback();
    641     }
    642 
    643     if (m_DeviceFilters.isBackedUp())
    644     {
    645         USBProxyService *service = m_Parent->virtualBox()->host()->usbProxyService();
     689        dataChanged = mData.hasActualChanges();
     690        mData.rollback();
     691    }
     692
     693    if (mDeviceFilters.isBackedUp())
     694    {
     695        USBProxyService *service = mParent->virtualBox()->host()->usbProxyService();
    646696        ComAssertRet (service, false);
    647697
    648698        /* uninitialize all new filters (absent in the backed up list) */
    649         DeviceFilterList::const_iterator it = m_DeviceFilters->begin();
    650         DeviceFilterList *backedList = m_DeviceFilters.backedUpData();
    651         while (it != m_DeviceFilters->end())
     699        DeviceFilterList::const_iterator it = mDeviceFilters->begin();
     700        DeviceFilterList *backedList = mDeviceFilters.backedUpData();
     701        while (it != mDeviceFilters->end())
    652702        {
    653703            if (std::find (backedList->begin(), backedList->end(), *it) ==
     
    688738            while (it != backedList->end())
    689739            {
    690                 if (std::find (m_DeviceFilters->begin(), m_DeviceFilters->end(), *it) ==
    691                     m_DeviceFilters->end())
     740                if (std::find (mDeviceFilters->begin(), mDeviceFilters->end(), *it) ==
     741                    mDeviceFilters->end())
    692742                {
    693743                    /* notify the proxy (only when necessary) */
     
    705755
    706756        /* restore the list */
    707         m_DeviceFilters.rollback();
     757        mDeviceFilters.rollback();
    708758    }
    709759
     
    712762
    713763    /* rollback any changes to filters after restoring the list */
    714     DeviceFilterList::const_iterator it = m_DeviceFilters->begin();
    715     while (it != m_DeviceFilters->end())
     764    DeviceFilterList::const_iterator it = mDeviceFilters->begin();
     765    while (it != mDeviceFilters->end())
    716766    {
    717767        if ((*it)->isModified())
     
    727777}
    728778
     779/** @note Locks objects for writing! */
    729780void USBController::commit()
    730781{
     782    AutoCaller autoCaller (this);
     783    AssertComRCReturnVoid (autoCaller.rc());
     784
    731785    AutoLock alock (this);
    732786
    733     if (m_Data.isBackedUp())
    734     {
    735         m_Data.commit();
    736         if (m_Peer)
     787    if (mData.isBackedUp())
     788    {
     789        mData.commit();
     790        if (mPeer)
    737791        {
    738792            // attach new data to the peer and reshare it
    739             AutoLock peerlock (m_Peer);
    740             m_Peer->m_Data.attach (m_Data);
     793            AutoLock peerlock (mPeer);
     794            mPeer->mData.attach (mData);
    741795        }
    742796    }
     
    744798    bool commitFilters = false;
    745799
    746     if (m_DeviceFilters.isBackedUp())
    747     {
    748         m_DeviceFilters.commit();
     800    if (mDeviceFilters.isBackedUp())
     801    {
     802        mDeviceFilters.commit();
    749803
    750804        // apply changes to peer
    751         if (m_Peer)
    752         {
    753             AutoLock peerlock (m_Peer);
     805        if (mPeer)
     806        {
     807            AutoLock peerlock (mPeer);
    754808            // commit all changes to new filters (this will reshare data with
    755809            // peers for those who have peers)
    756810            DeviceFilterList *newList = new DeviceFilterList();
    757             DeviceFilterList::const_iterator it = m_DeviceFilters->begin();
    758             while (it != m_DeviceFilters->end())
     811            DeviceFilterList::const_iterator it = mDeviceFilters->begin();
     812            while (it != mDeviceFilters->end())
    759813            {
    760814                (*it)->commit();
     
    767821                    // create a peer owning data this filter share it with
    768822                    peer.createObject();
    769                     peer->init (m_Peer, *it, true /* aReshare */);
     823                    peer->init (mPeer, *it, true /* aReshare */);
    770824                }
    771825                else
    772826                {
    773827                    // remove peer from the old list
    774                     m_Peer->m_DeviceFilters->remove (peer);
     828                    mPeer->mDeviceFilters->remove (peer);
    775829                }
    776830                // and add it to the new list
     
    781835
    782836            // uninit old peer's filters that are left
    783             it = m_Peer->m_DeviceFilters->begin();
    784             while (it != m_Peer->m_DeviceFilters->end())
     837            it = mPeer->mDeviceFilters->begin();
     838            while (it != mPeer->mDeviceFilters->end())
    785839            {
    786840                (*it)->uninit();
     
    789843
    790844            // attach new list of filters to our peer
    791             m_Peer->m_DeviceFilters.attach (newList);
     845            mPeer->mDeviceFilters.attach (newList);
    792846        }
    793847        else
     
    807861    if (commitFilters)
    808862    {
    809         DeviceFilterList::const_iterator it = m_DeviceFilters->begin();
    810         while (it != m_DeviceFilters->end())
     863        DeviceFilterList::const_iterator it = mDeviceFilters->begin();
     864        while (it != mDeviceFilters->end())
    811865        {
    812866            (*it)->commit();
     
    816870}
    817871
     872/** @note Locks object for writing and that object for reading! */
    818873void USBController::copyFrom (USBController *aThat)
    819874{
    820     AutoLock alock (this);
    821     AutoLock thatLock (aThat);
    822 
    823     if (m_Parent->isRegistered())
    824     {
    825         // reuse onMachineRegistered to tell USB proxy to remove all current filters
     875    AutoCaller autoCaller (this);
     876    AssertComRCReturnVoid (autoCaller.rc());
     877
     878    AutoMultiLock <2> alock (this->wlock(), aThat->rlock());
     879
     880    if (mParent->isRegistered())
     881    {
     882        /* reuse onMachineRegistered to tell USB proxy to remove all current
     883           filters */
    826884        HRESULT rc = onMachineRegistered (FALSE);
    827885        AssertComRCReturn (rc, (void) 0);
    828886    }
    829887
    830     // this will back up current data
    831     m_Data.assignCopy (aThat->m_Data);
    832 
    833     // create private copies of all filters
    834     m_DeviceFilters.backup();
    835     m_DeviceFilters->clear();
    836     for (DeviceFilterList::const_iterator it = aThat->m_DeviceFilters->begin();
    837         it != aThat->m_DeviceFilters->end();
     888    /* this will back up current data */
     889    mData.assignCopy (aThat->mData);
     890
     891    /* create private copies of all filters */
     892    mDeviceFilters.backup();
     893    mDeviceFilters->clear();
     894    for (DeviceFilterList::const_iterator it = aThat->mDeviceFilters->begin();
     895        it != aThat->mDeviceFilters->end();
    838896        ++ it)
    839897    {
     
    841899        filter.createObject();
    842900        filter->initCopy (this, *it);
    843         m_DeviceFilters->push_back (filter);
    844     }
    845 
    846     if (m_Parent->isRegistered())
    847     {
    848         // reuse onMachineRegistered to tell USB proxy to insert all current filters
     901        mDeviceFilters->push_back (filter);
     902    }
     903
     904    if (mParent->isRegistered())
     905    {
     906        /* reuse onMachineRegistered to tell USB proxy to insert all current
     907           filters */
    849908        HRESULT rc = onMachineRegistered (TRUE);
    850909        AssertComRCReturn (rc, (void) 0);
     
    857916 *
    858917 *  @param aRegistered  new registered state of the machine
     918 *
     919 *  @note Locks nothing.
    859920 */
    860921HRESULT USBController::onMachineRegistered (BOOL aRegistered)
    861922{
    862     AutoLock alock (this);
    863     CHECK_READY();
     923    AutoCaller autoCaller (this);
     924    AssertComRCReturnRC (autoCaller.rc());
    864925
    865926    /// @todo After rewriting Win32 USB support, no more necessary;
     
    874935/**
    875936 *  Called by setter methods of all USB device filters.
     937 *
     938 *  @note Locks nothing.
    876939 */
    877940HRESULT USBController::onDeviceFilterChange (USBDeviceFilter *aFilter,
    878941                                             BOOL aActiveChanged /* = FALSE */)
    879942{
    880     AutoLock alock (this);
    881     CHECK_READY();
     943    AutoCaller autoCaller (this);
     944    AssertComRCReturnRC (autoCaller.rc());
    882945
    883946    /// @todo After rewriting Win32 USB support, no more necessary;
     
    886949#else
    887950    /* we need the machine state */
    888     Machine::AutoStateDependency <Machine::MutableStateDep> adep (m_Parent);
     951    Machine::AutoStateDependency <Machine::MutableStateDep> adep (mParent);
    889952    AssertComRCReturnRC (adep.rc());
    890953
     
    894957#endif
    895958
    896     if (aFilter->mInList && m_Parent->isRegistered())
    897     {
    898         USBProxyService *service = m_Parent->virtualBox()->host()->usbProxyService();
     959    /* we don't modify our data fields -- no need to lock */
     960
     961    if (aFilter->mInList && mParent->isRegistered())
     962    {
     963        USBProxyService *service = mParent->virtualBox()->host()->usbProxyService();
    899964        ComAssertRet (service, E_FAIL);
    900965
     
    9361001 *
    9371002 *  A HostUSBDevice specific version.
     1003 *
     1004 *  @note Locks this object for reading.
    9381005 */
    9391006bool USBController::hasMatchingFilter (ComObjPtr <HostUSBDevice> &aDevice)
    9401007{
    941     AutoLock alock (this);
    942     if (!isReady())
    943         return false;
     1008    AutoCaller autoCaller (this);
     1009    AssertComRCReturn (autoCaller.rc(), false);
     1010
     1011    AutoReaderLock alock (this);
    9441012
    9451013    bool match = false;
    9461014
    947     // apply self filters
    948     for (DeviceFilterList::const_iterator it = m_DeviceFilters->begin();
    949          !match && it != m_DeviceFilters->end();
     1015    /* apply self filters */
     1016    for (DeviceFilterList::const_iterator it = mDeviceFilters->begin();
     1017         !match && it != mDeviceFilters->end();
    9501018         ++ it)
    9511019    {
     
    9661034 *      This method MUST correlate with HostUSBDevice::isMatch()
    9671035 *      in the sense of the device matching logic.
     1036 *
     1037 *  @note Locks this object for reading.
    9681038 */
    9691039bool USBController::hasMatchingFilter (IUSBDevice *aUSBDevice)
    9701040{
    971     LogFlowThisFunc (("\n"));
    972 
    973     AutoLock alock (this);
    974     if (!isReady())
    975         return false;
     1041    LogFlowThisFuncEnter();
     1042
     1043    AutoCaller autoCaller (this);
     1044    AssertComRCReturn (autoCaller.rc(), false);
     1045
     1046    AutoReaderLock alock (this);
    9761047
    9771048    HRESULT rc = S_OK;
    9781049
    979     // query fields
     1050    /* query fields */
    9801051
    9811052    USHORT vendorId = 0;
     
    10201091    bool match = false;
    10211092
    1022     // apply self filters
    1023     for (DeviceFilterList::const_iterator it = m_DeviceFilters->begin();
    1024          it != m_DeviceFilters->end();
     1093    /* apply self filters */
     1094    for (DeviceFilterList::const_iterator it = mDeviceFilters->begin();
     1095         it != mDeviceFilters->end();
    10251096         ++ it)
    10261097    {
     
    10381109
    10391110#if !defined (__WIN__)
    1040         // these filters are temporarily ignored on Win32
     1111        /* these filters are temporarily ignored on Win32 */
    10411112        if (!aData.mManufacturer.isMatch (manufacturer))
    10421113            continue;
     
    10561127    }
    10571128
    1058     LogFlowMember (("USBController::hasMatchingFilter() returns: %d\n", match));
     1129    LogFlowThisFunc (("returns: %d\n", match));
     1130    LogFlowThisFuncLeave();
     1131
    10591132    return match;
    10601133}
    10611134
     1135/**
     1136 *  Notifies the proxy service about all filters as requested by the
     1137 *  @a aInsertFilters argument.
     1138 *
     1139 *  @param aInsertFilters   @c true to insert filters, @c false to remove.
     1140 *
     1141 *  @note Locks this object for reading.
     1142 */
    10621143HRESULT USBController::notifyProxy (bool aInsertFilters)
    10631144{
    10641145    LogFlowThisFunc (("aInsertFilters=%RTbool\n", aInsertFilters));
    10651146
    1066     AutoLock alock (this);
    1067     if (!isReady())
    1068         return false;
    1069 
    1070     USBProxyService *service = m_Parent->virtualBox()->host()->usbProxyService();
     1147    AutoCaller autoCaller (this);
     1148    AssertComRCReturn (autoCaller.rc(), false);
     1149
     1150    AutoReaderLock alock (this);
     1151
     1152    USBProxyService *service = mParent->virtualBox()->host()->usbProxyService();
    10711153    AssertReturn (service, E_FAIL);
    10721154
    1073     DeviceFilterList::const_iterator it = m_DeviceFilters->begin();
    1074     while (it != m_DeviceFilters->end())
     1155    DeviceFilterList::const_iterator it = mDeviceFilters->begin();
     1156    while (it != mDeviceFilters->end())
    10751157    {
    10761158        USBDeviceFilter *flt = *it; /* resolve ambiguity (for ComPtr below) */
  • trunk/src/VBox/Main/include/USBControllerImpl.h

    r2805 r2845  
    3333class HostUSBDevice;
    3434
     35/**
     36 *  @note we cannot use VirtualBoxBaseWithTypedChildren <USBDeviceFilter> as a
     37 *  base class, because we want a quick (map-based) way of validating
     38 *  IUSBDeviceFilter pointers passed from outside as method parameters that
     39 *  VirtualBoxBaseWithChildren::getDependentChild() gives us.
     40 */
     41
    3542class ATL_NO_VTABLE USBController :
    36     public VirtualBoxBaseWithChildren,
    37     public VirtualBoxSupportErrorInfoImpl<USBController, IUSBController>,
    38     public VirtualBoxSupportTranslation<USBController>,
     43    public VirtualBoxBaseWithChildrenNEXT,
     44    public VirtualBoxSupportErrorInfoImpl <USBController, IUSBController>,
     45    public VirtualBoxSupportTranslation <USBController>,
    3946    public IUSBController
    4047{
     
    4451    {
    4552        /* Constructor. */
    46         Data() : m_fEnabled(FALSE) { }
     53        Data() : mEnabled (FALSE) { }
    4754
    4855        bool operator== (const Data &that) const
    4956        {
    50             return this == &that || m_fEnabled == that.m_fEnabled;
     57            return this == &that || mEnabled == that.mEnabled;
    5158        }
    5259
    5360        /** Enabled indicator. */
    54         BOOL                            m_fEnabled;
     61        BOOL mEnabled;
    5562    };
    5663
    5764public:
    5865
    59     DECLARE_NOT_AGGREGATABLE(USBController)
     66    VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (USBController)
     67
     68    DECLARE_NOT_AGGREGATABLE (USBController)
    6069
    6170    DECLARE_PROTECT_FINAL_CONSTRUCT()
    6271
    6372    BEGIN_COM_MAP(USBController)
    64         COM_INTERFACE_ENTRY(ISupportErrorInfo)
    65         COM_INTERFACE_ENTRY(IUSBController)
     73        COM_INTERFACE_ENTRY (ISupportErrorInfo)
     74        COM_INTERFACE_ENTRY (IUSBController)
    6675    END_COM_MAP()
    6776
    6877    NS_DECL_ISUPPORTS
     78
     79    DECLARE_EMPTY_CTOR_DTOR (USBController)
    6980
    7081    HRESULT FinalConstruct();
     
    7283
    7384    // public initializer/uninitializer for internal purposes only
    74     HRESULT init (Machine *parent);
    75     HRESULT init (Machine *parent, USBController *that);
    76     HRESULT initCopy (Machine *parent, USBController *that);
     85    HRESULT init (Machine *aParent);
     86    HRESULT init (Machine *aParent, USBController *aThat);
     87    HRESULT initCopy (Machine *aParent, USBController *aThat);
    7788    void uninit();
    7889
    7990    // IUSBController properties
    80     STDMETHOD(COMGETTER(Enabled))(BOOL *a_pfEnabled);
    81     STDMETHOD(COMSETTER(Enabled))(BOOL a_fEnabled);
    82     STDMETHOD(COMGETTER(USBStandard))(USHORT *a_pusUSBStandard);
    83     STDMETHOD(COMGETTER(DeviceFilters))(IUSBDeviceFilterCollection **aDevicesFilters);
     91    STDMETHOD(COMGETTER(Enabled)) (BOOL *aEnabled);
     92    STDMETHOD(COMSETTER(Enabled)) (BOOL aEnabled);
     93    STDMETHOD(COMGETTER(USBStandard)) (USHORT *aUSBStandard);
     94    STDMETHOD(COMGETTER(DeviceFilters)) (IUSBDeviceFilterCollection **aDevicesFilters);
    8495
    8596    // IUSBController methods
     
    90101    // public methods only for internal purposes
    91102
    92     ComObjPtr <Machine, ComWeakRef> &parent() { return m_Parent; };
     103    const ComObjPtr <Machine, ComWeakRef> &parent() { return mParent; };
    93104
    94105    HRESULT loadSettings (CFGNODE aMachine);
     
    101112    void copyFrom (USBController *aThat);
    102113
    103     const Backupable<Data> &data() { return m_Data; }
     114    const Backupable<Data> &data() { return mData; }
    104115
    105116    HRESULT onMachineRegistered (BOOL aRegistered);
     
    130141
    131142    /** Parent object. */
    132     ComObjPtr<Machine, ComWeakRef>  m_Parent;
     143    const ComObjPtr<Machine, ComWeakRef> mParent;
    133144    /** Peer object. */
    134     ComObjPtr<USBController>        m_Peer;
     145    const ComObjPtr <USBController> mPeer;
    135146    /** Data. */
    136     Backupable<Data>                m_Data;
     147    Backupable <Data> mData;
    137148
    138149    // the following fields need special backup/rollback/commit handling,
     
    140151
    141152    typedef std::list <ComObjPtr <USBDeviceFilter> > DeviceFilterList;
    142     Backupable <DeviceFilterList> m_DeviceFilters;
     153    Backupable <DeviceFilterList> mDeviceFilters;
    143154};
    144155
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