Skip to content

Commit

Permalink
Fix some data races flagged when running the AudioPlaybackDemo with X…
Browse files Browse the repository at this point in the history
…code's thread sanitiser enabled
  • Loading branch information
ed95 committed Apr 1, 2019
1 parent 98244f1 commit fb5cfcd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
11 changes: 5 additions & 6 deletions modules/juce_audio_basics/sources/juce_ResamplingAudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ ResamplingAudioSource::ResamplingAudioSource (AudioSource* const inputSource,
const bool deleteInputWhenDeleted,
const int channels)
: input (inputSource, deleteInputWhenDeleted),
ratio (1.0),
lastRatio (1.0),
bufferPos (0),
sampsInBuffer (0),
subSampleOffset (0),
numChannels (channels)
{
jassert (input != nullptr);
Expand Down Expand Up @@ -67,6 +62,8 @@ void ResamplingAudioSource::prepareToPlay (int samplesPerBlockExpected, double s

void ResamplingAudioSource::flushBuffers()
{
const ScopedLock sl (callbackLock);

buffer.clear();
bufferPos = 0;
sampsInBuffer = 0;
Expand All @@ -82,10 +79,12 @@ void ResamplingAudioSource::releaseResources()

void ResamplingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
const ScopedLock sl (callbackLock);

double localRatio;

{
const SpinLock::ScopedLockType sl (ratioLock);
const SpinLock::ScopedLockType ratioSl (ratioLock);
localRatio = ratio;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ class JUCE_API ResamplingAudioSource : public AudioSource
private:
//==============================================================================
OptionalScopedPointer<AudioSource> input;
double ratio, lastRatio;
double ratio = 1.0, lastRatio = 1.0;
AudioBuffer<float> buffer;
int bufferPos, sampsInBuffer;
double subSampleOffset;
int bufferPos = 0, sampsInBuffer = 0;
double subSampleOffset = 0.0;
double coefficients[6];
SpinLock ratioLock;
CriticalSection callbackLock;
const int numChannels;
HeapBlock<float*> destBuffers;
HeapBlock<const float*> srcBuffers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ void AudioTransportSource::stop()
{
if (playing)
{
{
const ScopedLock sl (callbackLock);
playing = false;
}
playing = false;

int n = 500;
while (--n >= 0 && ! stopped)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class JUCE_API AudioTransportSource : public PositionableAudioSource,

CriticalSection callbackLock;
float gain = 1.0f, lastGain = 1.0f;
bool playing = false, stopped = true;
std::atomic<bool> playing { false }, stopped { true };
double sampleRate = 44100.0, sourceSampleRate = 0;
int blockSize = 128, readAheadBufferSize = 0;
bool isPrepared = false, inputStreamEOF = false;
Expand Down
25 changes: 13 additions & 12 deletions modules/juce_audio_utils/gui/juce_AudioThumbnail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class AudioThumbnail::LevelDataSource : public TimeSliceClient
std::unique_ptr<InputSource> source;
std::unique_ptr<AudioFormatReader> reader;
CriticalSection readerLock;
uint32 lastReaderUseTime = 0;
std::atomic<uint32> lastReaderUseTime { 0 };

void createReader()
{
Expand Down Expand Up @@ -645,6 +645,7 @@ bool AudioThumbnail::setDataSource (LevelDataSource* newSource)
JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED

numSamplesFinished = 0;
auto wasSuccessful = [&] { return sampleRate > 0 && totalSamples > 0; };

if (cache.loadThumb (*this, newSource->hashCode) && isFullyLoaded())
{
Expand All @@ -654,22 +655,22 @@ bool AudioThumbnail::setDataSource (LevelDataSource* newSource)
source->sampleRate = sampleRate;
source->numChannels = (unsigned int) numChannels;
source->numSamplesFinished = numSamplesFinished;

return wasSuccessful();
}
else
{
source.reset (newSource); // (make sure this isn't done before loadThumb is called)

const ScopedLock sl (lock);
source->initialise (numSamplesFinished);
source.reset (newSource);

totalSamples = source->lengthInSamples;
sampleRate = source->sampleRate;
numChannels = (int32) source->numChannels;
const ScopedLock sl (lock);
source->initialise (numSamplesFinished);

createChannels (1 + (int) (totalSamples / samplesPerThumbSample));
}
totalSamples = source->lengthInSamples;
sampleRate = source->sampleRate;
numChannels = (int32) source->numChannels;

createChannels (1 + (int) (totalSamples / samplesPerThumbSample));

return sampleRate > 0 && totalSamples > 0;
return wasSuccessful();
}

bool AudioThumbnail::setSource (InputSource* const newSource)
Expand Down

0 comments on commit fb5cfcd

Please sign in to comment.