From 47be26deedf788f1c769be575397a6d5de6af999 Mon Sep 17 00:00:00 2001 From: Anthony Nicholls Date: Mon, 27 Nov 2023 14:08:26 +0000 Subject: [PATCH] Timer: Replace DeletedAtShutdown with SharedResourcePointer --- modules/juce_events/timers/juce_Timer.cpp | 99 +++++++++-------------- modules/juce_events/timers/juce_Timer.h | 2 +- 2 files changed, 37 insertions(+), 64 deletions(-) diff --git a/modules/juce_events/timers/juce_Timer.cpp b/modules/juce_events/timers/juce_Timer.cpp index 79cec02508..7475fb0857 100644 --- a/modules/juce_events/timers/juce_Timer.cpp +++ b/modules/juce_events/timers/juce_Timer.cpp @@ -24,7 +24,6 @@ namespace juce { class Timer::TimerThread final : private Thread, - private DeletedAtShutdown, private AsyncUpdater { public: @@ -42,10 +41,6 @@ public: signalThreadShouldExit(); callbackArrived.signal(); stopThread (4000); - jassert (instance == this || instance == nullptr); - - if (instance == this) - instance = nullptr; } void run() override @@ -137,54 +132,10 @@ public: callTimers(); } - static void add (Timer* tim) noexcept - { - if (instance == nullptr) - instance = new TimerThread(); - - instance->addTimer (tim); - } - - static void remove (Timer* tim) noexcept - { - if (instance != nullptr) - instance->removeTimer (tim); - } - - static void resetCounter (Timer* tim) noexcept - { - if (instance != nullptr) - instance->resetTimerCounter (tim); - } - - static TimerThread* instance; - static LockType lock; - -private: - struct TimerCountdown - { - Timer* timer; - int countdownMs; - }; - - std::vector timers; - - WaitableEvent callbackArrived; - - struct CallTimersMessage final : public MessageManager::MessageBase - { - CallTimersMessage() {} - - void messageCallback() override - { - if (instance != nullptr) - instance->callTimers(); - } - }; - - //============================================================================== void addTimer (Timer* t) { + const LockType::ScopedLockType sl (lock); + // Trying to add a timer that's already here - shouldn't get to this point, // so if you get this assertion, let me know! jassert (std::none_of (timers.begin(), timers.end(), @@ -200,6 +151,8 @@ private: void removeTimer (Timer* t) { + const LockType::ScopedLockType sl (lock); + auto pos = t->positionInQueue; auto lastIndex = timers.size() - 1; @@ -217,6 +170,8 @@ private: void resetTimerCounter (Timer* t) noexcept { + const LockType::ScopedLockType sl (lock); + auto pos = t->positionInQueue; jassert (pos < timers.size()); @@ -238,6 +193,31 @@ private: } } +private: + LockType lock; + + struct TimerCountdown + { + Timer* timer; + int countdownMs; + }; + + std::vector timers; + + WaitableEvent callbackArrived; + + struct CallTimersMessage final : public MessageManager::MessageBase + { + CallTimersMessage() = default; + + void messageCallback() override + { + if (auto instance = SharedResourcePointer::getSharedObjectWithoutCreating()) + (*instance)->callTimers(); + } + }; + + //============================================================================== void shuffleTimerBackInQueue (size_t pos) { auto numTimers = timers.size(); @@ -309,9 +289,6 @@ private: JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TimerThread) }; -Timer::TimerThread* Timer::TimerThread::instance = nullptr; -Timer::TimerThread::LockType Timer::TimerThread::lock; - //============================================================================== Timer::Timer() noexcept {} Timer::Timer (const Timer&) noexcept {} @@ -335,15 +312,13 @@ void Timer::startTimer (int interval) noexcept // running, then you're not going to get any timer callbacks! JUCE_ASSERT_MESSAGE_MANAGER_EXISTS - const TimerThread::LockType::ScopedLockType sl (TimerThread::lock); - bool wasStopped = (timerPeriodMs == 0); timerPeriodMs = jmax (1, interval); if (wasStopped) - TimerThread::add (this); + timerThread->addTimer (this); else - TimerThread::resetCounter (this); + timerThread->resetTimerCounter (this); } void Timer::startTimerHz (int timerFrequencyHz) noexcept @@ -356,19 +331,17 @@ void Timer::startTimerHz (int timerFrequencyHz) noexcept void Timer::stopTimer() noexcept { - const TimerThread::LockType::ScopedLockType sl (TimerThread::lock); - if (timerPeriodMs > 0) { - TimerThread::remove (this); + timerThread->removeTimer (this); timerPeriodMs = 0; } } void JUCE_CALLTYPE Timer::callPendingTimersSynchronously() { - if (TimerThread::instance != nullptr) - TimerThread::instance->callTimersSynchronously(); + if (auto instance = SharedResourcePointer::getSharedObjectWithoutCreating()) + (*instance)->callTimersSynchronously(); } struct LambdaInvoker final : private Timer diff --git a/modules/juce_events/timers/juce_Timer.h b/modules/juce_events/timers/juce_Timer.h index 4d0c1acd07..24761624d9 100644 --- a/modules/juce_events/timers/juce_Timer.h +++ b/modules/juce_events/timers/juce_Timer.h @@ -127,9 +127,9 @@ public: private: class TimerThread; - friend class TimerThread; size_t positionInQueue = (size_t) -1; int timerPeriodMs = 0; + SharedResourcePointer timerThread; Timer& operator= (const Timer&) = delete; };