LCOV - code coverage report
Current view: top level - lunchbox - scopedMutex.h (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 7 18 38.9 %
Date: 2018-10-03 05:33:11 Functions: 3 6 50.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2006-2017, 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/lockable.h> // used in inline method
      22             : #include <lunchbox/types.h>
      23             : #include <mutex>
      24             : 
      25             : namespace lunchbox
      26             : {
      27             : /**
      28             :  * A scoped shared mutex.
      29             :  *
      30             :  * The mutex is automatically set upon creation, and released when the scoped
      31             :  * mutex is destroyed, e.g., when the scope is left. The scoped mutex does
      32             :  * nothing if a nullptr for the lock is passed.
      33             :  */
      34             : template <class L>
      35             : class UniqueSharedLock
      36             : {
      37             : public:
      38           0 :     explicit UniqueSharedLock(L& lock)
      39           0 :         : _lock(&lock)
      40             :     {
      41           0 :         lock.lock_shared();
      42           0 :     }
      43             : 
      44             :     explicit UniqueSharedLock(L* lock)
      45             :         : _lock(lock)
      46             :     {
      47             :         if (lock)
      48             :             lock->lock_shared();
      49             :     }
      50             : 
      51             :     template <typename LB>
      52           0 :     explicit UniqueSharedLock(const LB& lockable)
      53           0 :         : UniqueSharedLock(lockable.lock)
      54             :     {
      55           0 :     }
      56             : 
      57             :     /** Destruct the scoped mutex and unset the mutex. @version 1.0 */
      58           0 :     ~UniqueSharedLock()
      59             :     {
      60           0 :         if (_lock)
      61           0 :             _lock->unlock_shared();
      62           0 :     }
      63             : 
      64             : private:
      65             :     UniqueSharedLock() = delete;
      66             :     UniqueSharedLock(const UniqueSharedLock&) = delete;
      67             :     UniqueSharedLock(UniqueSharedLock&&) = delete;
      68             :     UniqueSharedLock& operator=(const UniqueSharedLock&) = delete;
      69             :     UniqueSharedLock& operator=(UniqueSharedLock&&) = delete;
      70             : 
      71             :     L* const _lock;
      72             : };
      73             : 
      74             : /**
      75             :  * A scoped mutex.
      76             :  *
      77             :  * The mutex is automatically set upon creation, and released when the scoped
      78             :  * mutex is destroyed, e.g., when the scope is left. The scoped mutex does
      79             :  * nothing if a nullptr for the lock is passed.
      80             :  */
      81             : template <class L>
      82        8044 : class UniqueLock : public std::unique_lock<L>
      83             : {
      84             : public:
      85             :     UniqueLock(L* lock_)
      86             :         : std::unique_lock<L>(lock_ ? std::unique_lock<L>(*lock_)
      87             :                                     : std::unique_lock<L>())
      88             :     {
      89             :     }
      90             : 
      91        8048 :     UniqueLock(L& lock_)
      92        8048 :         : std::unique_lock<L>(lock_)
      93             :     {
      94        8060 :     }
      95             : 
      96             :     template <typename LB>
      97        3049 :     explicit UniqueLock(const LB& lockable)
      98        3049 :         : UniqueLock(lockable.lock)
      99             :     {
     100        3049 :     }
     101             : 
     102             : private:
     103             :     UniqueLock() = delete;
     104             :     UniqueLock(const UniqueLock&) = delete;
     105             :     UniqueLock& operator=(const UniqueLock&) = delete;
     106             : };
     107             : 
     108             : /** A scoped mutex for a fast uncontended read operation. @version 1.1.2 */
     109             : using ScopedFastRead = UniqueSharedLock<SpinLock>;
     110             : 
     111             : /** A scoped mutex for a fast uncontended write operation. @version 1.1.2 */
     112             : using ScopedFastWrite = UniqueLock<SpinLock>;
     113             : 
     114             : /** A scoped mutex for a read operation. @version 1.1.5 */
     115             : using ScopedRead = UniqueLock<std::mutex>;
     116             : 
     117             : /** A scoped mutex for a write operation. @version 1.1.5 */
     118             : using ScopedWrite = UniqueLock<std::mutex>;
     119             : }
     120             : #endif // LUNCHBOX_SCOPEDMUTEX_H

Generated by: LCOV version 1.11