]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
9e84b847 | 2 | // Name: src/msw/thread.cpp |
2bda0e17 KB |
3 | // Purpose: wxThread Implementation |
4 | // Author: Original from Wolfram Gloger/Guilhem Lavaux | |
bee503b0 | 5 | // Modified by: Vadim Zeitlin to make it work :-) |
2bda0e17 KB |
6 | // Created: 04/22/98 |
7 | // RCS-ID: $Id$ | |
9e84b847 VZ |
8 | // Copyright: (c) Wolfram Gloger (1996, 1997), Guilhem Lavaux (1998); |
9 | // Vadim Zeitlin (1999-2002) | |
2bda0e17 KB |
10 | // Licence: wxWindows licence |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #ifdef __GNUG__ | |
3222fde2 | 14 | #pragma implementation "thread.h" |
2bda0e17 KB |
15 | #endif |
16 | ||
3222fde2 VZ |
17 | // ---------------------------------------------------------------------------- |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
a3b46648 | 21 | // For compilers that support precompilation, includes "wx.h". |
2bda0e17 KB |
22 | #include "wx/wxprec.h" |
23 | ||
24 | #if defined(__BORLANDC__) | |
3222fde2 | 25 | #pragma hdrstop |
2bda0e17 KB |
26 | #endif |
27 | ||
28 | #ifndef WX_PRECOMP | |
60ab3272 VS |
29 | #include "wx/intl.h" |
30 | #include "wx/app.h" | |
2bda0e17 KB |
31 | #endif |
32 | ||
3222fde2 VZ |
33 | #if wxUSE_THREADS |
34 | ||
e2478fde VZ |
35 | #include "wx/apptrait.h" |
36 | ||
0d0512bd | 37 | #include "wx/msw/private.h" |
17b439e8 | 38 | #include "wx/msw/missing.h" |
3222fde2 | 39 | |
2bda0e17 KB |
40 | #include "wx/module.h" |
41 | #include "wx/thread.h" | |
42 | ||
b568d04f VZ |
43 | // must have this symbol defined to get _beginthread/_endthread declarations |
44 | #ifndef _MT | |
45 | #define _MT | |
46 | #endif | |
47 | ||
9f51f2b6 VZ |
48 | #if defined(__BORLANDC__) |
49 | #if !defined(__MT__) | |
50 | // I can't set -tWM in the IDE (anyone?) so have to do this | |
51 | #define __MT__ | |
52 | #endif | |
53 | ||
54 | #if !defined(__MFC_COMPAT__) | |
55 | // Needed to know about _beginthreadex etc.. | |
56 | #define __MFC_COMPAT__ | |
57 | #endif | |
58 | #endif // BC++ | |
59 | ||
60 | // define wxUSE_BEGIN_THREAD if the compiler has _beginthreadex() function | |
61 | // which should be used instead of Win32 ::CreateThread() if possible | |
e0256755 GRG |
62 | #if defined(__VISUALC__) || \ |
63 | (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \ | |
9f51f2b6 | 64 | (defined(__GNUG__) && defined(__MSVCRT__)) || \ |
ba14d986 | 65 | defined(__WATCOMC__) || defined(__MWERKS__) |
ccebc98a | 66 | |
4676948b | 67 | #ifndef __WXWINCE__ |
9f51f2b6 VZ |
68 | #undef wxUSE_BEGIN_THREAD |
69 | #define wxUSE_BEGIN_THREAD | |
8536082d BJ |
70 | #endif |
71 | ||
4676948b JS |
72 | #endif |
73 | ||
9f51f2b6 VZ |
74 | #ifdef wxUSE_BEGIN_THREAD |
75 | // this is where _beginthreadex() is declared | |
b568d04f | 76 | #include <process.h> |
9f51f2b6 VZ |
77 | |
78 | // the return type of the thread function entry point | |
79 | typedef unsigned THREAD_RETVAL; | |
80 | ||
81 | // the calling convention of the thread function entry point | |
82 | #define THREAD_CALLCONV __stdcall | |
83 | #else | |
84 | // the settings for CreateThread() | |
85 | typedef DWORD THREAD_RETVAL; | |
86 | #define THREAD_CALLCONV WINAPI | |
b568d04f VZ |
87 | #endif |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // constants | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
bf1852e1 VZ |
93 | // the possible states of the thread ("=>" shows all possible transitions from |
94 | // this state) | |
95 | enum wxThreadState | |
96 | { | |
97 | STATE_NEW, // didn't start execution yet (=> RUNNING) | |
98 | STATE_RUNNING, // thread is running (=> PAUSED, CANCELED) | |
99 | STATE_PAUSED, // thread is temporarily suspended (=> RUNNING) | |
100 | STATE_CANCELED, // thread should terminate a.s.a.p. (=> EXITED) | |
101 | STATE_EXITED // thread is terminating | |
2bda0e17 KB |
102 | }; |
103 | ||
3222fde2 | 104 | // ---------------------------------------------------------------------------- |
b568d04f | 105 | // this module globals |
3222fde2 | 106 | // ---------------------------------------------------------------------------- |
2bda0e17 | 107 | |
bf1852e1 | 108 | // TLS index of the slot where we store the pointer to the current thread |
b568d04f | 109 | static DWORD gs_tlsThisThread = 0xFFFFFFFF; |
bf1852e1 | 110 | |
3222fde2 VZ |
111 | // id of the main thread - the one which can call GUI functions without first |
112 | // calling wxMutexGuiEnter() | |
b568d04f | 113 | static DWORD gs_idMainThread = 0; |
bee503b0 VZ |
114 | |
115 | // if it's FALSE, some secondary thread is holding the GUI lock | |
b568d04f | 116 | static bool gs_bGuiOwnedByMainThread = TRUE; |
2bda0e17 | 117 | |
3222fde2 VZ |
118 | // critical section which controls access to all GUI functions: any secondary |
119 | // thread (i.e. except the main one) must enter this crit section before doing | |
120 | // any GUI calls | |
b568d04f | 121 | static wxCriticalSection *gs_critsectGui = NULL; |
bee503b0 | 122 | |
b568d04f VZ |
123 | // critical section which protects gs_nWaitingForGui variable |
124 | static wxCriticalSection *gs_critsectWaitingForGui = NULL; | |
bee503b0 VZ |
125 | |
126 | // number of threads waiting for GUI in wxMutexGuiEnter() | |
b568d04f | 127 | static size_t gs_nWaitingForGui = 0; |
2bda0e17 | 128 | |
bf1852e1 | 129 | // are we waiting for a thread termination? |
b568d04f | 130 | static bool gs_waitingForThread = FALSE; |
bf1852e1 | 131 | |
3222fde2 | 132 | // ============================================================================ |
9e84b847 | 133 | // Windows implementation of thread and related classes |
3222fde2 VZ |
134 | // ============================================================================ |
135 | ||
136 | // ---------------------------------------------------------------------------- | |
9e84b847 VZ |
137 | // wxCriticalSection |
138 | // ---------------------------------------------------------------------------- | |
139 | ||
140 | wxCriticalSection::wxCriticalSection() | |
141 | { | |
dac348b9 | 142 | wxCOMPILE_TIME_ASSERT( sizeof(CRITICAL_SECTION) <= sizeof(wxCritSectBuffer), |
9e84b847 VZ |
143 | wxCriticalSectionBufferTooSmall ); |
144 | ||
145 | ::InitializeCriticalSection((CRITICAL_SECTION *)m_buffer); | |
146 | } | |
147 | ||
148 | wxCriticalSection::~wxCriticalSection() | |
149 | { | |
150 | ::DeleteCriticalSection((CRITICAL_SECTION *)m_buffer); | |
151 | } | |
152 | ||
153 | void wxCriticalSection::Enter() | |
154 | { | |
155 | ::EnterCriticalSection((CRITICAL_SECTION *)m_buffer); | |
156 | } | |
157 | ||
158 | void wxCriticalSection::Leave() | |
159 | { | |
160 | ::LeaveCriticalSection((CRITICAL_SECTION *)m_buffer); | |
161 | } | |
162 | ||
163 | // ---------------------------------------------------------------------------- | |
164 | // wxMutex | |
3222fde2 | 165 | // ---------------------------------------------------------------------------- |
0d0512bd | 166 | |
3222fde2 VZ |
167 | class wxMutexInternal |
168 | { | |
2bda0e17 | 169 | public: |
9e84b847 VZ |
170 | wxMutexInternal(wxMutexType mutexType); |
171 | ~wxMutexInternal(); | |
7f684264 | 172 | |
9e84b847 VZ |
173 | bool IsOk() const { return m_mutex != NULL; } |
174 | ||
175 | wxMutexError Lock() { return LockTimeout(INFINITE); } | |
176 | wxMutexError TryLock() { return LockTimeout(0); } | |
177 | wxMutexError Unlock(); | |
178 | ||
179 | private: | |
180 | wxMutexError LockTimeout(DWORD milliseconds); | |
7f684264 | 181 | |
7f684264 | 182 | HANDLE m_mutex; |
22f3361e VZ |
183 | |
184 | DECLARE_NO_COPY_CLASS(wxMutexInternal) | |
2bda0e17 KB |
185 | }; |
186 | ||
9e84b847 VZ |
187 | // all mutexes are recursive under Win32 so we don't use mutexType |
188 | wxMutexInternal::wxMutexInternal(wxMutexType WXUNUSED(mutexType)) | |
2bda0e17 | 189 | { |
9e84b847 VZ |
190 | // create a nameless (hence intra process and always private) mutex |
191 | m_mutex = ::CreateMutex | |
192 | ( | |
193 | NULL, // default secutiry attributes | |
194 | FALSE, // not initially locked | |
195 | NULL // no name | |
196 | ); | |
a6b0bd49 | 197 | |
9e84b847 VZ |
198 | if ( !m_mutex ) |
199 | { | |
200 | wxLogLastError(_T("CreateMutex()")); | |
201 | } | |
2bda0e17 KB |
202 | } |
203 | ||
9e84b847 | 204 | wxMutexInternal::~wxMutexInternal() |
2bda0e17 | 205 | { |
9e84b847 | 206 | if ( m_mutex ) |
7f684264 | 207 | { |
9e84b847 VZ |
208 | if ( !::CloseHandle(m_mutex) ) |
209 | { | |
210 | wxLogLastError(_T("CloseHandle(mutex)")); | |
211 | } | |
7f684264 | 212 | } |
2bda0e17 KB |
213 | } |
214 | ||
9e84b847 | 215 | wxMutexError wxMutexInternal::LockTimeout(DWORD milliseconds) |
2bda0e17 | 216 | { |
9e84b847 VZ |
217 | DWORD rc = ::WaitForSingleObject(m_mutex, milliseconds); |
218 | if ( rc == WAIT_ABANDONED ) | |
3222fde2 | 219 | { |
9e84b847 VZ |
220 | // the previous caller died without releasing the mutex, but now we can |
221 | // really lock it | |
222 | wxLogDebug(_T("WaitForSingleObject() returned WAIT_ABANDONED")); | |
223 | ||
224 | // use 0 timeout, normally we should always get it | |
225 | rc = ::WaitForSingleObject(m_mutex, 0); | |
226 | } | |
3222fde2 | 227 | |
9e84b847 VZ |
228 | switch ( rc ) |
229 | { | |
3222fde2 VZ |
230 | case WAIT_OBJECT_0: |
231 | // ok | |
232 | break; | |
2bda0e17 | 233 | |
3222fde2 | 234 | case WAIT_TIMEOUT: |
9e84b847 VZ |
235 | return wxMUTEX_BUSY; |
236 | ||
237 | case WAIT_ABANDONED: // checked for above | |
3222fde2 | 238 | default: |
223d09f6 | 239 | wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock")); |
9e84b847 | 240 | // fall through |
3222fde2 | 241 | |
9e84b847 VZ |
242 | case WAIT_FAILED: |
243 | wxLogLastError(_T("WaitForSingleObject(mutex)")); | |
244 | return wxMUTEX_MISC_ERROR; | |
245 | } | |
2bda0e17 | 246 | |
3222fde2 | 247 | return wxMUTEX_NO_ERROR; |
2bda0e17 KB |
248 | } |
249 | ||
9e84b847 | 250 | wxMutexError wxMutexInternal::Unlock() |
2bda0e17 | 251 | { |
9e84b847 | 252 | if ( !::ReleaseMutex(m_mutex) ) |
3222fde2 | 253 | { |
2b5f62a0 | 254 | wxLogLastError(_T("ReleaseMutex()")); |
9e84b847 | 255 | |
3222fde2 VZ |
256 | return wxMUTEX_MISC_ERROR; |
257 | } | |
a6b0bd49 | 258 | |
3222fde2 | 259 | return wxMUTEX_NO_ERROR; |
2bda0e17 KB |
260 | } |
261 | ||
be809868 | 262 | // -------------------------------------------------------------------------- |
9e84b847 | 263 | // wxSemaphore |
be809868 VZ |
264 | // -------------------------------------------------------------------------- |
265 | ||
9e84b847 | 266 | // a trivial wrapper around Win32 semaphore |
be809868 | 267 | class wxSemaphoreInternal |
3222fde2 | 268 | { |
2bda0e17 | 269 | public: |
9e84b847 | 270 | wxSemaphoreInternal(int initialcount, int maxcount); |
be809868 VZ |
271 | ~wxSemaphoreInternal(); |
272 | ||
9e84b847 | 273 | bool IsOk() const { return m_semaphore != NULL; } |
be809868 | 274 | |
9e84b847 | 275 | wxSemaError Wait() { return WaitTimeout(INFINITE); } |
918dc519 VZ |
276 | |
277 | wxSemaError TryWait() | |
278 | { | |
279 | wxSemaError rc = WaitTimeout(0); | |
280 | if ( rc == wxSEMA_TIMEOUT ) | |
281 | rc = wxSEMA_BUSY; | |
282 | ||
283 | return rc; | |
284 | } | |
285 | ||
9e84b847 | 286 | wxSemaError WaitTimeout(unsigned long milliseconds); |
be809868 | 287 | |
9e84b847 | 288 | wxSemaError Post(); |
be809868 VZ |
289 | |
290 | private: | |
291 | HANDLE m_semaphore; | |
22f3361e VZ |
292 | |
293 | DECLARE_NO_COPY_CLASS(wxSemaphoreInternal) | |
be809868 VZ |
294 | }; |
295 | ||
9e84b847 | 296 | wxSemaphoreInternal::wxSemaphoreInternal(int initialcount, int maxcount) |
be809868 | 297 | { |
4676948b | 298 | #ifndef __WXWINCE__ |
be809868 | 299 | if ( maxcount == 0 ) |
b568d04f | 300 | { |
be809868 VZ |
301 | // make it practically infinite |
302 | maxcount = INT_MAX; | |
303 | } | |
4d1c1c3c | 304 | |
9e84b847 VZ |
305 | m_semaphore = ::CreateSemaphore |
306 | ( | |
307 | NULL, // default security attributes | |
308 | initialcount, | |
309 | maxcount, | |
310 | NULL // no name | |
311 | ); | |
4676948b | 312 | #endif |
be809868 VZ |
313 | if ( !m_semaphore ) |
314 | { | |
315 | wxLogLastError(_T("CreateSemaphore()")); | |
b568d04f | 316 | } |
be809868 VZ |
317 | } |
318 | ||
319 | wxSemaphoreInternal::~wxSemaphoreInternal() | |
320 | { | |
9e84b847 | 321 | if ( m_semaphore ) |
b568d04f | 322 | { |
9e84b847 VZ |
323 | if ( !::CloseHandle(m_semaphore) ) |
324 | { | |
325 | wxLogLastError(_T("CloseHandle(semaphore)")); | |
326 | } | |
be809868 VZ |
327 | } |
328 | } | |
b568d04f | 329 | |
9e84b847 | 330 | wxSemaError wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds) |
be809868 | 331 | { |
9e84b847 | 332 | DWORD rc = ::WaitForSingleObject( m_semaphore, milliseconds ); |
be809868 | 333 | |
9e84b847 | 334 | switch ( rc ) |
be809868 VZ |
335 | { |
336 | case WAIT_OBJECT_0: | |
9e84b847 | 337 | return wxSEMA_NO_ERROR; |
b568d04f | 338 | |
be809868 | 339 | case WAIT_TIMEOUT: |
918dc519 | 340 | return wxSEMA_TIMEOUT; |
be809868 VZ |
341 | |
342 | default: | |
9e84b847 | 343 | wxLogLastError(_T("WaitForSingleObject(semaphore)")); |
b568d04f VZ |
344 | } |
345 | ||
9e84b847 | 346 | return wxSEMA_MISC_ERROR; |
be809868 VZ |
347 | } |
348 | ||
9e84b847 | 349 | wxSemaError wxSemaphoreInternal::Post() |
be809868 | 350 | { |
4676948b | 351 | #ifndef __WXWINCE__ |
9e84b847 | 352 | if ( !::ReleaseSemaphore(m_semaphore, 1, NULL /* ptr to previous count */) ) |
4676948b | 353 | #endif |
4d1c1c3c | 354 | { |
be809868 | 355 | wxLogLastError(_T("ReleaseSemaphore")); |
be809868 | 356 | |
9e84b847 VZ |
357 | return wxSEMA_MISC_ERROR; |
358 | } | |
be809868 | 359 | |
9e84b847 | 360 | return wxSEMA_NO_ERROR; |
be809868 | 361 | } |
4d1c1c3c | 362 | |
be809868 | 363 | // -------------------------------------------------------------------------- |
9e84b847 | 364 | // wxCondition |
be809868 VZ |
365 | // -------------------------------------------------------------------------- |
366 | ||
9e84b847 VZ |
367 | // Win32 doesn't have explicit support for the POSIX condition variables and |
368 | // the Win32 events have quite different semantics, so we reimplement the | |
369 | // conditions from scratch using the mutexes and semaphores | |
be809868 VZ |
370 | class wxConditionInternal |
371 | { | |
372 | public: | |
c112e100 | 373 | wxConditionInternal(wxMutex& mutex); |
be809868 | 374 | |
9e84b847 | 375 | bool IsOk() const { return m_mutex.IsOk() && m_semaphore.IsOk(); } |
be809868 | 376 | |
9e84b847 VZ |
377 | wxCondError Wait(); |
378 | wxCondError WaitTimeout(unsigned long milliseconds); | |
be809868 | 379 | |
9e84b847 VZ |
380 | wxCondError Signal(); |
381 | wxCondError Broadcast(); | |
be809868 VZ |
382 | |
383 | private: | |
9e84b847 VZ |
384 | // the number of threads currently waiting for this condition |
385 | LONG m_numWaiters; | |
be809868 | 386 | |
9e84b847 VZ |
387 | // the critical section protecting m_numWaiters |
388 | wxCriticalSection m_csWaiters; | |
be809868 | 389 | |
9e84b847 | 390 | wxMutex& m_mutex; |
be809868 VZ |
391 | wxSemaphore m_semaphore; |
392 | }; | |
393 | ||
c112e100 VZ |
394 | wxConditionInternal::wxConditionInternal(wxMutex& mutex) |
395 | : m_mutex(mutex) | |
be809868 | 396 | { |
9e84b847 VZ |
397 | // another thread can't access it until we return from ctor, so no need to |
398 | // protect access to m_numWaiters here | |
be809868 VZ |
399 | m_numWaiters = 0; |
400 | } | |
401 | ||
9e84b847 | 402 | wxCondError wxConditionInternal::Wait() |
be809868 VZ |
403 | { |
404 | // increment the number of waiters | |
9e84b847 | 405 | ::InterlockedIncrement(&m_numWaiters); |
be809868 | 406 | |
c112e100 | 407 | m_mutex.Unlock(); |
be809868 VZ |
408 | |
409 | // a potential race condition can occur here | |
410 | // | |
411 | // after a thread increments nwaiters, and unlocks the mutex and before the | |
412 | // semaphore.Wait() is called, if another thread can cause a signal to be | |
413 | // generated | |
414 | // | |
415 | // this race condition is handled by using a semaphore and incrementing the | |
416 | // semaphore only if 'nwaiters' is greater that zero since the semaphore, | |
417 | // can 'remember' signals the race condition will not occur | |
418 | ||
419 | // wait ( if necessary ) and decrement semaphore | |
9e84b847 | 420 | wxSemaError err = m_semaphore.Wait(); |
c112e100 | 421 | m_mutex.Lock(); |
9e84b847 VZ |
422 | |
423 | return err == wxSEMA_NO_ERROR ? wxCOND_NO_ERROR : wxCOND_MISC_ERROR; | |
be809868 VZ |
424 | } |
425 | ||
9e84b847 | 426 | wxCondError wxConditionInternal::WaitTimeout(unsigned long milliseconds) |
be809868 | 427 | { |
9e84b847 | 428 | ::InterlockedIncrement(&m_numWaiters); |
be809868 | 429 | |
c112e100 | 430 | m_mutex.Unlock(); |
be809868 VZ |
431 | |
432 | // a race condition can occur at this point in the code | |
433 | // | |
434 | // please see the comments in Wait(), for details | |
435 | ||
9e84b847 | 436 | wxSemaError err = m_semaphore.WaitTimeout(milliseconds); |
be809868 | 437 | |
9e84b847 | 438 | if ( err == wxSEMA_BUSY ) |
4d1c1c3c | 439 | { |
be809868 VZ |
440 | // another potential race condition exists here it is caused when a |
441 | // 'waiting' thread timesout, and returns from WaitForSingleObject, but | |
442 | // has not yet decremented 'nwaiters'. | |
443 | // | |
444 | // at this point if another thread calls signal() then the semaphore | |
445 | // will be incremented, but the waiting thread will miss it. | |
446 | // | |
447 | // to handle this particular case, the waiting thread calls | |
448 | // WaitForSingleObject again with a timeout of 0, after locking | |
449 | // 'nwaiters_mutex'. this call does not block because of the zero | |
450 | // timeout, but will allow the waiting thread to catch the missed | |
451 | // signals. | |
9e84b847 VZ |
452 | wxCriticalSectionLocker lock(m_csWaiters); |
453 | ||
454 | err = m_semaphore.WaitTimeout(0); | |
be809868 | 455 | |
9e84b847 | 456 | if ( err != wxSEMA_NO_ERROR ) |
4d1c1c3c | 457 | { |
be809868 | 458 | m_numWaiters--; |
4d1c1c3c VZ |
459 | } |
460 | } | |
461 | ||
c112e100 | 462 | m_mutex.Lock(); |
be809868 | 463 | |
9e84b847 | 464 | return err == wxSEMA_NO_ERROR ? wxCOND_NO_ERROR : wxCOND_MISC_ERROR; |
be809868 VZ |
465 | } |
466 | ||
9e84b847 | 467 | wxCondError wxConditionInternal::Signal() |
be809868 | 468 | { |
9e84b847 | 469 | wxCriticalSectionLocker lock(m_csWaiters); |
be809868 VZ |
470 | |
471 | if ( m_numWaiters > 0 ) | |
b568d04f | 472 | { |
be809868 | 473 | // increment the semaphore by 1 |
9e84b847 VZ |
474 | if ( m_semaphore.Post() != wxSEMA_NO_ERROR ) |
475 | return wxCOND_MISC_ERROR; | |
be809868 VZ |
476 | |
477 | m_numWaiters--; | |
b568d04f VZ |
478 | } |
479 | ||
9e84b847 | 480 | return wxCOND_NO_ERROR; |
be809868 | 481 | } |
4d1c1c3c | 482 | |
9e84b847 | 483 | wxCondError wxConditionInternal::Broadcast() |
be809868 | 484 | { |
9e84b847 | 485 | wxCriticalSectionLocker lock(m_csWaiters); |
2bda0e17 | 486 | |
be809868 VZ |
487 | while ( m_numWaiters > 0 ) |
488 | { | |
9e84b847 VZ |
489 | if ( m_semaphore.Post() != wxSEMA_NO_ERROR ) |
490 | return wxCOND_MISC_ERROR; | |
491 | ||
be809868 VZ |
492 | m_numWaiters--; |
493 | } | |
494 | ||
9e84b847 | 495 | return wxCOND_NO_ERROR; |
3222fde2 VZ |
496 | } |
497 | ||
498 | // ---------------------------------------------------------------------------- | |
499 | // wxThread implementation | |
500 | // ---------------------------------------------------------------------------- | |
501 | ||
bf1852e1 VZ |
502 | // wxThreadInternal class |
503 | // ---------------------- | |
504 | ||
3222fde2 VZ |
505 | class wxThreadInternal |
506 | { | |
2bda0e17 | 507 | public: |
bf1852e1 VZ |
508 | wxThreadInternal() |
509 | { | |
510 | m_hThread = 0; | |
511 | m_state = STATE_NEW; | |
512 | m_priority = WXTHREAD_DEFAULT_PRIORITY; | |
513 | } | |
514 | ||
b568d04f VZ |
515 | ~wxThreadInternal() |
516 | { | |
517 | Free(); | |
518 | } | |
519 | ||
520 | void Free() | |
521 | { | |
522 | if ( m_hThread ) | |
523 | { | |
524 | if ( !::CloseHandle(m_hThread) ) | |
525 | { | |
f6bcfd97 | 526 | wxLogLastError(wxT("CloseHandle(thread)")); |
b568d04f VZ |
527 | } |
528 | ||
529 | m_hThread = 0; | |
530 | } | |
531 | } | |
532 | ||
bf1852e1 | 533 | // create a new (suspended) thread (for the given thread object) |
6fe73788 | 534 | bool Create(wxThread *thread, unsigned int stackSize); |
bf1852e1 | 535 | |
68d41720 VZ |
536 | // wait for the thread to terminate, either by itself, or by asking it |
537 | // (politely, this is not Kill()!) to do it | |
538 | wxThreadError WaitForTerminate(bool shouldCancel, | |
539 | wxCriticalSection& cs, | |
540 | wxThread::ExitCode *pRc); | |
541 | ||
542 | // kill the thread unconditionally | |
543 | wxThreadError Kill(); | |
544 | ||
bf1852e1 VZ |
545 | // suspend/resume/terminate |
546 | bool Suspend(); | |
547 | bool Resume(); | |
548 | void Cancel() { m_state = STATE_CANCELED; } | |
549 | ||
550 | // thread state | |
551 | void SetState(wxThreadState state) { m_state = state; } | |
552 | wxThreadState GetState() const { return m_state; } | |
553 | ||
554 | // thread priority | |
b568d04f | 555 | void SetPriority(unsigned int priority); |
bf1852e1 VZ |
556 | unsigned int GetPriority() const { return m_priority; } |
557 | ||
558 | // thread handle and id | |
559 | HANDLE GetHandle() const { return m_hThread; } | |
560 | DWORD GetId() const { return m_tid; } | |
561 | ||
562 | // thread function | |
9f51f2b6 | 563 | static THREAD_RETVAL THREAD_CALLCONV WinThreadStart(void *thread); |
2bda0e17 | 564 | |
bf1852e1 VZ |
565 | private: |
566 | HANDLE m_hThread; // handle of the thread | |
567 | wxThreadState m_state; // state, see wxThreadState enum | |
568 | unsigned int m_priority; // thread priority in "wx" units | |
569 | DWORD m_tid; // thread id | |
22f3361e VZ |
570 | |
571 | DECLARE_NO_COPY_CLASS(wxThreadInternal) | |
2bda0e17 KB |
572 | }; |
573 | ||
08d7397c | 574 | THREAD_RETVAL THREAD_CALLCONV wxThreadInternal::WinThreadStart(void *param) |
2bda0e17 | 575 | { |
9f51f2b6 | 576 | THREAD_RETVAL rc; |
f6bcfd97 BP |
577 | bool wasCancelled; |
578 | ||
579 | // first of all, check whether we hadn't been cancelled already and don't | |
580 | // start the user code at all then | |
9f51f2b6 | 581 | wxThread *thread = (wxThread *)param; |
696e1ea0 VZ |
582 | if ( thread->m_internal->GetState() == STATE_EXITED ) |
583 | { | |
9f51f2b6 | 584 | rc = (THREAD_RETVAL)-1; |
f6bcfd97 | 585 | wasCancelled = TRUE; |
696e1ea0 | 586 | } |
f6bcfd97 | 587 | else // do run thread |
bf1852e1 | 588 | { |
f6bcfd97 BP |
589 | // store the thread object in the TLS |
590 | if ( !::TlsSetValue(gs_tlsThisThread, thread) ) | |
591 | { | |
592 | wxLogSysError(_("Can not start thread: error writing TLS.")); | |
bf1852e1 | 593 | |
f6bcfd97 BP |
594 | return (DWORD)-1; |
595 | } | |
bf1852e1 | 596 | |
9f51f2b6 | 597 | rc = (THREAD_RETVAL)thread->Entry(); |
b568d04f | 598 | |
f6bcfd97 BP |
599 | // enter m_critsect before changing the thread state |
600 | thread->m_critsect.Enter(); | |
601 | wasCancelled = thread->m_internal->GetState() == STATE_CANCELED; | |
602 | thread->m_internal->SetState(STATE_EXITED); | |
603 | thread->m_critsect.Leave(); | |
604 | } | |
b568d04f | 605 | |
bee503b0 | 606 | thread->OnExit(); |
2bda0e17 | 607 | |
f6bcfd97 | 608 | // if the thread was cancelled (from Delete()), then its handle is still |
b568d04f VZ |
609 | // needed there |
610 | if ( thread->IsDetached() && !wasCancelled ) | |
611 | { | |
612 | // auto delete | |
613 | delete thread; | |
614 | } | |
615 | //else: the joinable threads handle will be closed when Wait() is done | |
bf1852e1 | 616 | |
b568d04f | 617 | return rc; |
2bda0e17 KB |
618 | } |
619 | ||
b568d04f | 620 | void wxThreadInternal::SetPriority(unsigned int priority) |
2bda0e17 | 621 | { |
b568d04f | 622 | m_priority = priority; |
3222fde2 | 623 | |
bf1852e1 VZ |
624 | // translate wxWindows priority to the Windows one |
625 | int win_priority; | |
626 | if (m_priority <= 20) | |
627 | win_priority = THREAD_PRIORITY_LOWEST; | |
628 | else if (m_priority <= 40) | |
629 | win_priority = THREAD_PRIORITY_BELOW_NORMAL; | |
630 | else if (m_priority <= 60) | |
631 | win_priority = THREAD_PRIORITY_NORMAL; | |
632 | else if (m_priority <= 80) | |
633 | win_priority = THREAD_PRIORITY_ABOVE_NORMAL; | |
634 | else if (m_priority <= 100) | |
635 | win_priority = THREAD_PRIORITY_HIGHEST; | |
3222fde2 VZ |
636 | else |
637 | { | |
223d09f6 | 638 | wxFAIL_MSG(wxT("invalid value of thread priority parameter")); |
bf1852e1 | 639 | win_priority = THREAD_PRIORITY_NORMAL; |
3222fde2 VZ |
640 | } |
641 | ||
b568d04f | 642 | if ( !::SetThreadPriority(m_hThread, win_priority) ) |
bee503b0 VZ |
643 | { |
644 | wxLogSysError(_("Can't set thread priority")); | |
645 | } | |
b568d04f VZ |
646 | } |
647 | ||
6fe73788 | 648 | bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize) |
b568d04f | 649 | { |
50ef4401 VZ |
650 | wxASSERT_MSG( m_state == STATE_NEW && !m_hThread, |
651 | _T("Create()ing thread twice?") ); | |
652 | ||
b568d04f VZ |
653 | // for compilers which have it, we should use C RTL function for thread |
654 | // creation instead of Win32 API one because otherwise we will have memory | |
655 | // leaks if the thread uses C RTL (and most threads do) | |
9f51f2b6 | 656 | #ifdef wxUSE_BEGIN_THREAD |
414c639c VZ |
657 | |
658 | // Watcom is reported to not like 0 stack size (which means "use default" | |
659 | // for the other compilers and is also the default value for stackSize) | |
660 | #ifdef __WATCOMC__ | |
661 | if ( !stackSize ) | |
662 | stackSize = 10240; | |
663 | #endif // __WATCOMC__ | |
664 | ||
9f51f2b6 VZ |
665 | m_hThread = (HANDLE)_beginthreadex |
666 | ( | |
6fe73788 RL |
667 | NULL, // default security |
668 | stackSize, | |
669 | wxThreadInternal::WinThreadStart, // entry point | |
670 | thread, | |
671 | CREATE_SUSPENDED, | |
672 | (unsigned int *)&m_tid | |
9f51f2b6 | 673 | ); |
611cb666 | 674 | #else // compiler doesn't have _beginthreadex |
b568d04f VZ |
675 | m_hThread = ::CreateThread |
676 | ( | |
677 | NULL, // default security | |
414c639c | 678 | stackSize, // stack size |
9f51f2b6 | 679 | wxThreadInternal::WinThreadStart, // thread entry point |
b568d04f VZ |
680 | (LPVOID)thread, // parameter |
681 | CREATE_SUSPENDED, // flags | |
682 | &m_tid // [out] thread id | |
683 | ); | |
611cb666 | 684 | #endif // _beginthreadex/CreateThread |
b568d04f VZ |
685 | |
686 | if ( m_hThread == NULL ) | |
687 | { | |
688 | wxLogSysError(_("Can't create thread")); | |
689 | ||
690 | return FALSE; | |
691 | } | |
692 | ||
693 | if ( m_priority != WXTHREAD_DEFAULT_PRIORITY ) | |
694 | { | |
695 | SetPriority(m_priority); | |
696 | } | |
3222fde2 | 697 | |
bf1852e1 | 698 | return TRUE; |
2bda0e17 KB |
699 | } |
700 | ||
68d41720 VZ |
701 | wxThreadError wxThreadInternal::Kill() |
702 | { | |
703 | if ( !::TerminateThread(m_hThread, (DWORD)-1) ) | |
704 | { | |
705 | wxLogSysError(_("Couldn't terminate thread")); | |
706 | ||
707 | return wxTHREAD_MISC_ERROR; | |
708 | } | |
709 | ||
710 | Free(); | |
711 | ||
712 | return wxTHREAD_NO_ERROR; | |
713 | } | |
714 | ||
715 | wxThreadError | |
716 | wxThreadInternal::WaitForTerminate(bool shouldCancel, | |
717 | wxCriticalSection& cs, | |
718 | wxThread::ExitCode *pRc) | |
719 | { | |
720 | wxThread::ExitCode rc = 0; | |
721 | ||
722 | // Delete() is always safe to call, so consider all possible states | |
723 | ||
724 | // we might need to resume the thread, but we might also not need to cancel | |
725 | // it if it doesn't run yet | |
726 | bool shouldResume = FALSE, | |
727 | isRunning = FALSE; | |
728 | ||
729 | // check if the thread already started to run | |
730 | { | |
731 | wxCriticalSectionLocker lock(cs); | |
732 | ||
733 | if ( m_state == STATE_NEW ) | |
734 | { | |
735 | if ( shouldCancel ) | |
736 | { | |
737 | // WinThreadStart() will see it and terminate immediately, no need | |
738 | // to cancel the thread - but we still need to resume it to let it | |
739 | // run | |
740 | m_state = STATE_EXITED; | |
741 | ||
742 | Resume(); // it knows about STATE_EXITED special case | |
743 | ||
744 | shouldCancel = FALSE; | |
745 | } | |
746 | ||
747 | isRunning = TRUE; | |
748 | ||
749 | // shouldResume is correctly set to FALSE here | |
750 | } | |
751 | else | |
752 | { | |
753 | shouldResume = m_state == STATE_PAUSED; | |
754 | } | |
755 | } | |
756 | ||
757 | // resume the thread if it is paused | |
758 | if ( shouldResume ) | |
759 | Resume(); | |
760 | ||
761 | // does is still run? | |
762 | if ( isRunning || m_state == STATE_RUNNING ) | |
763 | { | |
764 | if ( wxThread::IsMain() ) | |
765 | { | |
766 | // set flag for wxIsWaitingForThread() | |
767 | gs_waitingForThread = TRUE; | |
768 | } | |
769 | ||
770 | // ask the thread to terminate | |
771 | if ( shouldCancel ) | |
772 | { | |
773 | wxCriticalSectionLocker lock(cs); | |
774 | ||
775 | Cancel(); | |
776 | } | |
777 | ||
778 | // we can't just wait for the thread to terminate because it might be | |
779 | // calling some GUI functions and so it will never terminate before we | |
780 | // process the Windows messages that result from these functions | |
781 | // (note that even in console applications we might have to process | |
782 | // messages if we use wxExecute() or timers or ...) | |
783 | DWORD result = 0; // suppress warnings from broken compilers | |
784 | do | |
785 | { | |
786 | if ( wxThread::IsMain() ) | |
787 | { | |
788 | // give the thread we're waiting for chance to do the GUI call | |
789 | // it might be in | |
790 | if ( (gs_nWaitingForGui > 0) && wxGuiOwnedByMainThread() ) | |
791 | { | |
792 | wxMutexGuiLeave(); | |
793 | } | |
794 | } | |
795 | ||
796 | result = ::MsgWaitForMultipleObjects | |
797 | ( | |
798 | 1, // number of objects to wait for | |
799 | &m_hThread, // the objects | |
800 | FALSE, // don't wait for all objects | |
801 | INFINITE, // no timeout | |
802 | QS_ALLINPUT | // return as soon as there are any events | |
803 | QS_ALLPOSTMESSAGE | |
804 | ); | |
805 | ||
806 | switch ( result ) | |
807 | { | |
808 | case 0xFFFFFFFF: | |
809 | // error | |
810 | wxLogSysError(_("Can not wait for thread termination")); | |
811 | Kill(); | |
812 | return wxTHREAD_KILLED; | |
813 | ||
814 | case WAIT_OBJECT_0: | |
815 | // thread we're waiting for terminated | |
816 | break; | |
817 | ||
818 | case WAIT_OBJECT_0 + 1: | |
819 | // new message arrived, process it | |
820 | { | |
821 | // it looks that sometimes WAIT_OBJECT_0 + 1 is | |
822 | // returned but there are no messages in the thread | |
823 | // queue -- prevent DoMessageFromThreadWait() from | |
824 | // blocking inside ::GetMessage() forever in this case | |
825 | ::PostMessage(NULL, WM_NULL, 0, 0); | |
826 | ||
827 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() | |
828 | : NULL; | |
829 | ||
830 | if ( traits && !traits->DoMessageFromThreadWait() ) | |
831 | { | |
832 | // WM_QUIT received: kill the thread | |
833 | Kill(); | |
834 | ||
835 | return wxTHREAD_KILLED; | |
836 | } | |
837 | } | |
838 | break; | |
839 | ||
840 | default: | |
841 | wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject")); | |
842 | } | |
843 | } while ( result != WAIT_OBJECT_0 ); | |
844 | ||
845 | if ( wxThread::IsMain() ) | |
846 | { | |
847 | gs_waitingForThread = FALSE; | |
848 | } | |
849 | } | |
850 | ||
851 | // although the thread might be already in the EXITED state it might not | |
852 | // have terminated yet and so we are not sure that it has actually | |
853 | // terminated if the "if" above hadn't been taken | |
854 | do | |
855 | { | |
856 | if ( !::GetExitCodeThread(m_hThread, (LPDWORD)&rc) ) | |
857 | { | |
858 | wxLogLastError(wxT("GetExitCodeThread")); | |
859 | ||
860 | rc = (wxThread::ExitCode)-1; | |
861 | } | |
862 | } while ( (DWORD)rc == STILL_ACTIVE ); | |
863 | ||
864 | if ( pRc ) | |
865 | *pRc = rc; | |
866 | ||
867 | return rc == (wxThread::ExitCode)-1 ? wxTHREAD_MISC_ERROR | |
868 | : wxTHREAD_NO_ERROR; | |
869 | } | |
870 | ||
bf1852e1 | 871 | bool wxThreadInternal::Suspend() |
2bda0e17 | 872 | { |
bf1852e1 VZ |
873 | DWORD nSuspendCount = ::SuspendThread(m_hThread); |
874 | if ( nSuspendCount == (DWORD)-1 ) | |
bee503b0 | 875 | { |
bf1852e1 VZ |
876 | wxLogSysError(_("Can not suspend thread %x"), m_hThread); |
877 | ||
878 | return FALSE; | |
bee503b0 | 879 | } |
2bda0e17 | 880 | |
bf1852e1 VZ |
881 | m_state = STATE_PAUSED; |
882 | ||
883 | return TRUE; | |
a6b0bd49 VZ |
884 | } |
885 | ||
bf1852e1 | 886 | bool wxThreadInternal::Resume() |
a6b0bd49 | 887 | { |
bf1852e1 | 888 | DWORD nSuspendCount = ::ResumeThread(m_hThread); |
a6b0bd49 VZ |
889 | if ( nSuspendCount == (DWORD)-1 ) |
890 | { | |
bf1852e1 | 891 | wxLogSysError(_("Can not resume thread %x"), m_hThread); |
a6b0bd49 | 892 | |
bf1852e1 | 893 | return FALSE; |
a6b0bd49 VZ |
894 | } |
895 | ||
f6bcfd97 BP |
896 | // don't change the state from STATE_EXITED because it's special and means |
897 | // we are going to terminate without running any user code - if we did it, | |
898 | // the codei n Delete() wouldn't work | |
899 | if ( m_state != STATE_EXITED ) | |
900 | { | |
901 | m_state = STATE_RUNNING; | |
902 | } | |
bee503b0 | 903 | |
bf1852e1 | 904 | return TRUE; |
a6b0bd49 VZ |
905 | } |
906 | ||
bf1852e1 VZ |
907 | // static functions |
908 | // ---------------- | |
909 | ||
910 | wxThread *wxThread::This() | |
a6b0bd49 | 911 | { |
b568d04f | 912 | wxThread *thread = (wxThread *)::TlsGetValue(gs_tlsThisThread); |
bf1852e1 VZ |
913 | |
914 | // be careful, 0 may be a valid return value as well | |
915 | if ( !thread && (::GetLastError() != NO_ERROR) ) | |
a6b0bd49 | 916 | { |
bf1852e1 | 917 | wxLogSysError(_("Couldn't get the current thread pointer")); |
a6b0bd49 | 918 | |
bf1852e1 | 919 | // return NULL... |
a6b0bd49 VZ |
920 | } |
921 | ||
bf1852e1 VZ |
922 | return thread; |
923 | } | |
924 | ||
925 | bool wxThread::IsMain() | |
926 | { | |
b568d04f | 927 | return ::GetCurrentThreadId() == gs_idMainThread; |
bf1852e1 VZ |
928 | } |
929 | ||
c25a510b JS |
930 | #ifdef Yield |
931 | #undef Yield | |
932 | #endif | |
933 | ||
bf1852e1 VZ |
934 | void wxThread::Yield() |
935 | { | |
b568d04f VZ |
936 | // 0 argument to Sleep() is special and means to just give away the rest of |
937 | // our timeslice | |
bf1852e1 VZ |
938 | ::Sleep(0); |
939 | } | |
940 | ||
941 | void wxThread::Sleep(unsigned long milliseconds) | |
942 | { | |
943 | ::Sleep(milliseconds); | |
944 | } | |
945 | ||
ef8d96c2 VZ |
946 | int wxThread::GetCPUCount() |
947 | { | |
28a4627c VZ |
948 | SYSTEM_INFO si; |
949 | GetSystemInfo(&si); | |
950 | ||
951 | return si.dwNumberOfProcessors; | |
ef8d96c2 VZ |
952 | } |
953 | ||
4958ea8f RD |
954 | unsigned long wxThread::GetCurrentId() |
955 | { | |
956 | return (unsigned long)::GetCurrentThreadId(); | |
957 | } | |
958 | ||
ef8d96c2 VZ |
959 | bool wxThread::SetConcurrency(size_t level) |
960 | { | |
4676948b | 961 | #ifndef __WXWINCE__ |
28a4627c VZ |
962 | wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") ); |
963 | ||
ef8d96c2 | 964 | // ok only for the default one |
28a4627c VZ |
965 | if ( level == 0 ) |
966 | return 0; | |
967 | ||
968 | // get system affinity mask first | |
969 | HANDLE hProcess = ::GetCurrentProcess(); | |
970 | DWORD dwProcMask, dwSysMask; | |
971 | if ( ::GetProcessAffinityMask(hProcess, &dwProcMask, &dwSysMask) == 0 ) | |
972 | { | |
973 | wxLogLastError(_T("GetProcessAffinityMask")); | |
974 | ||
975 | return FALSE; | |
976 | } | |
977 | ||
978 | // how many CPUs have we got? | |
979 | if ( dwSysMask == 1 ) | |
980 | { | |
981 | // don't bother with all this complicated stuff - on a single | |
982 | // processor system it doesn't make much sense anyhow | |
983 | return level == 1; | |
984 | } | |
985 | ||
986 | // calculate the process mask: it's a bit vector with one bit per | |
987 | // processor; we want to schedule the process to run on first level | |
988 | // CPUs | |
989 | DWORD bit = 1; | |
990 | while ( bit ) | |
991 | { | |
992 | if ( dwSysMask & bit ) | |
993 | { | |
994 | // ok, we can set this bit | |
995 | dwProcMask |= bit; | |
996 | ||
997 | // another process added | |
998 | if ( !--level ) | |
999 | { | |
1000 | // and that's enough | |
1001 | break; | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | // next bit | |
1006 | bit <<= 1; | |
1007 | } | |
1008 | ||
1009 | // could we set all bits? | |
1010 | if ( level != 0 ) | |
1011 | { | |
1012 | wxLogDebug(_T("bad level %u in wxThread::SetConcurrency()"), level); | |
1013 | ||
1014 | return FALSE; | |
1015 | } | |
1016 | ||
1017 | // set it: we can't link to SetProcessAffinityMask() because it doesn't | |
1018 | // exist in Win9x, use RT binding instead | |
1019 | ||
696e1ea0 | 1020 | typedef BOOL (*SETPROCESSAFFINITYMASK)(HANDLE, DWORD); |
28a4627c VZ |
1021 | |
1022 | // can use static var because we're always in the main thread here | |
1023 | static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask = NULL; | |
1024 | ||
1025 | if ( !pfnSetProcessAffinityMask ) | |
1026 | { | |
1027 | HMODULE hModKernel = ::LoadLibrary(_T("kernel32")); | |
1028 | if ( hModKernel ) | |
1029 | { | |
1030 | pfnSetProcessAffinityMask = (SETPROCESSAFFINITYMASK) | |
f6bcfd97 | 1031 | ::GetProcAddress(hModKernel, "SetProcessAffinityMask"); |
28a4627c VZ |
1032 | } |
1033 | ||
1034 | // we've discovered a MT version of Win9x! | |
1035 | wxASSERT_MSG( pfnSetProcessAffinityMask, | |
f6bcfd97 | 1036 | _T("this system has several CPUs but no SetProcessAffinityMask function?") ); |
28a4627c VZ |
1037 | } |
1038 | ||
1039 | if ( !pfnSetProcessAffinityMask ) | |
1040 | { | |
1041 | // msg given above - do it only once | |
1042 | return FALSE; | |
1043 | } | |
1044 | ||
696e1ea0 | 1045 | if ( pfnSetProcessAffinityMask(hProcess, dwProcMask) == 0 ) |
28a4627c VZ |
1046 | { |
1047 | wxLogLastError(_T("SetProcessAffinityMask")); | |
1048 | ||
1049 | return FALSE; | |
1050 | } | |
4676948b | 1051 | #endif |
28a4627c | 1052 | return TRUE; |
ef8d96c2 VZ |
1053 | } |
1054 | ||
b568d04f VZ |
1055 | // ctor and dtor |
1056 | // ------------- | |
1057 | ||
1058 | wxThread::wxThread(wxThreadKind kind) | |
1059 | { | |
9fc3ad34 | 1060 | m_internal = new wxThreadInternal(); |
b568d04f VZ |
1061 | |
1062 | m_isDetached = kind == wxTHREAD_DETACHED; | |
1063 | } | |
1064 | ||
1065 | wxThread::~wxThread() | |
1066 | { | |
9fc3ad34 | 1067 | delete m_internal; |
b568d04f VZ |
1068 | } |
1069 | ||
bf1852e1 VZ |
1070 | // create/start thread |
1071 | // ------------------- | |
1072 | ||
6fe73788 | 1073 | wxThreadError wxThread::Create(unsigned int stackSize) |
bf1852e1 | 1074 | { |
b568d04f VZ |
1075 | wxCriticalSectionLocker lock(m_critsect); |
1076 | ||
6fe73788 | 1077 | if ( !m_internal->Create(this, stackSize) ) |
bf1852e1 | 1078 | return wxTHREAD_NO_RESOURCE; |
bee503b0 | 1079 | |
a6b0bd49 | 1080 | return wxTHREAD_NO_ERROR; |
2bda0e17 KB |
1081 | } |
1082 | ||
bf1852e1 | 1083 | wxThreadError wxThread::Run() |
2bda0e17 | 1084 | { |
bf1852e1 VZ |
1085 | wxCriticalSectionLocker lock(m_critsect); |
1086 | ||
9fc3ad34 | 1087 | if ( m_internal->GetState() != STATE_NEW ) |
bf1852e1 VZ |
1088 | { |
1089 | // actually, it may be almost any state at all, not only STATE_RUNNING | |
1090 | return wxTHREAD_RUNNING; | |
1091 | } | |
1092 | ||
b568d04f | 1093 | // the thread has just been created and is still suspended - let it run |
bf1852e1 | 1094 | return Resume(); |
2bda0e17 KB |
1095 | } |
1096 | ||
bf1852e1 VZ |
1097 | // suspend/resume thread |
1098 | // --------------------- | |
1099 | ||
1100 | wxThreadError wxThread::Pause() | |
2bda0e17 | 1101 | { |
bf1852e1 VZ |
1102 | wxCriticalSectionLocker lock(m_critsect); |
1103 | ||
9fc3ad34 | 1104 | return m_internal->Suspend() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; |
2bda0e17 KB |
1105 | } |
1106 | ||
bf1852e1 | 1107 | wxThreadError wxThread::Resume() |
2bda0e17 | 1108 | { |
bf1852e1 VZ |
1109 | wxCriticalSectionLocker lock(m_critsect); |
1110 | ||
9fc3ad34 | 1111 | return m_internal->Resume() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; |
2bda0e17 KB |
1112 | } |
1113 | ||
bf1852e1 VZ |
1114 | // stopping thread |
1115 | // --------------- | |
1116 | ||
b568d04f VZ |
1117 | wxThread::ExitCode wxThread::Wait() |
1118 | { | |
1119 | // although under Windows we can wait for any thread, it's an error to | |
1120 | // wait for a detached one in wxWin API | |
1121 | wxCHECK_MSG( !IsDetached(), (ExitCode)-1, | |
68d41720 | 1122 | _T("wxThread::Wait(): can't wait for detached thread") ); |
b568d04f VZ |
1123 | |
1124 | ExitCode rc = (ExitCode)-1; | |
1125 | ||
68d41720 | 1126 | (void)m_internal->WaitForTerminate(false, m_critsect, &rc); |
b568d04f | 1127 | |
9fc3ad34 | 1128 | m_internal->Free(); |
b568d04f | 1129 | |
68d41720 VZ |
1130 | wxCriticalSectionLocker lock(m_critsect); |
1131 | m_internal->SetState(STATE_EXITED); | |
1132 | ||
b568d04f VZ |
1133 | return rc; |
1134 | } | |
1135 | ||
1136 | wxThreadError wxThread::Delete(ExitCode *pRc) | |
2bda0e17 | 1137 | { |
68d41720 | 1138 | wxThreadError rc = m_internal->WaitForTerminate(true, m_critsect, pRc); |
bf1852e1 | 1139 | |
b568d04f VZ |
1140 | if ( IsDetached() ) |
1141 | { | |
b568d04f | 1142 | delete this; |
bf1852e1 | 1143 | } |
68d41720 VZ |
1144 | else // joinable |
1145 | { | |
1146 | // update the status of the joinable thread | |
1147 | wxCriticalSectionLocker lock(m_critsect); | |
1148 | m_internal->SetState(STATE_EXITED); | |
1149 | } | |
bf1852e1 | 1150 | |
68d41720 | 1151 | return rc; |
2bda0e17 KB |
1152 | } |
1153 | ||
bf1852e1 | 1154 | wxThreadError wxThread::Kill() |
2bda0e17 | 1155 | { |
bf1852e1 VZ |
1156 | if ( !IsRunning() ) |
1157 | return wxTHREAD_NOT_RUNNING; | |
1158 | ||
68d41720 | 1159 | wxThreadError rc = m_internal->Kill(); |
b568d04f VZ |
1160 | |
1161 | if ( IsDetached() ) | |
1162 | { | |
1163 | delete this; | |
1164 | } | |
57fd9bb2 VZ |
1165 | else // joinable |
1166 | { | |
1167 | // update the status of the joinable thread | |
1168 | wxCriticalSectionLocker lock(m_critsect); | |
1169 | m_internal->SetState(STATE_EXITED); | |
1170 | } | |
bf1852e1 | 1171 | |
68d41720 | 1172 | return rc; |
2bda0e17 KB |
1173 | } |
1174 | ||
b568d04f | 1175 | void wxThread::Exit(ExitCode status) |
2bda0e17 | 1176 | { |
9fc3ad34 | 1177 | m_internal->Free(); |
2bda0e17 | 1178 | |
b568d04f VZ |
1179 | if ( IsDetached() ) |
1180 | { | |
1181 | delete this; | |
1182 | } | |
57fd9bb2 VZ |
1183 | else // joinable |
1184 | { | |
1185 | // update the status of the joinable thread | |
1186 | wxCriticalSectionLocker lock(m_critsect); | |
1187 | m_internal->SetState(STATE_EXITED); | |
1188 | } | |
b568d04f | 1189 | |
9f51f2b6 | 1190 | #ifdef wxUSE_BEGIN_THREAD |
b568d04f VZ |
1191 | _endthreadex((unsigned)status); |
1192 | #else // !VC++ | |
bf1852e1 | 1193 | ::ExitThread((DWORD)status); |
b568d04f | 1194 | #endif // VC++/!VC++ |
2bda0e17 | 1195 | |
223d09f6 | 1196 | wxFAIL_MSG(wxT("Couldn't return from ExitThread()!")); |
bf1852e1 | 1197 | } |
2bda0e17 | 1198 | |
b568d04f VZ |
1199 | // priority setting |
1200 | // ---------------- | |
1201 | ||
bf1852e1 VZ |
1202 | void wxThread::SetPriority(unsigned int prio) |
1203 | { | |
1204 | wxCriticalSectionLocker lock(m_critsect); | |
1205 | ||
9fc3ad34 | 1206 | m_internal->SetPriority(prio); |
bf1852e1 | 1207 | } |
2bda0e17 | 1208 | |
bf1852e1 VZ |
1209 | unsigned int wxThread::GetPriority() const |
1210 | { | |
b568d04f | 1211 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
2bda0e17 | 1212 | |
9fc3ad34 | 1213 | return m_internal->GetPriority(); |
2bda0e17 KB |
1214 | } |
1215 | ||
b568d04f | 1216 | unsigned long wxThread::GetId() const |
2bda0e17 | 1217 | { |
b568d04f | 1218 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
bf1852e1 | 1219 | |
9fc3ad34 | 1220 | return (unsigned long)m_internal->GetId(); |
2bda0e17 KB |
1221 | } |
1222 | ||
72fd19a1 JS |
1223 | bool wxThread::IsRunning() const |
1224 | { | |
b568d04f | 1225 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
bf1852e1 | 1226 | |
9fc3ad34 | 1227 | return m_internal->GetState() == STATE_RUNNING; |
72fd19a1 JS |
1228 | } |
1229 | ||
1230 | bool wxThread::IsAlive() const | |
1231 | { | |
b568d04f | 1232 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
bf1852e1 | 1233 | |
9fc3ad34 VZ |
1234 | return (m_internal->GetState() == STATE_RUNNING) || |
1235 | (m_internal->GetState() == STATE_PAUSED); | |
72fd19a1 JS |
1236 | } |
1237 | ||
a737331d GL |
1238 | bool wxThread::IsPaused() const |
1239 | { | |
b568d04f | 1240 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
a737331d | 1241 | |
9fc3ad34 | 1242 | return m_internal->GetState() == STATE_PAUSED; |
a737331d GL |
1243 | } |
1244 | ||
8c10faf1 | 1245 | bool wxThread::TestDestroy() |
2bda0e17 | 1246 | { |
b568d04f | 1247 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
bf1852e1 | 1248 | |
9fc3ad34 | 1249 | return m_internal->GetState() == STATE_CANCELED; |
2bda0e17 KB |
1250 | } |
1251 | ||
3222fde2 VZ |
1252 | // ---------------------------------------------------------------------------- |
1253 | // Automatic initialization for thread module | |
1254 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1255 | |
3222fde2 | 1256 | class wxThreadModule : public wxModule |
a6b0bd49 | 1257 | { |
3222fde2 VZ |
1258 | public: |
1259 | virtual bool OnInit(); | |
1260 | virtual void OnExit(); | |
d524867f | 1261 | |
3222fde2 VZ |
1262 | private: |
1263 | DECLARE_DYNAMIC_CLASS(wxThreadModule) | |
1264 | }; | |
d524867f RR |
1265 | |
1266 | IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule) | |
1267 | ||
3222fde2 | 1268 | bool wxThreadModule::OnInit() |
d524867f | 1269 | { |
bf1852e1 | 1270 | // allocate TLS index for storing the pointer to the current thread |
b568d04f VZ |
1271 | gs_tlsThisThread = ::TlsAlloc(); |
1272 | if ( gs_tlsThisThread == 0xFFFFFFFF ) | |
bf1852e1 VZ |
1273 | { |
1274 | // in normal circumstances it will only happen if all other | |
1275 | // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other | |
1276 | // words, this should never happen | |
f6bcfd97 | 1277 | wxLogSysError(_("Thread module initialization failed: impossible to allocate index in thread local storage")); |
bf1852e1 VZ |
1278 | |
1279 | return FALSE; | |
1280 | } | |
1281 | ||
1282 | // main thread doesn't have associated wxThread object, so store 0 in the | |
1283 | // TLS instead | |
b568d04f | 1284 | if ( !::TlsSetValue(gs_tlsThisThread, (LPVOID)0) ) |
bf1852e1 | 1285 | { |
b568d04f VZ |
1286 | ::TlsFree(gs_tlsThisThread); |
1287 | gs_tlsThisThread = 0xFFFFFFFF; | |
bf1852e1 | 1288 | |
f6bcfd97 | 1289 | wxLogSysError(_("Thread module initialization failed: can not store value in thread local storage")); |
bf1852e1 VZ |
1290 | |
1291 | return FALSE; | |
1292 | } | |
1293 | ||
b568d04f | 1294 | gs_critsectWaitingForGui = new wxCriticalSection(); |
bee503b0 | 1295 | |
b568d04f VZ |
1296 | gs_critsectGui = new wxCriticalSection(); |
1297 | gs_critsectGui->Enter(); | |
bee503b0 | 1298 | |
bf1852e1 | 1299 | // no error return for GetCurrentThreadId() |
b568d04f | 1300 | gs_idMainThread = ::GetCurrentThreadId(); |
3222fde2 | 1301 | |
d524867f RR |
1302 | return TRUE; |
1303 | } | |
1304 | ||
3222fde2 | 1305 | void wxThreadModule::OnExit() |
d524867f | 1306 | { |
b568d04f | 1307 | if ( !::TlsFree(gs_tlsThisThread) ) |
bf1852e1 | 1308 | { |
f6bcfd97 | 1309 | wxLogLastError(wxT("TlsFree failed.")); |
bf1852e1 VZ |
1310 | } |
1311 | ||
b568d04f | 1312 | if ( gs_critsectGui ) |
3222fde2 | 1313 | { |
b568d04f VZ |
1314 | gs_critsectGui->Leave(); |
1315 | delete gs_critsectGui; | |
1316 | gs_critsectGui = NULL; | |
3222fde2 | 1317 | } |
bee503b0 | 1318 | |
b568d04f VZ |
1319 | delete gs_critsectWaitingForGui; |
1320 | gs_critsectWaitingForGui = NULL; | |
3222fde2 VZ |
1321 | } |
1322 | ||
bee503b0 | 1323 | // ---------------------------------------------------------------------------- |
b568d04f | 1324 | // under Windows, these functions are implemented using a critical section and |
3222fde2 | 1325 | // not a mutex, so the names are a bit confusing |
bee503b0 VZ |
1326 | // ---------------------------------------------------------------------------- |
1327 | ||
0c2f35dc | 1328 | void WXDLLIMPEXP_BASE wxMutexGuiEnter() |
3222fde2 | 1329 | { |
bee503b0 VZ |
1330 | // this would dead lock everything... |
1331 | wxASSERT_MSG( !wxThread::IsMain(), | |
223d09f6 | 1332 | wxT("main thread doesn't want to block in wxMutexGuiEnter()!") ); |
bee503b0 VZ |
1333 | |
1334 | // the order in which we enter the critical sections here is crucial!! | |
1335 | ||
1336 | // set the flag telling to the main thread that we want to do some GUI | |
1337 | { | |
b568d04f | 1338 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); |
bee503b0 | 1339 | |
b568d04f | 1340 | gs_nWaitingForGui++; |
bee503b0 VZ |
1341 | } |
1342 | ||
1343 | wxWakeUpMainThread(); | |
1344 | ||
1345 | // now we may block here because the main thread will soon let us in | |
1346 | // (during the next iteration of OnIdle()) | |
b568d04f | 1347 | gs_critsectGui->Enter(); |
3222fde2 VZ |
1348 | } |
1349 | ||
0c2f35dc | 1350 | void WXDLLIMPEXP_BASE wxMutexGuiLeave() |
3222fde2 | 1351 | { |
b568d04f | 1352 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); |
bee503b0 VZ |
1353 | |
1354 | if ( wxThread::IsMain() ) | |
1355 | { | |
b568d04f | 1356 | gs_bGuiOwnedByMainThread = FALSE; |
bee503b0 VZ |
1357 | } |
1358 | else | |
1359 | { | |
4d1c1c3c | 1360 | // decrement the number of threads waiting for GUI access now |
b568d04f | 1361 | wxASSERT_MSG( gs_nWaitingForGui > 0, |
223d09f6 | 1362 | wxT("calling wxMutexGuiLeave() without entering it first?") ); |
bee503b0 | 1363 | |
b568d04f | 1364 | gs_nWaitingForGui--; |
bee503b0 VZ |
1365 | |
1366 | wxWakeUpMainThread(); | |
1367 | } | |
1368 | ||
b568d04f | 1369 | gs_critsectGui->Leave(); |
bee503b0 VZ |
1370 | } |
1371 | ||
0c2f35dc | 1372 | void WXDLLIMPEXP_BASE wxMutexGuiLeaveOrEnter() |
bee503b0 VZ |
1373 | { |
1374 | wxASSERT_MSG( wxThread::IsMain(), | |
223d09f6 | 1375 | wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") ); |
bee503b0 | 1376 | |
b568d04f | 1377 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); |
bee503b0 | 1378 | |
b568d04f | 1379 | if ( gs_nWaitingForGui == 0 ) |
bee503b0 VZ |
1380 | { |
1381 | // no threads are waiting for GUI - so we may acquire the lock without | |
1382 | // any danger (but only if we don't already have it) | |
1383 | if ( !wxGuiOwnedByMainThread() ) | |
1384 | { | |
b568d04f | 1385 | gs_critsectGui->Enter(); |
bee503b0 | 1386 | |
b568d04f | 1387 | gs_bGuiOwnedByMainThread = TRUE; |
bee503b0 VZ |
1388 | } |
1389 | //else: already have it, nothing to do | |
1390 | } | |
1391 | else | |
1392 | { | |
1393 | // some threads are waiting, release the GUI lock if we have it | |
1394 | if ( wxGuiOwnedByMainThread() ) | |
1395 | { | |
1396 | wxMutexGuiLeave(); | |
1397 | } | |
1398 | //else: some other worker thread is doing GUI | |
1399 | } | |
1400 | } | |
1401 | ||
0c2f35dc | 1402 | bool WXDLLIMPEXP_BASE wxGuiOwnedByMainThread() |
bee503b0 | 1403 | { |
b568d04f | 1404 | return gs_bGuiOwnedByMainThread; |
bee503b0 VZ |
1405 | } |
1406 | ||
1407 | // wake up the main thread if it's in ::GetMessage() | |
0c2f35dc | 1408 | void WXDLLIMPEXP_BASE wxWakeUpMainThread() |
bee503b0 VZ |
1409 | { |
1410 | // sending any message would do - hopefully WM_NULL is harmless enough | |
b568d04f | 1411 | if ( !::PostThreadMessage(gs_idMainThread, WM_NULL, 0, 0) ) |
bee503b0 VZ |
1412 | { |
1413 | // should never happen | |
f6bcfd97 | 1414 | wxLogLastError(wxT("PostThreadMessage(WM_NULL)")); |
bee503b0 | 1415 | } |
3222fde2 | 1416 | } |
d524867f | 1417 | |
0c2f35dc | 1418 | bool WXDLLIMPEXP_BASE wxIsWaitingForThread() |
bf1852e1 | 1419 | { |
b568d04f | 1420 | return gs_waitingForThread; |
bf1852e1 VZ |
1421 | } |
1422 | ||
9e84b847 VZ |
1423 | // ---------------------------------------------------------------------------- |
1424 | // include common implementation code | |
1425 | // ---------------------------------------------------------------------------- | |
1426 | ||
1427 | #include "wx/thrimpl.cpp" | |
1428 | ||
3222fde2 | 1429 | #endif // wxUSE_THREADS |
6fe73788 | 1430 |