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.

192 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. TimeSliceThread::TimeSliceThread (const String& name) : Thread (name)
  20. {
  21. }
  22. TimeSliceThread::~TimeSliceThread()
  23. {
  24. stopThread (2000);
  25. }
  26. //==============================================================================
  27. void TimeSliceThread::addTimeSliceClient (TimeSliceClient* const client, int millisecondsBeforeStarting)
  28. {
  29. if (client != nullptr)
  30. {
  31. const ScopedLock sl (listLock);
  32. client->nextCallTime = Time::getCurrentTime() + RelativeTime::milliseconds (millisecondsBeforeStarting);
  33. clients.addIfNotAlreadyThere (client);
  34. notify();
  35. }
  36. }
  37. void TimeSliceThread::removeTimeSliceClient (TimeSliceClient* const client)
  38. {
  39. const ScopedLock sl1 (listLock);
  40. // if there's a chance we're in the middle of calling this client, we need to
  41. // also lock the outer lock..
  42. if (clientBeingCalled == client)
  43. {
  44. const ScopedUnlock ul (listLock); // unlock first to get the order right..
  45. const ScopedLock sl2 (callbackLock);
  46. const ScopedLock sl3 (listLock);
  47. clients.removeFirstMatchingValue (client);
  48. }
  49. else
  50. {
  51. clients.removeFirstMatchingValue (client);
  52. }
  53. }
  54. void TimeSliceThread::removeAllClients()
  55. {
  56. for (;;)
  57. {
  58. if (auto* c = getClient (0))
  59. removeTimeSliceClient (c);
  60. else
  61. break;
  62. }
  63. }
  64. void TimeSliceThread::moveToFrontOfQueue (TimeSliceClient* client)
  65. {
  66. const ScopedLock sl (listLock);
  67. if (clients.contains (client))
  68. {
  69. client->nextCallTime = Time::getCurrentTime();
  70. notify();
  71. }
  72. }
  73. int TimeSliceThread::getNumClients() const
  74. {
  75. const ScopedLock sl (listLock);
  76. return clients.size();
  77. }
  78. TimeSliceClient* TimeSliceThread::getClient (const int i) const
  79. {
  80. const ScopedLock sl (listLock);
  81. return clients[i];
  82. }
  83. bool TimeSliceThread::contains (const TimeSliceClient* c) const
  84. {
  85. const ScopedLock sl (listLock);
  86. return std::any_of (clients.begin(), clients.end(), [=] (auto* registered) { return registered == c; });
  87. }
  88. //==============================================================================
  89. TimeSliceClient* TimeSliceThread::getNextClient (int index) const
  90. {
  91. Time soonest;
  92. TimeSliceClient* client = nullptr;
  93. for (int i = clients.size(); --i >= 0;)
  94. {
  95. auto* c = clients.getUnchecked ((i + index) % clients.size());
  96. if (c != nullptr && (client == nullptr || c->nextCallTime < soonest))
  97. {
  98. client = c;
  99. soonest = c->nextCallTime;
  100. }
  101. }
  102. return client;
  103. }
  104. void TimeSliceThread::run()
  105. {
  106. int index = 0;
  107. while (! threadShouldExit())
  108. {
  109. int timeToWait = 500;
  110. {
  111. Time nextClientTime;
  112. int numClients = 0;
  113. {
  114. const ScopedLock sl2 (listLock);
  115. numClients = clients.size();
  116. index = numClients > 0 ? ((index + 1) % numClients) : 0;
  117. if (auto* firstClient = getNextClient (index))
  118. nextClientTime = firstClient->nextCallTime;
  119. }
  120. if (numClients > 0)
  121. {
  122. auto now = Time::getCurrentTime();
  123. if (nextClientTime > now)
  124. {
  125. timeToWait = (int) jmin ((int64) 500, (nextClientTime - now).inMilliseconds());
  126. }
  127. else
  128. {
  129. timeToWait = index == 0 ? 1 : 0;
  130. const ScopedLock sl (callbackLock);
  131. {
  132. const ScopedLock sl2 (listLock);
  133. clientBeingCalled = getNextClient (index);
  134. }
  135. if (clientBeingCalled != nullptr)
  136. {
  137. const int msUntilNextCall = clientBeingCalled->useTimeSlice();
  138. const ScopedLock sl2 (listLock);
  139. if (msUntilNextCall >= 0)
  140. clientBeingCalled->nextCallTime = now + RelativeTime::milliseconds (msUntilNextCall);
  141. else
  142. clients.removeFirstMatchingValue (clientBeingCalled);
  143. clientBeingCalled = nullptr;
  144. }
  145. }
  146. }
  147. }
  148. if (timeToWait > 0)
  149. wait (timeToWait);
  150. }
  151. }
  152. } // namespace juce