]>
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 | { |
4a268913 | 303 | #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 300) |
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 | { |
4a268913 | 356 | #if !defined(_WIN32_WCE) || (_WIN32_WCE >= 300) |
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 | |
5eba4394 DS |
487 | bool isExited = (thread->m_internal->GetState() == STATE_EXITED); |
488 | ||
489 | if ( isExited ) | |
696e1ea0 | 490 | { |
9f51f2b6 | 491 | rc = (THREAD_RETVAL)-1; |
696e1ea0 | 492 | } |
f6bcfd97 | 493 | else // do run thread |
bf1852e1 | 494 | { |
f6bcfd97 BP |
495 | // store the thread object in the TLS |
496 | if ( !::TlsSetValue(gs_tlsThisThread, thread) ) | |
497 | { | |
498 | wxLogSysError(_("Can not start thread: error writing TLS.")); | |
bf1852e1 | 499 | |
f6bcfd97 BP |
500 | return (DWORD)-1; |
501 | } | |
bf1852e1 | 502 | |
9f51f2b6 | 503 | rc = (THREAD_RETVAL)thread->Entry(); |
5eba4394 DS |
504 | } |
505 | ||
506 | thread->OnExit(); | |
507 | ||
508 | // save IsDetached because thread object can be deleted by joinable | |
509 | // threads after state is changed to STATE_EXITED. | |
510 | bool isDetached = thread->IsDetached(); | |
b568d04f | 511 | |
5eba4394 DS |
512 | if (!isExited) |
513 | { | |
f6bcfd97 | 514 | // enter m_critsect before changing the thread state |
3ef22a5b | 515 | wxCriticalSectionLocker lock(thread->m_critsect); |
f6bcfd97 | 516 | thread->m_internal->SetState(STATE_EXITED); |
f6bcfd97 | 517 | } |
b568d04f | 518 | |
3ef22a5b | 519 | // the thread may delete itself now if it wants, we don't need it any more |
5eba4394 | 520 | if (isDetached) thread->m_internal->LetDie(); |
bf1852e1 | 521 | |
b568d04f | 522 | return rc; |
2bda0e17 KB |
523 | } |
524 | ||
b568d04f | 525 | void wxThreadInternal::SetPriority(unsigned int priority) |
2bda0e17 | 526 | { |
b568d04f | 527 | m_priority = priority; |
3222fde2 | 528 | |
bf1852e1 VZ |
529 | // translate wxWindows priority to the Windows one |
530 | int win_priority; | |
531 | if (m_priority <= 20) | |
532 | win_priority = THREAD_PRIORITY_LOWEST; | |
533 | else if (m_priority <= 40) | |
534 | win_priority = THREAD_PRIORITY_BELOW_NORMAL; | |
535 | else if (m_priority <= 60) | |
536 | win_priority = THREAD_PRIORITY_NORMAL; | |
537 | else if (m_priority <= 80) | |
538 | win_priority = THREAD_PRIORITY_ABOVE_NORMAL; | |
539 | else if (m_priority <= 100) | |
540 | win_priority = THREAD_PRIORITY_HIGHEST; | |
3222fde2 VZ |
541 | else |
542 | { | |
223d09f6 | 543 | wxFAIL_MSG(wxT("invalid value of thread priority parameter")); |
bf1852e1 | 544 | win_priority = THREAD_PRIORITY_NORMAL; |
3222fde2 VZ |
545 | } |
546 | ||
b568d04f | 547 | if ( !::SetThreadPriority(m_hThread, win_priority) ) |
bee503b0 VZ |
548 | { |
549 | wxLogSysError(_("Can't set thread priority")); | |
550 | } | |
b568d04f VZ |
551 | } |
552 | ||
6fe73788 | 553 | bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize) |
b568d04f | 554 | { |
50ef4401 VZ |
555 | wxASSERT_MSG( m_state == STATE_NEW && !m_hThread, |
556 | _T("Create()ing thread twice?") ); | |
557 | ||
b568d04f VZ |
558 | // for compilers which have it, we should use C RTL function for thread |
559 | // creation instead of Win32 API one because otherwise we will have memory | |
560 | // leaks if the thread uses C RTL (and most threads do) | |
9f51f2b6 | 561 | #ifdef wxUSE_BEGIN_THREAD |
414c639c VZ |
562 | |
563 | // Watcom is reported to not like 0 stack size (which means "use default" | |
564 | // for the other compilers and is also the default value for stackSize) | |
565 | #ifdef __WATCOMC__ | |
566 | if ( !stackSize ) | |
567 | stackSize = 10240; | |
568 | #endif // __WATCOMC__ | |
569 | ||
9f51f2b6 VZ |
570 | m_hThread = (HANDLE)_beginthreadex |
571 | ( | |
6fe73788 RL |
572 | NULL, // default security |
573 | stackSize, | |
574 | wxThreadInternal::WinThreadStart, // entry point | |
575 | thread, | |
576 | CREATE_SUSPENDED, | |
577 | (unsigned int *)&m_tid | |
9f51f2b6 | 578 | ); |
611cb666 | 579 | #else // compiler doesn't have _beginthreadex |
b568d04f VZ |
580 | m_hThread = ::CreateThread |
581 | ( | |
582 | NULL, // default security | |
414c639c | 583 | stackSize, // stack size |
9f51f2b6 | 584 | wxThreadInternal::WinThreadStart, // thread entry point |
b568d04f VZ |
585 | (LPVOID)thread, // parameter |
586 | CREATE_SUSPENDED, // flags | |
587 | &m_tid // [out] thread id | |
588 | ); | |
611cb666 | 589 | #endif // _beginthreadex/CreateThread |
b568d04f VZ |
590 | |
591 | if ( m_hThread == NULL ) | |
592 | { | |
593 | wxLogSysError(_("Can't create thread")); | |
594 | ||
3ef22a5b | 595 | return false; |
b568d04f VZ |
596 | } |
597 | ||
598 | if ( m_priority != WXTHREAD_DEFAULT_PRIORITY ) | |
599 | { | |
600 | SetPriority(m_priority); | |
601 | } | |
3222fde2 | 602 | |
3ef22a5b | 603 | return true; |
2bda0e17 KB |
604 | } |
605 | ||
68d41720 VZ |
606 | wxThreadError wxThreadInternal::Kill() |
607 | { | |
608 | if ( !::TerminateThread(m_hThread, (DWORD)-1) ) | |
609 | { | |
610 | wxLogSysError(_("Couldn't terminate thread")); | |
611 | ||
612 | return wxTHREAD_MISC_ERROR; | |
613 | } | |
614 | ||
615 | Free(); | |
616 | ||
617 | return wxTHREAD_NO_ERROR; | |
618 | } | |
619 | ||
620 | wxThreadError | |
3ef22a5b VZ |
621 | wxThreadInternal::WaitForTerminate(wxCriticalSection& cs, |
622 | wxThread::ExitCode *pRc, | |
623 | wxThread *threadToDelete) | |
68d41720 | 624 | { |
3ef22a5b VZ |
625 | // prevent the thread C++ object from disappearing as long as we are using |
626 | // it here | |
627 | wxThreadKeepAlive keepAlive(*this); | |
628 | ||
629 | ||
630 | // we may either wait passively for the thread to terminate (when called | |
631 | // from Wait()) or ask it to terminate (when called from Delete()) | |
632 | bool shouldDelete = threadToDelete != NULL; | |
633 | ||
68d41720 VZ |
634 | wxThread::ExitCode rc = 0; |
635 | ||
636 | // Delete() is always safe to call, so consider all possible states | |
637 | ||
638 | // we might need to resume the thread, but we might also not need to cancel | |
639 | // it if it doesn't run yet | |
3ef22a5b VZ |
640 | bool shouldResume = false, |
641 | isRunning = false; | |
68d41720 VZ |
642 | |
643 | // check if the thread already started to run | |
644 | { | |
645 | wxCriticalSectionLocker lock(cs); | |
646 | ||
647 | if ( m_state == STATE_NEW ) | |
648 | { | |
3ef22a5b | 649 | if ( shouldDelete ) |
68d41720 | 650 | { |
63fa42b3 VZ |
651 | // WinThreadStart() will see it and terminate immediately, no |
652 | // need to cancel the thread -- but we still need to resume it | |
653 | // to let it run | |
68d41720 VZ |
654 | m_state = STATE_EXITED; |
655 | ||
656 | Resume(); // it knows about STATE_EXITED special case | |
657 | ||
3ef22a5b | 658 | shouldDelete = false; |
68d41720 VZ |
659 | } |
660 | ||
3ef22a5b | 661 | isRunning = true; |
68d41720 | 662 | |
3ef22a5b | 663 | // shouldResume is correctly set to false here |
68d41720 | 664 | } |
63fa42b3 VZ |
665 | else if ( m_state == STATE_EXITED ) |
666 | { | |
667 | return wxTHREAD_NOT_RUNNING; | |
668 | } | |
669 | else // running (but maybe paused or cancelled) | |
68d41720 VZ |
670 | { |
671 | shouldResume = m_state == STATE_PAUSED; | |
672 | } | |
673 | } | |
674 | ||
675 | // resume the thread if it is paused | |
676 | if ( shouldResume ) | |
677 | Resume(); | |
678 | ||
4f45ba63 | 679 | // is it still running? |
68d41720 VZ |
680 | if ( isRunning || m_state == STATE_RUNNING ) |
681 | { | |
682 | if ( wxThread::IsMain() ) | |
683 | { | |
684 | // set flag for wxIsWaitingForThread() | |
3ef22a5b | 685 | gs_waitingForThread = true; |
68d41720 VZ |
686 | } |
687 | ||
688 | // ask the thread to terminate | |
3ef22a5b | 689 | if ( shouldDelete ) |
68d41720 VZ |
690 | { |
691 | wxCriticalSectionLocker lock(cs); | |
692 | ||
693 | Cancel(); | |
694 | } | |
695 | ||
696 | // we can't just wait for the thread to terminate because it might be | |
697 | // calling some GUI functions and so it will never terminate before we | |
698 | // process the Windows messages that result from these functions | |
699 | // (note that even in console applications we might have to process | |
700 | // messages if we use wxExecute() or timers or ...) | |
999836aa | 701 | DWORD result wxDUMMY_INITIALIZE(0); |
68d41720 VZ |
702 | do |
703 | { | |
704 | if ( wxThread::IsMain() ) | |
705 | { | |
706 | // give the thread we're waiting for chance to do the GUI call | |
707 | // it might be in | |
708 | if ( (gs_nWaitingForGui > 0) && wxGuiOwnedByMainThread() ) | |
709 | { | |
710 | wxMutexGuiLeave(); | |
711 | } | |
712 | } | |
713 | ||
714 | result = ::MsgWaitForMultipleObjects | |
715 | ( | |
716 | 1, // number of objects to wait for | |
717 | &m_hThread, // the objects | |
3ef22a5b | 718 | false, // don't wait for all objects |
68d41720 VZ |
719 | INFINITE, // no timeout |
720 | QS_ALLINPUT | // return as soon as there are any events | |
721 | QS_ALLPOSTMESSAGE | |
722 | ); | |
723 | ||
724 | switch ( result ) | |
725 | { | |
726 | case 0xFFFFFFFF: | |
727 | // error | |
728 | wxLogSysError(_("Can not wait for thread termination")); | |
729 | Kill(); | |
730 | return wxTHREAD_KILLED; | |
731 | ||
732 | case WAIT_OBJECT_0: | |
733 | // thread we're waiting for terminated | |
734 | break; | |
735 | ||
736 | case WAIT_OBJECT_0 + 1: | |
0e2f1b3e VZ |
737 | // new message arrived, process it -- but only if we're the |
738 | // main thread as we don't support processing messages in | |
739 | // the other ones | |
740 | // | |
741 | // NB: we still must include QS_ALLINPUT even when waiting | |
742 | // in a secondary thread because if it had created some | |
743 | // window somehow (possible not even using wxWindows) | |
744 | // the system might dead lock then | |
4a08283e | 745 | if ( wxThread::IsMain() ) |
68d41720 VZ |
746 | { |
747 | // it looks that sometimes WAIT_OBJECT_0 + 1 is | |
748 | // returned but there are no messages in the thread | |
749 | // queue -- prevent DoMessageFromThreadWait() from | |
750 | // blocking inside ::GetMessage() forever in this case | |
751 | ::PostMessage(NULL, WM_NULL, 0, 0); | |
752 | ||
753 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() | |
754 | : NULL; | |
755 | ||
756 | if ( traits && !traits->DoMessageFromThreadWait() ) | |
757 | { | |
758 | // WM_QUIT received: kill the thread | |
759 | Kill(); | |
760 | ||
761 | return wxTHREAD_KILLED; | |
762 | } | |
763 | } | |
764 | break; | |
765 | ||
766 | default: | |
767 | wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject")); | |
768 | } | |
769 | } while ( result != WAIT_OBJECT_0 ); | |
770 | ||
771 | if ( wxThread::IsMain() ) | |
772 | { | |
3ef22a5b | 773 | gs_waitingForThread = false; |
68d41720 VZ |
774 | } |
775 | } | |
776 | ||
777 | // although the thread might be already in the EXITED state it might not | |
778 | // have terminated yet and so we are not sure that it has actually | |
779 | // terminated if the "if" above hadn't been taken | |
780 | do | |
781 | { | |
782 | if ( !::GetExitCodeThread(m_hThread, (LPDWORD)&rc) ) | |
783 | { | |
784 | wxLogLastError(wxT("GetExitCodeThread")); | |
785 | ||
786 | rc = (wxThread::ExitCode)-1; | |
787 | } | |
788 | } while ( (DWORD)rc == STILL_ACTIVE ); | |
789 | ||
790 | if ( pRc ) | |
791 | *pRc = rc; | |
792 | ||
3ef22a5b | 793 | // we don't need the thread handle any more in any case |
4f45ba63 VZ |
794 | Free(); |
795 | ||
4f45ba63 | 796 | |
68d41720 VZ |
797 | return rc == (wxThread::ExitCode)-1 ? wxTHREAD_MISC_ERROR |
798 | : wxTHREAD_NO_ERROR; | |
799 | } | |
800 | ||
bf1852e1 | 801 | bool wxThreadInternal::Suspend() |
2bda0e17 | 802 | { |
bf1852e1 VZ |
803 | DWORD nSuspendCount = ::SuspendThread(m_hThread); |
804 | if ( nSuspendCount == (DWORD)-1 ) | |
bee503b0 | 805 | { |
bf1852e1 VZ |
806 | wxLogSysError(_("Can not suspend thread %x"), m_hThread); |
807 | ||
3ef22a5b | 808 | return false; |
bee503b0 | 809 | } |
2bda0e17 | 810 | |
bf1852e1 VZ |
811 | m_state = STATE_PAUSED; |
812 | ||
3ef22a5b | 813 | return true; |
a6b0bd49 VZ |
814 | } |
815 | ||
bf1852e1 | 816 | bool wxThreadInternal::Resume() |
a6b0bd49 | 817 | { |
bf1852e1 | 818 | DWORD nSuspendCount = ::ResumeThread(m_hThread); |
a6b0bd49 VZ |
819 | if ( nSuspendCount == (DWORD)-1 ) |
820 | { | |
bf1852e1 | 821 | wxLogSysError(_("Can not resume thread %x"), m_hThread); |
a6b0bd49 | 822 | |
3ef22a5b | 823 | return false; |
a6b0bd49 VZ |
824 | } |
825 | ||
f6bcfd97 BP |
826 | // don't change the state from STATE_EXITED because it's special and means |
827 | // we are going to terminate without running any user code - if we did it, | |
828 | // the codei n Delete() wouldn't work | |
829 | if ( m_state != STATE_EXITED ) | |
830 | { | |
831 | m_state = STATE_RUNNING; | |
832 | } | |
bee503b0 | 833 | |
3ef22a5b | 834 | return true; |
a6b0bd49 VZ |
835 | } |
836 | ||
bf1852e1 VZ |
837 | // static functions |
838 | // ---------------- | |
839 | ||
840 | wxThread *wxThread::This() | |
a6b0bd49 | 841 | { |
b568d04f | 842 | wxThread *thread = (wxThread *)::TlsGetValue(gs_tlsThisThread); |
bf1852e1 VZ |
843 | |
844 | // be careful, 0 may be a valid return value as well | |
845 | if ( !thread && (::GetLastError() != NO_ERROR) ) | |
a6b0bd49 | 846 | { |
bf1852e1 | 847 | wxLogSysError(_("Couldn't get the current thread pointer")); |
a6b0bd49 | 848 | |
bf1852e1 | 849 | // return NULL... |
a6b0bd49 VZ |
850 | } |
851 | ||
bf1852e1 VZ |
852 | return thread; |
853 | } | |
854 | ||
855 | bool wxThread::IsMain() | |
856 | { | |
b568d04f | 857 | return ::GetCurrentThreadId() == gs_idMainThread; |
bf1852e1 VZ |
858 | } |
859 | ||
860 | void wxThread::Yield() | |
861 | { | |
b568d04f VZ |
862 | // 0 argument to Sleep() is special and means to just give away the rest of |
863 | // our timeslice | |
bf1852e1 VZ |
864 | ::Sleep(0); |
865 | } | |
866 | ||
867 | void wxThread::Sleep(unsigned long milliseconds) | |
868 | { | |
869 | ::Sleep(milliseconds); | |
870 | } | |
871 | ||
ef8d96c2 VZ |
872 | int wxThread::GetCPUCount() |
873 | { | |
28a4627c VZ |
874 | SYSTEM_INFO si; |
875 | GetSystemInfo(&si); | |
876 | ||
877 | return si.dwNumberOfProcessors; | |
ef8d96c2 VZ |
878 | } |
879 | ||
4958ea8f RD |
880 | unsigned long wxThread::GetCurrentId() |
881 | { | |
882 | return (unsigned long)::GetCurrentThreadId(); | |
883 | } | |
884 | ||
ef8d96c2 VZ |
885 | bool wxThread::SetConcurrency(size_t level) |
886 | { | |
4676948b | 887 | #ifndef __WXWINCE__ |
28a4627c VZ |
888 | wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") ); |
889 | ||
ef8d96c2 | 890 | // ok only for the default one |
28a4627c VZ |
891 | if ( level == 0 ) |
892 | return 0; | |
893 | ||
894 | // get system affinity mask first | |
895 | HANDLE hProcess = ::GetCurrentProcess(); | |
896 | DWORD dwProcMask, dwSysMask; | |
897 | if ( ::GetProcessAffinityMask(hProcess, &dwProcMask, &dwSysMask) == 0 ) | |
898 | { | |
899 | wxLogLastError(_T("GetProcessAffinityMask")); | |
900 | ||
3ef22a5b | 901 | return false; |
28a4627c VZ |
902 | } |
903 | ||
904 | // how many CPUs have we got? | |
905 | if ( dwSysMask == 1 ) | |
906 | { | |
907 | // don't bother with all this complicated stuff - on a single | |
908 | // processor system it doesn't make much sense anyhow | |
909 | return level == 1; | |
910 | } | |
911 | ||
912 | // calculate the process mask: it's a bit vector with one bit per | |
913 | // processor; we want to schedule the process to run on first level | |
914 | // CPUs | |
915 | DWORD bit = 1; | |
916 | while ( bit ) | |
917 | { | |
918 | if ( dwSysMask & bit ) | |
919 | { | |
920 | // ok, we can set this bit | |
921 | dwProcMask |= bit; | |
922 | ||
923 | // another process added | |
924 | if ( !--level ) | |
925 | { | |
926 | // and that's enough | |
927 | break; | |
928 | } | |
929 | } | |
930 | ||
931 | // next bit | |
932 | bit <<= 1; | |
933 | } | |
934 | ||
935 | // could we set all bits? | |
936 | if ( level != 0 ) | |
937 | { | |
938 | wxLogDebug(_T("bad level %u in wxThread::SetConcurrency()"), level); | |
939 | ||
3ef22a5b | 940 | return false; |
28a4627c VZ |
941 | } |
942 | ||
943 | // set it: we can't link to SetProcessAffinityMask() because it doesn't | |
944 | // exist in Win9x, use RT binding instead | |
945 | ||
696e1ea0 | 946 | typedef BOOL (*SETPROCESSAFFINITYMASK)(HANDLE, DWORD); |
28a4627c VZ |
947 | |
948 | // can use static var because we're always in the main thread here | |
949 | static SETPROCESSAFFINITYMASK pfnSetProcessAffinityMask = NULL; | |
950 | ||
951 | if ( !pfnSetProcessAffinityMask ) | |
952 | { | |
953 | HMODULE hModKernel = ::LoadLibrary(_T("kernel32")); | |
954 | if ( hModKernel ) | |
955 | { | |
956 | pfnSetProcessAffinityMask = (SETPROCESSAFFINITYMASK) | |
f6bcfd97 | 957 | ::GetProcAddress(hModKernel, "SetProcessAffinityMask"); |
28a4627c VZ |
958 | } |
959 | ||
960 | // we've discovered a MT version of Win9x! | |
961 | wxASSERT_MSG( pfnSetProcessAffinityMask, | |
f6bcfd97 | 962 | _T("this system has several CPUs but no SetProcessAffinityMask function?") ); |
28a4627c VZ |
963 | } |
964 | ||
965 | if ( !pfnSetProcessAffinityMask ) | |
966 | { | |
967 | // msg given above - do it only once | |
3ef22a5b | 968 | return false; |
28a4627c VZ |
969 | } |
970 | ||
696e1ea0 | 971 | if ( pfnSetProcessAffinityMask(hProcess, dwProcMask) == 0 ) |
28a4627c VZ |
972 | { |
973 | wxLogLastError(_T("SetProcessAffinityMask")); | |
974 | ||
3ef22a5b | 975 | return false; |
28a4627c | 976 | } |
4676948b | 977 | #endif |
3ef22a5b | 978 | return true; |
ef8d96c2 VZ |
979 | } |
980 | ||
b568d04f VZ |
981 | // ctor and dtor |
982 | // ------------- | |
983 | ||
984 | wxThread::wxThread(wxThreadKind kind) | |
985 | { | |
3ef22a5b | 986 | m_internal = new wxThreadInternal(this); |
b568d04f VZ |
987 | |
988 | m_isDetached = kind == wxTHREAD_DETACHED; | |
989 | } | |
990 | ||
991 | wxThread::~wxThread() | |
992 | { | |
9fc3ad34 | 993 | delete m_internal; |
b568d04f VZ |
994 | } |
995 | ||
bf1852e1 VZ |
996 | // create/start thread |
997 | // ------------------- | |
998 | ||
6fe73788 | 999 | wxThreadError wxThread::Create(unsigned int stackSize) |
bf1852e1 | 1000 | { |
b568d04f VZ |
1001 | wxCriticalSectionLocker lock(m_critsect); |
1002 | ||
6fe73788 | 1003 | if ( !m_internal->Create(this, stackSize) ) |
bf1852e1 | 1004 | return wxTHREAD_NO_RESOURCE; |
bee503b0 | 1005 | |
a6b0bd49 | 1006 | return wxTHREAD_NO_ERROR; |
2bda0e17 KB |
1007 | } |
1008 | ||
bf1852e1 | 1009 | wxThreadError wxThread::Run() |
2bda0e17 | 1010 | { |
bf1852e1 VZ |
1011 | wxCriticalSectionLocker lock(m_critsect); |
1012 | ||
9fc3ad34 | 1013 | if ( m_internal->GetState() != STATE_NEW ) |
bf1852e1 VZ |
1014 | { |
1015 | // actually, it may be almost any state at all, not only STATE_RUNNING | |
1016 | return wxTHREAD_RUNNING; | |
1017 | } | |
1018 | ||
b568d04f | 1019 | // the thread has just been created and is still suspended - let it run |
bf1852e1 | 1020 | return Resume(); |
2bda0e17 KB |
1021 | } |
1022 | ||
bf1852e1 VZ |
1023 | // suspend/resume thread |
1024 | // --------------------- | |
1025 | ||
1026 | wxThreadError wxThread::Pause() | |
2bda0e17 | 1027 | { |
bf1852e1 VZ |
1028 | wxCriticalSectionLocker lock(m_critsect); |
1029 | ||
9fc3ad34 | 1030 | return m_internal->Suspend() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; |
2bda0e17 KB |
1031 | } |
1032 | ||
bf1852e1 | 1033 | wxThreadError wxThread::Resume() |
2bda0e17 | 1034 | { |
bf1852e1 VZ |
1035 | wxCriticalSectionLocker lock(m_critsect); |
1036 | ||
9fc3ad34 | 1037 | return m_internal->Resume() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; |
2bda0e17 KB |
1038 | } |
1039 | ||
bf1852e1 VZ |
1040 | // stopping thread |
1041 | // --------------- | |
1042 | ||
b568d04f VZ |
1043 | wxThread::ExitCode wxThread::Wait() |
1044 | { | |
1045 | // although under Windows we can wait for any thread, it's an error to | |
1046 | // wait for a detached one in wxWin API | |
1047 | wxCHECK_MSG( !IsDetached(), (ExitCode)-1, | |
68d41720 | 1048 | _T("wxThread::Wait(): can't wait for detached thread") ); |
b568d04f VZ |
1049 | |
1050 | ExitCode rc = (ExitCode)-1; | |
1051 | ||
3ef22a5b | 1052 | (void)m_internal->WaitForTerminate(m_critsect, &rc); |
b568d04f | 1053 | |
b568d04f VZ |
1054 | return rc; |
1055 | } | |
1056 | ||
1057 | wxThreadError wxThread::Delete(ExitCode *pRc) | |
2bda0e17 | 1058 | { |
3ef22a5b | 1059 | return m_internal->WaitForTerminate(m_critsect, pRc, this); |
2bda0e17 KB |
1060 | } |
1061 | ||
bf1852e1 | 1062 | wxThreadError wxThread::Kill() |
2bda0e17 | 1063 | { |
bf1852e1 VZ |
1064 | if ( !IsRunning() ) |
1065 | return wxTHREAD_NOT_RUNNING; | |
1066 | ||
68d41720 | 1067 | wxThreadError rc = m_internal->Kill(); |
b568d04f VZ |
1068 | |
1069 | if ( IsDetached() ) | |
1070 | { | |
1071 | delete this; | |
1072 | } | |
57fd9bb2 VZ |
1073 | else // joinable |
1074 | { | |
1075 | // update the status of the joinable thread | |
1076 | wxCriticalSectionLocker lock(m_critsect); | |
1077 | m_internal->SetState(STATE_EXITED); | |
1078 | } | |
bf1852e1 | 1079 | |
68d41720 | 1080 | return rc; |
2bda0e17 KB |
1081 | } |
1082 | ||
b568d04f | 1083 | void wxThread::Exit(ExitCode status) |
2bda0e17 | 1084 | { |
9fc3ad34 | 1085 | m_internal->Free(); |
2bda0e17 | 1086 | |
b568d04f VZ |
1087 | if ( IsDetached() ) |
1088 | { | |
1089 | delete this; | |
1090 | } | |
57fd9bb2 VZ |
1091 | else // joinable |
1092 | { | |
1093 | // update the status of the joinable thread | |
1094 | wxCriticalSectionLocker lock(m_critsect); | |
1095 | m_internal->SetState(STATE_EXITED); | |
1096 | } | |
b568d04f | 1097 | |
9f51f2b6 | 1098 | #ifdef wxUSE_BEGIN_THREAD |
b568d04f VZ |
1099 | _endthreadex((unsigned)status); |
1100 | #else // !VC++ | |
bf1852e1 | 1101 | ::ExitThread((DWORD)status); |
b568d04f | 1102 | #endif // VC++/!VC++ |
2bda0e17 | 1103 | |
223d09f6 | 1104 | wxFAIL_MSG(wxT("Couldn't return from ExitThread()!")); |
bf1852e1 | 1105 | } |
2bda0e17 | 1106 | |
b568d04f VZ |
1107 | // priority setting |
1108 | // ---------------- | |
1109 | ||
bf1852e1 VZ |
1110 | void wxThread::SetPriority(unsigned int prio) |
1111 | { | |
1112 | wxCriticalSectionLocker lock(m_critsect); | |
1113 | ||
9fc3ad34 | 1114 | m_internal->SetPriority(prio); |
bf1852e1 | 1115 | } |
2bda0e17 | 1116 | |
bf1852e1 VZ |
1117 | unsigned int wxThread::GetPriority() const |
1118 | { | |
b568d04f | 1119 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
2bda0e17 | 1120 | |
9fc3ad34 | 1121 | return m_internal->GetPriority(); |
2bda0e17 KB |
1122 | } |
1123 | ||
b568d04f | 1124 | unsigned long wxThread::GetId() const |
2bda0e17 | 1125 | { |
b568d04f | 1126 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
bf1852e1 | 1127 | |
9fc3ad34 | 1128 | return (unsigned long)m_internal->GetId(); |
2bda0e17 KB |
1129 | } |
1130 | ||
72fd19a1 JS |
1131 | bool wxThread::IsRunning() const |
1132 | { | |
b568d04f | 1133 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
bf1852e1 | 1134 | |
9fc3ad34 | 1135 | return m_internal->GetState() == STATE_RUNNING; |
72fd19a1 JS |
1136 | } |
1137 | ||
1138 | bool wxThread::IsAlive() const | |
1139 | { | |
b568d04f | 1140 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
bf1852e1 | 1141 | |
9fc3ad34 VZ |
1142 | return (m_internal->GetState() == STATE_RUNNING) || |
1143 | (m_internal->GetState() == STATE_PAUSED); | |
72fd19a1 JS |
1144 | } |
1145 | ||
a737331d GL |
1146 | bool wxThread::IsPaused() const |
1147 | { | |
b568d04f | 1148 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
a737331d | 1149 | |
9fc3ad34 | 1150 | return m_internal->GetState() == STATE_PAUSED; |
a737331d GL |
1151 | } |
1152 | ||
8c10faf1 | 1153 | bool wxThread::TestDestroy() |
2bda0e17 | 1154 | { |
b568d04f | 1155 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); // const_cast |
bf1852e1 | 1156 | |
9fc3ad34 | 1157 | return m_internal->GetState() == STATE_CANCELED; |
2bda0e17 KB |
1158 | } |
1159 | ||
3222fde2 VZ |
1160 | // ---------------------------------------------------------------------------- |
1161 | // Automatic initialization for thread module | |
1162 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1163 | |
3222fde2 | 1164 | class wxThreadModule : public wxModule |
a6b0bd49 | 1165 | { |
3222fde2 VZ |
1166 | public: |
1167 | virtual bool OnInit(); | |
1168 | virtual void OnExit(); | |
d524867f | 1169 | |
3222fde2 VZ |
1170 | private: |
1171 | DECLARE_DYNAMIC_CLASS(wxThreadModule) | |
1172 | }; | |
d524867f RR |
1173 | |
1174 | IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule) | |
1175 | ||
3222fde2 | 1176 | bool wxThreadModule::OnInit() |
d524867f | 1177 | { |
bf1852e1 | 1178 | // allocate TLS index for storing the pointer to the current thread |
b568d04f VZ |
1179 | gs_tlsThisThread = ::TlsAlloc(); |
1180 | if ( gs_tlsThisThread == 0xFFFFFFFF ) | |
bf1852e1 VZ |
1181 | { |
1182 | // in normal circumstances it will only happen if all other | |
1183 | // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other | |
1184 | // words, this should never happen | |
f6bcfd97 | 1185 | wxLogSysError(_("Thread module initialization failed: impossible to allocate index in thread local storage")); |
bf1852e1 | 1186 | |
3ef22a5b | 1187 | return false; |
bf1852e1 VZ |
1188 | } |
1189 | ||
1190 | // main thread doesn't have associated wxThread object, so store 0 in the | |
1191 | // TLS instead | |
b568d04f | 1192 | if ( !::TlsSetValue(gs_tlsThisThread, (LPVOID)0) ) |
bf1852e1 | 1193 | { |
b568d04f VZ |
1194 | ::TlsFree(gs_tlsThisThread); |
1195 | gs_tlsThisThread = 0xFFFFFFFF; | |
bf1852e1 | 1196 | |
f6bcfd97 | 1197 | wxLogSysError(_("Thread module initialization failed: can not store value in thread local storage")); |
bf1852e1 | 1198 | |
3ef22a5b | 1199 | return false; |
bf1852e1 VZ |
1200 | } |
1201 | ||
b568d04f | 1202 | gs_critsectWaitingForGui = new wxCriticalSection(); |
bee503b0 | 1203 | |
b568d04f VZ |
1204 | gs_critsectGui = new wxCriticalSection(); |
1205 | gs_critsectGui->Enter(); | |
bee503b0 | 1206 | |
3ef22a5b VZ |
1207 | gs_critsectThreadDelete = new wxCriticalSection; |
1208 | ||
bf1852e1 | 1209 | // no error return for GetCurrentThreadId() |
b568d04f | 1210 | gs_idMainThread = ::GetCurrentThreadId(); |
3222fde2 | 1211 | |
3ef22a5b | 1212 | return true; |
d524867f RR |
1213 | } |
1214 | ||
3222fde2 | 1215 | void wxThreadModule::OnExit() |
d524867f | 1216 | { |
b568d04f | 1217 | if ( !::TlsFree(gs_tlsThisThread) ) |
bf1852e1 | 1218 | { |
f6bcfd97 | 1219 | wxLogLastError(wxT("TlsFree failed.")); |
bf1852e1 VZ |
1220 | } |
1221 | ||
3ef22a5b VZ |
1222 | delete gs_critsectThreadDelete; |
1223 | gs_critsectThreadDelete = NULL; | |
1224 | ||
b568d04f | 1225 | if ( gs_critsectGui ) |
3222fde2 | 1226 | { |
b568d04f VZ |
1227 | gs_critsectGui->Leave(); |
1228 | delete gs_critsectGui; | |
1229 | gs_critsectGui = NULL; | |
3222fde2 | 1230 | } |
bee503b0 | 1231 | |
b568d04f VZ |
1232 | delete gs_critsectWaitingForGui; |
1233 | gs_critsectWaitingForGui = NULL; | |
3222fde2 VZ |
1234 | } |
1235 | ||
bee503b0 | 1236 | // ---------------------------------------------------------------------------- |
b568d04f | 1237 | // under Windows, these functions are implemented using a critical section and |
3222fde2 | 1238 | // not a mutex, so the names are a bit confusing |
bee503b0 VZ |
1239 | // ---------------------------------------------------------------------------- |
1240 | ||
0c2f35dc | 1241 | void WXDLLIMPEXP_BASE wxMutexGuiEnter() |
3222fde2 | 1242 | { |
bee503b0 VZ |
1243 | // this would dead lock everything... |
1244 | wxASSERT_MSG( !wxThread::IsMain(), | |
223d09f6 | 1245 | wxT("main thread doesn't want to block in wxMutexGuiEnter()!") ); |
bee503b0 VZ |
1246 | |
1247 | // the order in which we enter the critical sections here is crucial!! | |
1248 | ||
1249 | // set the flag telling to the main thread that we want to do some GUI | |
1250 | { | |
b568d04f | 1251 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); |
bee503b0 | 1252 | |
b568d04f | 1253 | gs_nWaitingForGui++; |
bee503b0 VZ |
1254 | } |
1255 | ||
1256 | wxWakeUpMainThread(); | |
1257 | ||
1258 | // now we may block here because the main thread will soon let us in | |
1259 | // (during the next iteration of OnIdle()) | |
b568d04f | 1260 | gs_critsectGui->Enter(); |
3222fde2 VZ |
1261 | } |
1262 | ||
0c2f35dc | 1263 | void WXDLLIMPEXP_BASE wxMutexGuiLeave() |
3222fde2 | 1264 | { |
b568d04f | 1265 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); |
bee503b0 VZ |
1266 | |
1267 | if ( wxThread::IsMain() ) | |
1268 | { | |
3ef22a5b | 1269 | gs_bGuiOwnedByMainThread = false; |
bee503b0 VZ |
1270 | } |
1271 | else | |
1272 | { | |
4d1c1c3c | 1273 | // decrement the number of threads waiting for GUI access now |
b568d04f | 1274 | wxASSERT_MSG( gs_nWaitingForGui > 0, |
223d09f6 | 1275 | wxT("calling wxMutexGuiLeave() without entering it first?") ); |
bee503b0 | 1276 | |
b568d04f | 1277 | gs_nWaitingForGui--; |
bee503b0 VZ |
1278 | |
1279 | wxWakeUpMainThread(); | |
1280 | } | |
1281 | ||
b568d04f | 1282 | gs_critsectGui->Leave(); |
bee503b0 VZ |
1283 | } |
1284 | ||
0c2f35dc | 1285 | void WXDLLIMPEXP_BASE wxMutexGuiLeaveOrEnter() |
bee503b0 VZ |
1286 | { |
1287 | wxASSERT_MSG( wxThread::IsMain(), | |
223d09f6 | 1288 | wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") ); |
bee503b0 | 1289 | |
b568d04f | 1290 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); |
bee503b0 | 1291 | |
b568d04f | 1292 | if ( gs_nWaitingForGui == 0 ) |
bee503b0 VZ |
1293 | { |
1294 | // no threads are waiting for GUI - so we may acquire the lock without | |
1295 | // any danger (but only if we don't already have it) | |
1296 | if ( !wxGuiOwnedByMainThread() ) | |
1297 | { | |
b568d04f | 1298 | gs_critsectGui->Enter(); |
bee503b0 | 1299 | |
3ef22a5b | 1300 | gs_bGuiOwnedByMainThread = true; |
bee503b0 VZ |
1301 | } |
1302 | //else: already have it, nothing to do | |
1303 | } | |
1304 | else | |
1305 | { | |
1306 | // some threads are waiting, release the GUI lock if we have it | |
1307 | if ( wxGuiOwnedByMainThread() ) | |
1308 | { | |
1309 | wxMutexGuiLeave(); | |
1310 | } | |
1311 | //else: some other worker thread is doing GUI | |
1312 | } | |
1313 | } | |
1314 | ||
0c2f35dc | 1315 | bool WXDLLIMPEXP_BASE wxGuiOwnedByMainThread() |
bee503b0 | 1316 | { |
b568d04f | 1317 | return gs_bGuiOwnedByMainThread; |
bee503b0 VZ |
1318 | } |
1319 | ||
1320 | // wake up the main thread if it's in ::GetMessage() | |
0c2f35dc | 1321 | void WXDLLIMPEXP_BASE wxWakeUpMainThread() |
bee503b0 VZ |
1322 | { |
1323 | // sending any message would do - hopefully WM_NULL is harmless enough | |
b568d04f | 1324 | if ( !::PostThreadMessage(gs_idMainThread, WM_NULL, 0, 0) ) |
bee503b0 VZ |
1325 | { |
1326 | // should never happen | |
f6bcfd97 | 1327 | wxLogLastError(wxT("PostThreadMessage(WM_NULL)")); |
bee503b0 | 1328 | } |
3222fde2 | 1329 | } |
d524867f | 1330 | |
0c2f35dc | 1331 | bool WXDLLIMPEXP_BASE wxIsWaitingForThread() |
bf1852e1 | 1332 | { |
b568d04f | 1333 | return gs_waitingForThread; |
bf1852e1 VZ |
1334 | } |
1335 | ||
9e84b847 VZ |
1336 | // ---------------------------------------------------------------------------- |
1337 | // include common implementation code | |
1338 | // ---------------------------------------------------------------------------- | |
1339 | ||
1340 | #include "wx/thrimpl.cpp" | |
1341 | ||
3222fde2 | 1342 | #endif // wxUSE_THREADS |
6fe73788 | 1343 |