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