LCOV - code coverage report
Current view: top level - lunchbox - scopedMutex.h (source / functions) Hit Total Coverage
Test: lcov2.info Lines: 10 10 100.0 %
Date: 2014-10-01 Functions: 17 17 100.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2006-2013, Stefan Eilemann <eile@equalizergraphics.com>
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or modify it under
       5             :  * the terms of the GNU Lesser General Public License version 2.1 as published
       6             :  * by the Free Software Foundation.
       7             :  *
       8             :  * This library is distributed in the hope that it will be useful, but WITHOUT
       9             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      10             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      11             :  * details.
      12             :  *
      13             :  * You should have received a copy of the GNU Lesser General Public License
      14             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      15             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      16             :  */
      17             : 
      18             : #ifndef LUNCHBOX_SCOPEDMUTEX_H
      19             : #define LUNCHBOX_SCOPEDMUTEX_H
      20             : 
      21             : #include <lunchbox/condition.h>   // used in inline method
      22             : #include <lunchbox/lock.h>        // used in inline method
      23             : #include <lunchbox/lockable.h>    // used in inline method
      24             : #include <lunchbox/types.h>
      25             : 
      26             : namespace lunchbox
      27             : {
      28             : class WriteOp;
      29             : class ReadOp;
      30             : 
      31             : /** @cond IGNORE */
      32             : template< class L, class T > struct ScopedMutexLocker {};
      33             : template< class L > struct ScopedMutexLocker< L, WriteOp >
      34             : {
      35    18349224 :     static inline void set( L& lock ) { lock.set(); }
      36    18413135 :     static inline void unset( L& lock ) { lock.unset(); }
      37             : };
      38             : template< class L > struct ScopedMutexLocker< L, ReadOp >
      39             : {
      40             :     static inline void set( L& lock ) { lock.setRead(); }
      41             :     static inline void unset( L& lock ) { lock.unsetRead(); }
      42             : };
      43             : template<> struct ScopedMutexLocker< Condition, WriteOp >
      44             : {
      45     1523714 :     static inline void set( Condition& cond ) { cond.lock(); }
      46     1532704 :     static inline void unset( Condition& cond ) { cond.unlock(); }
      47             : };
      48             : /** @endcond */
      49             : 
      50             : /**
      51             :  * A scoped mutex.
      52             :  *
      53             :  * The mutex is automatically set upon creation, and released when the scoped
      54             :  * mutex is destroyed, e.g., when the scope is left. The scoped mutex does
      55             :  * nothing if a 0 pointer for the lock is passed.
      56             :  * @deprecated Use boost::scoped_lock
      57             :  */
      58             : template< class L = Lock, class T = WriteOp >
      59             : class ScopedMutex
      60             : {
      61             :     typedef ScopedMutexLocker< L, T > LockTraits;
      62             : 
      63             : public:
      64             :     /**
      65             :      * Construct a new scoped mutex and set the given lock.
      66             :      *
      67             :      * Providing no Lock (0) is allowed, in which case the scoped mutex does
      68             :      * nothing.
      69             :      *
      70             :      * @param lock the mutex to set and unset, or 0.
      71             :      * @version 1.0
      72             :      */
      73     9445342 :     explicit ScopedMutex( L* lock ) : _lock( lock )
      74     9445342 :         { if( lock ) LockTraits::set( *lock ); }
      75             : 
      76             :     /** Construct a new scoped mutex and set the given lock. @version 1.0 */
      77    10512715 :     explicit ScopedMutex( L& lock ) : _lock( &lock )
      78    10512715 :         { LockTraits::set( lock ); }
      79             : 
      80             :     /** Move lock from rhs to new mutex. @version 1.5 */
      81             :     ScopedMutex( const ScopedMutex& rhs ) : _lock( rhs._lock )
      82             :     { const_cast< ScopedMutex& >( rhs )._lock = 0; }
      83             : 
      84             :     /** Move lock from rhs to this mutex. @version 1.5 */
      85             :     ScopedMutex& operator = ( ScopedMutex& rhs )
      86             :     {
      87             :         if( this != &rhs )
      88             :         {
      89             :             _lock = rhs._lock;
      90             :             rhs._lock = 0;
      91             :         }
      92             :         return *this;
      93             :     }
      94             : 
      95             :     /**
      96             :      * Construct a new scoped mutex for the given Lockable data structure.
      97             :      * @version 1.0
      98             :      */
      99             :     template< typename LB > explicit ScopedMutex( const LB& lockable )
     100             :     : _lock( &lockable.lock ) { LockTraits::set( lockable.lock ); }
     101             : 
     102             :     /** Destruct the scoped mutex and unset the mutex. @version 1.0 */
     103    19960783 :     ~ScopedMutex() { leave(); }
     104             : 
     105             :     /** Leave and unlock the mutex immediately. @version 1.0 */
     106    19960780 :     void leave() { if( _lock ) LockTraits::unset( *_lock ); _lock = 0; }
     107             : 
     108             : private:
     109             :     ScopedMutex();
     110             :     L* _lock;
     111             : }; // LB_DEPRECATED;
     112             : 
     113             : /** A scoped mutex for a fast uncontended read operation. @version 1.1.2 */
     114             : typedef ScopedMutex< SpinLock, ReadOp > ScopedFastRead;
     115             : 
     116             : /** A scoped mutex for a fast uncontended write operation. @version 1.1.2 */
     117             : typedef ScopedMutex< SpinLock, WriteOp > ScopedFastWrite;
     118             : 
     119             : /** A scoped mutex for a read operation. @version 1.1.5 */
     120             : typedef ScopedMutex< Lock, ReadOp > ScopedRead;
     121             : 
     122             : /** A scoped mutex for a write operation. @version 1.1.5 */
     123             : typedef ScopedMutex< Lock, WriteOp > ScopedWrite;
     124             : 
     125             : /** A scoped mutex for a write operation on a condition. @version 1.3.6 */
     126             : typedef ScopedMutex< Condition, WriteOp > ScopedCondition;
     127             : }
     128             : #endif //LUNCHBOX_SCOPEDMUTEX_H

Generated by: LCOV version 1.10