1
/*---------------------------------------------------------------------\
3
| |__ / \ / / . \ . \ |
8
\---------------------------------------------------------------------*/
9
/** \file zypp/thread/Mutex.cc
11
#include "zypp/thread/Mutex.h"
12
#include "zypp/thread/MutexException.h"
13
#include "zypp/base/Gettext.h"
16
//////////////////////////////////////////////////////////////////////
18
{ ////////////////////////////////////////////////////////////////////
20
////////////////////////////////////////////////////////////////////
22
{ //////////////////////////////////////////////////////////////////
24
// -------------------------------------------------------------
27
pthread_mutexattr_t attr;
29
int ret = pthread_mutexattr_init(&attr);
32
ZYPP_THROW_ERRNO_MSG(zypp::thread::MutexException,
33
_("Can't initialize mutex attributes"));
36
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
39
ZYPP_THROW_ERRNO_MSG(MutexException,
40
_("Can't set recursive mutex attribute"));
43
ret = pthread_mutex_init(&m_mutex, &attr);
46
ZYPP_THROW_ERRNO_MSG(MutexException,
47
_("Can't initialize recursive mutex"));
51
// -------------------------------------------------------------
54
if( pthread_mutex_destroy(&m_mutex) != 0 && errno == EBUSY)
56
// try to unlock and to destroy again...
57
if( pthread_mutex_unlock(&m_mutex) == 0)
59
pthread_mutex_destroy(&m_mutex);
64
ZYPP_THROW_ERRNO_MSG(MutexException,
65
_("Can't destroy mutex owned by another thread"));
71
// -------------------------------------------------------------
74
if( pthread_mutex_lock(&m_mutex) != 0)
76
ZYPP_THROW_ERRNO_MSG(MutexException,
77
_("Can't acquire the mutex lock"));
81
// -------------------------------------------------------------
84
if( pthread_mutex_unlock(&m_mutex) != 0)
86
ZYPP_THROW_ERRNO_MSG(MutexException,
87
_("Can't release the mutex lock"));
91
// -------------------------------------------------------------
94
return (pthread_mutex_trylock(&m_mutex) == 0);
98
//////////////////////////////////////////////////////////////////
100
////////////////////////////////////////////////////////////////////
102
////////////////////////////////////////////////////////////////////
104
//////////////////////////////////////////////////////////////////////
107
** vim: set ts=2 sts=2 sw=2 ai et: