diff --git a/CMakeLists.txt b/CMakeLists.txt index f561791622..30d6096f4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -247,6 +247,12 @@ option(POCO_ENABLE_STD_MUTEX "Set to OFF|NO using mutex from standard library (d if (POCO_ENABLE_STD_MUTEX) add_definitions(-DPOCO_ENABLE_STD_MUTEX) endif () + +option(POCO_ENABLE_SIG_LOCK "Set to ON if signal handlers use code that lock mutexes to prevent deadlocks (default OFF)" OFF) + +if (POCO_ENABLE_SIG_LOCK) + add_definitions(-DPOCO_ENABLE_SIG_LOCK) +endif () include(DefinePlatformSpecifc) # Collect the built libraries and include dirs, the will be used to create the PocoConfig.cmake file diff --git a/Foundation/include/Poco/Mutex_POSIX.h b/Foundation/include/Poco/Mutex_POSIX.h index 6e6c42a525..59dbd02b69 100644 --- a/Foundation/include/Poco/Mutex_POSIX.h +++ b/Foundation/include/Poco/Mutex_POSIX.h @@ -23,6 +23,10 @@ #include #include +#ifdef POCO_ENABLE_SIG_LOCK +#include +#endif + namespace Poco { @@ -40,6 +44,7 @@ class Foundation_API MutexImpl private: pthread_mutex_t _mutex; + sigset_t set, oldset; }; @@ -56,6 +61,10 @@ class Foundation_API FastMutexImpl: public MutexImpl // inline void MutexImpl::lockImpl() { +#ifdef POCO_ENABLE_SIG_LOCK + sigfillset(&set); + pthread_sigmask(SIG_BLOCK, &set, &oldset); +#endif if (pthread_mutex_lock(&_mutex)) throw SystemException("cannot lock mutex"); } @@ -77,6 +86,10 @@ inline void MutexImpl::unlockImpl() { if (pthread_mutex_unlock(&_mutex)) throw SystemException("cannot unlock mutex"); + +#ifdef POCO_ENABLE_SIG_LOCK + pthread_sigmask(SIG_SETMASK, &oldset, NULL); +#endif }