juce_TimeSliceThread.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_TIMESLICETHREAD_H_INCLUDED
  22. #define JUCE_TIMESLICETHREAD_H_INCLUDED
  23. class TimeSliceThread;
  24. //==============================================================================
  25. /**
  26. Used by the TimeSliceThread class.
  27. To register your class with a TimeSliceThread, derive from this class and
  28. use the TimeSliceThread::addTimeSliceClient() method to add it to the list.
  29. Make sure you always call TimeSliceThread::removeTimeSliceClient() before
  30. deleting your client!
  31. @see TimeSliceThread
  32. */
  33. class JUCE_API TimeSliceClient
  34. {
  35. public:
  36. /** Destructor. */
  37. virtual ~TimeSliceClient() {}
  38. /** Called back by a TimeSliceThread.
  39. When you register this class with it, a TimeSliceThread will repeatedly call
  40. this method.
  41. The implementation of this method should use its time-slice to do something that's
  42. quick - never block for longer than absolutely necessary.
  43. @returns Your method should return the number of milliseconds which it would like to wait before being called
  44. again. Returning 0 will make the thread call again as soon as possible (after possibly servicing
  45. other busy clients). If you return a value below zero, your client will be removed from the list of clients,
  46. and won't be called again. The value you specify isn't a guaranteee, and is only used as a hint by the
  47. thread - the actual time before the next callback may be more or less than specified.
  48. You can force the TimeSliceThread to wake up and poll again immediately by calling its notify() method.
  49. */
  50. virtual int useTimeSlice() = 0;
  51. private:
  52. friend class TimeSliceThread;
  53. Time nextCallTime;
  54. };
  55. //==============================================================================
  56. /**
  57. A thread that keeps a list of clients, and calls each one in turn, giving them
  58. all a chance to run some sort of short task.
  59. @see TimeSliceClient, Thread
  60. */
  61. class JUCE_API TimeSliceThread : public Thread
  62. {
  63. public:
  64. //==============================================================================
  65. /**
  66. Creates a TimeSliceThread.
  67. When first created, the thread is not running. Use the startThread()
  68. method to start it.
  69. */
  70. explicit TimeSliceThread (const String& threadName);
  71. /** Destructor.
  72. Deleting a Thread object that is running will only give the thread a
  73. brief opportunity to stop itself cleanly, so it's recommended that you
  74. should always call stopThread() with a decent timeout before deleting,
  75. to avoid the thread being forcibly killed (which is a Bad Thing).
  76. */
  77. ~TimeSliceThread();
  78. //==============================================================================
  79. /** Adds a client to the list.
  80. The client's callbacks will start after the number of milliseconds specified
  81. by millisecondsBeforeStarting (and this may happen before this method has returned).
  82. */
  83. void addTimeSliceClient (TimeSliceClient* client, int millisecondsBeforeStarting = 0);
  84. /** Removes a client from the list.
  85. This method will make sure that all callbacks to the client have completely
  86. finished before the method returns.
  87. */
  88. void removeTimeSliceClient (TimeSliceClient* client);
  89. /** If the given client is waiting in the queue, it will be moved to the front
  90. and given a time-slice as soon as possible.
  91. If the specified client has not been added, nothing will happen.
  92. */
  93. void moveToFrontOfQueue (TimeSliceClient* client);
  94. /** Returns the number of registered clients. */
  95. int getNumClients() const;
  96. /** Returns one of the registered clients. */
  97. TimeSliceClient* getClient (int index) const;
  98. //==============================================================================
  99. #ifndef DOXYGEN
  100. void run() override;
  101. #endif
  102. //==============================================================================
  103. private:
  104. CriticalSection callbackLock, listLock;
  105. Array <TimeSliceClient*> clients;
  106. TimeSliceClient* clientBeingCalled;
  107. TimeSliceClient* getNextClient (int index) const;
  108. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TimeSliceThread)
  109. };
  110. #endif // JUCE_TIMESLICETHREAD_H_INCLUDED