VirtualBox

Changeset 25282 in vbox for trunk/src/VBox/Main


Ignore:
Timestamp:
Dec 9, 2009 7:21:21 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
55828
Message:

Main: preparation for deadlock detection: move more logic to AutoLockBase

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

Legend:

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

    r25281 r25282  
    115115{
    116116    delete m;
     117}
     118
     119/**
     120 * Requests ownership of all contained lock handles by calling
     121 * the pure virtual acquireImpl() function on each of them,
     122 * which must be implemented by the descendant class; in the
     123 * implementation, AutoWriteLock will request a write lock
     124 * whereas AutoReadLock will request a read lock.
     125 */
     126void AutoLockBase::acquire()
     127{
     128    if (m->pHandle)
     129    {
     130        AssertMsg(!m->fIsLocked, ("m->fIsLocked is true, attempting to lock twice!"));
     131        // call virtual function implemented in AutoWriteLock or AutoReadLock
     132        this->acquireImpl(*m->pHandle);
     133        m->fIsLocked = true;
     134    }
     135}
     136
     137/**
     138 * Releases ownership of all contained lock handles by calling
     139 * the pure virtual releaseImpl() function on each of them,
     140 * which must be implemented by the descendant class; in the
     141 * implementation, AutoWriteLock will release a write lock
     142 * whereas AutoReadLock will release a read lock.
     143 */
     144void AutoLockBase::release()
     145{
     146    if (m->pHandle)
     147    {
     148        AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot release!"));
     149        // call virtual function implemented in AutoWriteLock or AutoReadLock
     150        this->releaseImpl(*m->pHandle);
     151        m->fIsLocked = false;
     152    }
    117153}
    118154
     
    145181}
    146182
    147 void AutoWriteLock::acquire()
    148 {
    149     if (m->pHandle)
    150     {
    151         AssertMsg(!m->fIsLocked, ("m->fIsLocked is true, attempting to lock twice!"));
    152         m->pHandle->lockWrite();
    153         m->fIsLocked = true;
    154     }
    155 }
    156 
    157 void AutoWriteLock::release()
    158 {
    159     if (m->pHandle)
    160     {
    161         AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot release!"));
    162         m->pHandle->unlockWrite();
    163         m->fIsLocked = false;
    164     }
     183/**
     184 * Implementation of the pure virtual declared in AutoLockBase.
     185 * This gets called by AutoLockBase.acquire() to actually request
     186 * the semaphore; in the AutoWriteLock implementation, we request
     187 * the semaphore in write mode.
     188 */
     189/*virtual*/ void AutoWriteLock::acquireImpl(LockHandle &l)
     190{
     191    l.lockWrite();
     192}
     193
     194/**
     195 * Implementation of the pure virtual declared in AutoLockBase.
     196 * This gets called by AutoLockBase.release() to actually release
     197 * the semaphore; in the AutoWriteLock implementation, we release
     198 * the semaphore in write mode.
     199 */
     200/*virtual*/ void AutoWriteLock::releaseImpl(LockHandle &l)
     201{
     202    l.unlockWrite();
    165203}
    166204
     
    307345
    308346/**
    309  * Requests a read (shared) lock. If a read lock is already owned by
    310  * this thread, increases the lock level (allowing for nested read locks on
    311  * the same thread). Blocks indefinitely if a write lock is already owned by
    312  * another thread until that tread releases the write lock, otherwise
    313  * returns immediately.
    314  *
    315  * Note that this method returns immediately even if any number of other
    316  * threads owns read locks on the same semaphore. Also returns immediately
    317  * if a write lock on this semaphore is owned by the current thread which
    318  * allows for read locks nested into write locks on the same thread.
    319  */
    320 void AutoReadLock::acquire()
    321 {
    322     if (m->pHandle)
    323     {
    324         AssertMsg(!m->fIsLocked, ("m->fIsLocked is true, attempting to lock twice!"));
    325         m->pHandle->lockRead();
    326         m->fIsLocked = true;
    327     }
    328 }
    329 
    330 /**
    331  * Decreases the read lock level increased by #lock(). If the level drops to
    332  * zero (e.g. the number of nested #unlock() calls matches the number of
    333  * nested #lock() calls), releases the lock making the managed semaphore
    334  * available for locking by other threads.
    335  */
    336 void AutoReadLock::release()
    337 {
    338     if (m->pHandle)
    339     {
    340         AssertMsg(m->fIsLocked, ("m->fIsLocked is false, cannot release!"));
    341         m->pHandle->unlockRead();
    342         m->fIsLocked = false;
    343     }
    344 }
    345 
     347 * Implementation of the pure virtual declared in AutoLockBase.
     348 * This gets called by AutoLockBase.acquire() to actually request
     349 * the semaphore; in the AutoReadLock implementation, we request
     350 * the semaphore in read mode.
     351 */
     352/*virtual*/ void AutoReadLock::acquireImpl(LockHandle &l)
     353{
     354    l.lockRead();
     355}
     356
     357/**
     358 * Implementation of the pure virtual declared in AutoLockBase.
     359 * This gets called by AutoLockBase.release() to actually release
     360 * the semaphore; in the AutoReadLock implementation, we release
     361 * the semaphore in read mode.
     362 */
     363/*virtual*/ void AutoReadLock::releaseImpl(LockHandle &l)
     364{
     365    l.unlockRead();
     366}
    346367
    347368} /* namespace util */
  • trunk/src/VBox/Main/include/AutoLock.h

    r25281 r25282  
    323323    Data *m;
    324324
     325    virtual void acquireImpl(LockHandle &l) = 0;
     326    virtual void releaseImpl(LockHandle &l) = 0;
     327
     328public:
     329    void acquire();
     330    void release();
     331
    325332private:
    326333    // prohibit copy + assignment
     
    407414    void cleanup();
    408415
    409     void acquire();
    410     void release();
     416    virtual void acquireImpl(LockHandle &l);
     417    virtual void releaseImpl(LockHandle &l);
     418
    411419    void leave();
    412420    void enter();
     
    524532    virtual ~AutoReadLock();
    525533
    526     void acquire();
    527     void release();
     534    virtual void acquireImpl(LockHandle &l);
     535    virtual void releaseImpl(LockHandle &l);
    528536};
    529537
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