Skip to content

Commit

Permalink
enh(Mutex): Error code for pthread_mutex_lock failure #4712
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-f committed Oct 3, 2024
1 parent ecbb334 commit 9c31aa6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Foundation/include/Poco/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class Foundation_API Error
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.
};


Expand Down
14 changes: 8 additions & 6 deletions Foundation/include/Poco/Mutex_POSIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include <pthread.h>
#include <errno.h>


namespace Poco {
Expand Down Expand Up @@ -56,8 +56,9 @@ class Foundation_API FastMutexImpl: public MutexImpl
//
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));
}


Expand All @@ -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));
}


Expand Down
5 changes: 3 additions & 2 deletions Foundation/include/Poco/Mutex_WIN32.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include "Poco/UnWindows.h"


Expand Down Expand Up @@ -55,7 +56,7 @@ inline void MutexImpl::lockImpl()
}
catch (...)
{
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}
}

Expand All @@ -69,7 +70,7 @@ inline bool MutexImpl::tryLockImpl()
catch (...)
{
}
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}


Expand Down
14 changes: 8 additions & 6 deletions Foundation/include/Poco/RWLock_VX.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

#include "Poco/Foundation.h"
#include "Poco/Exception.h"
#include "Poco/Error.h"
#include <pthread.h>
#include <errno.h>


namespace Poco {
Expand All @@ -48,8 +48,9 @@ class Foundation_API RWLockImpl
//
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));
}


Expand All @@ -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));

}

Expand All @@ -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));
}


Expand Down
7 changes: 6 additions & 1 deletion Foundation/src/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ namespace Poco {
return helper.message();
}


#endif


std::string Error::getLastMessage()
{
return getMessage(last());
}


} // namespace Poco
4 changes: 2 additions & 2 deletions Foundation/src/Mutex_POSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Foundation/src/Mutex_WIN32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
}
catch (...)
{
throw SystemException("cannot lock mutex");
throw SystemException("cannot lock mutex", Error::getLastMessage());
}
Sleep(sleepMillis);
}
Expand Down

0 comments on commit 9c31aa6

Please sign in to comment.