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.

281 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #ifndef __JUCE_THREAD_JUCEHEADER__
  24. #define __JUCE_THREAD_JUCEHEADER__
  25. #include "juce_WaitableEvent.h"
  26. #include "juce_CriticalSection.h"
  27. //==============================================================================
  28. /**
  29. Encapsulates a thread.
  30. Subclasses derive from Thread and implement the run() method, in which they
  31. do their business. The thread can then be started with the startThread() method
  32. and controlled with various other methods.
  33. This class also contains some thread-related static methods, such
  34. as sleep(), yield(), getCurrentThreadId() etc.
  35. @see CriticalSection, WaitableEvent, Process, ThreadWithProgressWindow,
  36. MessageManagerLock
  37. */
  38. class JUCE_API Thread
  39. {
  40. public:
  41. //==============================================================================
  42. /**
  43. Creates a thread.
  44. When first created, the thread is not running. Use the startThread()
  45. method to start it.
  46. */
  47. Thread (const String& threadName);
  48. /** Destructor.
  49. Deleting a Thread object that is running will only give the thread a
  50. brief opportunity to stop itself cleanly, so it's recommended that you
  51. should always call stopThread() with a decent timeout before deleting,
  52. to avoid the thread being forcibly killed (which is a Bad Thing).
  53. */
  54. virtual ~Thread();
  55. //==============================================================================
  56. /** Must be implemented to perform the thread's actual code.
  57. Remember that the thread must regularly check the threadShouldExit()
  58. method whilst running, and if this returns true it should return from
  59. the run() method as soon as possible to avoid being forcibly killed.
  60. @see threadShouldExit, startThread
  61. */
  62. virtual void run() = 0;
  63. //==============================================================================
  64. // Thread control functions..
  65. /** Starts the thread running.
  66. This will start the thread's run() method.
  67. (if it's already started, startThread() won't do anything).
  68. @see stopThread
  69. */
  70. void startThread();
  71. /** Starts the thread with a given priority.
  72. Launches the thread with a given priority, where 0 = lowest, 10 = highest.
  73. If the thread is already running, its priority will be changed.
  74. @see startThread, setPriority
  75. */
  76. void startThread (const int priority);
  77. /** Attempts to stop the thread running.
  78. This method will cause the threadShouldExit() method to return true
  79. and call notify() in case the thread is currently waiting.
  80. Hopefully the thread will then respond to this by exiting cleanly, and
  81. the stopThread method will wait for a given time-period for this to
  82. happen.
  83. If the thread is stuck and fails to respond after the time-out, it gets
  84. forcibly killed, which is a very bad thing to happen, as it could still
  85. be holding locks, etc. which are needed by other parts of your program.
  86. @param timeOutMilliseconds The number of milliseconds to wait for the
  87. thread to finish before killing it by force. A negative
  88. value in here will wait forever.
  89. @see signalThreadShouldExit, threadShouldExit, waitForThreadToExit, isThreadRunning
  90. */
  91. void stopThread (const int timeOutMilliseconds);
  92. //==============================================================================
  93. /** Returns true if the thread is currently active */
  94. bool isThreadRunning() const throw();
  95. /** Sets a flag to tell the thread it should stop.
  96. Calling this means that the threadShouldExit() method will then return true.
  97. The thread should be regularly checking this to see whether it should exit.
  98. @see threadShouldExit
  99. @see waitForThreadToExit
  100. */
  101. void signalThreadShouldExit() throw();
  102. /** Checks whether the thread has been told to stop running.
  103. Threads need to check this regularly, and if it returns true, they should
  104. return from their run() method at the first possible opportunity.
  105. @see signalThreadShouldExit
  106. */
  107. inline bool threadShouldExit() const throw() { return threadShouldExit_; }
  108. /** Waits for the thread to stop.
  109. This will waits until isThreadRunning() is false or until a timeout expires.
  110. @param timeOutMilliseconds the time to wait, in milliseconds. If this value
  111. is less than zero, it will wait forever.
  112. @returns true if the thread exits, or false if the timeout expires first.
  113. */
  114. bool waitForThreadToExit (const int timeOutMilliseconds) const;
  115. //==============================================================================
  116. /** Changes the thread's priority.
  117. @param priority the new priority, in the range 0 (lowest) to 10 (highest). A priority
  118. of 5 is normal.
  119. */
  120. void setPriority (const int priority);
  121. /** Changes the priority of the caller thread.
  122. Similar to setPriority(), but this static method acts on the caller thread.
  123. @see setPriority
  124. */
  125. static void setCurrentThreadPriority (const int priority);
  126. //==============================================================================
  127. /** Sets the affinity mask for the thread.
  128. This will only have an effect next time the thread is started - i.e. if the
  129. thread is already running when called, it'll have no effect.
  130. @see setCurrentThreadAffinityMask
  131. */
  132. void setAffinityMask (const uint32 affinityMask);
  133. /** Changes the affinity mask for the caller thread.
  134. This will change the affinity mask for the thread that calls this static method.
  135. @see setAffinityMask
  136. */
  137. static void setCurrentThreadAffinityMask (const uint32 affinityMask);
  138. //==============================================================================
  139. // this can be called from any thread that needs to pause..
  140. static void sleep (int milliseconds) throw();
  141. /** Yields the calling thread's current time-slot. */
  142. static void yield() throw();
  143. //==============================================================================
  144. /** Makes the thread wait for a notification.
  145. This puts the thread to sleep until either the timeout period expires, or
  146. another thread calls the notify() method to wake it up.
  147. @returns true if the event has been signalled, false if the timeout expires.
  148. */
  149. bool wait (const int timeOutMilliseconds) const throw();
  150. /** Wakes up the thread.
  151. If the thread has called the wait() method, this will wake it up.
  152. @see wait
  153. */
  154. void notify() const throw();
  155. //==============================================================================
  156. /** Returns an id that identifies the caller thread.
  157. To find the ID of a particular thread object, use getThreadId().
  158. @returns a unique identifier that identifies the calling thread.
  159. @see getThreadId
  160. */
  161. static int getCurrentThreadId() throw();
  162. /** Returns the ID of this thread.
  163. That means the ID of this thread object - not of the thread that's calling the method.
  164. This can change when the thread is started and stopped, and will be invalid if the
  165. thread's not actually running.
  166. @see getCurrentThreadId
  167. */
  168. int getThreadId() const throw();
  169. /** Returns the name of the thread.
  170. This is the name that gets set in the constructor.
  171. */
  172. const String getThreadName() const throw() { return threadName_; }
  173. //==============================================================================
  174. /** Returns the number of currently-running threads.
  175. @returns the number of Thread objects known to be currently running.
  176. @see stopAllThreads
  177. */
  178. static int getNumRunningThreads();
  179. /** Tries to stop all currently-running threads.
  180. This will attempt to stop all the threads known to be running at the moment.
  181. */
  182. static void stopAllThreads (const int timeoutInMillisecs);
  183. //==============================================================================
  184. juce_UseDebuggingNewOperator
  185. private:
  186. const String threadName_;
  187. void* volatile threadHandle_;
  188. CriticalSection startStopLock;
  189. WaitableEvent startSuspensionEvent_, defaultEvent_;
  190. int threadPriority_, threadId_;
  191. uint32 affinityMask_;
  192. bool volatile threadShouldExit_;
  193. friend void JUCE_API juce_threadEntryPoint (void*);
  194. static void threadEntryPoint (Thread* thread);
  195. Thread (const Thread&);
  196. const Thread& operator= (const Thread&);
  197. };
  198. #endif // __JUCE_THREAD_JUCEHEADER__