comparison 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
comparison
equal deleted inserted replaced
1856:ecd3152750a5 1857:14c776dad920
18 18
19 #include <QString> 19 #include <QString>
20 #include <QMutex> 20 #include <QMutex>
21 21
22 #include <map> 22 #include <map>
23 #include <atomic>
23 24
24 class Serialiser 25 class Serialiser
25 { 26 {
26 public: 27 public:
28 /**
29 * Construct a serialiser that takes the lock associated with the
30 * given id. That is, the constructor will only complete after all
31 * existing serialisers with the given id have been deleted.
32 */
27 Serialiser(QString id); 33 Serialiser(QString id);
34
35 /**
36 * Construct a cancellable serialiser that takes the lock
37 * associated with the given id. That is, the constructor will
38 * only complete when all existing serialisers with the given id
39 * have been deleted, or when the (occasionally polled) bool flag
40 * pointed to by cancelled has been found to be true.
41 */
42 Serialiser(QString id, const std::atomic<bool> *cancelled);
43
44 /**
45 * Release the lock associated with the given id (if taken, rather
46 * than cancelled).
47 */
28 ~Serialiser(); 48 ~Serialiser();
29 49
30 QString getId() const { return m_id; } 50 QString getId() const { return m_id; }
31 51
32 protected: 52 protected:
33 QString m_id; 53 QString m_id;
54 const std::atomic<bool> *m_cancelled;
55 bool m_locked;
34 static QMutex m_mapMutex; 56 static QMutex m_mapMutex;
35 static std::map<QString, QMutex *> m_mutexMap; 57 static std::map<QString, QMutex *> m_mutexMap;
36 }; 58 };
37 59
38 #endif 60 #endif