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