The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

387 lines
11KB

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