comparison data/model/Model.h @ 1798:13bd41bd8a17

Some work on making Model classes thread-safe in typical use - and documenting this. Some of the implementations are simpler now that EventSeries is thread-safe
author Chris Cannam
date Tue, 01 Oct 2019 11:22:48 +0100
parents aa0b56d72f27
children c546429d4c2f
comparison
equal deleted inserted replaced
1797:e8b552549225 1798:13bd41bd8a17
15 15
16 #ifndef SV_MODEL_H 16 #ifndef SV_MODEL_H
17 #define SV_MODEL_H 17 #define SV_MODEL_H
18 18
19 #include <vector> 19 #include <vector>
20 #include <atomic>
21
20 #include <QObject> 22 #include <QObject>
23 #include <QMutex>
21 24
22 #include "base/ById.h" 25 #include "base/ById.h"
23 #include "base/XmlExportable.h" 26 #include "base/XmlExportable.h"
24 #include "base/Playable.h" 27 #include "base/Playable.h"
25 #include "base/BaseTypes.h" 28 #include "base/BaseTypes.h"
29 class AlignmentModel; 32 class AlignmentModel;
30 33
31 /** 34 /**
32 * Model is the base class for all data models that represent any sort 35 * Model is the base class for all data models that represent any sort
33 * of data on a time scale based on an audio frame rate. 36 * of data on a time scale based on an audio frame rate.
37 *
38 * Model classes are expected to be thread-safe, particularly with
39 * regard to content rather than metadata (e.g. populating a model
40 * from a transform running in a background thread while displaying it
41 * in a UI layer).
42 *
43 * Never store a pointer to a model unless it is completely private to
44 * the code owning it. Models should be referred to using their
45 * ModelId id and looked up from the ById pool when needed. This
46 * returns a shared pointer, which ensures a sufficient lifespan for a
47 * transient operation locally. See notes in ById.h. The document
48 * unregisters models when it is closed, and then they are deleted
49 * when the last shared pointer instance expires.
34 */ 50 */
35 class Model : public QObject, 51 class Model : public QObject,
36 public WithTypedId<Model>, 52 public WithTypedId<Model>,
37 public XmlExportable, 53 public XmlExportable,
38 public Playable 54 public Playable
299 315
300 private slots: 316 private slots:
301 void alignmentModelCompletionChanged(ModelId); 317 void alignmentModelCompletionChanged(ModelId);
302 318
303 protected: 319 protected:
304 Model() : 320 Model() : m_extendTo(0) { }
305 m_extendTo(0) { }
306 321
307 // Not provided. 322 // Not provided.
308 Model(const Model &) =delete; 323 Model(const Model &) =delete;
309 Model &operator=(const Model &) =delete; 324 Model &operator=(const Model &) =delete;
310 325
326 mutable QMutex m_mutex;
311 ModelId m_sourceModel; 327 ModelId m_sourceModel;
312 ModelId m_alignmentModel; 328 ModelId m_alignmentModel;
313 QString m_typeUri; 329 QString m_typeUri;
314 sv_frame_t m_extendTo; 330 std::atomic<sv_frame_t> m_extendTo;
315 }; 331 };
316 332
317 typedef Model::Id ModelId; 333 typedef Model::Id ModelId;
318 typedef TypedById<Model, Model::Id> ModelById; 334 typedef TypedById<Model, Model::Id> ModelById;
319 335