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