From 296d03858c9f41ccfe0b736c0556a284cef55b10 Mon Sep 17 00:00:00 2001 From: KhabarlakKonstantin Date: Wed, 28 Nov 2018 13:36:44 +0200 Subject: [PATCH] Fix deadlock in named_condition::notify Avoid deadlock in named_condition::notify when BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING is defined --- include/boost/interprocess/sync/spin/mutex.hpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/include/boost/interprocess/sync/spin/mutex.hpp b/include/boost/interprocess/sync/spin/mutex.hpp index 7663673..ad61ddd 100644 --- a/include/boost/interprocess/sync/spin/mutex.hpp +++ b/include/boost/interprocess/sync/spin/mutex.hpp @@ -64,7 +64,21 @@ inline spin_mutex::~spin_mutex() } inline void spin_mutex::lock(void) -{ return ipcdetail::try_based_lock(*this); } +{ +#ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING + boost::posix_time::ptime wait_time + = microsec_clock::universal_time() + + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS); + if (!timed_lock(wait_time)) + { + throw interprocess_exception(timeout_when_locking_error + , "Interprocess mutex timeout when locking. Possible deadlock: " + "owner died without unlocking?"); + } +#else + return ipcdetail::try_based_lock(*this); +#endif +} inline bool spin_mutex::try_lock(void) {