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