]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: thread.cpp | |
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$ | |
bee503b0 VZ |
8 | // Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998), |
9 | // Vadim Zeitlin (1999) | |
2bda0e17 KB |
10 | // Licence: wxWindows licence |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #ifdef __GNUG__ | |
3222fde2 | 14 | #pragma implementation "thread.h" |
2bda0e17 KB |
15 | #endif |
16 | ||
3222fde2 VZ |
17 | // ---------------------------------------------------------------------------- |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
a3b46648 | 21 | // For compilers that support precompilation, includes "wx.h". |
2bda0e17 KB |
22 | #include "wx/wxprec.h" |
23 | ||
24 | #if defined(__BORLANDC__) | |
3222fde2 | 25 | #pragma hdrstop |
2bda0e17 KB |
26 | #endif |
27 | ||
28 | #ifndef WX_PRECOMP | |
3222fde2 | 29 | #include "wx/wx.h" |
2bda0e17 KB |
30 | #endif |
31 | ||
3222fde2 VZ |
32 | #if wxUSE_THREADS |
33 | ||
0d0512bd | 34 | #include "wx/msw/private.h" |
3222fde2 | 35 | |
2bda0e17 KB |
36 | #include "wx/module.h" |
37 | #include "wx/thread.h" | |
38 | ||
bf1852e1 VZ |
39 | // the possible states of the thread ("=>" shows all possible transitions from |
40 | // this state) | |
41 | enum wxThreadState | |
42 | { | |
43 | STATE_NEW, // didn't start execution yet (=> RUNNING) | |
44 | STATE_RUNNING, // thread is running (=> PAUSED, CANCELED) | |
45 | STATE_PAUSED, // thread is temporarily suspended (=> RUNNING) | |
46 | STATE_CANCELED, // thread should terminate a.s.a.p. (=> EXITED) | |
47 | STATE_EXITED // thread is terminating | |
2bda0e17 KB |
48 | }; |
49 | ||
3222fde2 VZ |
50 | // ---------------------------------------------------------------------------- |
51 | // static variables | |
52 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 53 | |
bf1852e1 VZ |
54 | // TLS index of the slot where we store the pointer to the current thread |
55 | static DWORD s_tlsThisThread = 0xFFFFFFFF; | |
56 | ||
3222fde2 VZ |
57 | // id of the main thread - the one which can call GUI functions without first |
58 | // calling wxMutexGuiEnter() | |
bee503b0 VZ |
59 | static DWORD s_idMainThread = 0; |
60 | ||
61 | // if it's FALSE, some secondary thread is holding the GUI lock | |
62 | static bool s_bGuiOwnedByMainThread = TRUE; | |
2bda0e17 | 63 | |
3222fde2 VZ |
64 | // critical section which controls access to all GUI functions: any secondary |
65 | // thread (i.e. except the main one) must enter this crit section before doing | |
66 | // any GUI calls | |
bee503b0 VZ |
67 | static wxCriticalSection *s_critsectGui = NULL; |
68 | ||
69 | // critical section which protects s_nWaitingForGui variable | |
70 | static wxCriticalSection *s_critsectWaitingForGui = NULL; | |
71 | ||
72 | // number of threads waiting for GUI in wxMutexGuiEnter() | |
73 | static size_t s_nWaitingForGui = 0; | |
2bda0e17 | 74 | |
bf1852e1 VZ |
75 | // are we waiting for a thread termination? |
76 | static bool s_waitingForThread = FALSE; | |
77 | ||
3222fde2 VZ |
78 | // ============================================================================ |
79 | // Windows implementation of thread classes | |
80 | // ============================================================================ | |
81 | ||
82 | // ---------------------------------------------------------------------------- | |
83 | // wxMutex implementation | |
84 | // ---------------------------------------------------------------------------- | |
0d0512bd | 85 | |
3222fde2 VZ |
86 | class wxMutexInternal |
87 | { | |
2bda0e17 | 88 | public: |
3222fde2 | 89 | HANDLE p_mutex; |
2bda0e17 KB |
90 | }; |
91 | ||
ee4f8c2a | 92 | wxMutex::wxMutex() |
2bda0e17 | 93 | { |
3222fde2 VZ |
94 | p_internal = new wxMutexInternal; |
95 | p_internal->p_mutex = CreateMutex(NULL, FALSE, NULL); | |
96 | if ( !p_internal->p_mutex ) | |
97 | { | |
98 | wxLogSysError(_("Can not create mutex.")); | |
99 | } | |
a6b0bd49 | 100 | |
3222fde2 | 101 | m_locked = 0; |
2bda0e17 KB |
102 | } |
103 | ||
ee4f8c2a | 104 | wxMutex::~wxMutex() |
2bda0e17 | 105 | { |
3222fde2 | 106 | if (m_locked > 0) |
223d09f6 | 107 | wxLogDebug(wxT("Warning: freeing a locked mutex (%d locks)."), m_locked); |
3222fde2 | 108 | CloseHandle(p_internal->p_mutex); |
2bda0e17 KB |
109 | } |
110 | ||
ee4f8c2a | 111 | wxMutexError wxMutex::Lock() |
2bda0e17 | 112 | { |
3222fde2 VZ |
113 | DWORD ret; |
114 | ||
115 | ret = WaitForSingleObject(p_internal->p_mutex, INFINITE); | |
116 | switch ( ret ) | |
117 | { | |
118 | case WAIT_ABANDONED: | |
119 | return wxMUTEX_BUSY; | |
120 | ||
121 | case WAIT_OBJECT_0: | |
122 | // ok | |
123 | break; | |
2bda0e17 | 124 | |
3222fde2 VZ |
125 | case WAIT_FAILED: |
126 | wxLogSysError(_("Couldn't acquire a mutex lock")); | |
127 | return wxMUTEX_MISC_ERROR; | |
2bda0e17 | 128 | |
3222fde2 VZ |
129 | case WAIT_TIMEOUT: |
130 | default: | |
223d09f6 | 131 | wxFAIL_MSG(wxT("impossible return value in wxMutex::Lock")); |
3222fde2 VZ |
132 | } |
133 | ||
134 | m_locked++; | |
135 | return wxMUTEX_NO_ERROR; | |
2bda0e17 KB |
136 | } |
137 | ||
ee4f8c2a | 138 | wxMutexError wxMutex::TryLock() |
2bda0e17 | 139 | { |
3222fde2 | 140 | DWORD ret; |
2bda0e17 | 141 | |
3222fde2 VZ |
142 | ret = WaitForSingleObject(p_internal->p_mutex, 0); |
143 | if (ret == WAIT_TIMEOUT || ret == WAIT_ABANDONED) | |
144 | return wxMUTEX_BUSY; | |
2bda0e17 | 145 | |
3222fde2 VZ |
146 | m_locked++; |
147 | return wxMUTEX_NO_ERROR; | |
2bda0e17 KB |
148 | } |
149 | ||
ee4f8c2a | 150 | wxMutexError wxMutex::Unlock() |
2bda0e17 | 151 | { |
3222fde2 VZ |
152 | if (m_locked > 0) |
153 | m_locked--; | |
2bda0e17 | 154 | |
3222fde2 VZ |
155 | BOOL ret = ReleaseMutex(p_internal->p_mutex); |
156 | if ( ret == 0 ) | |
157 | { | |
158 | wxLogSysError(_("Couldn't release a mutex")); | |
159 | return wxMUTEX_MISC_ERROR; | |
160 | } | |
a6b0bd49 | 161 | |
3222fde2 | 162 | return wxMUTEX_NO_ERROR; |
2bda0e17 KB |
163 | } |
164 | ||
3222fde2 VZ |
165 | // ---------------------------------------------------------------------------- |
166 | // wxCondition implementation | |
167 | // ---------------------------------------------------------------------------- | |
168 | ||
169 | class wxConditionInternal | |
170 | { | |
2bda0e17 | 171 | public: |
3222fde2 VZ |
172 | HANDLE event; |
173 | int waiters; | |
2bda0e17 KB |
174 | }; |
175 | ||
ee4f8c2a | 176 | wxCondition::wxCondition() |
2bda0e17 | 177 | { |
3222fde2 VZ |
178 | p_internal = new wxConditionInternal; |
179 | p_internal->event = CreateEvent(NULL, FALSE, FALSE, NULL); | |
180 | if ( !p_internal->event ) | |
181 | { | |
182 | wxLogSysError(_("Can not create event object.")); | |
183 | } | |
a6b0bd49 | 184 | |
3222fde2 | 185 | p_internal->waiters = 0; |
2bda0e17 KB |
186 | } |
187 | ||
ee4f8c2a | 188 | wxCondition::~wxCondition() |
2bda0e17 | 189 | { |
3222fde2 | 190 | CloseHandle(p_internal->event); |
2bda0e17 KB |
191 | } |
192 | ||
193 | void wxCondition::Wait(wxMutex& mutex) | |
194 | { | |
3222fde2 VZ |
195 | mutex.Unlock(); |
196 | p_internal->waiters++; | |
197 | WaitForSingleObject(p_internal->event, INFINITE); | |
198 | p_internal->waiters--; | |
199 | mutex.Lock(); | |
2bda0e17 KB |
200 | } |
201 | ||
3222fde2 VZ |
202 | bool wxCondition::Wait(wxMutex& mutex, |
203 | unsigned long sec, | |
2bda0e17 KB |
204 | unsigned long nsec) |
205 | { | |
3222fde2 | 206 | DWORD ret; |
2bda0e17 | 207 | |
3222fde2 VZ |
208 | mutex.Unlock(); |
209 | p_internal->waiters++; | |
210 | ret = WaitForSingleObject(p_internal->event, (sec*1000)+(nsec/1000000)); | |
211 | p_internal->waiters--; | |
212 | mutex.Lock(); | |
2bda0e17 | 213 | |
3222fde2 | 214 | return (ret != WAIT_TIMEOUT); |
2bda0e17 KB |
215 | } |
216 | ||
ee4f8c2a | 217 | void wxCondition::Signal() |
2bda0e17 | 218 | { |
3222fde2 | 219 | SetEvent(p_internal->event); |
2bda0e17 KB |
220 | } |
221 | ||
ee4f8c2a | 222 | void wxCondition::Broadcast() |
2bda0e17 | 223 | { |
3222fde2 | 224 | int i; |
2bda0e17 | 225 | |
3222fde2 | 226 | for (i=0;i<p_internal->waiters;i++) |
a6b0bd49 | 227 | { |
3222fde2 VZ |
228 | if ( SetEvent(p_internal->event) == 0 ) |
229 | { | |
230 | wxLogSysError(_("Couldn't change the state of event object.")); | |
231 | } | |
a6b0bd49 | 232 | } |
2bda0e17 KB |
233 | } |
234 | ||
3222fde2 VZ |
235 | // ---------------------------------------------------------------------------- |
236 | // wxCriticalSection implementation | |
237 | // ---------------------------------------------------------------------------- | |
238 | ||
3222fde2 VZ |
239 | wxCriticalSection::wxCriticalSection() |
240 | { | |
0d0512bd VZ |
241 | wxASSERT_MSG( sizeof(CRITICAL_SECTION) == sizeof(m_buffer), |
242 | _T("must increase buffer size in wx/thread.h") ); | |
243 | ||
244 | ::InitializeCriticalSection((CRITICAL_SECTION *)m_buffer); | |
3222fde2 VZ |
245 | } |
246 | ||
247 | wxCriticalSection::~wxCriticalSection() | |
248 | { | |
0d0512bd | 249 | ::DeleteCriticalSection((CRITICAL_SECTION *)m_buffer); |
3222fde2 VZ |
250 | } |
251 | ||
252 | void wxCriticalSection::Enter() | |
253 | { | |
0d0512bd | 254 | ::EnterCriticalSection((CRITICAL_SECTION *)m_buffer); |
3222fde2 VZ |
255 | } |
256 | ||
257 | void wxCriticalSection::Leave() | |
258 | { | |
0d0512bd | 259 | ::LeaveCriticalSection((CRITICAL_SECTION *)m_buffer); |
3222fde2 VZ |
260 | } |
261 | ||
262 | // ---------------------------------------------------------------------------- | |
263 | // wxThread implementation | |
264 | // ---------------------------------------------------------------------------- | |
265 | ||
bf1852e1 VZ |
266 | // wxThreadInternal class |
267 | // ---------------------- | |
268 | ||
3222fde2 VZ |
269 | class wxThreadInternal |
270 | { | |
2bda0e17 | 271 | public: |
bf1852e1 VZ |
272 | wxThreadInternal() |
273 | { | |
274 | m_hThread = 0; | |
275 | m_state = STATE_NEW; | |
276 | m_priority = WXTHREAD_DEFAULT_PRIORITY; | |
277 | } | |
278 | ||
279 | // create a new (suspended) thread (for the given thread object) | |
280 | bool Create(wxThread *thread); | |
281 | ||
282 | // suspend/resume/terminate | |
283 | bool Suspend(); | |
284 | bool Resume(); | |
285 | void Cancel() { m_state = STATE_CANCELED; } | |
286 | ||
287 | // thread state | |
288 | void SetState(wxThreadState state) { m_state = state; } | |
289 | wxThreadState GetState() const { return m_state; } | |
290 | ||
291 | // thread priority | |
292 | void SetPriority(unsigned int priority) { m_priority = priority; } | |
293 | unsigned int GetPriority() const { return m_priority; } | |
294 | ||
295 | // thread handle and id | |
296 | HANDLE GetHandle() const { return m_hThread; } | |
297 | DWORD GetId() const { return m_tid; } | |
298 | ||
299 | // thread function | |
bee503b0 | 300 | static DWORD WinThreadStart(wxThread *thread); |
2bda0e17 | 301 | |
bf1852e1 VZ |
302 | private: |
303 | HANDLE m_hThread; // handle of the thread | |
304 | wxThreadState m_state; // state, see wxThreadState enum | |
305 | unsigned int m_priority; // thread priority in "wx" units | |
306 | DWORD m_tid; // thread id | |
2bda0e17 KB |
307 | }; |
308 | ||
bee503b0 | 309 | DWORD wxThreadInternal::WinThreadStart(wxThread *thread) |
2bda0e17 | 310 | { |
bf1852e1 VZ |
311 | // store the thread object in the TLS |
312 | if ( !::TlsSetValue(s_tlsThisThread, thread) ) | |
313 | { | |
314 | wxLogSysError(_("Can not start thread: error writing TLS.")); | |
315 | ||
316 | return (DWORD)-1; | |
317 | } | |
318 | ||
bee503b0 | 319 | DWORD ret = (DWORD)thread->Entry(); |
bf1852e1 | 320 | thread->p_internal->SetState(STATE_EXITED); |
bee503b0 | 321 | thread->OnExit(); |
2bda0e17 | 322 | |
bf1852e1 VZ |
323 | delete thread; |
324 | ||
3222fde2 | 325 | return ret; |
2bda0e17 KB |
326 | } |
327 | ||
bf1852e1 | 328 | bool wxThreadInternal::Create(wxThread *thread) |
2bda0e17 | 329 | { |
bf1852e1 VZ |
330 | m_hThread = ::CreateThread |
331 | ( | |
332 | NULL, // default security | |
333 | 0, // default stack size | |
334 | (LPTHREAD_START_ROUTINE) // thread entry point | |
335 | wxThreadInternal::WinThreadStart, // | |
336 | (LPVOID)thread, // parameter | |
337 | CREATE_SUSPENDED, // flags | |
338 | &m_tid // [out] thread id | |
339 | ); | |
340 | ||
341 | if ( m_hThread == NULL ) | |
3222fde2 VZ |
342 | { |
343 | wxLogSysError(_("Can't create thread")); | |
bf1852e1 VZ |
344 | |
345 | return FALSE; | |
3222fde2 VZ |
346 | } |
347 | ||
bf1852e1 VZ |
348 | // translate wxWindows priority to the Windows one |
349 | int win_priority; | |
350 | if (m_priority <= 20) | |
351 | win_priority = THREAD_PRIORITY_LOWEST; | |
352 | else if (m_priority <= 40) | |
353 | win_priority = THREAD_PRIORITY_BELOW_NORMAL; | |
354 | else if (m_priority <= 60) | |
355 | win_priority = THREAD_PRIORITY_NORMAL; | |
356 | else if (m_priority <= 80) | |
357 | win_priority = THREAD_PRIORITY_ABOVE_NORMAL; | |
358 | else if (m_priority <= 100) | |
359 | win_priority = THREAD_PRIORITY_HIGHEST; | |
3222fde2 VZ |
360 | else |
361 | { | |
223d09f6 | 362 | wxFAIL_MSG(wxT("invalid value of thread priority parameter")); |
bf1852e1 | 363 | win_priority = THREAD_PRIORITY_NORMAL; |
3222fde2 VZ |
364 | } |
365 | ||
bf1852e1 | 366 | if ( ::SetThreadPriority(m_hThread, win_priority) == 0 ) |
bee503b0 VZ |
367 | { |
368 | wxLogSysError(_("Can't set thread priority")); | |
369 | } | |
3222fde2 | 370 | |
bf1852e1 | 371 | return TRUE; |
2bda0e17 KB |
372 | } |
373 | ||
bf1852e1 | 374 | bool wxThreadInternal::Suspend() |
2bda0e17 | 375 | { |
bf1852e1 VZ |
376 | DWORD nSuspendCount = ::SuspendThread(m_hThread); |
377 | if ( nSuspendCount == (DWORD)-1 ) | |
bee503b0 | 378 | { |
bf1852e1 VZ |
379 | wxLogSysError(_("Can not suspend thread %x"), m_hThread); |
380 | ||
381 | return FALSE; | |
bee503b0 | 382 | } |
2bda0e17 | 383 | |
bf1852e1 VZ |
384 | m_state = STATE_PAUSED; |
385 | ||
386 | return TRUE; | |
a6b0bd49 VZ |
387 | } |
388 | ||
bf1852e1 | 389 | bool wxThreadInternal::Resume() |
a6b0bd49 | 390 | { |
bf1852e1 | 391 | DWORD nSuspendCount = ::ResumeThread(m_hThread); |
a6b0bd49 VZ |
392 | if ( nSuspendCount == (DWORD)-1 ) |
393 | { | |
bf1852e1 | 394 | wxLogSysError(_("Can not resume thread %x"), m_hThread); |
a6b0bd49 | 395 | |
bf1852e1 | 396 | return FALSE; |
a6b0bd49 VZ |
397 | } |
398 | ||
bf1852e1 | 399 | m_state = STATE_RUNNING; |
bee503b0 | 400 | |
bf1852e1 | 401 | return TRUE; |
a6b0bd49 VZ |
402 | } |
403 | ||
bf1852e1 VZ |
404 | // static functions |
405 | // ---------------- | |
406 | ||
407 | wxThread *wxThread::This() | |
a6b0bd49 | 408 | { |
bf1852e1 VZ |
409 | wxThread *thread = (wxThread *)::TlsGetValue(s_tlsThisThread); |
410 | ||
411 | // be careful, 0 may be a valid return value as well | |
412 | if ( !thread && (::GetLastError() != NO_ERROR) ) | |
a6b0bd49 | 413 | { |
bf1852e1 | 414 | wxLogSysError(_("Couldn't get the current thread pointer")); |
a6b0bd49 | 415 | |
bf1852e1 | 416 | // return NULL... |
a6b0bd49 VZ |
417 | } |
418 | ||
bf1852e1 VZ |
419 | return thread; |
420 | } | |
421 | ||
422 | bool wxThread::IsMain() | |
423 | { | |
424 | return ::GetCurrentThreadId() == s_idMainThread; | |
425 | } | |
426 | ||
0a3d0d11 | 427 | #ifdef Yield |
42e69d6b | 428 | #undef Yield |
0a3d0d11 JS |
429 | #endif |
430 | ||
bf1852e1 VZ |
431 | void wxThread::Yield() |
432 | { | |
433 | // 0 argument to Sleep() is special | |
434 | ::Sleep(0); | |
435 | } | |
436 | ||
437 | void wxThread::Sleep(unsigned long milliseconds) | |
438 | { | |
439 | ::Sleep(milliseconds); | |
440 | } | |
441 | ||
442 | // create/start thread | |
443 | // ------------------- | |
444 | ||
445 | wxThreadError wxThread::Create() | |
446 | { | |
447 | if ( !p_internal->Create(this) ) | |
448 | return wxTHREAD_NO_RESOURCE; | |
bee503b0 | 449 | |
a6b0bd49 | 450 | return wxTHREAD_NO_ERROR; |
2bda0e17 KB |
451 | } |
452 | ||
bf1852e1 | 453 | wxThreadError wxThread::Run() |
2bda0e17 | 454 | { |
bf1852e1 VZ |
455 | wxCriticalSectionLocker lock(m_critsect); |
456 | ||
457 | if ( p_internal->GetState() != STATE_NEW ) | |
458 | { | |
459 | // actually, it may be almost any state at all, not only STATE_RUNNING | |
460 | return wxTHREAD_RUNNING; | |
461 | } | |
462 | ||
463 | return Resume(); | |
2bda0e17 KB |
464 | } |
465 | ||
bf1852e1 VZ |
466 | // suspend/resume thread |
467 | // --------------------- | |
468 | ||
469 | wxThreadError wxThread::Pause() | |
2bda0e17 | 470 | { |
bf1852e1 VZ |
471 | wxCriticalSectionLocker lock(m_critsect); |
472 | ||
473 | return p_internal->Suspend() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; | |
2bda0e17 KB |
474 | } |
475 | ||
bf1852e1 | 476 | wxThreadError wxThread::Resume() |
2bda0e17 | 477 | { |
bf1852e1 VZ |
478 | wxCriticalSectionLocker lock(m_critsect); |
479 | ||
480 | return p_internal->Resume() ? wxTHREAD_NO_ERROR : wxTHREAD_MISC_ERROR; | |
2bda0e17 KB |
481 | } |
482 | ||
bf1852e1 VZ |
483 | // stopping thread |
484 | // --------------- | |
485 | ||
486 | wxThread::ExitCode wxThread::Delete() | |
2bda0e17 | 487 | { |
bf1852e1 VZ |
488 | ExitCode rc = 0; |
489 | ||
490 | // Delete() is always safe to call, so consider all possible states | |
491 | if ( IsPaused() ) | |
492 | Resume(); | |
493 | ||
494 | if ( IsRunning() ) | |
495 | { | |
496 | if ( IsMain() ) | |
497 | { | |
498 | // set flag for wxIsWaitingForThread() | |
499 | s_waitingForThread = TRUE; | |
500 | ||
501 | wxBeginBusyCursor(); | |
502 | } | |
503 | ||
504 | HANDLE hThread; | |
505 | { | |
506 | wxCriticalSectionLocker lock(m_critsect); | |
507 | ||
508 | p_internal->Cancel(); | |
509 | hThread = p_internal->GetHandle(); | |
510 | } | |
511 | ||
512 | // we can't just wait for the thread to terminate because it might be | |
513 | // calling some GUI functions and so it will never terminate before we | |
514 | // process the Windows messages that result from these functions | |
515 | DWORD result; | |
516 | do | |
517 | { | |
518 | result = ::MsgWaitForMultipleObjects | |
519 | ( | |
520 | 1, // number of objects to wait for | |
521 | &hThread, // the objects | |
522 | FALSE, // don't wait for all objects | |
523 | INFINITE, // no timeout | |
524 | QS_ALLEVENTS // return as soon as there are any events | |
525 | ); | |
526 | ||
527 | switch ( result ) | |
528 | { | |
529 | case 0xFFFFFFFF: | |
530 | // error | |
531 | wxLogSysError(_("Can not wait for thread termination")); | |
532 | Kill(); | |
533 | return (ExitCode)-1; | |
534 | ||
535 | case WAIT_OBJECT_0: | |
536 | // thread we're waiting for terminated | |
537 | break; | |
538 | ||
539 | case WAIT_OBJECT_0 + 1: | |
540 | // new message arrived, process it | |
541 | if ( !wxTheApp->DoMessage() ) | |
542 | { | |
543 | // WM_QUIT received: kill the thread | |
544 | Kill(); | |
545 | ||
546 | return (ExitCode)-1; | |
547 | } | |
548 | ||
549 | if ( IsMain() ) | |
550 | { | |
551 | // give the thread we're waiting for chance to exit | |
552 | // from the GUI call it might have been in | |
553 | if ( (s_nWaitingForGui > 0) && wxGuiOwnedByMainThread() ) | |
554 | { | |
555 | wxMutexGuiLeave(); | |
556 | } | |
557 | } | |
558 | ||
559 | break; | |
560 | ||
561 | default: | |
223d09f6 | 562 | wxFAIL_MSG(wxT("unexpected result of MsgWaitForMultipleObject")); |
bf1852e1 VZ |
563 | } |
564 | } while ( result != WAIT_OBJECT_0 ); | |
565 | ||
566 | if ( IsMain() ) | |
567 | { | |
568 | s_waitingForThread = FALSE; | |
569 | ||
570 | wxEndBusyCursor(); | |
571 | } | |
572 | ||
573 | if ( !::GetExitCodeThread(hThread, (LPDWORD)&rc) ) | |
574 | { | |
575 | wxLogLastError("GetExitCodeThread"); | |
576 | ||
577 | rc = (ExitCode)-1; | |
578 | } | |
579 | ||
580 | wxASSERT_MSG( (LPVOID)rc != (LPVOID)STILL_ACTIVE, | |
223d09f6 | 581 | wxT("thread must be already terminated.") ); |
bf1852e1 VZ |
582 | |
583 | ::CloseHandle(hThread); | |
584 | } | |
585 | ||
586 | return rc; | |
2bda0e17 KB |
587 | } |
588 | ||
bf1852e1 | 589 | wxThreadError wxThread::Kill() |
2bda0e17 | 590 | { |
bf1852e1 VZ |
591 | if ( !IsRunning() ) |
592 | return wxTHREAD_NOT_RUNNING; | |
593 | ||
594 | if ( !::TerminateThread(p_internal->GetHandle(), (DWORD)-1) ) | |
595 | { | |
596 | wxLogSysError(_("Couldn't terminate thread")); | |
597 | ||
598 | return wxTHREAD_MISC_ERROR; | |
599 | } | |
600 | ||
601 | delete this; | |
602 | ||
603 | return wxTHREAD_NO_ERROR; | |
2bda0e17 KB |
604 | } |
605 | ||
bf1852e1 | 606 | void wxThread::Exit(void *status) |
2bda0e17 | 607 | { |
bf1852e1 | 608 | delete this; |
2bda0e17 | 609 | |
bf1852e1 | 610 | ::ExitThread((DWORD)status); |
2bda0e17 | 611 | |
223d09f6 | 612 | wxFAIL_MSG(wxT("Couldn't return from ExitThread()!")); |
bf1852e1 | 613 | } |
2bda0e17 | 614 | |
bf1852e1 VZ |
615 | void wxThread::SetPriority(unsigned int prio) |
616 | { | |
617 | wxCriticalSectionLocker lock(m_critsect); | |
618 | ||
619 | p_internal->SetPriority(prio); | |
620 | } | |
2bda0e17 | 621 | |
bf1852e1 VZ |
622 | unsigned int wxThread::GetPriority() const |
623 | { | |
624 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); | |
2bda0e17 | 625 | |
bf1852e1 | 626 | return p_internal->GetPriority(); |
2bda0e17 KB |
627 | } |
628 | ||
ee4f8c2a | 629 | unsigned long wxThread::GetID() const |
2bda0e17 | 630 | { |
bf1852e1 VZ |
631 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); |
632 | ||
633 | return (unsigned long)p_internal->GetId(); | |
2bda0e17 KB |
634 | } |
635 | ||
72fd19a1 JS |
636 | bool wxThread::IsRunning() const |
637 | { | |
bf1852e1 VZ |
638 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); |
639 | ||
640 | return p_internal->GetState() == STATE_RUNNING; | |
72fd19a1 JS |
641 | } |
642 | ||
643 | bool wxThread::IsAlive() const | |
644 | { | |
bf1852e1 VZ |
645 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); |
646 | ||
647 | return (p_internal->GetState() == STATE_RUNNING) || | |
648 | (p_internal->GetState() == STATE_PAUSED); | |
72fd19a1 JS |
649 | } |
650 | ||
a737331d GL |
651 | bool wxThread::IsPaused() const |
652 | { | |
653 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); | |
654 | ||
655 | return (p_internal->GetState() == STATE_PAUSED); | |
656 | } | |
657 | ||
8c10faf1 | 658 | bool wxThread::TestDestroy() |
2bda0e17 | 659 | { |
bf1852e1 VZ |
660 | wxCriticalSectionLocker lock((wxCriticalSection &)m_critsect); |
661 | ||
662 | return p_internal->GetState() == STATE_CANCELED; | |
2bda0e17 KB |
663 | } |
664 | ||
665 | wxThread::wxThread() | |
666 | { | |
3222fde2 | 667 | p_internal = new wxThreadInternal(); |
2bda0e17 KB |
668 | } |
669 | ||
670 | wxThread::~wxThread() | |
671 | { | |
3222fde2 | 672 | delete p_internal; |
2bda0e17 KB |
673 | } |
674 | ||
3222fde2 VZ |
675 | // ---------------------------------------------------------------------------- |
676 | // Automatic initialization for thread module | |
677 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 678 | |
3222fde2 | 679 | class wxThreadModule : public wxModule |
a6b0bd49 | 680 | { |
3222fde2 VZ |
681 | public: |
682 | virtual bool OnInit(); | |
683 | virtual void OnExit(); | |
d524867f | 684 | |
3222fde2 VZ |
685 | private: |
686 | DECLARE_DYNAMIC_CLASS(wxThreadModule) | |
687 | }; | |
d524867f RR |
688 | |
689 | IMPLEMENT_DYNAMIC_CLASS(wxThreadModule, wxModule) | |
690 | ||
3222fde2 | 691 | bool wxThreadModule::OnInit() |
d524867f | 692 | { |
bf1852e1 VZ |
693 | // allocate TLS index for storing the pointer to the current thread |
694 | s_tlsThisThread = ::TlsAlloc(); | |
695 | if ( s_tlsThisThread == 0xFFFFFFFF ) | |
696 | { | |
697 | // in normal circumstances it will only happen if all other | |
698 | // TLS_MINIMUM_AVAILABLE (>= 64) indices are already taken - in other | |
699 | // words, this should never happen | |
700 | wxLogSysError(_("Thread module initialization failed: " | |
701 | "impossible to allocate index in thread " | |
702 | "local storage")); | |
703 | ||
704 | return FALSE; | |
705 | } | |
706 | ||
707 | // main thread doesn't have associated wxThread object, so store 0 in the | |
708 | // TLS instead | |
709 | if ( !::TlsSetValue(s_tlsThisThread, (LPVOID)0) ) | |
710 | { | |
711 | ::TlsFree(s_tlsThisThread); | |
712 | s_tlsThisThread = 0xFFFFFFFF; | |
713 | ||
714 | wxLogSysError(_("Thread module initialization failed: " | |
715 | "can not store value in thread local storage")); | |
716 | ||
717 | return FALSE; | |
718 | } | |
719 | ||
bee503b0 VZ |
720 | s_critsectWaitingForGui = new wxCriticalSection(); |
721 | ||
3222fde2 VZ |
722 | s_critsectGui = new wxCriticalSection(); |
723 | s_critsectGui->Enter(); | |
bee503b0 | 724 | |
bf1852e1 | 725 | // no error return for GetCurrentThreadId() |
bee503b0 | 726 | s_idMainThread = ::GetCurrentThreadId(); |
3222fde2 | 727 | |
d524867f RR |
728 | return TRUE; |
729 | } | |
730 | ||
3222fde2 | 731 | void wxThreadModule::OnExit() |
d524867f | 732 | { |
bf1852e1 VZ |
733 | if ( !::TlsFree(s_tlsThisThread) ) |
734 | { | |
735 | wxLogLastError("TlsFree failed."); | |
736 | } | |
737 | ||
3222fde2 VZ |
738 | if ( s_critsectGui ) |
739 | { | |
740 | s_critsectGui->Leave(); | |
741 | delete s_critsectGui; | |
742 | s_critsectGui = NULL; | |
743 | } | |
bee503b0 VZ |
744 | |
745 | wxDELETE(s_critsectWaitingForGui); | |
3222fde2 VZ |
746 | } |
747 | ||
bee503b0 | 748 | // ---------------------------------------------------------------------------- |
3222fde2 VZ |
749 | // under Windows, these functions are implemented usign a critical section and |
750 | // not a mutex, so the names are a bit confusing | |
bee503b0 VZ |
751 | // ---------------------------------------------------------------------------- |
752 | ||
3222fde2 VZ |
753 | void WXDLLEXPORT wxMutexGuiEnter() |
754 | { | |
bee503b0 VZ |
755 | // this would dead lock everything... |
756 | wxASSERT_MSG( !wxThread::IsMain(), | |
223d09f6 | 757 | wxT("main thread doesn't want to block in wxMutexGuiEnter()!") ); |
bee503b0 VZ |
758 | |
759 | // the order in which we enter the critical sections here is crucial!! | |
760 | ||
761 | // set the flag telling to the main thread that we want to do some GUI | |
762 | { | |
763 | wxCriticalSectionLocker enter(*s_critsectWaitingForGui); | |
764 | ||
765 | s_nWaitingForGui++; | |
766 | } | |
767 | ||
768 | wxWakeUpMainThread(); | |
769 | ||
770 | // now we may block here because the main thread will soon let us in | |
771 | // (during the next iteration of OnIdle()) | |
772 | s_critsectGui->Enter(); | |
3222fde2 VZ |
773 | } |
774 | ||
775 | void WXDLLEXPORT wxMutexGuiLeave() | |
776 | { | |
bee503b0 VZ |
777 | wxCriticalSectionLocker enter(*s_critsectWaitingForGui); |
778 | ||
779 | if ( wxThread::IsMain() ) | |
780 | { | |
781 | s_bGuiOwnedByMainThread = FALSE; | |
782 | } | |
783 | else | |
784 | { | |
785 | // decrement the number of waiters now | |
786 | wxASSERT_MSG( s_nWaitingForGui > 0, | |
223d09f6 | 787 | wxT("calling wxMutexGuiLeave() without entering it first?") ); |
bee503b0 VZ |
788 | |
789 | s_nWaitingForGui--; | |
790 | ||
791 | wxWakeUpMainThread(); | |
792 | } | |
793 | ||
794 | s_critsectGui->Leave(); | |
795 | } | |
796 | ||
797 | void WXDLLEXPORT wxMutexGuiLeaveOrEnter() | |
798 | { | |
799 | wxASSERT_MSG( wxThread::IsMain(), | |
223d09f6 | 800 | wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") ); |
bee503b0 VZ |
801 | |
802 | wxCriticalSectionLocker enter(*s_critsectWaitingForGui); | |
803 | ||
804 | if ( s_nWaitingForGui == 0 ) | |
805 | { | |
806 | // no threads are waiting for GUI - so we may acquire the lock without | |
807 | // any danger (but only if we don't already have it) | |
808 | if ( !wxGuiOwnedByMainThread() ) | |
809 | { | |
810 | s_critsectGui->Enter(); | |
811 | ||
812 | s_bGuiOwnedByMainThread = TRUE; | |
813 | } | |
814 | //else: already have it, nothing to do | |
815 | } | |
816 | else | |
817 | { | |
818 | // some threads are waiting, release the GUI lock if we have it | |
819 | if ( wxGuiOwnedByMainThread() ) | |
820 | { | |
821 | wxMutexGuiLeave(); | |
822 | } | |
823 | //else: some other worker thread is doing GUI | |
824 | } | |
825 | } | |
826 | ||
827 | bool WXDLLEXPORT wxGuiOwnedByMainThread() | |
828 | { | |
829 | return s_bGuiOwnedByMainThread; | |
830 | } | |
831 | ||
832 | // wake up the main thread if it's in ::GetMessage() | |
833 | void WXDLLEXPORT wxWakeUpMainThread() | |
834 | { | |
835 | // sending any message would do - hopefully WM_NULL is harmless enough | |
836 | if ( !::PostThreadMessage(s_idMainThread, WM_NULL, 0, 0) ) | |
837 | { | |
838 | // should never happen | |
839 | wxLogLastError("PostThreadMessage(WM_NULL)"); | |
840 | } | |
3222fde2 | 841 | } |
d524867f | 842 | |
bf1852e1 VZ |
843 | bool WXDLLEXPORT wxIsWaitingForThread() |
844 | { | |
845 | return s_waitingForThread; | |
846 | } | |
847 | ||
3222fde2 | 848 | #endif // wxUSE_THREADS |