Fix crash when host does not support midi-in; Add missing file
[juce-lv2.git] / juce / source / src / threads / juce_Thread.h
blob3cceb5660b91cf637c00cef5a26a2818e0a557f8
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_THREAD_JUCEHEADER__
27 #define __JUCE_THREAD_JUCEHEADER__
29 #include "juce_WaitableEvent.h"
30 #include "juce_CriticalSection.h"
33 //==============================================================================
34 /**
35 Encapsulates a thread.
37 Subclasses derive from Thread and implement the run() method, in which they
38 do their business. The thread can then be started with the startThread() method
39 and controlled with various other methods.
41 This class also contains some thread-related static methods, such
42 as sleep(), yield(), getCurrentThreadId() etc.
44 @see CriticalSection, WaitableEvent, Process, ThreadWithProgressWindow,
45 MessageManagerLock
47 class JUCE_API Thread
49 public:
50 //==============================================================================
51 /**
52 Creates a thread.
54 When first created, the thread is not running. Use the startThread()
55 method to start it.
57 explicit Thread (const String& threadName);
59 /** Destructor.
61 Deleting a Thread object that is running will only give the thread a
62 brief opportunity to stop itself cleanly, so it's recommended that you
63 should always call stopThread() with a decent timeout before deleting,
64 to avoid the thread being forcibly killed (which is a Bad Thing).
66 virtual ~Thread();
68 //==============================================================================
69 /** Must be implemented to perform the thread's actual code.
71 Remember that the thread must regularly check the threadShouldExit()
72 method whilst running, and if this returns true it should return from
73 the run() method as soon as possible to avoid being forcibly killed.
75 @see threadShouldExit, startThread
77 virtual void run() = 0;
79 //==============================================================================
80 // Thread control functions..
82 /** Starts the thread running.
84 This will start the thread's run() method.
85 (if it's already started, startThread() won't do anything).
87 @see stopThread
89 void startThread();
91 /** Starts the thread with a given priority.
93 Launches the thread with a given priority, where 0 = lowest, 10 = highest.
94 If the thread is already running, its priority will be changed.
96 @see startThread, setPriority
98 void startThread (int priority);
100 /** Attempts to stop the thread running.
102 This method will cause the threadShouldExit() method to return true
103 and call notify() in case the thread is currently waiting.
105 Hopefully the thread will then respond to this by exiting cleanly, and
106 the stopThread method will wait for a given time-period for this to
107 happen.
109 If the thread is stuck and fails to respond after the time-out, it gets
110 forcibly killed, which is a very bad thing to happen, as it could still
111 be holding locks, etc. which are needed by other parts of your program.
113 @param timeOutMilliseconds The number of milliseconds to wait for the
114 thread to finish before killing it by force. A negative
115 value in here will wait forever.
116 @see signalThreadShouldExit, threadShouldExit, waitForThreadToExit, isThreadRunning
118 void stopThread (int timeOutMilliseconds);
120 //==============================================================================
121 /** Returns true if the thread is currently active */
122 bool isThreadRunning() const;
124 /** Sets a flag to tell the thread it should stop.
126 Calling this means that the threadShouldExit() method will then return true.
127 The thread should be regularly checking this to see whether it should exit.
129 If your thread makes use of wait(), you might want to call notify() after calling
130 this method, to interrupt any waits that might be in progress, and allow it
131 to reach a point where it can exit.
133 @see threadShouldExit
134 @see waitForThreadToExit
136 void signalThreadShouldExit();
138 /** Checks whether the thread has been told to stop running.
140 Threads need to check this regularly, and if it returns true, they should
141 return from their run() method at the first possible opportunity.
143 @see signalThreadShouldExit
145 inline bool threadShouldExit() const { return threadShouldExit_; }
147 /** Waits for the thread to stop.
149 This will waits until isThreadRunning() is false or until a timeout expires.
151 @param timeOutMilliseconds the time to wait, in milliseconds. If this value
152 is less than zero, it will wait forever.
153 @returns true if the thread exits, or false if the timeout expires first.
155 bool waitForThreadToExit (int timeOutMilliseconds) const;
157 //==============================================================================
158 /** Changes the thread's priority.
159 May return false if for some reason the priority can't be changed.
161 @param priority the new priority, in the range 0 (lowest) to 10 (highest). A priority
162 of 5 is normal.
164 bool setPriority (int priority);
166 /** Changes the priority of the caller thread.
168 Similar to setPriority(), but this static method acts on the caller thread.
169 May return false if for some reason the priority can't be changed.
171 @see setPriority
173 static bool setCurrentThreadPriority (int priority);
175 //==============================================================================
176 /** Sets the affinity mask for the thread.
178 This will only have an effect next time the thread is started - i.e. if the
179 thread is already running when called, it'll have no effect.
181 @see setCurrentThreadAffinityMask
183 void setAffinityMask (uint32 affinityMask);
185 /** Changes the affinity mask for the caller thread.
187 This will change the affinity mask for the thread that calls this static method.
189 @see setAffinityMask
191 static void setCurrentThreadAffinityMask (uint32 affinityMask);
193 //==============================================================================
194 // this can be called from any thread that needs to pause..
195 static void JUCE_CALLTYPE sleep (int milliseconds);
197 /** Yields the calling thread's current time-slot. */
198 static void JUCE_CALLTYPE yield();
200 //==============================================================================
201 /** Makes the thread wait for a notification.
203 This puts the thread to sleep until either the timeout period expires, or
204 another thread calls the notify() method to wake it up.
206 A negative time-out value means that the method will wait indefinitely.
208 @returns true if the event has been signalled, false if the timeout expires.
210 bool wait (int timeOutMilliseconds) const;
212 /** Wakes up the thread.
214 If the thread has called the wait() method, this will wake it up.
216 @see wait
218 void notify() const;
220 //==============================================================================
221 /** A value type used for thread IDs.
222 @see getCurrentThreadId(), getThreadId()
224 typedef void* ThreadID;
226 /** Returns an id that identifies the caller thread.
228 To find the ID of a particular thread object, use getThreadId().
230 @returns a unique identifier that identifies the calling thread.
231 @see getThreadId
233 static ThreadID getCurrentThreadId();
235 /** Finds the thread object that is currently running.
237 Note that the main UI thread (or other non-Juce threads) don't have a Thread
238 object associated with them, so this will return 0.
240 static Thread* getCurrentThread();
242 /** Returns the ID of this thread.
244 That means the ID of this thread object - not of the thread that's calling the method.
246 This can change when the thread is started and stopped, and will be invalid if the
247 thread's not actually running.
249 @see getCurrentThreadId
251 ThreadID getThreadId() const noexcept { return threadId_; }
253 /** Returns the name of the thread.
255 This is the name that gets set in the constructor.
257 const String& getThreadName() const { return threadName_; }
259 /** Changes the name of the caller thread.
260 Different OSes may place different length or content limits on this name.
262 static void setCurrentThreadName (const String& newThreadName);
264 //==============================================================================
265 /** Returns the number of currently-running threads.
267 @returns the number of Thread objects known to be currently running.
268 @see stopAllThreads
270 static int getNumRunningThreads();
272 /** Tries to stop all currently-running threads.
274 This will attempt to stop all the threads known to be running at the moment.
276 static void stopAllThreads (int timeoutInMillisecs);
279 private:
280 //==============================================================================
281 const String threadName_;
282 void* volatile threadHandle_;
283 ThreadID threadId_;
284 CriticalSection startStopLock;
285 WaitableEvent startSuspensionEvent_, defaultEvent_;
286 int threadPriority_;
287 uint32 affinityMask_;
288 bool volatile threadShouldExit_;
290 #ifndef DOXYGEN
291 friend class MessageManager;
292 friend void JUCE_API juce_threadEntryPoint (void*);
293 #endif
295 void launchThread();
296 void closeThreadHandle();
297 void killThread();
298 void threadEntryPoint();
299 static bool setThreadPriority (void* handle, int priority);
301 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Thread);
304 #endif // __JUCE_THREAD_JUCEHEADER__