comparison base/Serialiser.cpp @ 406:d095214ffbaf

* Fix deadlock in Serialiser
author Chris Cannam
date Tue, 29 Apr 2008 10:59:19 +0000
parents be49bf95d4a5
children 14c776dad920
comparison
equal deleted inserted replaced
405:65311fb86166 406:d095214ffbaf
13 COPYING included with this distribution for more information. 13 COPYING included with this distribution for more information.
14 */ 14 */
15 15
16 #include "Serialiser.h" 16 #include "Serialiser.h"
17 17
18 #include <iostream>
19
18 QMutex 20 QMutex
19 Serialiser::m_mapMutex; 21 Serialiser::m_mapMutex;
20 22
21 std::map<QString, QMutex *> 23 std::map<QString, QMutex *>
22 Serialiser::m_mutexMap; 24 Serialiser::m_mutexMap;
28 30
29 if (m_mutexMap.find(m_id) == m_mutexMap.end()) { 31 if (m_mutexMap.find(m_id) == m_mutexMap.end()) {
30 m_mutexMap[m_id] = new QMutex; 32 m_mutexMap[m_id] = new QMutex;
31 } 33 }
32 34
33 m_mutexMap[m_id]->lock(); 35 // The id mutexes are never deleted, so once we have a reference
36 // to the one we need, we can hold on to it while we release the
37 // map mutex. We need to release the map mutex, otherwise if the
38 // id mutex is currently held, it will never be released (because
39 // the destructor needs to hold the map mutex to release the id
40 // mutex).
41
42 QMutex *idMutex = m_mutexMap[m_id];
34 43
35 m_mapMutex.unlock(); 44 m_mapMutex.unlock();
45
46 idMutex->lock();
36 } 47 }
37 48
38 Serialiser::~Serialiser() 49 Serialiser::~Serialiser()
39 { 50 {
40 m_mapMutex.lock(); 51 m_mapMutex.lock();