# HG changeset patch # User Chris Cannam # Date 1453456350 0 # Node ID def42cbba8bc92641b57aa4cf35ebb76f4740301 # Parent fc4cb390131656932525049989eee1dd1ba01fd0 Adjust locking when filling range cache in background thread, so as to spend less time waiting on locks diff -r fc4cb3901316 -r def42cbba8bc data/model/ReadOnlyWaveFileModel.cpp --- a/data/model/ReadOnlyWaveFileModel.cpp Fri Jan 22 09:52:08 2016 +0000 +++ b/data/model/ReadOnlyWaveFileModel.cpp Fri Jan 22 09:52:30 2016 +0000 @@ -193,7 +193,7 @@ // playback or input to transforms. #ifdef DEBUG_WAVE_FILE_MODEL - cout << "ReadOnlyWaveFileModel::getData[" << this << "]: " << channel << ", " << start << ", " << count << ", " << buffer << endl; + cout << "ReadOnlyWaveFileModel::getData[" << this << "]: " << channel << ", " << start << ", " << count << endl; #endif int channels = getChannelCount(); @@ -252,7 +252,7 @@ // playback or input to transforms. #ifdef DEBUG_WAVE_FILE_MODEL - cout << "ReadOnlyWaveFileModel::getData[" << this << "]: " << fromchannel << "," << tochannel << ", " << start << ", " << count << ", " << buffer << endl; + cout << "ReadOnlyWaveFileModel::getData[" << this << "]: " << fromchannel << "," << tochannel << ", " << start << ", " << count << endl; #endif int channels = getChannelCount(); @@ -322,7 +322,7 @@ void ReadOnlyWaveFileModel::getSummaries(int channel, sv_frame_t start, sv_frame_t count, - RangeBlock &ranges, int &blockSize) const + RangeBlock &ranges, int &blockSize) const { ranges.clear(); if (!isOK()) return; @@ -448,7 +448,7 @@ } #ifdef DEBUG_WAVE_FILE_MODEL - SVDEBUG << "returning " << ranges.size() << " ranges" << endl; + cerr << "returning " << ranges.size() << " ranges" << endl; #endif return; } @@ -573,7 +573,7 @@ sqrt(2.) + 0.01)); sv_frame_t frame = 0; - const sv_frame_t readBlockSize = 16384; + const sv_frame_t readBlockSize = 32768; vector block; if (!m_model.isOK()) return; @@ -583,7 +583,9 @@ if (updating) { while (channels == 0 && !m_model.m_exiting) { -// SVDEBUG << "ReadOnlyWaveFileModel::fill: Waiting for channels..." << endl; +#ifdef DEBUG_WAVE_FILE_MODEL + cerr << "ReadOnlyWaveFileModel::fill: Waiting for channels..." << endl; +#endif sleep(1); channels = m_model.getChannelCount(); } @@ -604,38 +606,38 @@ updating = m_model.m_reader->isUpdating(); m_frameCount = m_model.getFrameCount(); -// SVDEBUG << "ReadOnlyWaveFileModel::fill: frame = " << frame << ", count = " << m_frameCount << endl; + m_model.m_mutex.lock(); while (frame < m_frameCount) { -// SVDEBUG << "ReadOnlyWaveFileModel::fill inner loop: frame = " << frame << ", count = " << m_frameCount << ", blocksize " << readBlockSize << endl; + m_model.m_mutex.unlock(); + +#ifdef DEBUG_WAVE_FILE_MODEL + cerr << "ReadOnlyWaveFileModel::fill inner loop: frame = " << frame << ", count = " << m_frameCount << ", blocksize " << readBlockSize << endl; +#endif if (updating && (frame + readBlockSize > m_frameCount)) break; block = m_model.m_reader->getInterleavedFrames(frame, readBlockSize); -// cerr << "block is " << block.size() << endl; + sv_frame_t gotBlockSize = block.size() / channels; - for (sv_frame_t i = 0; i < readBlockSize; ++i) { + m_model.m_mutex.lock(); + + for (sv_frame_t i = 0; i < gotBlockSize; ++i) { - if (channels * i + channels > (int)block.size()) break; - for (int ch = 0; ch < channels; ++ch) { sv_frame_t index = channels * i + ch; float sample = block[index]; - for (int cacheType = 0; cacheType < 2; ++cacheType) { // cache type - + for (int cacheType = 0; cacheType < 2; ++cacheType) { sv_frame_t rangeIndex = ch * 2 + cacheType; range[rangeIndex].sample(sample); means[rangeIndex] += fabsf(sample); } } - //!!! this looks like a ludicrous way to do synchronisation - QMutexLocker locker(&m_model.m_mutex); - for (int cacheType = 0; cacheType < 2; ++cacheType) { if (++count[cacheType] == cacheBlockSize[cacheType]) { @@ -655,18 +657,16 @@ ++frame; } - + if (m_model.m_exiting) break; - m_fillExtent = frame; } -// cerr << "ReadOnlyWaveFileModel: inner loop ended" << endl; - + m_model.m_mutex.unlock(); + first = false; if (m_model.m_exiting) break; if (updating) { -// cerr << "sleeping..." << endl; sleep(1); } }