src: Fixed shared memory garbage collector mutex stuck
* Try unlocking mutex 5 times * Then force ownership * If everything fails run without garbage collector
This commit is contained in:
@@ -134,7 +134,7 @@ Application::~Application()
|
||||
m_mainView->saveLayout();
|
||||
}
|
||||
|
||||
SharedMemoryGarbageCollector* collector = SharedMemoryGarbageCollector::createInstance();
|
||||
SharedMemoryGarbageCollector* collector = SharedMemoryGarbageCollector::getInstance();
|
||||
if (collector)
|
||||
{
|
||||
collector->stop();
|
||||
|
||||
@@ -168,9 +168,36 @@ SharedMemory::~SharedMemory()
|
||||
}
|
||||
}
|
||||
|
||||
void SharedMemory::unlockSharedMutex()
|
||||
bool SharedMemory::checkSharedMutex()
|
||||
{
|
||||
getMutex().unlock();
|
||||
try
|
||||
{
|
||||
boost::interprocess::named_mutex& mutex = getMutex();
|
||||
|
||||
for (size_t i = 0; i < 5; i++)
|
||||
{
|
||||
{
|
||||
// try to get ownership of the mutex a couple times
|
||||
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(mutex, boost::interprocess::try_to_lock);
|
||||
if (lock.owns()) // mutex successfully locked
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
}
|
||||
|
||||
// locking kept failing, try to get ownership
|
||||
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(mutex, boost::interprocess::accept_ownership);
|
||||
return true;
|
||||
}
|
||||
catch (boost::interprocess::interprocess_exception& e)
|
||||
{
|
||||
LOG_ERROR_STREAM(<< "boost exception thrown at shared mutex check: " << e.what());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string SharedMemory::getMemoryName() const
|
||||
|
||||
@@ -100,7 +100,7 @@ public:
|
||||
std::string m_memoryName;
|
||||
};
|
||||
|
||||
void unlockSharedMutex();
|
||||
bool checkSharedMutex();
|
||||
|
||||
private:
|
||||
static const char* s_memoryNamePrefix;
|
||||
|
||||
@@ -22,10 +22,17 @@ SharedMemoryGarbageCollector* SharedMemoryGarbageCollector::createInstance()
|
||||
if (!s_instance)
|
||||
{
|
||||
s_instance = std::shared_ptr<SharedMemoryGarbageCollector>(new SharedMemoryGarbageCollector());
|
||||
|
||||
if (!s_instance->m_memory.checkSharedMutex())
|
||||
{
|
||||
LOG_ERROR_STREAM(<< "Shared memory mutex check failed. Shared memory garbage collection disabled.");
|
||||
s_instance.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (boost::interprocess::interprocess_exception)
|
||||
catch (boost::interprocess::interprocess_exception& e)
|
||||
{
|
||||
LOG_ERROR_STREAM(<< "boost exception thrown at shared memory garbage collector: " << e.what());
|
||||
}
|
||||
|
||||
return s_instance.get();
|
||||
@@ -40,7 +47,6 @@ SharedMemoryGarbageCollector::SharedMemoryGarbageCollector()
|
||||
: m_memory(getMemoryName(), 65536 /* 64 kB */, SharedMemory::OPEN_OR_CREATE)
|
||||
, m_loopIsRunning(false)
|
||||
{
|
||||
m_memory.unlockSharedMutex();
|
||||
}
|
||||
|
||||
SharedMemoryGarbageCollector::~SharedMemoryGarbageCollector()
|
||||
|
||||
Reference in New Issue
Block a user