VirtualBox

Ignore:
Timestamp:
Jul 2, 2007 12:44:39 PM (17 years ago)
Author:
vboxsync
Message:

Main: Converted AudioAdapter and NetworkAdapter to the new locking scheme.

File:
1 edited

Legend:

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

    r3191 r3348  
    2424#include "Logging.h"
    2525
     26#include <iprt/cpputils.h>
     27
    2628// constructor / destructor
    2729/////////////////////////////////////////////////////////////////////////////
    2830
     31DEFINE_EMPTY_CTOR_DTOR (AudioAdapter)
     32
    2933HRESULT AudioAdapter::FinalConstruct()
    3034{
     
    3438void AudioAdapter::FinalRelease()
    3539{
    36     if (isReady())
    37         uninit ();
     40    uninit ();
    3841}
    3942
     
    4245
    4346/**
    44  * Initializes the audio adapter object.
    45  *
    46  * @returns COM result indicator
    47  */
    48 HRESULT AudioAdapter::init (Machine *parent)
    49 {
    50     LogFlowMember (("AudioAdapter::init (%p)\n", parent));
    51 
    52     ComAssertRet (parent, E_INVALIDARG);
    53 
    54     AutoLock alock (this);
    55     ComAssertRet (!isReady(), E_UNEXPECTED);
    56 
    57     mParent = parent;
    58     // mPeer is left null
     47 *  Initializes the audio adapter object.
     48 *
     49 *  @param aParent  Handle of the parent object.
     50 */
     51HRESULT AudioAdapter::init (Machine *aParent)
     52{
     53    LogFlowThisFunc (("aParent=%p\n", aParent));
     54
     55    ComAssertRet (aParent, E_INVALIDARG);
     56
     57    /* Enclose the state transition NotReady->InInit->Ready */
     58    AutoInitSpan autoInitSpan (this);
     59    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     60
     61    unconst (mParent) = aParent;
     62    /* mPeer is left null */
    5963
    6064    mData.allocate();
    6165
    62     setReady (true);
     66    /* Confirm a successful initialization */
     67    autoInitSpan.setSucceeded();
     68
    6369    return S_OK;
    6470}
     
    7177 *  @note This object must be destroyed before the original object
    7278 *  it shares data with is destroyed.
    73  */
    74 HRESULT AudioAdapter::init (Machine *parent, AudioAdapter *that)
    75 {
    76     LogFlowMember (("AudioAdapter::init (%p, %p)\n", parent, that));
    77 
    78     ComAssertRet (parent && that, E_INVALIDARG);
    79 
    80     AutoLock alock (this);
    81     ComAssertRet (!isReady(), E_UNEXPECTED);
    82 
    83     mParent = parent;
    84     mPeer = that;
    85 
    86     AutoLock thatlock (that);
    87     mData.share (that->mData);
    88 
    89     setReady (true);
     79 *
     80 *  @note Locks @a aThat object for reading.
     81 */
     82HRESULT AudioAdapter::init (Machine *aParent, AudioAdapter *aThat)
     83{
     84    LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
     85
     86    ComAssertRet (aParent && aThat, E_INVALIDARG);
     87
     88    /* Enclose the state transition NotReady->InInit->Ready */
     89    AutoInitSpan autoInitSpan (this);
     90    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     91
     92    unconst (mParent) = aParent;
     93    unconst (mPeer) = aThat;
     94
     95    AutoCaller thatCaller (aThat);
     96    AssertComRCReturnRC (thatCaller.rc());
     97
     98    AutoReaderLock thatLock (aThat);
     99    mData.share (aThat->mData);
     100
     101    /* Confirm a successful initialization */
     102    autoInitSpan.setSucceeded();
     103
    90104    return S_OK;
    91105}
     
    95109 *  (a kind of copy constructor). This object makes a private copy of data
    96110 *  of the original object passed as an argument.
    97  */
    98 HRESULT AudioAdapter::initCopy (Machine *parent, AudioAdapter *that)
    99 {
    100     LogFlowMember (("AudioAdapter::initCopy (%p, %p)\n", parent, that));
    101 
    102     ComAssertRet (parent && that, E_INVALIDARG);
    103 
    104     AutoLock alock (this);
    105     ComAssertRet (!isReady(), E_UNEXPECTED);
    106 
    107     mParent = parent;
    108     // mPeer is left null
    109 
    110     AutoLock thatlock (that);
    111     mData.attachCopy (that->mData);
    112 
    113     setReady (true);
     111 *
     112 *  @note Locks @a aThat object for reading.
     113 */
     114HRESULT AudioAdapter::initCopy (Machine *aParent, AudioAdapter *aThat)
     115{
     116    LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
     117
     118    ComAssertRet (aParent && aThat, E_INVALIDARG);
     119
     120    /* Enclose the state transition NotReady->InInit->Ready */
     121    AutoInitSpan autoInitSpan (this);
     122    AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
     123
     124    unconst (mParent) = aParent;
     125    /* mPeer is left null */
     126
     127    AutoCaller thatCaller (aThat);
     128    AssertComRCReturnRC (thatCaller.rc());
     129
     130    AutoReaderLock thatLock (aThat);
     131    mData.attachCopy (aThat->mData);
     132
     133    /* Confirm a successful initialization */
     134    autoInitSpan.setSucceeded();
     135
    114136    return S_OK;
    115137}
     
    121143void AudioAdapter::uninit()
    122144{
    123     LogFlowMember (("AudioAdapter::uninit()\n"));
     145    LogFlowThisFunc (("\n"));
     146
     147    /* Enclose the state transition Ready->InUninit->NotReady */
     148    AutoUninitSpan autoUninitSpan (this);
     149    if (autoUninitSpan.uninitDone())
     150        return;
     151
     152    mData.free();
     153
     154    unconst (mPeer).setNull();
     155    unconst (mParent).setNull();
     156}
     157
     158// IAudioAdapter properties
     159/////////////////////////////////////////////////////////////////////////////
     160
     161STDMETHODIMP AudioAdapter::COMGETTER(Enabled)(BOOL *aEnabled)
     162{
     163    if (!aEnabled)
     164        return E_POINTER;
     165
     166    AutoCaller autoCaller (this);
     167    CheckComRCReturnRC (autoCaller.rc());
     168
     169    AutoReaderLock alock (this);
     170
     171    *aEnabled = mData->mEnabled;
     172
     173    return S_OK;
     174}
     175
     176STDMETHODIMP AudioAdapter::COMSETTER(Enabled)(BOOL aEnabled)
     177{
     178    AutoCaller autoCaller (this);
     179    CheckComRCReturnRC (autoCaller.rc());
     180
     181    /* the machine needs to be mutable */
     182    Machine::AutoMutableStateDependency adep (mParent);
     183    CheckComRCReturnRC (adep.rc());
    124184
    125185    AutoLock alock (this);
    126     AssertReturn (isReady(), (void) 0);
    127 
    128     mData.free();
    129 
    130     mPeer.setNull();
    131     mParent.setNull();
    132 
    133     setReady(false);
    134 }
    135 
    136 // IAudioAdapter properties
    137 /////////////////////////////////////////////////////////////////////////////
    138 
    139 /**
    140  * Returns the enabled status
    141  *
    142  * @returns COM status code
    143  * @param enabled address of result variable
    144  */
    145 STDMETHODIMP AudioAdapter::COMGETTER(Enabled)(BOOL *enabled)
    146 {
    147     if (!enabled)
    148         return E_POINTER;
    149 
    150     AutoLock lock(this);
    151     CHECK_READY();
    152 
    153     *enabled = mData->mEnabled;
    154     return S_OK;
    155 }
    156 
    157 /**
    158  * Sets the enabled state
    159  *
    160  * @returns COM status code
    161  * @param enabled address of result variable
    162  */
    163 STDMETHODIMP AudioAdapter::COMSETTER(Enabled)(BOOL enabled)
    164 {
    165     AutoLock lock(this);
    166     CHECK_READY();
    167 
    168     CHECK_MACHINE_MUTABILITY (mParent);
    169 
    170     if (mData->mEnabled != enabled)
     186
     187    if (mData->mEnabled != aEnabled)
    171188    {
    172189        mData.backup();
    173         mData->mEnabled = enabled;
     190        mData->mEnabled = aEnabled;
    174191    }
    175192
     
    177194}
    178195
    179 /**
    180  * Returns the current audio driver type
    181  *
    182  * @returns COM status code
    183  * @param audioDriver address of result variable
    184  */
    185 STDMETHODIMP AudioAdapter::COMGETTER(AudioDriver)(AudioDriverType_T *audioDriver)
    186 {
    187     if (!audioDriver)
     196STDMETHODIMP AudioAdapter::COMGETTER(AudioDriver)(AudioDriverType_T *aAudioDriver)
     197{
     198    if (!aAudioDriver)
    188199        return E_POINTER;
    189200
    190     AutoLock lock(this);
    191     CHECK_READY();
    192 
    193     *audioDriver = mData->mAudioDriver;
    194     return S_OK;
    195 }
    196 
    197 /**
    198  * Sets the audio driver type
    199  *
    200  * @returns COM status code
    201  * @param audioDriver audio driver type to use
    202  */
    203 STDMETHODIMP AudioAdapter::COMSETTER(AudioDriver)(AudioDriverType_T audioDriver)
    204 {
    205     AutoLock lock(this);
    206     CHECK_READY();
    207 
    208     CHECK_MACHINE_MUTABILITY (mParent);
     201    AutoCaller autoCaller (this);
     202    CheckComRCReturnRC (autoCaller.rc());
     203
     204    AutoReaderLock alock (this);
     205
     206    *aAudioDriver = mData->mAudioDriver;
     207
     208    return S_OK;
     209}
     210
     211STDMETHODIMP AudioAdapter::COMSETTER(AudioDriver)(AudioDriverType_T aAudioDriver)
     212{
     213    AutoCaller autoCaller (this);
     214    CheckComRCReturnRC (autoCaller.rc());
     215
     216    /* the machine needs to be mutable */
     217    Machine::AutoMutableStateDependency adep (mParent);
     218    CheckComRCReturnRC (adep.rc());
     219
     220    AutoLock alock (this);
    209221
    210222    HRESULT rc = S_OK;
    211223
    212     if (mData->mAudioDriver != audioDriver)
     224    if (mData->mAudioDriver != aAudioDriver)
    213225    {
    214226        /*
    215227         * which audio driver type are we supposed to use?
    216228         */
    217         switch (audioDriver)
     229        switch (aAudioDriver)
    218230        {
    219231            case AudioDriverType_NullAudioDriver:
     
    238250            {
    239251                mData.backup();
    240                 mData->mAudioDriver = audioDriver;
     252                mData->mAudioDriver = aAudioDriver;
    241253                break;
    242254            }
     
    244256            default:
    245257            {
    246                 Log(("wrong audio driver type specified!\n"));
     258                AssertMsgFailed (("Wrong audio driver type %d\n",
     259                                  aAudioDriver));
    247260                rc = E_FAIL;
    248261            }
     
    259272/////////////////////////////////////////////////////////////////////////////
    260273
     274/**
     275 *  @note Locks this object for writing.
     276 */
     277bool AudioAdapter::rollback()
     278{
     279    /* sanity */
     280    AutoCaller autoCaller (this);
     281    AssertComRCReturn (autoCaller.rc(), false);
     282
     283    AutoLock alock (this);
     284
     285    bool changed = false;
     286
     287    if (mData.isBackedUp())
     288    {
     289        /* we need to check all data to see whether anything will be changed
     290         * after rollback */
     291        changed = mData.hasActualChanges();
     292        mData.rollback();
     293    }
     294
     295    return changed;
     296}
     297
     298/**
     299 *  @note Locks this object for writing, together with the peer object (also
     300 *  for writing) if there is one.
     301 */
    261302void AudioAdapter::commit()
    262303{
    263     AutoLock alock (this);
     304    /* sanity */
     305    AutoCaller autoCaller (this);
     306    AssertComRCReturnVoid (autoCaller.rc());
     307
     308    /* sanity too */
     309    AutoCaller thatCaller (mPeer);
     310    AssertComRCReturnVoid (thatCaller.rc());
     311
     312    /* lock both for writing since we modify both */
     313    AutoMultiLock <2> alock (this->wlock(), AutoLock::maybeWlock (mPeer));
     314
    264315    if (mData.isBackedUp())
    265316    {
     
    267318        if (mPeer)
    268319        {
    269             // attach new data to the peer and reshare it
    270             AutoLock peerlock (mPeer);
     320            /* attach new data to the peer and reshare it */
    271321            mPeer->mData.attach (mData);
    272322        }
     
    274324}
    275325
     326/**
     327 *  @note Locks this object for writing, together with the peer object
     328 *  represented by @a aThat (locked for reading).
     329 */
    276330void AudioAdapter::copyFrom (AudioAdapter *aThat)
    277331{
    278     AutoLock alock (this);
    279 
    280     // this will back up current data
     332    AssertReturnVoid (aThat != NULL);
     333
     334    /* sanity */
     335    AutoCaller autoCaller (this);
     336    AssertComRCReturnVoid (autoCaller.rc());
     337
     338    /* sanity too */
     339    AutoCaller thatCaller (mPeer);
     340    AssertComRCReturnVoid (thatCaller.rc());
     341
     342    /* peer is not modified, lock it for reading */
     343    AutoMultiLock <2> alock (this->wlock(), aThat->rlock());
     344
     345    /* this will back up current data */
    281346    mData.assignCopy (aThat->mData);
    282347}
    283 
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