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