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.

179 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. TimeSliceThread::TimeSliceThread (const String& name)
  24. : Thread (name),
  25. clientBeingCalled (nullptr)
  26. {
  27. }
  28. TimeSliceThread::~TimeSliceThread()
  29. {
  30. stopThread (2000);
  31. }
  32. //==============================================================================
  33. void TimeSliceThread::addTimeSliceClient (TimeSliceClient* const client, int millisecondsBeforeStarting)
  34. {
  35. if (client != nullptr)
  36. {
  37. const ScopedLock sl (listLock);
  38. client->nextCallTime = Time::getCurrentTime() + RelativeTime::milliseconds (millisecondsBeforeStarting);
  39. clients.addIfNotAlreadyThere (client);
  40. notify();
  41. }
  42. }
  43. void TimeSliceThread::removeTimeSliceClient (TimeSliceClient* const client)
  44. {
  45. const ScopedLock sl1 (listLock);
  46. // if there's a chance we're in the middle of calling this client, we need to
  47. // also lock the outer lock..
  48. if (clientBeingCalled == client)
  49. {
  50. const ScopedUnlock ul (listLock); // unlock first to get the order right..
  51. const ScopedLock sl2 (callbackLock);
  52. const ScopedLock sl3 (listLock);
  53. clients.removeFirstMatchingValue (client);
  54. }
  55. else
  56. {
  57. clients.removeFirstMatchingValue (client);
  58. }
  59. }
  60. void TimeSliceThread::moveToFrontOfQueue (TimeSliceClient* client)
  61. {
  62. const ScopedLock sl (listLock);
  63. if (clients.contains (client))
  64. {
  65. client->nextCallTime = Time::getCurrentTime();
  66. notify();
  67. }
  68. }
  69. int TimeSliceThread::getNumClients() const
  70. {
  71. return clients.size();
  72. }
  73. TimeSliceClient* TimeSliceThread::getClient (const int i) const
  74. {
  75. const ScopedLock sl (listLock);
  76. return clients [i];
  77. }
  78. //==============================================================================
  79. TimeSliceClient* TimeSliceThread::getNextClient (int index) const
  80. {
  81. Time soonest;
  82. TimeSliceClient* client = nullptr;
  83. for (int i = clients.size(); --i >= 0;)
  84. {
  85. TimeSliceClient* const c = clients.getUnchecked ((i + index) % clients.size());
  86. if (client == nullptr || c->nextCallTime < soonest)
  87. {
  88. client = c;
  89. soonest = c->nextCallTime;
  90. }
  91. }
  92. return client;
  93. }
  94. void TimeSliceThread::run()
  95. {
  96. int index = 0;
  97. while (! threadShouldExit())
  98. {
  99. int timeToWait = 500;
  100. {
  101. Time nextClientTime;
  102. int numClients = 0;
  103. {
  104. const ScopedLock sl2 (listLock);
  105. numClients = clients.size();
  106. index = numClients > 0 ? ((index + 1) % numClients) : 0;
  107. if (TimeSliceClient* const firstClient = getNextClient (index))
  108. nextClientTime = firstClient->nextCallTime;
  109. }
  110. if (numClients > 0)
  111. {
  112. const Time now (Time::getCurrentTime());
  113. if (nextClientTime > now)
  114. {
  115. timeToWait = (int) jmin ((int64) 500, (nextClientTime - now).inMilliseconds());
  116. }
  117. else
  118. {
  119. timeToWait = index == 0 ? 1 : 0;
  120. const ScopedLock sl (callbackLock);
  121. {
  122. const ScopedLock sl2 (listLock);
  123. clientBeingCalled = getNextClient (index);
  124. }
  125. if (clientBeingCalled != nullptr)
  126. {
  127. const int msUntilNextCall = clientBeingCalled->useTimeSlice();
  128. const ScopedLock sl2 (listLock);
  129. if (msUntilNextCall >= 0)
  130. clientBeingCalled->nextCallTime = now + RelativeTime::milliseconds (msUntilNextCall);
  131. else
  132. clients.removeFirstMatchingValue (clientBeingCalled);
  133. clientBeingCalled = nullptr;
  134. }
  135. }
  136. }
  137. }
  138. if (timeToWait > 0)
  139. wait (timeToWait);
  140. }
  141. }