]>
Commit | Line | Data |
---|---|---|
10b959e3 | 1 | ///////////////////////////////////////////////////////////////////////////// |
9e84b847 | 2 | // Name: wx/thread.h |
10b959e3 JS |
3 | // Purpose: Thread API |
4 | // Author: Guilhem Lavaux | |
bf1852e1 VZ |
5 | // Modified by: Vadim Zeitlin (modifications partly inspired by omnithreads |
6 | // package from Olivetti & Oracle Research Laboratory) | |
10b959e3 JS |
7 | // Created: 04/13/98 |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) Guilhem Lavaux | |
10 | // Licence: wxWindows licence | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
9e84b847 VZ |
13 | #ifndef _WX_THREAD_H_ |
14 | #define _WX_THREAD_H_ | |
10b959e3 | 15 | |
9d133d87 VZ |
16 | // ---------------------------------------------------------------------------- |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
bf1852e1 VZ |
19 | |
20 | // get the value of wxUSE_THREADS configuration flag | |
9e84b847 | 21 | #include "wx/defs.h" |
9d133d87 VZ |
22 | |
23 | #if wxUSE_THREADS | |
6d167489 VZ |
24 | |
25 | // only for wxUSE_THREADS - otherwise we'd get undefined symbols | |
80cb83be KB |
26 | #ifdef __GNUG__ |
27 | #pragma interface "thread.h" | |
28 | #endif | |
bf1852e1 VZ |
29 | |
30 | // Windows headers define it | |
31 | #ifdef Yield | |
32 | #undef Yield | |
33 | #endif | |
34 | ||
d524867f | 35 | #include "wx/module.h" |
10b959e3 | 36 | |
a6b0bd49 VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // constants | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
6d167489 | 41 | enum wxMutexError |
d524867f | 42 | { |
9e84b847 VZ |
43 | wxMUTEX_NO_ERROR = 0, // operation completed successfully |
44 | wxMUTEX_INVALID, // mutex hasn't been initialized | |
45 | wxMUTEX_DEAD_LOCK, // mutex is already locked by the calling thread | |
46 | wxMUTEX_BUSY, // mutex is already locked by another thread | |
47 | wxMUTEX_UNLOCKED, // attempt to unlock a mutex which is not locked | |
48 | wxMUTEX_MISC_ERROR // any other error | |
49 | }; | |
50 | ||
51 | enum wxCondError | |
52 | { | |
53 | wxCOND_NO_ERROR = 0, | |
54 | wxCOND_INVALID, | |
55 | wxCOND_TIMEOUT, // WaitTimeout() has timed out | |
56 | wxCOND_MISC_ERROR | |
57 | }; | |
58 | ||
59 | enum wxSemaError | |
60 | { | |
61 | wxSEMA_NO_ERROR = 0, | |
62 | wxSEMA_INVALID, // semaphore hasn't been initialized successfully | |
63 | wxSEMA_BUSY, // returned by TryWait() if Wait() would block | |
64 | wxSEMA_TIMEOUT, // returned by WaitTimeout() | |
65 | wxSEMA_OVERFLOW, // Post() would increase counter past the max | |
66 | wxSEMA_MISC_ERROR | |
6d167489 | 67 | }; |
10b959e3 | 68 | |
6d167489 | 69 | enum wxThreadError |
d524867f | 70 | { |
9fc3ad34 VZ |
71 | wxTHREAD_NO_ERROR = 0, // No error |
72 | wxTHREAD_NO_RESOURCE, // No resource left to create a new thread | |
73 | wxTHREAD_RUNNING, // The thread is already running | |
74 | wxTHREAD_NOT_RUNNING, // The thread isn't running | |
75 | wxTHREAD_KILLED, // Thread we waited for had to be killed | |
76 | wxTHREAD_MISC_ERROR // Some other error | |
6d167489 | 77 | }; |
10b959e3 | 78 | |
b568d04f VZ |
79 | enum wxThreadKind |
80 | { | |
81 | wxTHREAD_DETACHED, | |
82 | wxTHREAD_JOINABLE | |
83 | }; | |
84 | ||
bf1852e1 | 85 | // defines the interval of priority |
6d167489 VZ |
86 | enum |
87 | { | |
88 | WXTHREAD_MIN_PRIORITY = 0u, | |
89 | WXTHREAD_DEFAULT_PRIORITY = 50u, | |
90 | WXTHREAD_MAX_PRIORITY = 100u | |
91 | }; | |
10b959e3 | 92 | |
9e84b847 VZ |
93 | // There are 2 types of mutexes: normal mutexes and recursive ones. The attempt |
94 | // to lock a normal mutex by a thread which already owns it results in | |
95 | // undefined behaviour (it always works under Windows, it will almost always | |
96 | // result in a deadlock under Unix). Locking a recursive mutex in such | |
97 | // situation always succeeds and it must be unlocked as many times as it has | |
98 | // been locked. | |
99 | // | |
100 | // However recursive mutexes have several important drawbacks: first, in the | |
101 | // POSIX implementation, they're less efficient. Second, and more importantly, | |
102 | // they CAN NOT BE USED WITH CONDITION VARIABLES under Unix! Using them with | |
103 | // wxCondition will work under Windows and some Unices (notably Linux) but will | |
104 | // deadlock under other Unix versions (e.g. Solaris). As it might be difficult | |
105 | // to ensure that a recursive mutex is not used with wxCondition, it is a good | |
106 | // idea to avoid using recursive mutexes at all. Also, the last problem with | |
107 | // them is that some (older) Unix versions don't support this at all -- which | |
108 | // results in a configure warning when building and a deadlock when using them. | |
109 | enum wxMutexType | |
110 | { | |
111 | // normal mutex: try to always use this one | |
112 | wxMUTEX_DEFAULT, | |
113 | ||
114 | // recursive mutex: don't use these ones with wxCondition | |
115 | wxMUTEX_RECURSIVE | |
116 | }; | |
117 | ||
118 | // forward declarations | |
119 | class WXDLLEXPORT wxConditionInternal; | |
120 | class WXDLLEXPORT wxMutexInternal; | |
121 | class WXDLLEXPORT wxSemaphoreInternal; | |
122 | class WXDLLEXPORT wxThreadInternal; | |
123 | ||
d524867f | 124 | // ---------------------------------------------------------------------------- |
9d133d87 VZ |
125 | // A mutex object is a synchronization object whose state is set to signaled |
126 | // when it is not owned by any thread, and nonsignaled when it is owned. Its | |
127 | // name comes from its usefulness in coordinating mutually-exclusive access to | |
128 | // a shared resource. Only one thread at a time can own a mutex object. | |
a6b0bd49 | 129 | // ---------------------------------------------------------------------------- |
d524867f | 130 | |
9d133d87 VZ |
131 | // you should consider wxMutexLocker whenever possible instead of directly |
132 | // working with wxMutex class - it is safer | |
bf1852e1 | 133 | class WXDLLEXPORT wxMutex |
d524867f | 134 | { |
10b959e3 | 135 | public: |
bf1852e1 | 136 | // constructor & destructor |
9e84b847 VZ |
137 | // ------------------------ |
138 | ||
139 | // create either default (always safe) or recursive mutex | |
140 | wxMutex(wxMutexType mutexType = wxMUTEX_DEFAULT); | |
141 | ||
142 | // destroys the mutex kernel object | |
cb4f1ca4 | 143 | ~wxMutex(); |
10b959e3 | 144 | |
9e84b847 VZ |
145 | // test if the mutex has been created successfully |
146 | bool IsOk() const; | |
147 | ||
148 | // mutex operations | |
149 | // ---------------- | |
150 | ||
151 | // Lock the mutex, blocking on it until it is unlocked by the other thread. | |
152 | // The result of locking a mutex already locked by the current thread | |
153 | // depend on the mutex type. | |
154 | // | |
155 | // The caller must call Unlock() later if Lock() returned wxMUTEX_NO_ERROR. | |
cb4f1ca4 | 156 | wxMutexError Lock(); |
9e84b847 VZ |
157 | |
158 | // Try to lock the mutex: if it is currently locked, return immediately | |
159 | // with an error. Otherwise the caller must call Unlock(). | |
cb4f1ca4 | 160 | wxMutexError TryLock(); |
10b959e3 | 161 | |
9e84b847 VZ |
162 | // Unlock the mutex. It is an error to unlock an already unlocked mutex |
163 | wxMutexError Unlock(); | |
a6b0bd49 | 164 | |
10b959e3 | 165 | protected: |
9fc3ad34 | 166 | wxMutexInternal *m_internal; |
be809868 VZ |
167 | |
168 | friend class wxConditionInternal; | |
9e84b847 VZ |
169 | |
170 | DECLARE_NO_COPY_CLASS(wxMutex) | |
10b959e3 JS |
171 | }; |
172 | ||
9d133d87 VZ |
173 | // a helper class which locks the mutex in the ctor and unlocks it in the dtor: |
174 | // this ensures that mutex is always unlocked, even if the function returns or | |
175 | // throws an exception before it reaches the end | |
176 | class WXDLLEXPORT wxMutexLocker | |
177 | { | |
178 | public: | |
179 | // lock the mutex in the ctor | |
be52b341 GD |
180 | wxMutexLocker(wxMutex& mutex) |
181 | : m_isOk(FALSE), m_mutex(mutex) | |
182 | { m_isOk = ( m_mutex.Lock() == wxMUTEX_NO_ERROR ); } | |
9d133d87 VZ |
183 | |
184 | // returns TRUE if mutex was successfully locked in ctor | |
7c3d7e2d VZ |
185 | bool IsOk() const |
186 | { return m_isOk; } | |
9d133d87 VZ |
187 | |
188 | // unlock the mutex in dtor | |
7c3d7e2d VZ |
189 | ~wxMutexLocker() |
190 | { if ( IsOk() ) m_mutex.Unlock(); } | |
9d133d87 VZ |
191 | |
192 | private: | |
cb4f1ca4 VZ |
193 | // no assignment operator nor copy ctor |
194 | wxMutexLocker(const wxMutexLocker&); | |
195 | wxMutexLocker& operator=(const wxMutexLocker&); | |
196 | ||
9d133d87 | 197 | bool m_isOk; |
7c3d7e2d | 198 | wxMutex& m_mutex; |
9d133d87 VZ |
199 | }; |
200 | ||
201 | // ---------------------------------------------------------------------------- | |
202 | // Critical section: this is the same as mutex but is only visible to the | |
203 | // threads of the same process. For the platforms which don't have native | |
204 | // support for critical sections, they're implemented entirely in terms of | |
6d167489 VZ |
205 | // mutexes. |
206 | // | |
207 | // NB: wxCriticalSection object does not allocate any memory in its ctor | |
208 | // which makes it possible to have static globals of this class | |
9d133d87 VZ |
209 | // ---------------------------------------------------------------------------- |
210 | ||
6d167489 VZ |
211 | // in order to avoid any overhead under platforms where critical sections are |
212 | // just mutexes make all wxCriticalSection class functions inline | |
acd9676e | 213 | #if !defined(__WXMSW__) && !defined(__WXPM__) |
6d167489 | 214 | #define wxCRITSECT_IS_MUTEX 1 |
d42e3d59 VZ |
215 | |
216 | #define wxCRITSECT_INLINE inline | |
acd9676e | 217 | #else // MSW || OS2 |
6d167489 | 218 | #define wxCRITSECT_IS_MUTEX 0 |
d42e3d59 VZ |
219 | |
220 | #define wxCRITSECT_INLINE | |
f6ddc54a | 221 | #endif // MSW/!MSW |
bf1852e1 VZ |
222 | |
223 | // you should consider wxCriticalSectionLocker whenever possible instead of | |
224 | // directly working with wxCriticalSection class - it is safer | |
9d133d87 VZ |
225 | class WXDLLEXPORT wxCriticalSection |
226 | { | |
227 | public: | |
228 | // ctor & dtor | |
d42e3d59 VZ |
229 | wxCRITSECT_INLINE wxCriticalSection(); |
230 | wxCRITSECT_INLINE ~wxCriticalSection(); | |
9d133d87 VZ |
231 | |
232 | // enter the section (the same as locking a mutex) | |
d42e3d59 | 233 | wxCRITSECT_INLINE void Enter(); |
9e84b847 | 234 | |
9d133d87 | 235 | // leave the critical section (same as unlocking a mutex) |
d42e3d59 | 236 | wxCRITSECT_INLINE void Leave(); |
9d133d87 VZ |
237 | |
238 | private: | |
6d167489 | 239 | #if wxCRITSECT_IS_MUTEX |
f6ddc54a | 240 | wxMutex m_mutex; |
6d167489 VZ |
241 | #elif defined(__WXMSW__) |
242 | // we can't allocate any memory in the ctor, so use placement new - | |
243 | // unfortunately, we have to hardcode the sizeof() here because we can't | |
244 | // include windows.h from this public header | |
9e84b847 VZ |
245 | // |
246 | // if CRITICAL_SECTION size changes in Windows, you'll get an assert from | |
247 | // thread.cpp and will need to increase the buffer size | |
6d167489 | 248 | char m_buffer[24]; |
6d167489 VZ |
249 | #else |
250 | // nothing for OS/2 | |
9e84b847 VZ |
251 | #endif // Unix/Win32/OS2 |
252 | ||
253 | DECLARE_NO_COPY_CLASS(wxCriticalSection) | |
9d133d87 VZ |
254 | }; |
255 | ||
9e84b847 VZ |
256 | #if wxCRITSECT_IS_MUTEX |
257 | // implement wxCriticalSection using mutexes | |
d42e3d59 VZ |
258 | inline wxCriticalSection::wxCriticalSection() { } |
259 | inline wxCriticalSection::~wxCriticalSection() { } | |
9e84b847 | 260 | |
d42e3d59 VZ |
261 | inline void wxCriticalSection::Enter() { (void)m_mutex.Lock(); } |
262 | inline void wxCriticalSection::Leave() { (void)m_mutex.Unlock(); } | |
9e84b847 | 263 | #endif // wxCRITSECT_IS_MUTEX |
bf1852e1 | 264 | |
d42e3d59 VZ |
265 | #undef wxCRITSECT_INLINE |
266 | #undef wxCRITSECT_IS_MUTEX | |
267 | ||
9d133d87 VZ |
268 | // wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is |
269 | // to th mutexes | |
270 | class WXDLLEXPORT wxCriticalSectionLocker | |
271 | { | |
272 | public: | |
9e84b847 VZ |
273 | wxCriticalSectionLocker(wxCriticalSection& cs) |
274 | : m_critsect(cs) | |
275 | { | |
276 | m_critsect.Enter(); | |
277 | } | |
9d133d87 | 278 | |
9e84b847 VZ |
279 | ~wxCriticalSectionLocker() |
280 | { | |
281 | m_critsect.Leave(); | |
282 | } | |
cb4f1ca4 | 283 | |
9e84b847 | 284 | private: |
bee503b0 | 285 | wxCriticalSection& m_critsect; |
9e84b847 VZ |
286 | |
287 | DECLARE_NO_COPY_CLASS(wxCriticalSectionLocker) | |
9d133d87 VZ |
288 | }; |
289 | ||
a6b0bd49 | 290 | // ---------------------------------------------------------------------------- |
be809868 VZ |
291 | // wxCondition models a POSIX condition variable which allows one (or more) |
292 | // thread(s) to wait until some condition is fulfilled | |
a6b0bd49 | 293 | // ---------------------------------------------------------------------------- |
d524867f | 294 | |
bf1852e1 | 295 | class WXDLLEXPORT wxCondition |
d524867f | 296 | { |
10b959e3 | 297 | public: |
9e84b847 VZ |
298 | // Each wxCondition object is associated with a (single) wxMutex object. |
299 | // The mutex object MUST be locked before calling Wait() | |
c112e100 | 300 | wxCondition(wxMutex& mutex); |
be809868 VZ |
301 | |
302 | // dtor is not virtual, don't use this class polymorphically | |
9fc3ad34 VZ |
303 | ~wxCondition(); |
304 | ||
9e84b847 VZ |
305 | // return TRUE if the condition has been created successfully |
306 | bool IsOk() const; | |
307 | ||
be809868 VZ |
308 | // NB: the associated mutex MUST be locked beforehand by the calling thread |
309 | // | |
310 | // it atomically releases the lock on the associated mutex | |
311 | // and starts waiting to be woken up by a Signal()/Broadcast() | |
312 | // once its signaled, then it will wait until it can reacquire | |
313 | // the lock on the associated mutex object, before returning. | |
9e84b847 | 314 | wxCondError Wait(); |
9fc3ad34 | 315 | |
be809868 VZ |
316 | // exactly as Wait() except that it may also return if the specified |
317 | // timeout ellapses even if the condition hasn't been signalled: in this | |
318 | // case, the return value is FALSE, otherwise (i.e. in case of a normal | |
319 | // return) it is TRUE | |
320 | // | |
321 | // the timeeout parameter specifies a interval that needs to be waited in | |
322 | // milliseconds | |
9e84b847 | 323 | wxCondError WaitTimeout(unsigned long milliseconds); |
be809868 VZ |
324 | |
325 | // NB: the associated mutex may or may not be locked by the calling thread | |
326 | // | |
327 | // this method unblocks one thread if any are blocking on the condition. | |
328 | // if no thread is blocking in Wait(), then the signal is NOT remembered | |
329 | // The thread which was blocking on Wait(), will then reacquire the lock | |
330 | // on the associated mutex object before returning | |
9e84b847 | 331 | wxCondError Signal(); |
a6b0bd49 | 332 | |
be809868 VZ |
333 | // NB: the associated mutex may or may not be locked by the calling thread |
334 | // | |
335 | // this method unblocks all threads if any are blocking on the condition. | |
336 | // if no thread is blocking in Wait(), then the signal is NOT remembered | |
337 | // The threads which were blocking on Wait(), will then reacquire the lock | |
338 | // on the associated mutex object before returning. | |
9e84b847 VZ |
339 | wxCondError Broadcast(); |
340 | ||
341 | ||
342 | // deprecated version, don't use | |
343 | bool Wait(unsigned long milliseconds) | |
344 | { return WaitTimeout(milliseconds) == wxCOND_NO_ERROR; } | |
8d5eff60 | 345 | |
10b959e3 | 346 | private: |
9fc3ad34 | 347 | wxConditionInternal *m_internal; |
9e84b847 VZ |
348 | |
349 | DECLARE_NO_COPY_CLASS(wxCondition) | |
10b959e3 JS |
350 | }; |
351 | ||
a6b0bd49 | 352 | // ---------------------------------------------------------------------------- |
be809868 VZ |
353 | // wxSemaphore: a counter limiting the number of threads concurrently accessing |
354 | // a shared resource | |
355 | // ---------------------------------------------------------------------------- | |
356 | ||
be809868 VZ |
357 | class WXDLLEXPORT wxSemaphore |
358 | { | |
359 | public: | |
360 | // specifying a maxcount of 0 actually makes wxSemaphore behave as if there | |
361 | // is no upper limit, if maxcount is 1 the semaphore behaves as a mutex | |
362 | wxSemaphore( int initialcount = 0, int maxcount = 0 ); | |
363 | ||
364 | // dtor is not virtual, don't use this class polymorphically | |
365 | ~wxSemaphore(); | |
366 | ||
9e84b847 VZ |
367 | // return TRUE if the semaphore has been created successfully |
368 | bool IsOk() const; | |
369 | ||
be809868 VZ |
370 | // wait indefinitely, until the semaphore count goes beyond 0 |
371 | // and then decrement it and return (this method might have been called | |
372 | // Acquire()) | |
9e84b847 | 373 | wxSemaError Wait(); |
be809868 | 374 | |
9e84b847 VZ |
375 | // same as Wait(), but does not block, returns wxSEMA_NO_ERROR if |
376 | // successful and wxSEMA_BUSY if the count is currently zero | |
377 | wxSemaError TryWait(); | |
be809868 | 378 | |
9e84b847 VZ |
379 | // same as Wait(), but as a timeout limit, returns wxSEMA_NO_ERROR if the |
380 | // semaphore was acquired and wxSEMA_TIMEOUT if the timeout has ellapsed | |
381 | wxSemaError WaitTimeout(unsigned long milliseconds); | |
be809868 VZ |
382 | |
383 | // increments the semaphore count and signals one of the waiting threads | |
9e84b847 | 384 | wxSemaError Post(); |
be809868 VZ |
385 | |
386 | private: | |
387 | wxSemaphoreInternal *m_internal; | |
9e84b847 VZ |
388 | |
389 | DECLARE_NO_COPY_CLASS(wxSemaphore) | |
be809868 VZ |
390 | }; |
391 | ||
392 | // ---------------------------------------------------------------------------- | |
393 | // wxThread: class encpasulating a thread of execution | |
a6b0bd49 | 394 | // ---------------------------------------------------------------------------- |
d524867f | 395 | |
b568d04f VZ |
396 | // there are two different kinds of threads: joinable and detached (default) |
397 | // ones. Only joinable threads can return a return code and only detached | |
398 | // threads auto-delete themselves - the user should delete the joinable | |
399 | // threads manually. | |
bf1852e1 VZ |
400 | |
401 | // NB: in the function descriptions the words "this thread" mean the thread | |
402 | // created by the wxThread object while "main thread" is the thread created | |
403 | // during the process initialization (a.k.a. the GUI thread) | |
b568d04f | 404 | |
547b93ab VZ |
405 | // On VMS thread pointers are 64 bits (also needed for other systems??? |
406 | #ifdef __VMS | |
407 | typedef unsigned long long wxThreadIdType; | |
408 | #else | |
409 | typedef unsigned long wxThreadIdType; | |
410 | #endif | |
411 | ||
bf1852e1 | 412 | class WXDLLEXPORT wxThread |
d524867f | 413 | { |
10b959e3 | 414 | public: |
bf1852e1 VZ |
415 | // the return type for the thread function |
416 | typedef void *ExitCode; | |
417 | ||
418 | // static functions | |
419 | // Returns the wxThread object for the calling thread. NULL is returned | |
420 | // if the caller is the main thread (but it's recommended to use | |
421 | // IsMain() and only call This() for threads other than the main one | |
422 | // because NULL is also returned on error). If the thread wasn't | |
423 | // created with wxThread class, the returned value is undefined. | |
424 | static wxThread *This(); | |
425 | ||
426 | // Returns true if current thread is the main thread. | |
427 | static bool IsMain(); | |
428 | ||
429 | // Release the rest of our time slice leting the other threads run | |
430 | static void Yield(); | |
431 | ||
432 | // Sleep during the specified period of time in milliseconds | |
433 | // | |
434 | // NB: at least under MSW worker threads can not call ::wxSleep()! | |
435 | static void Sleep(unsigned long milliseconds); | |
436 | ||
ef8d96c2 VZ |
437 | // get the number of system CPUs - useful with SetConcurrency() |
438 | // (the "best" value for it is usually number of CPUs + 1) | |
439 | // | |
440 | // Returns -1 if unknown, number of CPUs otherwise | |
441 | static int GetCPUCount(); | |
442 | ||
4958ea8f RD |
443 | // Get the platform specific thread ID and return as a long. This |
444 | // can be used to uniquely identify threads, even if they are not | |
445 | // wxThreads. This is used by wxPython. | |
547b93ab VZ |
446 | static wxThreadIdType GetCurrentId(); |
447 | ||
ef8d96c2 VZ |
448 | // sets the concurrency level: this is, roughly, the number of threads |
449 | // the system tries to schedule to run in parallel. 0 means the | |
450 | // default value (usually acceptable, but may not yield the best | |
451 | // performance for this process) | |
452 | // | |
453 | // Returns TRUE on success, FALSE otherwise (if not implemented, for | |
454 | // example) | |
455 | static bool SetConcurrency(size_t level); | |
456 | ||
b568d04f VZ |
457 | // constructor only creates the C++ thread object and doesn't create (or |
458 | // start) the real thread | |
459 | wxThread(wxThreadKind kind = wxTHREAD_DETACHED); | |
460 | ||
461 | // functions that change the thread state: all these can only be called | |
462 | // from _another_ thread (typically the thread that created this one, e.g. | |
463 | // the main thread), not from the thread itself | |
bf1852e1 | 464 | |
6fe73788 RL |
465 | // create a new thread and optionally set the stack size on |
466 | // platforms that support that - call Run() to start it | |
467 | // (special cased for watcom which won't accept 0 default) | |
468 | ||
6fe73788 | 469 | wxThreadError Create(unsigned int stackSize = 0); |
bf1852e1 | 470 | |
b568d04f VZ |
471 | // starts execution of the thread - from the moment Run() is called |
472 | // the execution of wxThread::Entry() may start at any moment, caller | |
bf1852e1 VZ |
473 | // shouldn't suppose that it starts after (or before) Run() returns. |
474 | wxThreadError Run(); | |
475 | ||
b568d04f VZ |
476 | // stops the thread if it's running and deletes the wxThread object if |
477 | // this is a detached thread freeing its memory - otherwise (for | |
478 | // joinable threads) you still need to delete wxThread object | |
479 | // yourself. | |
480 | // | |
481 | // this function only works if the thread calls TestDestroy() | |
482 | // periodically - the thread will only be deleted the next time it | |
483 | // does it! | |
484 | // | |
485 | // will fill the rc pointer with the thread exit code if it's !NULL | |
486 | wxThreadError Delete(ExitCode *rc = (ExitCode *)NULL); | |
487 | ||
488 | // waits for a joinable thread to finish and returns its exit code | |
489 | // | |
490 | // Returns (ExitCode)-1 on error (for example, if the thread is not | |
491 | // joinable) | |
492 | ExitCode Wait(); | |
bf1852e1 VZ |
493 | |
494 | // kills the thread without giving it any chance to clean up - should | |
495 | // not be used in normal circumstances, use Delete() instead. It is a | |
496 | // dangerous function that should only be used in the most extreme | |
b568d04f VZ |
497 | // cases! |
498 | // | |
499 | // The wxThread object is deleted by Kill() if the thread is | |
500 | // detachable, but you still have to delete it manually for joinable | |
501 | // threads. | |
bf1852e1 VZ |
502 | wxThreadError Kill(); |
503 | ||
b568d04f VZ |
504 | // pause a running thread: as Delete(), this only works if the thread |
505 | // calls TestDestroy() regularly | |
bf1852e1 VZ |
506 | wxThreadError Pause(); |
507 | ||
508 | // resume a paused thread | |
509 | wxThreadError Resume(); | |
510 | ||
511 | // priority | |
512 | // Sets the priority to "prio": see WXTHREAD_XXX_PRIORITY constants | |
513 | // | |
514 | // NB: the priority can only be set before the thread is created | |
515 | void SetPriority(unsigned int prio); | |
516 | ||
517 | // Get the current priority. | |
518 | unsigned int GetPriority() const; | |
519 | ||
bf1852e1 VZ |
520 | // thread status inquiries |
521 | // Returns true if the thread is alive: i.e. running or suspended | |
522 | bool IsAlive() const; | |
523 | // Returns true if the thread is running (not paused, not killed). | |
524 | bool IsRunning() const; | |
525 | // Returns true if the thread is suspended | |
a737331d | 526 | bool IsPaused() const; |
bf1852e1 | 527 | |
b568d04f VZ |
528 | // is the thread of detached kind? |
529 | bool IsDetached() const { return m_isDetached; } | |
530 | ||
531 | // Get the thread ID - a platform dependent number which uniquely | |
532 | // identifies a thread inside a process | |
547b93ab | 533 | wxThreadIdType GetId() const; |
4958ea8f | 534 | |
bf1852e1 VZ |
535 | // called when the thread exits - in the context of this thread |
536 | // | |
537 | // NB: this function will not be called if the thread is Kill()ed | |
538 | virtual void OnExit() { } | |
10b959e3 | 539 | |
b568d04f VZ |
540 | // dtor is public, but the detached threads should never be deleted - use |
541 | // Delete() instead (or leave the thread terminate by itself) | |
542 | virtual ~wxThread(); | |
543 | ||
bf1852e1 VZ |
544 | protected: |
545 | // Returns TRUE if the thread was asked to terminate: this function should | |
546 | // be called by the thread from time to time, otherwise the main thread | |
547 | // will be left forever in Delete()! | |
8c10faf1 | 548 | bool TestDestroy(); |
10b959e3 | 549 | |
bf1852e1 | 550 | // exits from the current thread - can be called only from this thread |
b568d04f | 551 | void Exit(ExitCode exitcode = 0); |
9d133d87 | 552 | |
bf1852e1 VZ |
553 | // entry point for the thread - called by Run() and executes in the context |
554 | // of this thread. | |
555 | virtual void *Entry() = 0; | |
a6b0bd49 | 556 | |
10b959e3 | 557 | private: |
bf1852e1 VZ |
558 | // no copy ctor/assignment operator |
559 | wxThread(const wxThread&); | |
560 | wxThread& operator=(const wxThread&); | |
10b959e3 | 561 | |
bf1852e1 VZ |
562 | friend class wxThreadInternal; |
563 | ||
564 | // the (platform-dependent) thread class implementation | |
9fc3ad34 | 565 | wxThreadInternal *m_internal; |
10b959e3 | 566 | |
bf1852e1 VZ |
567 | // protects access to any methods of wxThreadInternal object |
568 | wxCriticalSection m_critsect; | |
b568d04f VZ |
569 | |
570 | // true if the thread is detached, false if it is joinable | |
571 | bool m_isDetached; | |
10b959e3 JS |
572 | }; |
573 | ||
a6b0bd49 | 574 | // ---------------------------------------------------------------------------- |
d524867f | 575 | // Automatic initialization |
a6b0bd49 | 576 | // ---------------------------------------------------------------------------- |
10b959e3 | 577 | |
9d133d87 VZ |
578 | // GUI mutex handling. |
579 | void WXDLLEXPORT wxMutexGuiEnter(); | |
580 | void WXDLLEXPORT wxMutexGuiLeave(); | |
d524867f | 581 | |
72cdf4c9 VZ |
582 | // macros for entering/leaving critical sections which may be used without |
583 | // having to take them inside "#if wxUSE_THREADS" | |
b568d04f VZ |
584 | #define wxENTER_CRIT_SECT(cs) (cs).Enter() |
585 | #define wxLEAVE_CRIT_SECT(cs) (cs).Leave() | |
bdc72a22 | 586 | #define wxCRIT_SECT_DECLARE(cs) static wxCriticalSection cs |
b568d04f | 587 | #define wxCRIT_SECT_LOCKER(name, cs) wxCriticalSectionLocker name(cs) |
72cdf4c9 | 588 | |
9d133d87 | 589 | #else // !wxUSE_THREADS |
d524867f | 590 | |
9d133d87 VZ |
591 | // no thread support |
592 | inline void WXDLLEXPORT wxMutexGuiEnter() { } | |
593 | inline void WXDLLEXPORT wxMutexGuiLeave() { } | |
d524867f | 594 | |
72cdf4c9 VZ |
595 | // macros for entering/leaving critical sections which may be used without |
596 | // having to take them inside "#if wxUSE_THREADS" | |
597 | #define wxENTER_CRIT_SECT(cs) | |
598 | #define wxLEAVE_CRIT_SECT(cs) | |
bdc72a22 | 599 | #define wxCRIT_SECT_DECLARE(cs) |
72cdf4c9 VZ |
600 | #define wxCRIT_SECT_LOCKER(name, cs) |
601 | ||
9e84b847 | 602 | #endif // wxUSE_THREADS/!wxUSE_THREADS |
10b959e3 | 603 | |
4562f685 VZ |
604 | // mark part of code as being a critical section: this macro declares a |
605 | // critical section with the given name and enters it immediately and leaves | |
606 | // it at the end of the current scope | |
607 | // | |
608 | // example: | |
609 | // | |
610 | // int Count() | |
611 | // { | |
612 | // static int s_counter = 0; | |
613 | // | |
614 | // wxCRITICAL_SECTION(counter); | |
615 | // | |
616 | // return ++s_counter; | |
617 | // } | |
618 | // | |
619 | // this function is MT-safe in presence of the threads but there is no | |
620 | // overhead when the library is compiled without threads | |
621 | #define wxCRITICAL_SECTION(name) \ | |
622 | wxCRIT_SECT_DECLARE(s_cs##name); \ | |
623 | wxCRIT_SECT_LOCKER(cs##name##Locker, s_cs##name) | |
624 | ||
9e84b847 | 625 | // automatically lock GUI mutex in ctor and unlock it in dtor |
bee503b0 VZ |
626 | class WXDLLEXPORT wxMutexGuiLocker |
627 | { | |
628 | public: | |
629 | wxMutexGuiLocker() { wxMutexGuiEnter(); } | |
630 | ~wxMutexGuiLocker() { wxMutexGuiLeave(); } | |
631 | }; | |
632 | ||
f6ddc54a VZ |
633 | // ----------------------------------------------------------------------------- |
634 | // implementation only until the end of file | |
635 | // ----------------------------------------------------------------------------- | |
dcda1c71 | 636 | |
9838df2c | 637 | #if wxUSE_THREADS |
dcda1c71 | 638 | |
9e84b847 | 639 | #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXPM__) |
f6ddc54a VZ |
640 | // unlock GUI if there are threads waiting for and lock it back when |
641 | // there are no more of them - should be called periodically by the main | |
642 | // thread | |
a0abb8a8 | 643 | extern void WXDLLEXPORT wxMutexGuiLeaveOrEnter(); |
f6ddc54a VZ |
644 | |
645 | // returns TRUE if the main thread has GUI lock | |
a0abb8a8 | 646 | extern bool WXDLLEXPORT wxGuiOwnedByMainThread(); |
f6ddc54a | 647 | |
9e84b847 | 648 | #ifndef __WXPM__ |
f6ddc54a | 649 | // wakes up the main thread if it's sleeping inside ::GetMessage() |
a0abb8a8 | 650 | extern void WXDLLEXPORT wxWakeUpMainThread(); |
9e84b847 | 651 | #endif // !OS/2 |
bf1852e1 | 652 | |
e7549107 SC |
653 | // return TRUE if the main thread is waiting for some other to terminate: |
654 | // wxApp then should block all "dangerous" messages | |
655 | extern bool WXDLLEXPORT wxIsWaitingForThread(); | |
9e84b847 | 656 | #endif // MSW, Mac, OS/2 |
dcda1c71 | 657 | |
f6ddc54a VZ |
658 | #endif // wxUSE_THREADS |
659 | ||
9e84b847 | 660 | #endif // _WX_THREAD_H_ |
6fe73788 | 661 |