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