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.

176 lines
5.1KB

  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. void TimeSliceThread::moveToFrontOfQueue (TimeSliceClient* client)
  58. {
  59. const ScopedLock sl (listLock);
  60. if (clients.contains (client))
  61. {
  62. client->nextCallTime = Time::getCurrentTime();
  63. notify();
  64. }
  65. }
  66. int TimeSliceThread::getNumClients() const
  67. {
  68. return clients.size();
  69. }
  70. TimeSliceClient* TimeSliceThread::getClient (const int i) const
  71. {
  72. const ScopedLock sl (listLock);
  73. return clients [i];
  74. }
  75. //==============================================================================
  76. TimeSliceClient* TimeSliceThread::getNextClient (int index) const
  77. {
  78. Time soonest;
  79. TimeSliceClient* client = nullptr;
  80. for (int i = clients.size(); --i >= 0;)
  81. {
  82. TimeSliceClient* const c = clients.getUnchecked ((i + index) % clients.size());
  83. if (client == nullptr || c->nextCallTime < soonest)
  84. {
  85. client = c;
  86. soonest = c->nextCallTime;
  87. }
  88. }
  89. return client;
  90. }
  91. void TimeSliceThread::run()
  92. {
  93. int index = 0;
  94. while (! threadShouldExit())
  95. {
  96. int timeToWait = 500;
  97. {
  98. Time nextClientTime;
  99. {
  100. const ScopedLock sl2 (listLock);
  101. index = clients.size() > 0 ? ((index + 1) % clients.size()) : 0;
  102. TimeSliceClient* const firstClient = getNextClient (index);
  103. if (firstClient != nullptr)
  104. nextClientTime = firstClient->nextCallTime;
  105. }
  106. const Time now (Time::getCurrentTime());
  107. if (nextClientTime > now)
  108. {
  109. timeToWait = (int) jmin ((int64) 500, (nextClientTime - now).inMilliseconds());
  110. }
  111. else
  112. {
  113. timeToWait = index == 0 ? 1 : 0;
  114. const ScopedLock sl (callbackLock);
  115. {
  116. const ScopedLock sl2 (listLock);
  117. clientBeingCalled = getNextClient (index);
  118. }
  119. if (clientBeingCalled != nullptr)
  120. {
  121. const int msUntilNextCall = clientBeingCalled->useTimeSlice();
  122. const ScopedLock sl2 (listLock);
  123. if (msUntilNextCall >= 0)
  124. clientBeingCalled->nextCallTime += RelativeTime::milliseconds (msUntilNextCall);
  125. else
  126. clients.removeValue (clientBeingCalled);
  127. clientBeingCalled = nullptr;
  128. }
  129. }
  130. }
  131. if (timeToWait > 0)
  132. wait (timeToWait);
  133. }
  134. }
  135. END_JUCE_NAMESPACE