1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/palmos/thread.cpp
3 // Purpose: wxThread Implementation
4 // Author: William Osborne - minimal working wxPalmOS port
8 // Copyright: (c) William Osborne
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if defined(__BORLANDC__)
25 #include "wx/thread.h"
30 #include "wx/module.h"
33 #include "wx/apptrait.h"
35 #include "wx/palmos/private.h"
36 #include "wx/palmos/missing.h"
38 // must have this symbol defined to get _beginthread/_endthread declarations
43 #if defined(__BORLANDC__)
45 // I can't set -tWM in the IDE (anyone?) so have to do this
49 #if !defined(__MFC_COMPAT__)
50 // Needed to know about _beginthreadex etc..
51 #define __MFC_COMPAT__
55 // define wxUSE_BEGIN_THREAD if the compiler has _beginthreadex() function
56 // which should be used instead of Win32 ::CreateThread() if possible
57 #if defined(__VISUALC__) || \
58 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x500)) || \
59 (defined(__GNUG__) && defined(__MSVCRT__)) || \
60 defined(__WATCOMC__) || defined(__MWERKS__)
63 #undef wxUSE_BEGIN_THREAD
64 #define wxUSE_BEGIN_THREAD
69 #ifdef wxUSE_BEGIN_THREAD
70 // the return type of the thread function entry point
71 typedef unsigned THREAD_RETVAL
;
73 // the calling convention of the thread function entry point
74 #define THREAD_CALLCONV __stdcall
76 // the settings for CreateThread()
77 typedef DWORD THREAD_RETVAL
;
78 #define THREAD_CALLCONV WINAPI
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // the possible states of the thread ("=>" shows all possible transitions from
89 STATE_NEW
, // didn't start execution yet (=> RUNNING)
90 STATE_RUNNING
, // thread is running (=> PAUSED, CANCELED)
91 STATE_PAUSED
, // thread is temporarily suspended (=> RUNNING)
92 STATE_CANCELED
, // thread should terminate a.s.a.p. (=> EXITED)
93 STATE_EXITED
// thread is terminating
96 // ----------------------------------------------------------------------------
97 // this module globals
98 // ----------------------------------------------------------------------------
100 // TLS index of the slot where we store the pointer to the current thread
101 static DWORD gs_tlsThisThread
= 0xFFFFFFFF;
103 // id of the main thread - the one which can call GUI functions without first
104 // calling wxMutexGuiEnter()
105 static DWORD gs_idMainThread
= 0;
107 // if it's false, some secondary thread is holding the GUI lock
108 static bool gs_bGuiOwnedByMainThread
= true;
110 // critical section which controls access to all GUI functions: any secondary
111 // thread (i.e. except the main one) must enter this crit section before doing
113 static wxCriticalSection
*gs_critsectGui
= NULL
;
115 // critical section which protects gs_nWaitingForGui variable
116 static wxCriticalSection
*gs_critsectWaitingForGui
= NULL
;
118 // critical section which serializes WinThreadStart() and WaitForTerminate()
119 // (this is a potential bottleneck, we use a single crit sect for all threads
120 // in the system, but normally time spent inside it should be quite short)
121 static wxCriticalSection
*gs_critsectThreadDelete
= NULL
;
123 // number of threads waiting for GUI in wxMutexGuiEnter()
124 static size_t gs_nWaitingForGui
= 0;
126 // are we waiting for a thread termination?
127 static bool gs_waitingForThread
= false;
129 // ============================================================================
130 // Windows implementation of thread and related classes
131 // ============================================================================
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 wxCriticalSection::wxCriticalSection()
141 wxCriticalSection::~wxCriticalSection()
145 void wxCriticalSection::Enter()
149 void wxCriticalSection::Leave()
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 class wxMutexInternal
160 wxMutexInternal(wxMutexType mutexType
);
163 bool IsOk() const { return m_mutex
!= NULL
; }
165 wxMutexError
Lock() { return LockTimeout(INFINITE
); }
166 wxMutexError
TryLock() { return LockTimeout(0); }
167 wxMutexError
Unlock();
170 wxMutexError
LockTimeout(DWORD milliseconds
);
174 wxDECLARE_NO_COPY_CLASS(wxMutexInternal
);
177 // all mutexes are recursive under Win32 so we don't use mutexType
178 wxMutexInternal::wxMutexInternal(wxMutexType
WXUNUSED(mutexType
))
182 wxMutexInternal::~wxMutexInternal()
186 wxMutexError
wxMutexInternal::LockTimeout(DWORD milliseconds
)
188 return wxMUTEX_NO_ERROR
;
191 wxMutexError
wxMutexInternal::Unlock()
193 return wxMUTEX_NO_ERROR
;
196 // --------------------------------------------------------------------------
198 // --------------------------------------------------------------------------
200 // a trivial wrapper around Win32 semaphore
201 class wxSemaphoreInternal
204 wxSemaphoreInternal(int initialcount
, int maxcount
);
205 ~wxSemaphoreInternal();
207 bool IsOk() const { return m_semaphore
!= NULL
; }
209 wxSemaError
Wait() { return WaitTimeout(INFINITE
); }
211 wxSemaError
TryWait()
213 wxSemaError rc
= WaitTimeout(0);
214 if ( rc
== wxSEMA_TIMEOUT
)
220 wxSemaError
WaitTimeout(unsigned long milliseconds
);
227 wxDECLARE_NO_COPY_CLASS(wxSemaphoreInternal
);
230 wxSemaphoreInternal::wxSemaphoreInternal(int initialcount
, int maxcount
)
234 wxSemaphoreInternal::~wxSemaphoreInternal()
238 wxSemaError
wxSemaphoreInternal::WaitTimeout(unsigned long milliseconds
)
240 return wxSEMA_NO_ERROR
;
243 wxSemaError
wxSemaphoreInternal::Post()
245 return wxSEMA_NO_ERROR
;
248 // ----------------------------------------------------------------------------
249 // wxThread implementation
250 // ----------------------------------------------------------------------------
252 // wxThreadInternal class
253 // ----------------------
255 class wxThreadInternal
258 wxThreadInternal(wxThread
*thread
)
263 m_priority
= WXTHREAD_DEFAULT_PRIORITY
;
276 if ( !::CloseHandle(m_hThread
) )
278 wxLogLastError(wxT("CloseHandle(thread)"));
285 // create a new (suspended) thread (for the given thread object)
286 bool Create(wxThread
*thread
, unsigned int stackSize
);
288 // wait for the thread to terminate, either by itself, or by asking it
289 // (politely, this is not Kill()!) to do it
290 wxThreadError
WaitForTerminate(wxCriticalSection
& cs
,
291 wxThread::ExitCode
*pRc
,
292 wxThread
*threadToDelete
= NULL
);
294 // kill the thread unconditionally
295 wxThreadError
Kill();
297 // suspend/resume/terminate
300 void Cancel() { m_state
= STATE_CANCELED
; }
303 void SetState(wxThreadState state
) { m_state
= state
; }
304 wxThreadState
GetState() const { return m_state
; }
307 void SetPriority(unsigned int priority
);
308 unsigned int GetPriority() const { return m_priority
; }
310 // thread handle and id
311 HANDLE
GetHandle() const { return m_hThread
; }
312 DWORD
GetId() const { return m_tid
; }
315 static THREAD_RETVAL THREAD_CALLCONV
WinThreadStart(void *thread
);
319 if ( m_thread
->IsDetached() )
320 ::InterlockedIncrement(&m_nRef
);
325 if ( m_thread
->IsDetached() && !::InterlockedDecrement(&m_nRef
) )
330 // the thread we're associated with
333 HANDLE m_hThread
; // handle of the thread
334 wxThreadState m_state
; // state, see wxThreadState enum
335 unsigned int m_priority
; // thread priority in "wx" units
336 DWORD m_tid
; // thread id
338 // number of threads which need this thread to remain alive, when the count
339 // reaches 0 we kill the owning wxThread -- and die ourselves with it
342 wxDECLARE_NO_COPY_CLASS(wxThreadInternal
);
345 // small class which keeps a thread alive during its lifetime
346 class wxThreadKeepAlive
349 wxThreadKeepAlive(wxThreadInternal
& thrImpl
) : m_thrImpl(thrImpl
)
350 { m_thrImpl
.KeepAlive(); }
352 { m_thrImpl
.LetDie(); }
355 wxThreadInternal
& m_thrImpl
;
359 THREAD_RETVAL THREAD_CALLCONV
wxThreadInternal::WinThreadStart(void *param
)
366 void wxThreadInternal::SetPriority(unsigned int priority
)
370 bool wxThreadInternal::Create(wxThread
*thread
, unsigned int stackSize
)
375 wxThreadError
wxThreadInternal::Kill()
377 return wxTHREAD_NO_ERROR
;
381 wxThreadInternal::WaitForTerminate(wxCriticalSection
& cs
,
382 wxThread::ExitCode
*pRc
,
383 wxThread
*threadToDelete
)
385 return wxTHREAD_NO_ERROR
;
388 bool wxThreadInternal::Suspend()
393 bool wxThreadInternal::Resume()
401 wxThread
*wxThread::This()
406 bool wxThread::IsMain()
411 void wxThread::Yield()
415 int wxThread::GetCPUCount()
420 unsigned long wxThread::GetCurrentId()
425 bool wxThread::SetConcurrency(size_t level
)
433 wxThread::wxThread(wxThreadKind kind
)
437 wxThread::~wxThread()
441 // create/start thread
442 // -------------------
444 wxThreadError
wxThread::Create(unsigned int stackSize
)
446 return wxTHREAD_NO_ERROR
;
449 wxThreadError
wxThread::Run()
451 return wxTHREAD_RUNNING
;
454 // suspend/resume thread
455 // ---------------------
457 wxThreadError
wxThread::Pause()
459 return wxTHREAD_NO_ERROR
;
462 wxThreadError
wxThread::Resume()
464 return wxTHREAD_NO_ERROR
;
470 wxThread::ExitCode
wxThread::Wait()
475 wxThreadError
wxThread::Delete(ExitCode
*pRc
)
477 return wxTHREAD_NO_ERROR
;
480 wxThreadError
wxThread::Kill()
482 return wxTHREAD_NO_ERROR
;
485 void wxThread::Exit(ExitCode status
)
492 void wxThread::SetPriority(unsigned int prio
)
496 unsigned int wxThread::GetPriority() const
501 unsigned long wxThread::GetId() const
506 bool wxThread::IsRunning() const
511 bool wxThread::IsAlive() const
516 bool wxThread::IsPaused() const
521 bool wxThread::TestDestroy()
526 // ----------------------------------------------------------------------------
527 // Automatic initialization for thread module
528 // ----------------------------------------------------------------------------
530 class wxThreadModule
: public wxModule
533 virtual bool OnInit();
534 virtual void OnExit();
537 DECLARE_DYNAMIC_CLASS(wxThreadModule
)
540 IMPLEMENT_DYNAMIC_CLASS(wxThreadModule
, wxModule
)
542 bool wxThreadModule::OnInit()
547 void wxThreadModule::OnExit()
551 // ----------------------------------------------------------------------------
552 // under Windows, these functions are implemented using a critical section and
553 // not a mutex, so the names are a bit confusing
554 // ----------------------------------------------------------------------------
556 void wxMutexGuiEnterImpl()
560 void wxMutexGuiLeaveImpl()
564 void WXDLLIMPEXP_BASE
wxMutexGuiLeaveOrEnter()
568 bool WXDLLIMPEXP_BASE
wxGuiOwnedByMainThread()
573 // wake up the main thread if it's in ::GetMessage()
574 void WXDLLIMPEXP_BASE
wxWakeUpMainThread()
578 bool WXDLLIMPEXP_BASE
wxIsWaitingForThread()
583 // ----------------------------------------------------------------------------
584 // include common implementation code
585 // ----------------------------------------------------------------------------
587 #include "wx/thrimpl.cpp"
589 #endif // wxUSE_THREADS