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