svcore  1.9
Serialiser.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2007 QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "Serialiser.h"
00017 
00018 #include <iostream>
00019 
00020 QMutex
00021 Serialiser::m_mapMutex;
00022 
00023 std::map<QString, QMutex *>
00024 Serialiser::m_mutexMap;
00025 
00026 Serialiser::Serialiser(QString id) :
00027     m_id(id)
00028 {
00029     m_mapMutex.lock();
00030     
00031     if (m_mutexMap.find(m_id) == m_mutexMap.end()) {
00032         m_mutexMap[m_id] = new QMutex;
00033     }
00034 
00035     // The id mutexes are never deleted, so once we have a reference
00036     // to the one we need, we can hold on to it while we release the
00037     // map mutex.  We need to release the map mutex, otherwise if the
00038     // id mutex is currently held, it will never be released (because
00039     // the destructor needs to hold the map mutex to release the id
00040     // mutex).
00041 
00042     QMutex *idMutex = m_mutexMap[m_id];
00043 
00044     m_mapMutex.unlock();
00045 
00046     idMutex->lock();
00047 }
00048 
00049 Serialiser::~Serialiser()
00050 {
00051     m_mapMutex.lock();
00052     
00053     m_mutexMap[m_id]->unlock();
00054 
00055     m_mapMutex.unlock();
00056 }
00057 
00058 
00059