From 4808fdce304ba31501d141af6f21869d7f12312d Mon Sep 17 00:00:00 2001 From: Anthony Nicholls Date: Fri, 7 Jun 2024 17:05:44 +0100 Subject: [PATCH] CoreAudio: Prevent racing between calls to start and stop a device --- .../native/juce_CoreAudio_mac.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/juce_audio_devices/native/juce_CoreAudio_mac.cpp b/modules/juce_audio_devices/native/juce_CoreAudio_mac.cpp index 6851b5982e..0e596876e0 100644 --- a/modules/juce_audio_devices/native/juce_CoreAudio_mac.cpp +++ b/modules/juce_audio_devices/native/juce_CoreAudio_mac.cpp @@ -1051,7 +1051,7 @@ public: CoreAudioIODevice& owner; int bitDepth = 32; - int xruns = 0; + std::atomic xruns = 0; Array sampleRates; Array bufferSizes; AudioDeviceID deviceID; @@ -1159,7 +1159,7 @@ private: return x.mSelector == kAudioDeviceProcessorOverload; }); - intern.xruns += xruns; + intern.xruns += (int) xruns; const auto detailsChanged = std::any_of (pa, pa + numAddresses, [] (const AudioObjectPropertyAddress& x) { @@ -1325,6 +1325,8 @@ public: void start (AudioIODeviceCallback* callback) override { + const ScopedLock sl (startStopLock); + if (internal->start (callback)) pendingCallback = nullptr; } @@ -1332,12 +1334,14 @@ public: void stop() override { stopAndGetLastCallback(); + + const ScopedLock sl (startStopLock); pendingCallback = nullptr; } void stopWithPendingCallback() { - const ScopedLock sl (closeLock); + const ScopedLock sl (startStopLock); if (pendingCallback == nullptr) pendingCallback = stopAndGetLastCallback(); @@ -1399,7 +1403,7 @@ private: AudioIODeviceCallback* pendingCallback = nullptr; AsyncRestarter* restarter = nullptr; BigInteger inputChannelsRequested, outputChannelsRequested; - CriticalSection closeLock; + CriticalSection startStopLock; AudioIODeviceCallback* stopAndGetLastCallback() const { @@ -1424,6 +1428,7 @@ private: getCurrentSampleRate(), getCurrentBufferSizeSamples()); + const ScopedLock sl { startStopLock }; start (pendingCallback); }