1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxThread Implementation
4 // Author: Original from Wolfram Gloger/Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to make it work :-)
8 // Copyright: (c) Wolfram Gloger (1996, 1997); Guilhem Lavaux (1998),
9 // Vadim Zeitlin (1999)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "thread.h"
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
24 #if defined(__BORLANDC__)
38 #include "wx/module.h"
39 #include "wx/thread.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // id of the main thread - the one which can call GUI functions without first
55 // calling wxMutexGuiEnter()
56 static DWORD s_idMainThread
= 0;
58 // if it's FALSE, some secondary thread is holding the GUI lock
59 static bool s_bGuiOwnedByMainThread
= TRUE
;
61 // critical section which controls access to all GUI functions: any secondary
62 // thread (i.e. except the main one) must enter this crit section before doing
64 static wxCriticalSection
*s_critsectGui
= NULL
;
66 // critical section which protects s_nWaitingForGui variable
67 static wxCriticalSection
*s_critsectWaitingForGui
= NULL
;
69 // number of threads waiting for GUI in wxMutexGuiEnter()
70 static size_t s_nWaitingForGui
= 0;
72 // ============================================================================
73 // Windows implementation of thread classes
74 // ============================================================================
76 // ----------------------------------------------------------------------------
77 // wxMutex implementation
78 // ----------------------------------------------------------------------------
87 p_internal
= new wxMutexInternal
;
88 p_internal
->p_mutex
= CreateMutex(NULL
, FALSE
, NULL
);
89 if ( !p_internal
->p_mutex
)
91 wxLogSysError(_("Can not create mutex."));
100 wxLogDebug("Warning: freeing a locked mutex (%d locks).", m_locked
);
101 CloseHandle(p_internal
->p_mutex
);
104 wxMutexError
wxMutex::Lock()
108 ret
= WaitForSingleObject(p_internal
->p_mutex
, INFINITE
);
119 wxLogSysError(_("Couldn't acquire a mutex lock"));
120 return wxMUTEX_MISC_ERROR
;
124 wxFAIL_MSG("impossible return value in wxMutex::Lock");
128 return wxMUTEX_NO_ERROR
;
131 wxMutexError
wxMutex::TryLock()
135 ret
= WaitForSingleObject(p_internal
->p_mutex
, 0);
136 if (ret
== WAIT_TIMEOUT
|| ret
== WAIT_ABANDONED
)
140 return wxMUTEX_NO_ERROR
;
143 wxMutexError
wxMutex::Unlock()
148 BOOL ret
= ReleaseMutex(p_internal
->p_mutex
);
151 wxLogSysError(_("Couldn't release a mutex"));
152 return wxMUTEX_MISC_ERROR
;
155 return wxMUTEX_NO_ERROR
;
158 // ----------------------------------------------------------------------------
159 // wxCondition implementation
160 // ----------------------------------------------------------------------------
162 class wxConditionInternal
169 wxCondition::wxCondition()
171 p_internal
= new wxConditionInternal
;
172 p_internal
->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
173 if ( !p_internal
->event
)
175 wxLogSysError(_("Can not create event object."));
178 p_internal
->waiters
= 0;
181 wxCondition::~wxCondition()
183 CloseHandle(p_internal
->event
);
186 void wxCondition::Wait(wxMutex
& mutex
)
189 p_internal
->waiters
++;
190 WaitForSingleObject(p_internal
->event
, INFINITE
);
191 p_internal
->waiters
--;
195 bool wxCondition::Wait(wxMutex
& mutex
,
202 p_internal
->waiters
++;
203 ret
= WaitForSingleObject(p_internal
->event
, (sec
*1000)+(nsec
/1000000));
204 p_internal
->waiters
--;
207 return (ret
!= WAIT_TIMEOUT
);
210 void wxCondition::Signal()
212 SetEvent(p_internal
->event
);
215 void wxCondition::Broadcast()
219 for (i
=0;i
<p_internal
->waiters
;i
++)
221 if ( SetEvent(p_internal
->event
) == 0 )
223 wxLogSysError(_("Couldn't change the state of event object."));
228 // ----------------------------------------------------------------------------
229 // wxCriticalSection implementation
230 // ----------------------------------------------------------------------------
232 class wxCriticalSectionInternal
235 // init the critical section object
236 wxCriticalSectionInternal()
237 { ::InitializeCriticalSection(&m_data
); }
239 // implicit cast to the associated data
240 operator CRITICAL_SECTION
*() { return &m_data
; }
242 // free the associated ressources
243 ~wxCriticalSectionInternal()
244 { ::DeleteCriticalSection(&m_data
); }
247 CRITICAL_SECTION m_data
;
250 wxCriticalSection::wxCriticalSection()
252 m_critsect
= new wxCriticalSectionInternal
;
255 wxCriticalSection::~wxCriticalSection()
260 void wxCriticalSection::Enter()
262 ::EnterCriticalSection(*m_critsect
);
265 void wxCriticalSection::Leave()
267 ::LeaveCriticalSection(*m_critsect
);
270 // ----------------------------------------------------------------------------
271 // wxThread implementation
272 // ----------------------------------------------------------------------------
274 class wxThreadInternal
277 static DWORD
WinThreadStart(wxThread
*thread
);
285 DWORD
wxThreadInternal::WinThreadStart(wxThread
*thread
)
287 DWORD ret
= (DWORD
)thread
->Entry();
288 thread
->p_internal
->state
= STATE_EXITED
;
294 wxThreadError
wxThread::Create()
296 p_internal
->hThread
= ::CreateThread
298 NULL
, // default security
299 0, // default stack size
300 (LPTHREAD_START_ROUTINE
)
301 wxThreadInternal::WinThreadStart
, // entry point
302 (void *)this, // parameter
303 CREATE_SUSPENDED
, // flags
304 &p_internal
->tid
// [out] thread id
307 if ( p_internal
->hThread
== NULL
)
309 wxLogSysError(_("Can't create thread"));
310 return wxTHREAD_NO_RESOURCE
;
313 int win_prio
, prio
= p_internal
->prio
;
315 win_prio
= THREAD_PRIORITY_LOWEST
;
317 win_prio
= THREAD_PRIORITY_BELOW_NORMAL
;
319 win_prio
= THREAD_PRIORITY_NORMAL
;
321 win_prio
= THREAD_PRIORITY_ABOVE_NORMAL
;
322 else if (prio
<= 100)
323 win_prio
= THREAD_PRIORITY_HIGHEST
;
326 wxFAIL_MSG("invalid value of thread priority parameter");
327 win_prio
= THREAD_PRIORITY_NORMAL
;
330 if ( SetThreadPriority(p_internal
->hThread
, win_prio
) == 0 )
332 wxLogSysError(_("Can't set thread priority"));
338 wxThreadError
wxThread::Destroy()
340 if ( p_internal
->state
!= STATE_RUNNING
)
341 return wxTHREAD_NOT_RUNNING
;
343 if ( p_internal
->defer
)
345 // soft termination: just set the flag and wait until the thread
347 p_internal
->state
= STATE_CANCELED
;
353 if ( ::TerminateThread(p_internal
->hThread
, 0) == 0 )
355 wxLogLastError("TerminateThread");
359 return wxTHREAD_NO_ERROR
;
362 wxThreadError
wxThread::Pause()
364 DWORD nSuspendCount
= ::SuspendThread(p_internal
->hThread
);
365 if ( nSuspendCount
== (DWORD
)-1 )
367 wxLogSysError(_("Can not suspend thread %x"), p_internal
->hThread
);
369 return wxTHREAD_MISC_ERROR
; // no idea what might provoke this error...
372 p_internal
->state
= STATE_PAUSED
;
374 return wxTHREAD_NO_ERROR
;
377 wxThreadError
wxThread::Resume()
379 DWORD nSuspendCount
= ::ResumeThread(p_internal
->hThread
);
380 if ( nSuspendCount
== (DWORD
)-1 )
382 wxLogSysError(_("Can not resume thread %x"), p_internal
->hThread
);
384 return wxTHREAD_MISC_ERROR
; // no idea what might provoke this error...
387 p_internal
->state
= STATE_RUNNING
;
389 return wxTHREAD_NO_ERROR
;
392 void wxThread::Exit(void *status
)
394 p_internal
->state
= STATE_EXITED
;
395 ExitThread((DWORD
)status
);
398 void wxThread::SetPriority(int prio
)
400 p_internal
->prio
= prio
;
403 int wxThread::GetPriority() const
405 return p_internal
->prio
;
408 void wxThread::DeferDestroy(bool on
)
410 p_internal
->defer
= on
;
413 bool wxThread::TestDestroy()
415 return p_internal
->state
== STATE_CANCELED
;
418 void *wxThread::Join()
422 if (p_internal
->state
== STATE_IDLE
)
425 // FIXME this dead locks... wxThread class design must be changed
427 WaitForSingleObject(p_internal
->hThread
, INFINITE
);
429 ::TerminateThread(p_internal
->hThread
, 0);
432 GetExitCodeThread(p_internal
->hThread
, &exit_code
);
433 CloseHandle(p_internal
->hThread
);
435 p_internal
->state
= STATE_IDLE
;
437 return (void *)exit_code
;
440 unsigned long wxThread::GetID() const
442 return (unsigned long)p_internal
->tid
;
445 bool wxThread::IsRunning() const
447 return (p_internal
->state
== STATE_RUNNING
);
450 bool wxThread::IsAlive() const
452 return (p_internal
->state
== STATE_RUNNING
);
455 bool wxThread::IsMain()
457 return ::GetCurrentThreadId() == s_idMainThread
;
462 p_internal
= new wxThreadInternal();
464 p_internal
->defer
= FALSE
;
465 p_internal
->prio
= WXTHREAD_DEFAULT_PRIORITY
;
466 p_internal
->state
= STATE_IDLE
;
469 wxThread::~wxThread()
476 void wxThread::OnExit()
480 // ----------------------------------------------------------------------------
481 // Automatic initialization for thread module
482 // ----------------------------------------------------------------------------
484 class wxThreadModule
: public wxModule
487 virtual bool OnInit();
488 virtual void OnExit();
491 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
494 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
496 bool wxThreadModule::OnInit()
498 s_critsectWaitingForGui
= new wxCriticalSection();
500 s_critsectGui
= new wxCriticalSection();
501 s_critsectGui
->Enter();
503 s_idMainThread
= ::GetCurrentThreadId();
508 void wxThreadModule::OnExit()
512 s_critsectGui
->Leave();
513 delete s_critsectGui
;
514 s_critsectGui
= NULL
;
517 wxDELETE(s_critsectWaitingForGui
);
520 // ----------------------------------------------------------------------------
521 // under Windows, these functions are implemented usign a critical section and
522 // not a mutex, so the names are a bit confusing
523 // ----------------------------------------------------------------------------
525 void WXDLLEXPORT
wxMutexGuiEnter()
527 // this would dead lock everything...
528 wxASSERT_MSG( !wxThread::IsMain(),
529 "main thread doesn't want to block in wxMutexGuiEnter()!" );
531 // the order in which we enter the critical sections here is crucial!!
533 // set the flag telling to the main thread that we want to do some GUI
535 wxCriticalSectionLocker
enter(*s_critsectWaitingForGui
);
540 wxWakeUpMainThread();
542 // now we may block here because the main thread will soon let us in
543 // (during the next iteration of OnIdle())
544 s_critsectGui
->Enter();
547 void WXDLLEXPORT
wxMutexGuiLeave()
549 wxCriticalSectionLocker
enter(*s_critsectWaitingForGui
);
551 if ( wxThread::IsMain() )
553 s_bGuiOwnedByMainThread
= FALSE
;
557 // decrement the number of waiters now
558 wxASSERT_MSG( s_nWaitingForGui
> 0,
559 "calling wxMutexGuiLeave() without entering it first?" );
563 wxWakeUpMainThread();
566 s_critsectGui
->Leave();
569 void WXDLLEXPORT
wxMutexGuiLeaveOrEnter()
571 wxASSERT_MSG( wxThread::IsMain(),
572 "only main thread may call wxMutexGuiLeaveOrEnter()!" );
574 wxCriticalSectionLocker
enter(*s_critsectWaitingForGui
);
576 if ( s_nWaitingForGui
== 0 )
578 // no threads are waiting for GUI - so we may acquire the lock without
579 // any danger (but only if we don't already have it)
580 if ( !wxGuiOwnedByMainThread() )
582 s_critsectGui
->Enter();
584 s_bGuiOwnedByMainThread
= TRUE
;
586 //else: already have it, nothing to do
590 // some threads are waiting, release the GUI lock if we have it
591 if ( wxGuiOwnedByMainThread() )
595 //else: some other worker thread is doing GUI
599 bool WXDLLEXPORT
wxGuiOwnedByMainThread()
601 return s_bGuiOwnedByMainThread
;
604 // wake up the main thread if it's in ::GetMessage()
605 void WXDLLEXPORT
wxWakeUpMainThread()
607 // sending any message would do - hopefully WM_NULL is harmless enough
608 if ( !::PostThreadMessage(s_idMainThread
, WM_NULL
, 0, 0) )
610 // should never happen
611 wxLogLastError("PostThreadMessage(WM_NULL)");
615 #endif // wxUSE_THREADS