+
+ // create a new thread (and optionally set the stack size on platforms that
+ // support/need that), call Run() to start it
+ wxThreadError CreateThread(wxThreadKind kind = wxTHREAD_JOINABLE,
+ unsigned int stackSize = 0)
+ {
+ KillThread();
+
+ m_kind = kind;
+ m_thread = new wxThreadHelperThread(*this, m_kind);
+
+ return m_thread->Create(stackSize);
+ }
+
+ // entry point for the thread - called by Run() and executes in the context
+ // of this thread.
+ virtual void *Entry() = 0;
+
+ // returns a pointer to the thread which can be used to call Run()
+ wxThread *GetThread() const
+ {
+ wxCriticalSectionLocker locker((wxCriticalSection&)m_critSection);
+
+ wxThread* thread = m_thread;
+
+ return thread;
+ }
+
+protected:
+ wxThread *m_thread;
+ wxThreadKind m_kind;
+ wxCriticalSection m_critSection; // To guard the m_thread variable
+
+ friend class wxThreadHelperThread;
+};
+
+#if WXWIN_COMPATIBILITY_2_8
+inline wxThreadError wxThreadHelper::Create(unsigned int stackSize)
+{ return CreateThread(m_kind, stackSize); }
+#endif
+
+// call Entry() in owner, put it down here to avoid circular declarations
+inline void *wxThreadHelperThread::Entry()
+{
+ void * const result = m_owner.Entry();
+
+ wxCriticalSectionLocker locker(m_owner.m_critSection);
+
+ // Detached thread will be deleted after returning, so make sure
+ // wxThreadHelper::GetThread will not return an invalid pointer.
+ // And that wxThreadHelper::KillThread will not try to kill
+ // an already deleted thread
+ if ( m_owner.m_kind == wxTHREAD_DETACHED )
+ m_owner.m_thread = NULL;
+
+ return result;
+}
+
+// ----------------------------------------------------------------------------
+// Automatic initialization
+// ----------------------------------------------------------------------------
+
+// GUI mutex handling.
+void WXDLLIMPEXP_BASE wxMutexGuiEnter();
+void WXDLLIMPEXP_BASE wxMutexGuiLeave();
+
+// macros for entering/leaving critical sections which may be used without
+// having to take them inside "#if wxUSE_THREADS"
+#define wxENTER_CRIT_SECT(cs) (cs).Enter()
+#define wxLEAVE_CRIT_SECT(cs) (cs).Leave()
+#define wxCRIT_SECT_DECLARE(cs) static wxCriticalSection cs
+#define wxCRIT_SECT_DECLARE_MEMBER(cs) wxCriticalSection cs
+#define wxCRIT_SECT_LOCKER(name, cs) wxCriticalSectionLocker name(cs)
+
+// function for checking if we're in the main thread which may be used whether
+// wxUSE_THREADS is 0 or 1
+inline bool wxIsMainThread() { return wxThread::IsMain(); }
+
+#else // !wxUSE_THREADS
+
+// no thread support
+inline void wxMutexGuiEnter() { }
+inline void wxMutexGuiLeave() { }
+
+// macros for entering/leaving critical sections which may be used without
+// having to take them inside "#if wxUSE_THREADS"
+// (the implementation uses dummy structs to force semicolon after the macro;
+// also notice that Watcom doesn't like declaring a struct as a member so we
+// need to actually define it in wxCRIT_SECT_DECLARE_MEMBER)
+#define wxENTER_CRIT_SECT(cs) do {} while (0)
+#define wxLEAVE_CRIT_SECT(cs) do {} while (0)
+#define wxCRIT_SECT_DECLARE(cs) struct wxDummyCS##cs
+#define wxCRIT_SECT_DECLARE_MEMBER(cs) struct wxDummyCSMember##cs { }
+#define wxCRIT_SECT_LOCKER(name, cs) struct wxDummyCSLocker##name
+
+// if there is only one thread, it is always the main one
+inline bool wxIsMainThread() { return true; }
+
+#endif // wxUSE_THREADS/!wxUSE_THREADS
+
+// mark part of code as being a critical section: this macro declares a
+// critical section with the given name and enters it immediately and leaves
+// it at the end of the current scope
+//
+// example:
+//
+// int Count()
+// {
+// static int s_counter = 0;
+//
+// wxCRITICAL_SECTION(counter);
+//
+// return ++s_counter;
+// }
+//
+// this function is MT-safe in presence of the threads but there is no
+// overhead when the library is compiled without threads
+#define wxCRITICAL_SECTION(name) \
+ wxCRIT_SECT_DECLARE(s_cs##name); \
+ wxCRIT_SECT_LOCKER(cs##name##Locker, s_cs##name)
+
+// automatically lock GUI mutex in ctor and unlock it in dtor
+class WXDLLIMPEXP_BASE wxMutexGuiLocker
+{
+public:
+ wxMutexGuiLocker() { wxMutexGuiEnter(); }
+ ~wxMutexGuiLocker() { wxMutexGuiLeave(); }
+};
+
+// -----------------------------------------------------------------------------
+// implementation only until the end of file
+// -----------------------------------------------------------------------------
+
+#if wxUSE_THREADS
+
+#if defined(__WXMSW__) || defined(__OS2__) || defined(__EMX__)
+ // unlock GUI if there are threads waiting for and lock it back when
+ // there are no more of them - should be called periodically by the main
+ // thread
+ extern void WXDLLIMPEXP_BASE wxMutexGuiLeaveOrEnter();
+
+ // returns true if the main thread has GUI lock
+ extern bool WXDLLIMPEXP_BASE wxGuiOwnedByMainThread();
+
+ // wakes up the main thread if it's sleeping inside ::GetMessage()
+ extern void WXDLLIMPEXP_BASE wxWakeUpMainThread();
+
+ // return true if the main thread is waiting for some other to terminate:
+ // wxApp then should block all "dangerous" messages
+ extern bool WXDLLIMPEXP_BASE wxIsWaitingForThread();
+#endif // MSW, OS/2
+
+#endif // wxUSE_THREADS
+
+#endif // _WX_THREAD_H_