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