libzypp  10.5.0
Mutex.cc
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00011 #include <zypp/thread/Mutex.h>
00012 #include <zypp/thread/MutexException.h>
00013 #include <zypp/base/Gettext.h>
00014 
00015 
00017 namespace zypp
00018 { 
00019 
00021   namespace thread
00022   { 
00023 
00024     // -------------------------------------------------------------
00025     Mutex::Mutex()
00026     {
00027       pthread_mutexattr_t attr;
00028 
00029       int ret = pthread_mutexattr_init(&attr);
00030       if( ret != 0)
00031       {
00032         ZYPP_THROW_ERRNO_MSG(zypp::thread::MutexException,
00033         _("Can't initialize mutex attributes"));
00034       }
00035 
00036       ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
00037       if( ret != 0)
00038       {
00039         ZYPP_THROW_ERRNO_MSG(MutexException,
00040         _("Can't set recursive mutex attribute"));
00041       }
00042 
00043       ret = pthread_mutex_init(&m_mutex, &attr);
00044       if( ret != 0)
00045       {
00046         ZYPP_THROW_ERRNO_MSG(MutexException,
00047         _("Can't initialize recursive mutex"));
00048       }
00049     }
00050 
00051     // -------------------------------------------------------------
00052     Mutex::~Mutex()
00053     {
00054       if( pthread_mutex_destroy(&m_mutex) != 0 && errno == EBUSY)
00055       {
00056         // try to unlock and to destroy again...
00057         if( pthread_mutex_unlock(&m_mutex) == 0)
00058         {
00059             pthread_mutex_destroy(&m_mutex);
00060         }
00061         /*
00062         else
00063         {
00064           ZYPP_THROW_ERRNO_MSG(MutexException,
00065           _("Can't destroy mutex owned by another thread"));
00066         }
00067         */
00068       }
00069     }
00070  
00071     // -------------------------------------------------------------
00072     void Mutex::lock()
00073     {
00074       if( pthread_mutex_lock(&m_mutex) != 0)
00075       {
00076         ZYPP_THROW_ERRNO_MSG(MutexException,
00077         _("Can't acquire the mutex lock"));
00078       }
00079     }
00080 
00081     // -------------------------------------------------------------
00082     void Mutex::unlock()
00083     {
00084       if( pthread_mutex_unlock(&m_mutex) != 0)
00085       {
00086         ZYPP_THROW_ERRNO_MSG(MutexException,
00087         _("Can't release the mutex lock"));
00088       }
00089     }
00090 
00091     // -------------------------------------------------------------
00092     bool Mutex::trylock()
00093     {
00094       return (pthread_mutex_trylock(&m_mutex) == 0);
00095     }
00096 
00097 
00099   } // namespace thread
00101 
00103 } // namespace zypp
00105 
00106 /*
00107 ** vim: set ts=2 sts=2 sw=2 ai et:
00108 */