annotate base/Serialiser.h @ 1857:14c776dad920

Make Serialiser cancellable while waiting for its lock
author Chris Cannam
date Thu, 14 May 2020 16:35:39 +0100
parents ad5f892c0c4d
children
rev   line source
Chris@297 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@297 2
Chris@297 3 /*
Chris@297 4 Sonic Visualiser
Chris@297 5 An audio file viewer and annotation editor.
Chris@297 6 Centre for Digital Music, Queen Mary, University of London.
Chris@297 7 This file copyright 2007 QMUL.
Chris@297 8
Chris@297 9 This program is free software; you can redistribute it and/or
Chris@297 10 modify it under the terms of the GNU General Public License as
Chris@297 11 published by the Free Software Foundation; either version 2 of the
Chris@297 12 License, or (at your option) any later version. See the file
Chris@297 13 COPYING included with this distribution for more information.
Chris@297 14 */
Chris@297 15
Chris@1581 16 #ifndef SV_SERIALISER_H
Chris@1581 17 #define SV_SERIALISER_H
Chris@297 18
Chris@297 19 #include <QString>
Chris@297 20 #include <QMutex>
Chris@297 21
Chris@297 22 #include <map>
Chris@1857 23 #include <atomic>
Chris@297 24
Chris@297 25 class Serialiser
Chris@297 26 {
Chris@297 27 public:
Chris@1857 28 /**
Chris@1857 29 * Construct a serialiser that takes the lock associated with the
Chris@1857 30 * given id. That is, the constructor will only complete after all
Chris@1857 31 * existing serialisers with the given id have been deleted.
Chris@1857 32 */
Chris@297 33 Serialiser(QString id);
Chris@1857 34
Chris@1857 35 /**
Chris@1857 36 * Construct a cancellable serialiser that takes the lock
Chris@1857 37 * associated with the given id. That is, the constructor will
Chris@1857 38 * only complete when all existing serialisers with the given id
Chris@1857 39 * have been deleted, or when the (occasionally polled) bool flag
Chris@1857 40 * pointed to by cancelled has been found to be true.
Chris@1857 41 */
Chris@1857 42 Serialiser(QString id, const std::atomic<bool> *cancelled);
Chris@1857 43
Chris@1857 44 /**
Chris@1857 45 * Release the lock associated with the given id (if taken, rather
Chris@1857 46 * than cancelled).
Chris@1857 47 */
Chris@297 48 ~Serialiser();
Chris@297 49
Chris@406 50 QString getId() const { return m_id; }
Chris@406 51
Chris@297 52 protected:
Chris@297 53 QString m_id;
Chris@1857 54 const std::atomic<bool> *m_cancelled;
Chris@1857 55 bool m_locked;
Chris@297 56 static QMutex m_mapMutex;
Chris@297 57 static std::map<QString, QMutex *> m_mutexMap;
Chris@297 58 };
Chris@297 59
Chris@297 60 #endif