changeset 1858:14747f24ad04

Use cancellable serialiser; add some more profiling points
author Chris Cannam
date Thu, 14 May 2020 16:36:48 +0100
parents 14c776dad920
children 4c5736a517e0 edc2d0e635dc
files data/fileio/BQAFileReader.cpp data/fileio/BQAFileReader.h data/fileio/CodedAudioFileReader.cpp data/fileio/CodedAudioFileReader.h data/fileio/DecodingWavFileReader.cpp data/fileio/DecodingWavFileReader.h data/fileio/MP3FileReader.cpp data/fileio/MP3FileReader.h data/fileio/WavFileReader.cpp data/model/ReadOnlyWaveFileModel.cpp data/model/ReadOnlyWaveFileModel.h
diffstat 11 files changed, 56 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/BQAFileReader.cpp	Thu May 14 16:35:39 2020 +0100
+++ b/data/fileio/BQAFileReader.cpp	Thu May 14 16:36:48 2020 +0100
@@ -115,12 +115,14 @@
 
 BQAFileReader::~BQAFileReader()
 {
+    Profiler profiler("BQAFileReader::~BQAFileReader");
+    
     if (m_decodeThread) {
         m_cancelled = true;
         m_decodeThread->wait();
         delete m_decodeThread;
     }
-    
+
     delete m_stream;
 }
 
@@ -134,7 +136,11 @@
 BQAFileReader::DecodeThread::run()
 {
     if (m_reader->m_cacheMode == CacheInTemporaryFile) {
-        m_reader->startSerialised("BQAFileReader::Decode");
+        m_reader->startSerialised("BQAFileReader::Decode",
+                                  &m_reader->m_cancelled);
+        if (m_reader->m_cancelled) {
+            return;
+        }
     }
 
     sv_frame_t blockSize = 65536;
--- a/data/fileio/BQAFileReader.h	Thu May 14 16:35:39 2020 +0100
+++ b/data/fileio/BQAFileReader.h	Thu May 14 16:36:48 2020 +0100
@@ -21,6 +21,7 @@
 #include "base/Thread.h"
 
 #include <set>
+#include <atomic>
 
 class ProgressReporter;
 
@@ -69,7 +70,7 @@
 
     breakfastquay::AudioReadStream *m_stream;
 
-    bool m_cancelled;
+    std::atomic<bool> m_cancelled;
     int m_completion;
     ProgressReporter *m_reporter;
     
--- a/data/fileio/CodedAudioFileReader.cpp	Thu May 14 16:35:39 2020 +0100
+++ b/data/fileio/CodedAudioFileReader.cpp	Thu May 14 16:36:48 2020 +0100
@@ -105,18 +105,19 @@
 }
 
 void
-CodedAudioFileReader::startSerialised(QString id)
+CodedAudioFileReader::startSerialised(QString id,
+                                      const std::atomic<bool> *cancelled)
 {
-    SVDEBUG << "CodedAudioFileReader(" << this << ")::startSerialised: id = " << id << endl;
-
+//    SVCERR << "CodedAudioFileReader(" << this << ")::startSerialised: id = " << id << endl;
+    
     delete m_serialiser;
-    m_serialiser = new Serialiser(id);
+    m_serialiser = new Serialiser(id, cancelled);
 }
 
 void
 CodedAudioFileReader::endSerialised()
 {
-    SVDEBUG << "CodedAudioFileReader(" << this << ")::endSerialised: id = " << (m_serialiser ? m_serialiser->getId() : "(none)") << endl;
+//    SVCERR << "CodedAudioFileReader(" << this << ")::endSerialised: id = " << (m_serialiser ? m_serialiser->getId() : "(none)") << endl;
 
     delete m_serialiser;
     m_serialiser = nullptr;
@@ -548,6 +549,8 @@
 floatvec_t
 CodedAudioFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count) const
 {
+    Profiler profiler("CodedAudioFileReader::getInterleavedFrames");
+    
     // Lock is only required in CacheInMemory mode (the cache file
     // reader is expected to be thread safe and manage its own
     // locking)
--- a/data/fileio/CodedAudioFileReader.h	Thu May 14 16:35:39 2020 +0100
+++ b/data/fileio/CodedAudioFileReader.h	Thu May 14 16:36:48 2020 +0100
@@ -28,6 +28,8 @@
 
 #include <sndfile.h>
 
+#include <atomic>
+
 class WavFileReader;
 class Serialiser;
 
@@ -84,7 +86,7 @@
 
     bool isDecodeCacheInitialised() const { return m_initialised; }
 
-    void startSerialised(QString id);
+    void startSerialised(QString id, const std::atomic<bool> *cancelled);
     void endSerialised();
 
 private:
--- a/data/fileio/DecodingWavFileReader.cpp	Thu May 14 16:35:39 2020 +0100
+++ b/data/fileio/DecodingWavFileReader.cpp	Thu May 14 16:36:48 2020 +0100
@@ -125,7 +125,11 @@
 DecodingWavFileReader::DecodeThread::run()
 {
     if (m_reader->m_cacheMode == CacheInTemporaryFile) {
-        m_reader->startSerialised("DecodingWavFileReader::Decode");
+        m_reader->startSerialised("DecodingWavFileReader::Decode",
+                                  &m_reader->m_cancelled);
+        if (m_reader->m_cancelled) {
+            return;
+        }
     }
 
     sv_frame_t blockSize = 16384;
--- a/data/fileio/DecodingWavFileReader.h	Thu May 14 16:35:39 2020 +0100
+++ b/data/fileio/DecodingWavFileReader.h	Thu May 14 16:36:48 2020 +0100
@@ -21,6 +21,7 @@
 #include "base/Thread.h"
 
 #include <set>
+#include <atomic>
 
 class WavFileReader;
 class ProgressReporter;
@@ -63,7 +64,7 @@
     QString m_maker;
     QString m_path;
     QString m_error;
-    bool m_cancelled;
+    std::atomic<bool> m_cancelled;
     sv_frame_t m_processed;
     int m_completion;
 
--- a/data/fileio/MP3FileReader.cpp	Thu May 14 16:35:39 2020 +0100
+++ b/data/fileio/MP3FileReader.cpp	Thu May 14 16:36:48 2020 +0100
@@ -17,6 +17,7 @@
 
 #include "MP3FileReader.h"
 #include "base/ProgressReporter.h"
+#include "base/Profiler.h"
 
 #include "system/System.h"
 
@@ -173,6 +174,8 @@
 
 MP3FileReader::~MP3FileReader()
 {
+    Profiler profiler("MP3FileReader::~MP3FileReader");
+    
     if (m_decodeThread) {
         m_cancelled = true;
         m_decodeThread->wait();
@@ -302,7 +305,9 @@
         m_reader->m_sampleBuffer = nullptr;
     }
 
-    if (m_reader->isDecodeCacheInitialised()) m_reader->finishDecodeCache();
+    if (m_reader->isDecodeCacheInitialised()) {
+        m_reader->finishDecodeCache();
+    }
 
     m_reader->m_done = true;
     m_reader->m_completion = 100;
@@ -505,7 +510,10 @@
 
         if (m_cacheMode == CacheInTemporaryFile) {
 //            SVDEBUG << "MP3FileReader::accept: channel count " << m_channelCount << ", file rate " << m_fileRate << ", about to start serialised section" << endl;
-            startSerialised("MP3FileReader::Decode");
+            startSerialised("MP3FileReader::Decode", &m_cancelled);
+            if (m_cancelled) {
+                return MAD_FLOW_STOP;
+            }
         }
     }
     
--- a/data/fileio/MP3FileReader.h	Thu May 14 16:35:39 2020 +0100
+++ b/data/fileio/MP3FileReader.h	Thu May 14 16:36:48 2020 +0100
@@ -24,6 +24,7 @@
 #include <mad.h>
 
 #include <set>
+#include <atomic>
 
 class ProgressReporter;
 
@@ -117,7 +118,7 @@
     size_t m_sampleBufferSize;
 
     ProgressReporter *m_reporter;
-    bool m_cancelled;
+    std::atomic<bool> m_cancelled;
 
     bool m_decodeErrorShown;
 
--- a/data/fileio/WavFileReader.cpp	Thu May 14 16:35:39 2020 +0100
+++ b/data/fileio/WavFileReader.cpp	Thu May 14 16:36:48 2020 +0100
@@ -111,6 +111,8 @@
 
 WavFileReader::~WavFileReader()
 {
+    Profiler profiler("WavFileReader::~WavFileReader");
+    
     if (m_file) sf_close(m_file);
 }
 
--- a/data/model/ReadOnlyWaveFileModel.cpp	Thu May 14 16:35:39 2020 +0100
+++ b/data/model/ReadOnlyWaveFileModel.cpp	Thu May 14 16:36:48 2020 +0100
@@ -54,6 +54,8 @@
     m_lastDirectReadStart(0),
     m_lastDirectReadCount(0)
 {
+    Profiler profiler("ReadOnlyWaveFileModel::ReadOnlyWaveFileModel");
+
     SVDEBUG << "ReadOnlyWaveFileModel::ReadOnlyWaveFileModel: path "
             << m_path << ", target rate " << targetRate << endl;
     
@@ -103,6 +105,8 @@
     m_prevCompletion(0),
     m_exiting(false)
 {
+    Profiler profiler("ReadOnlyWaveFileModel::ReadOnlyWaveFileModel (with reader)");
+
     SVDEBUG << "ReadOnlyWaveFileModel::ReadOnlyWaveFileModel: path "
             << m_path << ", with reader" << endl;
     
@@ -117,6 +121,8 @@
 
 ReadOnlyWaveFileModel::~ReadOnlyWaveFileModel()
 {
+    Profiler profiler("ReadOnlyWaveFileModel::~ReadOnlyWaveFileModel");
+    
     PlayParameterRepository::getInstance()->removePlayable
         (getId().untyped);
     
@@ -653,6 +659,9 @@
             sleep(1);
             channels = m_model.getChannelCount();
         }
+        if (m_model.m_exiting) {
+            return;
+        }
     }
 
     Range *range = new Range[2 * channels];
@@ -734,7 +743,8 @@
         first = false;
         if (m_model.m_exiting) break;
         if (updating) {
-            sleep(1);
+            usleep(100000);
+            if (m_model.m_exiting) break;
         }
     }
 
--- a/data/model/ReadOnlyWaveFileModel.h	Thu May 14 16:35:39 2020 +0100
+++ b/data/model/ReadOnlyWaveFileModel.h	Thu May 14 16:36:48 2020 +0100
@@ -29,6 +29,8 @@
 
 #include <stdlib.h>
 
+#include <atomic>
+
 class AudioFileReader;
 
 class ReadOnlyWaveFileModel : public WaveFileModel
@@ -137,7 +139,7 @@
     QTimer *m_updateTimer;
     sv_frame_t m_lastFillExtent;
     mutable int m_prevCompletion;
-    bool m_exiting;
+    std::atomic<bool> m_exiting;
     static PowerOfSqrtTwoZoomConstraint m_zoomConstraint;
 
     mutable floatvec_t m_directRead;