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.

165 lines
4.8KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. TimeSliceThread::TimeSliceThread (const String& threadName)
  21. : Thread (threadName),
  22. clientBeingCalled (nullptr)
  23. {
  24. }
  25. TimeSliceThread::~TimeSliceThread()
  26. {
  27. stopThread (2000);
  28. }
  29. //==============================================================================
  30. void TimeSliceThread::addTimeSliceClient (TimeSliceClient* const client, int millisecondsBeforeStarting)
  31. {
  32. if (client != nullptr)
  33. {
  34. const ScopedLock sl (listLock);
  35. client->nextCallTime = Time::getCurrentTime() + RelativeTime::milliseconds (millisecondsBeforeStarting);
  36. clients.addIfNotAlreadyThere (client);
  37. notify();
  38. }
  39. }
  40. void TimeSliceThread::removeTimeSliceClient (TimeSliceClient* const client)
  41. {
  42. const ScopedLock sl1 (listLock);
  43. // if there's a chance we're in the middle of calling this client, we need to
  44. // also lock the outer lock..
  45. if (clientBeingCalled == client)
  46. {
  47. const ScopedUnlock ul (listLock); // unlock first to get the order right..
  48. const ScopedLock sl2 (callbackLock);
  49. const ScopedLock sl3 (listLock);
  50. clients.removeValue (client);
  51. }
  52. else
  53. {
  54. clients.removeValue (client);
  55. }
  56. }
  57. int TimeSliceThread::getNumClients() const
  58. {
  59. return clients.size();
  60. }
  61. TimeSliceClient* TimeSliceThread::getClient (const int i) const
  62. {
  63. const ScopedLock sl (listLock);
  64. return clients [i];
  65. }
  66. //==============================================================================
  67. TimeSliceClient* TimeSliceThread::getNextClient (int index) const
  68. {
  69. Time soonest;
  70. TimeSliceClient* client = nullptr;
  71. for (int i = clients.size(); --i >= 0;)
  72. {
  73. TimeSliceClient* const c = clients.getUnchecked ((i + index) % clients.size());
  74. if (client == nullptr || c->nextCallTime < soonest)
  75. {
  76. client = c;
  77. soonest = c->nextCallTime;
  78. }
  79. }
  80. return client;
  81. }
  82. void TimeSliceThread::run()
  83. {
  84. int index = 0;
  85. while (! threadShouldExit())
  86. {
  87. int timeToWait = 500;
  88. {
  89. Time nextClientTime;
  90. {
  91. const ScopedLock sl2 (listLock);
  92. index = clients.size() > 0 ? ((index + 1) % clients.size()) : 0;
  93. TimeSliceClient* const firstClient = getNextClient (index);
  94. if (firstClient != nullptr)
  95. nextClientTime = firstClient->nextCallTime;
  96. }
  97. const Time now (Time::getCurrentTime());
  98. if (nextClientTime > now)
  99. {
  100. timeToWait = (int) jmin ((int64) 500, (nextClientTime - now).inMilliseconds());
  101. }
  102. else
  103. {
  104. timeToWait = index == 0 ? 1 : 0;
  105. const ScopedLock sl (callbackLock);
  106. {
  107. const ScopedLock sl2 (listLock);
  108. clientBeingCalled = getNextClient (index);
  109. }
  110. if (clientBeingCalled != nullptr)
  111. {
  112. const int msUntilNextCall = clientBeingCalled->useTimeSlice();
  113. const ScopedLock sl2 (listLock);
  114. if (msUntilNextCall >= 0)
  115. clientBeingCalled->nextCallTime += RelativeTime::milliseconds (msUntilNextCall);
  116. else
  117. clients.removeValue (clientBeingCalled);
  118. clientBeingCalled = nullptr;
  119. }
  120. }
  121. }
  122. if (timeToWait > 0)
  123. wait (timeToWait);
  124. }
  125. }
  126. END_JUCE_NAMESPACE