Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_Timer.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class Timer::TimerThread : private Thread,
  18. private DeletedAtShutdown,
  19. private AsyncUpdater
  20. {
  21. public:
  22. typedef CriticalSection LockType; // (mysteriously, using a SpinLock here causes problems on some XP machines..)
  23. TimerThread()
  24. : Thread ("Juce Timer"),
  25. firstTimer (nullptr),
  26. callbackNeeded (0)
  27. {
  28. triggerAsyncUpdate();
  29. }
  30. ~TimerThread() noexcept
  31. {
  32. stopThread (4000);
  33. jassert (instance == this || instance == nullptr);
  34. if (instance == this)
  35. instance = nullptr;
  36. }
  37. void run() override
  38. {
  39. uint32 lastTime = Time::getMillisecondCounter();
  40. MessageManager::MessageBase::Ptr messageToSend (new CallTimersMessage());
  41. while (! threadShouldExit())
  42. {
  43. const uint32 now = Time::getMillisecondCounter();
  44. if (now == lastTime)
  45. {
  46. wait (1);
  47. continue;
  48. }
  49. const int elapsed = (int) (now >= lastTime ? (now - lastTime)
  50. : (std::numeric_limits<uint32>::max() - (lastTime - now)));
  51. lastTime = now;
  52. const int timeUntilFirstTimer = getTimeUntilFirstTimer (elapsed);
  53. if (timeUntilFirstTimer <= 0)
  54. {
  55. /* If we managed to set the atomic boolean to true then send a message, this is needed
  56. as a memory barrier so the message won't be sent before callbackNeeded is set to true,
  57. but if it fails it means the message-thread changed the value from under us so at least
  58. some processing is happenening and we can just loop around and try again
  59. */
  60. if (callbackNeeded.compareAndSetBool (1, 0))
  61. {
  62. messageToSend->post();
  63. /* Sometimes our message can get discarded by the OS (e.g. when running as an RTAS
  64. when the app has a modal loop), so this is how long to wait before assuming the
  65. message has been lost and trying again.
  66. */
  67. const uint32 messageDeliveryTimeout = now + 300;
  68. while (callbackNeeded.get() != 0)
  69. {
  70. wait (4);
  71. if (threadShouldExit())
  72. return;
  73. if (Time::getMillisecondCounter() > messageDeliveryTimeout)
  74. {
  75. messageToSend->post();
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. else
  82. {
  83. // don't wait for too long because running this loop also helps keep the
  84. // Time::getApproximateMillisecondTimer value stay up-to-date
  85. wait (jlimit (1, 50, timeUntilFirstTimer));
  86. }
  87. }
  88. }
  89. void callTimers()
  90. {
  91. const LockType::ScopedLockType sl (lock);
  92. while (firstTimer != nullptr && firstTimer->countdownMs <= 0)
  93. {
  94. Timer* const t = firstTimer;
  95. t->countdownMs = t->periodMs;
  96. removeTimer (t);
  97. addTimer (t);
  98. const LockType::ScopedUnlockType ul (lock);
  99. JUCE_TRY
  100. {
  101. t->timerCallback();
  102. }
  103. JUCE_CATCH_EXCEPTION
  104. }
  105. /* This is needed as a memory barrier to make sure all processing of current timers is done
  106. before the boolean is set. This set should never fail since if it was false in the first place,
  107. we wouldn't get a message (so it can't be changed from false to true from under us), and if we
  108. get a message then the value is true and the other thread can only set it to true again and
  109. we will get another callback to set it to false.
  110. */
  111. callbackNeeded.set (0);
  112. }
  113. void callTimersSynchronously()
  114. {
  115. if (! isThreadRunning())
  116. {
  117. // (This is relied on by some plugins in cases where the MM has
  118. // had to restart and the async callback never started)
  119. cancelPendingUpdate();
  120. triggerAsyncUpdate();
  121. }
  122. callTimers();
  123. }
  124. static inline void add (Timer* const tim) noexcept
  125. {
  126. if (instance == nullptr)
  127. instance = new TimerThread();
  128. instance->addTimer (tim);
  129. }
  130. static inline void remove (Timer* const tim) noexcept
  131. {
  132. if (instance != nullptr)
  133. instance->removeTimer (tim);
  134. }
  135. static inline void resetCounter (Timer* const tim, const int newCounter) noexcept
  136. {
  137. if (instance != nullptr)
  138. {
  139. tim->countdownMs = newCounter;
  140. tim->periodMs = newCounter;
  141. if ((tim->next != nullptr && tim->next->countdownMs < tim->countdownMs)
  142. || (tim->previous != nullptr && tim->previous->countdownMs > tim->countdownMs))
  143. {
  144. instance->removeTimer (tim);
  145. instance->addTimer (tim);
  146. }
  147. }
  148. }
  149. static TimerThread* instance;
  150. static LockType lock;
  151. private:
  152. Timer* volatile firstTimer;
  153. Atomic <int> callbackNeeded;
  154. struct CallTimersMessage : public MessageManager::MessageBase
  155. {
  156. CallTimersMessage() {}
  157. void messageCallback() override
  158. {
  159. if (instance != nullptr)
  160. instance->callTimers();
  161. }
  162. };
  163. //==============================================================================
  164. void addTimer (Timer* const t) noexcept
  165. {
  166. #if JUCE_DEBUG
  167. // trying to add a timer that's already here - shouldn't get to this point,
  168. // so if you get this assertion, let me know!
  169. jassert (! timerExists (t));
  170. #endif
  171. Timer* i = firstTimer;
  172. if (i == nullptr || i->countdownMs > t->countdownMs)
  173. {
  174. t->next = firstTimer;
  175. firstTimer = t;
  176. }
  177. else
  178. {
  179. while (i->next != nullptr && i->next->countdownMs <= t->countdownMs)
  180. i = i->next;
  181. jassert (i != nullptr);
  182. t->next = i->next;
  183. t->previous = i;
  184. i->next = t;
  185. }
  186. if (t->next != nullptr)
  187. t->next->previous = t;
  188. jassert ((t->next == nullptr || t->next->countdownMs >= t->countdownMs)
  189. && (t->previous == nullptr || t->previous->countdownMs <= t->countdownMs));
  190. notify();
  191. }
  192. void removeTimer (Timer* const t) noexcept
  193. {
  194. #if JUCE_DEBUG
  195. // trying to remove a timer that's not here - shouldn't get to this point,
  196. // so if you get this assertion, let me know!
  197. jassert (timerExists (t));
  198. #endif
  199. if (t->previous != nullptr)
  200. {
  201. jassert (firstTimer != t);
  202. t->previous->next = t->next;
  203. }
  204. else
  205. {
  206. jassert (firstTimer == t);
  207. firstTimer = t->next;
  208. }
  209. if (t->next != nullptr)
  210. t->next->previous = t->previous;
  211. t->next = nullptr;
  212. t->previous = nullptr;
  213. }
  214. int getTimeUntilFirstTimer (const int numMillisecsElapsed) const
  215. {
  216. const LockType::ScopedLockType sl (lock);
  217. for (Timer* t = firstTimer; t != nullptr; t = t->next)
  218. t->countdownMs -= numMillisecsElapsed;
  219. return firstTimer != nullptr ? firstTimer->countdownMs : 1000;
  220. }
  221. void handleAsyncUpdate() override
  222. {
  223. startThread (7);
  224. }
  225. #if JUCE_DEBUG
  226. bool timerExists (Timer* const t) const noexcept
  227. {
  228. for (Timer* tt = firstTimer; tt != nullptr; tt = tt->next)
  229. if (tt == t)
  230. return true;
  231. return false;
  232. }
  233. #endif
  234. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TimerThread)
  235. };
  236. Timer::TimerThread* Timer::TimerThread::instance = nullptr;
  237. Timer::TimerThread::LockType Timer::TimerThread::lock;
  238. //==============================================================================
  239. #if JUCE_DEBUG
  240. static SortedSet <Timer*> activeTimers;
  241. #endif
  242. Timer::Timer() noexcept
  243. : countdownMs (0),
  244. periodMs (0),
  245. previous (nullptr),
  246. next (nullptr)
  247. {
  248. #if JUCE_DEBUG
  249. const TimerThread::LockType::ScopedLockType sl (TimerThread::lock);
  250. activeTimers.add (this);
  251. #endif
  252. }
  253. Timer::Timer (const Timer&) noexcept
  254. : countdownMs (0),
  255. periodMs (0),
  256. previous (nullptr),
  257. next (nullptr)
  258. {
  259. #if JUCE_DEBUG
  260. const TimerThread::LockType::ScopedLockType sl (TimerThread::lock);
  261. activeTimers.add (this);
  262. #endif
  263. }
  264. Timer::~Timer()
  265. {
  266. stopTimer();
  267. #if JUCE_DEBUG
  268. activeTimers.removeValue (this);
  269. #endif
  270. }
  271. void Timer::startTimer (const int interval) noexcept
  272. {
  273. const TimerThread::LockType::ScopedLockType sl (TimerThread::lock);
  274. #if JUCE_DEBUG
  275. // this isn't a valid object! Your timer might be a dangling pointer or something..
  276. jassert (activeTimers.contains (this));
  277. #endif
  278. if (periodMs == 0)
  279. {
  280. countdownMs = interval;
  281. periodMs = jmax (1, interval);
  282. TimerThread::add (this);
  283. }
  284. else
  285. {
  286. TimerThread::resetCounter (this, interval);
  287. }
  288. }
  289. void Timer::stopTimer() noexcept
  290. {
  291. const TimerThread::LockType::ScopedLockType sl (TimerThread::lock);
  292. #if JUCE_DEBUG
  293. // this isn't a valid object! Your timer might be a dangling pointer or something..
  294. jassert (activeTimers.contains (this));
  295. #endif
  296. if (periodMs > 0)
  297. {
  298. TimerThread::remove (this);
  299. periodMs = 0;
  300. }
  301. }
  302. void JUCE_CALLTYPE Timer::callPendingTimersSynchronously()
  303. {
  304. if (TimerThread::instance != nullptr)
  305. TimerThread::instance->callTimersSynchronously();
  306. }