LCOV - code coverage report
Current view: top level - INDI/libcommon - MutexLock.hpp (source / functions) Coverage Total Hit
Test: MagAOX Lines: 87.1 % 31 27
Test Date: 2026-01-03 21:03:39 Functions: 100.0 % 6 6

            Line data    Source code
       1              : /// MutexLock.hpp
       2              : ///
       3              : /// @author Paul Grenz
       4              : ///
       5              : ////////////////////////////////////////////////////////////////////////////////
       6              : 
       7              : #ifndef PCF_MUTEX_LOCK_HPP
       8              : #define PCF_MUTEX_LOCK_HPP
       9              : 
      10              : #include <stdexcept>
      11              : #include <pthread.h>
      12              : #include <string.h>
      13              : 
      14              : //#include "../common/Logger.hpp"
      15              : 
      16              : ////////////////////////////////////////////////////////////////////////////////
      17              : 
      18              : namespace pcf
      19              : {
      20              : class MutexLock
      21              : {
      22              :   public:
      23              :     // This is a wrapper class for a mutex lock which will unlock when
      24              :     // the class goes out of scope.
      25              :     // todo: migrate it to its own file.
      26              :     class AutoLock
      27              :     {
      28              :       private:
      29              :         // This is private so the class may not be instantiated this way.
      30              :         AutoLock() : m_mutex( NULL )
      31              :         {
      32              :         }
      33              :       public:
      34           10 :         AutoLock( MutexLock *mutex ) : m_mutex( mutex )
      35              :         {
      36           10 :           if ( m_mutex == NULL )
      37            0 :             throw std::invalid_argument( "MutexLock::AutoLock: Mutex pointer is NULL" );
      38           10 :           m_mutex->lock();
      39           10 :         }
      40           10 :         ~AutoLock()
      41              :         {
      42           10 :           if ( m_mutex != NULL )
      43           10 :             m_mutex->unlock();
      44           10 :         }
      45              :       private:
      46              :         MutexLock *m_mutex;
      47              :     };
      48              : 
      49              :     // Constructor/destructor/operators.
      50              :   public:
      51           24 :     MutexLock()
      52           24 :     {
      53           24 :       int nErr = 0;
      54           24 :       if ( ( nErr = pthread_mutex_init( &m_idLock, NULL ) ) != 0 )
      55            0 :         throw std::runtime_error( std::string( "MutexLock::MutexLock: " ) + strerror( nErr ) );
      56              :       //m_logMsg.clear();
      57              :       //m_logMsg << pcf::Logger::enumDebug << "pthread_mutex_init" << std::endl;
      58           24 :     }
      59           24 :     virtual ~MutexLock() noexcept(false)
      60           24 :     {
      61           24 :       int nErr = 0;
      62           24 :       if ( ( nErr = pthread_mutex_destroy( &m_idLock ) ) != 0 )
      63            9 :        return;
      64              :       //throw std::runtime_error( std::string( "MutexLock::~MutexLock: " ) + strerror( nErr ) );
      65              :       //m_logMsg.clear();
      66              :       //m_logMsg << pcf::Logger::enumDebug << "pthread_mutex_destroy" << std::endl;
      67           24 :     }
      68              : 
      69              :   private:
      70              :     MutexLock( const MutexLock & )
      71              :     {
      72              :     }
      73              :     const MutexLock &operator=( const MutexLock & )
      74              :     {
      75              :       return *this;
      76              :     }
      77              : 
      78              :     // Methods.
      79              :   public:
      80           22 :     void lock()
      81              :     {
      82           22 :       int nErr = 0;
      83           22 :       if ( ( nErr = pthread_mutex_lock( &m_idLock ) ) != 0 )
      84            0 :         throw std::runtime_error( std::string( "MutexLock::lock: " ) + strerror( nErr ) );
      85              :       //m_logMsg.clear();
      86              :       //m_logMsg << pcf::Logger::enumDebug << "pthread_mutex_lock" << std::endl;
      87           22 :     }
      88           13 :     void unlock()
      89              :     {
      90           13 :       int nErr = 0;
      91           13 :       if ( ( nErr = pthread_mutex_unlock( &m_idLock ) ) != 0 )
      92            0 :         throw std::runtime_error( std::string( "MutexLock::unlock: " ) + strerror( nErr ) );
      93              :       //m_logMsg.clear();
      94              :       //m_logMsg << pcf::Logger::enumDebug << "pthread_mutex_unlock" << std::endl;
      95           13 :     }
      96              : 
      97              :     // Variables.
      98              :   private:
      99              :     /// id or handle of the mutex lock created.
     100              :     pthread_mutex_t m_idLock;
     101              :     //pcf::Logger m_logMsg;
     102              : 
     103              : }; // Class MutexLock
     104              : } // Namespace pcf
     105              : 
     106              : ////////////////////////////////////////////////////////////////////////////////
     107              : 
     108              : #endif // PCF_MUTEX_LOCK_HPP
        

Generated by: LCOV version 2.0-1