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.

170 lines
4.9KB

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