/* * @project The CERN Tape Archive (CTA) * @copyright Copyright © 2021-2022 CERN * @license This program is free software, distributed under the terms of the GNU General Public * Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". You can * redistribute it and/or modify it under the terms of the GPL Version 3, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License for more details. * * In applying this licence, CERN does not waive the privileges and immunities * granted to it by virtue of its status as an Intergovernmental Organization or * submit itself to any jurisdiction. */ #include "common/threading/Mutex.hpp" #include "common/exception/Errnum.hpp" #include "common/exception/Exception.hpp" namespace cta::threading { //------------------------------------------------------------------------------ //constructor //------------------------------------------------------------------------------ Mutex::Mutex() { pthread_mutexattr_t attr; cta::exception::Errnum::throwOnReturnedErrno( pthread_mutexattr_init(&attr), "Error from pthread_mutexattr_init in cta::threading::Mutex::Mutex()"); cta::exception::Errnum::throwOnReturnedErrno( pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK), "Error from pthread_mutexattr_settype in cta::threading::Mutex::Mutex()"); cta::exception::Errnum::throwOnReturnedErrno( pthread_mutex_init(&m_mutex, &attr), "Error from pthread_mutex_init in cta::threading::Mutex::Mutex()"); try { cta::exception::Errnum::throwOnReturnedErrno( pthread_mutexattr_destroy(&attr), "Error from pthread_mutexattr_destroy in cta::threading::Mutex::Mutex()"); } catch (...) { pthread_mutex_destroy(&m_mutex); throw; } } //------------------------------------------------------------------------------ //destructor //------------------------------------------------------------------------------ Mutex::~Mutex() { pthread_mutex_destroy(&m_mutex); } //------------------------------------------------------------------------------ //lock //------------------------------------------------------------------------------ void Mutex::lock() { cta::exception::Errnum::throwOnReturnedErrno( pthread_mutex_lock(&m_mutex), "Error from pthread_mutex_lock in cta::threading::Mutex::lock()"); } //------------------------------------------------------------------------------ //unlock //------------------------------------------------------------------------------ void Mutex::unlock() { cta::exception::Errnum::throwOnReturnedErrno( pthread_mutex_unlock(&m_mutex), "Error from pthread_mutex_unlock in cta::threading::Mutex::unlock()"); } } // namespace cta::threading