juce_TimeSliceThread.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. return clients.size();
  76. }
  77. TimeSliceClient* TimeSliceThread::getClient (const int i) const
  78. {
  79. const ScopedLock sl (listLock);
  80. return clients[i];
  81. }
  82. //==============================================================================
  83. TimeSliceClient* TimeSliceThread::getNextClient (int index) const
  84. {
  85. Time soonest;
  86. TimeSliceClient* client = nullptr;
  87. for (int i = clients.size(); --i >= 0;)
  88. {
  89. auto* c = clients.getUnchecked ((i + index) % clients.size());
  90. if (client == nullptr || c->nextCallTime < soonest)
  91. {
  92. client = c;
  93. soonest = c->nextCallTime;
  94. }
  95. }
  96. return client;
  97. }
  98. void TimeSliceThread::run()
  99. {
  100. int index = 0;
  101. while (! threadShouldExit())
  102. {
  103. int timeToWait = 500;
  104. {
  105. Time nextClientTime;
  106. int numClients = 0;
  107. {
  108. const ScopedLock sl2 (listLock);
  109. numClients = clients.size();
  110. index = numClients > 0 ? ((index + 1) % numClients) : 0;
  111. if (auto* firstClient = getNextClient (index))
  112. nextClientTime = firstClient->nextCallTime;
  113. }
  114. if (numClients > 0)
  115. {
  116. auto now = Time::getCurrentTime();
  117. if (nextClientTime > now)
  118. {
  119. timeToWait = (int) jmin ((int64) 500, (nextClientTime - now).inMilliseconds());
  120. }
  121. else
  122. {
  123. timeToWait = index == 0 ? 1 : 0;
  124. const ScopedLock sl (callbackLock);
  125. {
  126. const ScopedLock sl2 (listLock);
  127. clientBeingCalled = getNextClient (index);
  128. }
  129. if (clientBeingCalled != nullptr)
  130. {
  131. const int msUntilNextCall = clientBeingCalled->useTimeSlice();
  132. const ScopedLock sl2 (listLock);
  133. if (msUntilNextCall >= 0)
  134. clientBeingCalled->nextCallTime = now + RelativeTime::milliseconds (msUntilNextCall);
  135. else
  136. clients.removeFirstMatchingValue (clientBeingCalled);
  137. clientBeingCalled = nullptr;
  138. }
  139. }
  140. }
  141. }
  142. if (timeToWait > 0)
  143. wait (timeToWait);
  144. }
  145. }
  146. } // namespace juce