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.

151 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. class TimeSliceThread;
  20. //==============================================================================
  21. /**
  22. Used by the TimeSliceThread class.
  23. To register your class with a TimeSliceThread, derive from this class and
  24. use the TimeSliceThread::addTimeSliceClient() method to add it to the list.
  25. Make sure you always call TimeSliceThread::removeTimeSliceClient() before
  26. deleting your client!
  27. @see TimeSliceThread
  28. @tags{Core}
  29. */
  30. class JUCE_API TimeSliceClient
  31. {
  32. public:
  33. /** Destructor. */
  34. virtual ~TimeSliceClient() = default;
  35. /** Called back by a TimeSliceThread.
  36. When you register this class with it, a TimeSliceThread will repeatedly call
  37. this method.
  38. The implementation of this method should use its time-slice to do something that's
  39. quick - never block for longer than absolutely necessary.
  40. @returns Your method should return the number of milliseconds which it would like to wait before being called
  41. again. Returning 0 will make the thread call again as soon as possible (after possibly servicing
  42. other busy clients). If you return a value below zero, your client will be removed from the list of clients,
  43. and won't be called again. The value you specify isn't a guarantee, and is only used as a hint by the
  44. thread - the actual time before the next callback may be more or less than specified.
  45. You can force the TimeSliceThread to wake up and poll again immediately by calling its notify() method.
  46. */
  47. virtual int useTimeSlice() = 0;
  48. private:
  49. friend class TimeSliceThread;
  50. Time nextCallTime;
  51. };
  52. //==============================================================================
  53. /**
  54. A thread that keeps a list of clients, and calls each one in turn, giving them
  55. all a chance to run some sort of short task.
  56. @see TimeSliceClient, Thread
  57. @tags{Core}
  58. */
  59. class JUCE_API TimeSliceThread : public Thread
  60. {
  61. public:
  62. //==============================================================================
  63. /**
  64. Creates a TimeSliceThread.
  65. When first created, the thread is not running. Use the startThread()
  66. method to start it.
  67. */
  68. explicit TimeSliceThread (const String& threadName);
  69. /** Destructor.
  70. Deleting a Thread object that is running will only give the thread a
  71. brief opportunity to stop itself cleanly, so it's recommended that you
  72. should always call stopThread() with a decent timeout before deleting,
  73. to avoid the thread being forcibly killed (which is a Bad Thing).
  74. */
  75. ~TimeSliceThread() override;
  76. //==============================================================================
  77. /** Adds a client to the list.
  78. The client's callbacks will start after the number of milliseconds specified
  79. by millisecondsBeforeStarting (and this may happen before this method has returned).
  80. */
  81. void addTimeSliceClient (TimeSliceClient* clientToAdd, int millisecondsBeforeStarting = 0);
  82. /** If the given client is waiting in the queue, it will be moved to the front
  83. and given a time-slice as soon as possible.
  84. If the specified client has not been added, nothing will happen.
  85. */
  86. void moveToFrontOfQueue (TimeSliceClient* clientToMove);
  87. /** Removes a client from the list.
  88. This method will make sure that all callbacks to the client have completely
  89. finished before the method returns.
  90. */
  91. void removeTimeSliceClient (TimeSliceClient* clientToRemove);
  92. /** Removes all the active and pending clients from the list.
  93. This method will make sure that all callbacks to clients have finished before the
  94. method returns.
  95. */
  96. void removeAllClients();
  97. /** Returns the number of registered clients. */
  98. int getNumClients() const;
  99. /** Returns one of the registered clients. */
  100. TimeSliceClient* getClient (int index) const;
  101. //==============================================================================
  102. #ifndef DOXYGEN
  103. void run() override;
  104. #endif
  105. //==============================================================================
  106. private:
  107. CriticalSection callbackLock, listLock;
  108. Array<TimeSliceClient*> clients;
  109. TimeSliceClient* clientBeingCalled = nullptr;
  110. TimeSliceClient* getNextClient (int index) const;
  111. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TimeSliceThread)
  112. };
  113. } // namespace juce