AbstractMutex.cpp

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #include <fesa-core/Utilities/AbstractMutex.h>
00004 
00005 #include <fesa-core/Utilities/StringUtilities.h>
00006 #include <fesa-core/Exception/FesaException.h>
00007 
00008 #include <cmw-log/Logger.h>
00009 
00010 
00011 namespace
00012 {
00013 
00014 CMW::Log::Logger& logger = CMW::Log::LoggerFactory::getLogger("FESA.FWK.fesa-core.Utilities.AbstractMutex");
00015 
00016 } // namespace
00017 
00018 
00019 namespace fesa
00020 {
00021 
00022 AbstractMutex::~AbstractMutex()
00023 {
00024     int32_t err = pthread_mutex_destroy(lock_);
00025     if (err != 0)
00026     {
00027         std::string errorStr("An error occurred while destroying mutex");
00028         LOG_ERROR_IF(logger, errorStr);
00029     }
00030     if (memoryOwner_ == true)
00031     {
00032         delete lock_;
00033     }
00034 }
00035 
00036 void AbstractMutex::lock()
00037 {
00038     int32_t err = pthread_mutex_lock(lock_);
00039     if (err != 0)
00040     {
00041         std::string errorStr("An error occurred while locking mutex");
00042         LOG_ERROR_IF(logger, errorStr);
00043     }
00044 }
00045 
00046 void AbstractMutex::unlock()
00047 {
00048     int32_t err = pthread_mutex_unlock(lock_);
00049     if (err != 0)
00050     {
00051         std::string errorStr("An error occurred while unlocking mutex");
00052         LOG_ERROR_IF(logger, errorStr);
00053     }
00054 }
00055 
00056 int32_t AbstractMutex::getSize()
00057 {
00058     return sizeof(pthread_mutex_t);
00059 }
00060 
00061 void AbstractMutex::init(int32_t processShared)
00062 {
00063     std::string errorNumber("");
00064 
00065     pthread_mutexattr_t mutexAttr;
00066     // initialize the mutex attribute
00067     int32_t err = pthread_mutexattr_init(&mutexAttr);
00068     if (err != 0)
00069     {
00070         errorNumber = StringUtilities::toString(err);
00071         throw FesaException(__FILE__, __LINE__, FesaErrorInitializingMutexAttribute.c_str(), errorNumber.c_str());
00072     }
00073 
00074     // set mutex to PTHREAD_PROCESS_PRIVATE
00075     err = pthread_mutexattr_setpshared(&mutexAttr, processShared);
00076     if (err != 0)
00077     {
00078         errorNumber = StringUtilities::toString(err);
00079         throw FesaException(__FILE__, __LINE__, FesaErrorSettingPrivateMutexAttribute.c_str(), errorNumber.c_str());
00080     }
00081 
00082     // initialize the mutex
00083     err = pthread_mutex_init(lock_, &mutexAttr);
00084     if (err != 0)
00085     {
00086         errorNumber = StringUtilities::toString(err);
00087         throw FesaException(__FILE__, __LINE__, FesaErrorInitializingMutex.c_str(), errorNumber.c_str());
00088     }
00089 
00090     // destroy mutex attribute
00091     err = pthread_mutexattr_destroy(&mutexAttr);
00092     if (err != 0)
00093     {
00094         errorNumber = StringUtilities::toString(err);
00095         throw FesaException(__FILE__, __LINE__, FesaErrorDestroyingMutexAttribute.c_str(), errorNumber.c_str());
00096     }
00097 }
00098 
00099 } // fesa

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1