From f4c7a82acec400a16db88b4b281dce2b89b0efd8 Mon Sep 17 00:00:00 2001 From: Tom Poole Date: Tue, 28 Nov 2017 15:31:07 +0000 Subject: [PATCH] Made TimeHelpers::lastMSCounterValue atomic --- modules/juce_core/time/juce_Time.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/modules/juce_core/time/juce_Time.cpp b/modules/juce_core/time/juce_Time.cpp index e1a3a7d37d..ce49f302d0 100644 --- a/modules/juce_core/time/juce_Time.cpp +++ b/modules/juce_core/time/juce_Time.cpp @@ -177,7 +177,7 @@ namespace TimeHelpers + t.tm_sec; } - static uint32 lastMSCounterValue = 0; + static Atomic lastMSCounterValue { (uint32) 0 }; } //============================================================================== @@ -247,12 +247,12 @@ uint32 Time::getMillisecondCounter() noexcept { auto now = juce_millisecondsSinceStartup(); - if (now < TimeHelpers::lastMSCounterValue) + if (now < TimeHelpers::lastMSCounterValue.get()) { // in multi-threaded apps this might be called concurrently, so // make sure that our last counter value only increases and doesn't // go backwards.. - if (now < TimeHelpers::lastMSCounterValue - 1000) + if (now < TimeHelpers::lastMSCounterValue.get() - (uint32) 1000) TimeHelpers::lastMSCounterValue = now; } else @@ -265,10 +265,8 @@ uint32 Time::getMillisecondCounter() noexcept uint32 Time::getApproximateMillisecondCounter() noexcept { - if (TimeHelpers::lastMSCounterValue == 0) - getMillisecondCounter(); - - return TimeHelpers::lastMSCounterValue; + auto t = TimeHelpers::lastMSCounterValue.get(); + return t == 0 ? getMillisecondCounter() : t; } void Time::waitForMillisecondCounter (uint32 targetTime) noexcept