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