comparison data/model/SparseTimeValueModel.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 6d09d68165a4
children 343ef2a866a4
comparison
equal deleted inserted replaced
1797:e8b552549225 1798:13bd41bd8a17
103 int getResolution() const { return m_resolution; } 103 int getResolution() const { return m_resolution; }
104 104
105 bool canPlay() const override { return true; } 105 bool canPlay() const override { return true; }
106 bool getDefaultPlayAudible() const override { return false; } // user must unmute 106 bool getDefaultPlayAudible() const override { return false; } // user must unmute
107 107
108 QString getScaleUnits() const { return m_units; } 108 QString getScaleUnits() const {
109 QMutexLocker locker(&m_mutex);
110 return m_units;
111 }
109 void setScaleUnits(QString units) { 112 void setScaleUnits(QString units) {
113 QMutexLocker locker(&m_mutex);
110 m_units = units; 114 m_units = units;
111 UnitDatabase::getInstance()->registerUnit(units); 115 UnitDatabase::getInstance()->registerUnit(units);
112 } 116 }
113 117
114 bool hasTextLabels() const { return m_haveTextLabels; } 118 bool hasTextLabels() const { return m_haveTextLabels; }
118 122
119 int getCompletion() const override { return m_completion; } 123 int getCompletion() const override { return m_completion; }
120 124
121 void setCompletion(int completion, bool update = true) { 125 void setCompletion(int completion, bool update = true) {
122 126
123 { QMutexLocker locker(&m_mutex); 127 {
124 if (m_completion == completion) return; 128 if (m_completion == completion) return;
125 m_completion = completion; 129 m_completion = completion;
126 } 130 }
127 131
128 if (update) { 132 if (update) {
183 */ 187 */
184 void add(Event e) override { 188 void add(Event e) override {
185 189
186 bool allChange = false; 190 bool allChange = false;
187 191
188 { QMutexLocker locker(&m_mutex); 192 m_events.add(e.withoutDuration()); // can't have duration here
189 m_events.add(e.withoutDuration()); // can't have duration here 193
190 194 if (e.getLabel() != "") {
191 if (e.getLabel() != "") { 195 m_haveTextLabels = true;
192 m_haveTextLabels = true; 196 }
197
198 float v = e.getValue();
199 if (!ISNAN(v) && !ISINF(v)) {
200 if (!m_haveExtents || v < m_valueMinimum) {
201 m_valueMinimum = v; allChange = true;
193 } 202 }
194 203 if (!m_haveExtents || v > m_valueMaximum) {
195 float v = e.getValue(); 204 m_valueMaximum = v; allChange = true;
196 if (!ISNAN(v) && !ISINF(v)) {
197 if (!m_haveExtents || v < m_valueMinimum) {
198 m_valueMinimum = v; allChange = true;
199 }
200 if (!m_haveExtents || v > m_valueMaximum) {
201 m_valueMaximum = v; allChange = true;
202 }
203 m_haveExtents = true;
204 } 205 }
206 m_haveExtents = true;
205 } 207 }
206 208
207 m_notifier.update(e.getFrame(), m_resolution); 209 m_notifier.update(e.getFrame(), m_resolution);
208 210
209 if (allChange) { 211 if (allChange) {
210 emit modelChanged(getId()); 212 emit modelChanged(getId());
211 } 213 }
212 } 214 }
213 215
214 void remove(Event e) override { 216 void remove(Event e) override {
215 { 217 m_events.remove(e);
216 QMutexLocker locker(&m_mutex);
217 m_events.remove(e);
218 }
219 emit modelChangedWithin(getId(), 218 emit modelChangedWithin(getId(),
220 e.getFrame(), e.getFrame() + m_resolution); 219 e.getFrame(), e.getFrame() + m_resolution);
221 } 220 }
222 221
223 /** 222 /**
342 341
343 protected: 342 protected:
344 sv_samplerate_t m_sampleRate; 343 sv_samplerate_t m_sampleRate;
345 int m_resolution; 344 int m_resolution;
346 345
347 float m_valueMinimum; 346 std::atomic<float> m_valueMinimum;
348 float m_valueMaximum; 347 std::atomic<float> m_valueMaximum;
349 bool m_haveExtents; 348 std::atomic<bool> m_haveExtents;
350 bool m_haveTextLabels; 349 std::atomic<bool> m_haveTextLabels;
351 QString m_units; 350 QString m_units;
352 DeferredNotifier m_notifier; 351 DeferredNotifier m_notifier;
353 int m_completion; 352 std::atomic<int> m_completion;
354 353
355 EventSeries m_events; 354 EventSeries m_events;
356
357 mutable QMutex m_mutex;
358 }; 355 };
359 356
360
361 #endif 357 #endif
362 358
363 359
364 360