From 6773fc2f4f34db8d3e7cd2cda258da0bf5fe0afc Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 1 Aug 2014 09:11:30 +0100 Subject: [PATCH] Modified the posix HighResolutionTimer to cope with being started/stopped from its own callback. --- .../juce_core/native/juce_posix_SharedCode.h | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index 05ed874c76..2636cea5a7 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -1146,15 +1146,23 @@ struct HighResolutionTimer::Pimpl { if (periodMs != newPeriod) { - stop(); - periodMs = newPeriod; + if (thread != pthread_self()) + { + stop(); - shouldStop = false; + periodMs = newPeriod; + shouldStop = false; - if (pthread_create (&thread, nullptr, timerThread, this) == 0) - setThreadToRealtime (thread, (uint64) newPeriod); + if (pthread_create (&thread, nullptr, timerThread, this) == 0) + setThreadToRealtime (thread, (uint64) newPeriod); + else + jassertfalse; + } else - jassertfalse; + { + periodMs = newPeriod; + shouldStop = false; + } } } @@ -1191,12 +1199,19 @@ private: void timerThread() { - Clock clock (periodMs); + int lastPeriod = periodMs; + Clock clock (lastPeriod); while (! shouldStop) { clock.wait(); owner.hiResTimerCallback(); + + if (lastPeriod != periodMs) + { + lastPeriod = periodMs; + clock = Clock (lastPeriod); + } } periodMs = 0;