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