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