enh(Mutex): Error code for pthread_mutex_lock failure #4712

This commit is contained in:
Alex Fabijanic 2024-10-03 14:10:55 +02:00
parent ecbb334224
commit 9c31aa6512
7 changed files with 31 additions and 18 deletions

View File

@ -43,6 +43,9 @@ public:
static std::string getMessage(int errorCode);
/// Utility function translating numeric error code to string.
#endif
static std::string getLastMessage();
/// Utility function returning the last error message.
};

View File

@ -20,8 +20,8 @@
#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include <pthread.h>
#include <errno.h>
namespace Poco {
@ -56,8 +56,9 @@ protected:
//
inline void MutexImpl::lockImpl()
{
if (pthread_mutex_lock(&_mutex))
throw SystemException("cannot lock mutex");
int rc;
if ((rc = pthread_mutex_lock(&_mutex)))
throw SystemException("cannot lock mutex", Error::getMessage(rc));
}
@ -69,14 +70,15 @@ inline bool MutexImpl::tryLockImpl()
else if (rc == EBUSY)
return false;
else
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getMessage(rc));
}
inline void MutexImpl::unlockImpl()
{
if (pthread_mutex_unlock(&_mutex))
throw SystemException("cannot unlock mutex");
int rc;
if ((rc = pthread_mutex_unlock(&_mutex)))
throw SystemException("cannot unlock mutex", Error::getMessage(rc));
}

View File

@ -20,6 +20,7 @@
#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include "Poco/UnWindows.h"
@ -55,7 +56,7 @@ inline void MutexImpl::lockImpl()
}
catch (...)
{
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}
}
@ -69,7 +70,7 @@ inline bool MutexImpl::tryLockImpl()
catch (...)
{
}
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}

View File

@ -20,8 +20,8 @@
#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include <pthread.h>
#include <errno.h>
namespace Poco {
@ -48,8 +48,9 @@ private:
//
inline void RWLockImpl::readLockImpl()
{
if (pthread_mutex_lock(&_mutex))
throw SystemException("cannot lock mutex");
int rc = 0;
if ((rc = pthread_mutex_lock(&_mutex)))
throw SystemException("cannot lock mutex", Error::getMessage(rc));
}
@ -61,7 +62,7 @@ inline bool RWLockImpl::tryReadLockImpl()
else if (rc == EBUSY)
return false;
else
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getMessage(rc));
}
@ -81,8 +82,9 @@ inline bool RWLockImpl::tryWriteLockImpl()
inline void RWLockImpl::unlockImpl()
{
if (pthread_mutex_unlock(&_mutex))
throw SystemException("cannot unlock mutex");
int rc = 0;
if ((rc = pthread_mutex_unlock(&_mutex)))
throw SystemException("cannot unlock mutex", Error::getMessage(rc));
}

View File

@ -102,8 +102,13 @@ namespace Poco {
return helper.message();
}
#endif
std::string Error::getLastMessage()
{
return getMessage(last());
}
} // namespace Poco

View File

@ -131,7 +131,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
else if (rc == ETIMEDOUT)
return false;
else
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getMessage(rc));
#else
const int sleepMillis = 5;
Timestamp now;
@ -142,7 +142,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
if (rc == 0)
return true;
else if (rc != EBUSY)
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getMessage(rc));
#if defined(POCO_VXWORKS)
struct timespec ts;
ts.tv_sec = 0;

View File

@ -47,7 +47,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
}
catch (...)
{
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}
Sleep(sleepMillis);
}