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