/* Copyright (c) 20011 Hovanes Egiyan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "BaseIUModule.hh" //! Debug flag for objects of this class and derived classes unsigned int BaseIUModule::bmDebFlag = 0; //! Declare global mutex and its attributes pthread_mutex_t BaseIUModule::bmGlobMutex; pthread_mutexattr_t BaseIUModule::bmGlobMtxAttr; //! Initialize global mutex and its attributes int BaseIUModule::bmDummyInt = BaseIUModule::InitGlobalMutex(); //! Main constructor BaseIUModule::BaseIUModule( BaseIUDevice* devPtr, int addr ) : bmDevice(devPtr), bmAddress(addr) { if( bmDebFlag > 5 ) { cout << "BaseIUModule::BaseIUModule() : Main constructor is invoked for device at " << hex << showbase << devPtr << endl; cout << "The BaseIUModule object is at " << hex << showbase << this << endl; } InitMutex(); } //! Second constructor that looks like a copy constructor //! but allowing to have an address argument BaseIUModule::BaseIUModule( const BaseIUModule& module, int addr ) { if( bmDebFlag > 5 ) { cout << "BaseIUModule::BaseIUModule() : Second constructor is invoked for parent module at " << hex << showbase << &module << " and address " << addr << endl; cout << "The BaseIUModule object is at " << hex << showbase << this << endl; } InitMutex(); this->operator=( module ); bmAddress = addr; } //!Copy constructor BaseIUModule::BaseIUModule( const BaseIUModule& module ) { if( bmDebFlag > 5 ) { cout << "BaseIUModule::BaseIUModule() : Copy constructor is invoked" << endl; } InitMutex(); this->operator=( module ); } //! Destructor BaseIUModule::~BaseIUModule() { if( bmDebFlag > 5 ) { cout << "BaseIUModule::~BaseIUModule() : Destructor of module is called" << endl; } CloseMutex(); } //! Copy operator BaseIUModule& BaseIUModule::operator=( const BaseIUModule& module ) { if( bmDebFlag > 5 ) { cout << "BaseIUModule::operator=() : Assignment operator is invoked" << endl; } //! Do not do anything if assigned to itself if( this == &module ) return *this; // cout << "Trying to lock" << endl; MtxLock objLock( bmMutex ); // cout << "Lock successful" << endl; bmDevice = module.GetDevice(); // cout << "Got device" << endl; bmAddress = module.GetAddress(); if( bmDebFlag > 5 ) { cout << "BaseIUModule::operator=() : Assignment operator is done" << endl; } return *this; } //! Initialize global mutex int BaseIUModule::InitGlobalMutex() { pthread_mutexattr_init( &bmGlobMtxAttr ); pthread_mutexattr_setpshared( &bmGlobMtxAttr, PTHREAD_PROCESS_PRIVATE ); pthread_mutexattr_settype(&bmGlobMtxAttr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init( &bmGlobMutex, &bmGlobMtxAttr ); return 0; } //! Initialize mutex for individual objects void BaseIUModule::InitMutex() { pthread_mutexattr_init( &bmMtxAttr ); pthread_mutexattr_setpshared( &bmMtxAttr, PTHREAD_PROCESS_PRIVATE ); pthread_mutexattr_settype(&bmMtxAttr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init( &bmMutex, &bmMtxAttr ); return; } //! Destroy mutex for individual objects void BaseIUModule::CloseMutex() { pthread_mutex_destroy( &bmMutex ); pthread_mutexattr_destroy( &bmMtxAttr ); return; }