1998-05-21 15:02:02 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2002-06-06 18:31:59 +00:00
|
|
|
// Name: wx/thread.h
|
1998-05-21 15:02:02 +00:00
|
|
|
// Purpose: Thread API
|
|
|
|
// Author: Guilhem Lavaux
|
1999-01-17 22:39:58 +00:00
|
|
|
// Modified by: Vadim Zeitlin (modifications partly inspired by omnithreads
|
|
|
|
// package from Olivetti & Oracle Research Laboratory)
|
1998-05-21 15:02:02 +00:00
|
|
|
// Created: 04/13/98
|
|
|
|
// Copyright: (c) Guilhem Lavaux
|
2004-05-23 20:53:33 +00:00
|
|
|
// Licence: wxWindows licence
|
1998-05-21 15:02:02 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
#ifndef _WX_THREAD_H_
|
|
|
|
#define _WX_THREAD_H_
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-17 22:39:58 +00:00
|
|
|
|
|
|
|
// get the value of wxUSE_THREADS configuration flag
|
2002-06-06 18:31:59 +00:00
|
|
|
#include "wx/defs.h"
|
1999-01-05 03:04:48 +00:00
|
|
|
|
|
|
|
#if wxUSE_THREADS
|
1999-11-24 12:30:56 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-11-24 12:30:56 +00:00
|
|
|
enum wxMutexError
|
1999-01-02 22:24:41 +00:00
|
|
|
{
|
2002-06-06 18:31:59 +00:00
|
|
|
wxMUTEX_NO_ERROR = 0, // operation completed successfully
|
|
|
|
wxMUTEX_INVALID, // mutex hasn't been initialized
|
|
|
|
wxMUTEX_DEAD_LOCK, // mutex is already locked by the calling thread
|
|
|
|
wxMUTEX_BUSY, // mutex is already locked by another thread
|
|
|
|
wxMUTEX_UNLOCKED, // attempt to unlock a mutex which is not locked
|
2007-03-08 00:22:11 +00:00
|
|
|
wxMUTEX_TIMEOUT, // LockTimeout() has timed out
|
2002-06-06 18:31:59 +00:00
|
|
|
wxMUTEX_MISC_ERROR // any other error
|
|
|
|
};
|
|
|
|
|
|
|
|
enum wxCondError
|
|
|
|
{
|
|
|
|
wxCOND_NO_ERROR = 0,
|
|
|
|
wxCOND_INVALID,
|
|
|
|
wxCOND_TIMEOUT, // WaitTimeout() has timed out
|
|
|
|
wxCOND_MISC_ERROR
|
|
|
|
};
|
|
|
|
|
|
|
|
enum wxSemaError
|
|
|
|
{
|
|
|
|
wxSEMA_NO_ERROR = 0,
|
|
|
|
wxSEMA_INVALID, // semaphore hasn't been initialized successfully
|
|
|
|
wxSEMA_BUSY, // returned by TryWait() if Wait() would block
|
|
|
|
wxSEMA_TIMEOUT, // returned by WaitTimeout()
|
|
|
|
wxSEMA_OVERFLOW, // Post() would increase counter past the max
|
|
|
|
wxSEMA_MISC_ERROR
|
1999-11-24 12:30:56 +00:00
|
|
|
};
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1999-11-24 12:30:56 +00:00
|
|
|
enum wxThreadError
|
1999-01-02 22:24:41 +00:00
|
|
|
{
|
1999-11-29 23:05:23 +00:00
|
|
|
wxTHREAD_NO_ERROR = 0, // No error
|
|
|
|
wxTHREAD_NO_RESOURCE, // No resource left to create a new thread
|
|
|
|
wxTHREAD_RUNNING, // The thread is already running
|
|
|
|
wxTHREAD_NOT_RUNNING, // The thread isn't running
|
|
|
|
wxTHREAD_KILLED, // Thread we waited for had to be killed
|
|
|
|
wxTHREAD_MISC_ERROR // Some other error
|
1999-11-24 12:30:56 +00:00
|
|
|
};
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1999-11-27 22:57:06 +00:00
|
|
|
enum wxThreadKind
|
|
|
|
{
|
|
|
|
wxTHREAD_DETACHED,
|
|
|
|
wxTHREAD_JOINABLE
|
|
|
|
};
|
|
|
|
|
2011-03-14 11:54:32 +00:00
|
|
|
enum wxThreadWait
|
|
|
|
{
|
|
|
|
wxTHREAD_WAIT_BLOCK,
|
|
|
|
wxTHREAD_WAIT_YIELD, // process events while waiting; MSW only
|
|
|
|
|
|
|
|
// For compatibility reasons we use wxTHREAD_WAIT_YIELD by default as this
|
|
|
|
// was the default behaviour of wxMSW 2.8 but it should be avoided as it's
|
|
|
|
// dangerous and not portable.
|
|
|
|
#if WXWIN_COMPATIBILITY_2_8
|
|
|
|
wxTHREAD_WAIT_DEFAULT = wxTHREAD_WAIT_YIELD
|
|
|
|
#else
|
|
|
|
wxTHREAD_WAIT_DEFAULT = wxTHREAD_WAIT_BLOCK
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2013-01-20 02:10:07 +00:00
|
|
|
// Obsolete synonyms for wxPRIORITY_XXX for backwards compatibility-only
|
1999-11-24 12:30:56 +00:00
|
|
|
enum
|
|
|
|
{
|
2013-01-20 02:10:07 +00:00
|
|
|
WXTHREAD_MIN_PRIORITY = wxPRIORITY_MIN,
|
|
|
|
WXTHREAD_DEFAULT_PRIORITY = wxPRIORITY_DEFAULT,
|
|
|
|
WXTHREAD_MAX_PRIORITY = wxPRIORITY_MAX
|
1999-11-24 12:30:56 +00:00
|
|
|
};
|
1998-05-21 15:02:02 +00:00
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
// There are 2 types of mutexes: normal mutexes and recursive ones. The attempt
|
|
|
|
// to lock a normal mutex by a thread which already owns it results in
|
|
|
|
// undefined behaviour (it always works under Windows, it will almost always
|
|
|
|
// result in a deadlock under Unix). Locking a recursive mutex in such
|
|
|
|
// situation always succeeds and it must be unlocked as many times as it has
|
|
|
|
// been locked.
|
|
|
|
//
|
|
|
|
// However recursive mutexes have several important drawbacks: first, in the
|
|
|
|
// POSIX implementation, they're less efficient. Second, and more importantly,
|
|
|
|
// they CAN NOT BE USED WITH CONDITION VARIABLES under Unix! Using them with
|
|
|
|
// wxCondition will work under Windows and some Unices (notably Linux) but will
|
|
|
|
// deadlock under other Unix versions (e.g. Solaris). As it might be difficult
|
|
|
|
// to ensure that a recursive mutex is not used with wxCondition, it is a good
|
|
|
|
// idea to avoid using recursive mutexes at all. Also, the last problem with
|
|
|
|
// them is that some (older) Unix versions don't support this at all -- which
|
|
|
|
// results in a configure warning when building and a deadlock when using them.
|
|
|
|
enum wxMutexType
|
|
|
|
{
|
|
|
|
// normal mutex: try to always use this one
|
|
|
|
wxMUTEX_DEFAULT,
|
|
|
|
|
|
|
|
// recursive mutex: don't use these ones with wxCondition
|
|
|
|
wxMUTEX_RECURSIVE
|
|
|
|
};
|
|
|
|
|
|
|
|
// forward declarations
|
2007-07-09 10:09:52 +00:00
|
|
|
class WXDLLIMPEXP_FWD_BASE wxThreadHelper;
|
|
|
|
class WXDLLIMPEXP_FWD_BASE wxConditionInternal;
|
|
|
|
class WXDLLIMPEXP_FWD_BASE wxMutexInternal;
|
|
|
|
class WXDLLIMPEXP_FWD_BASE wxSemaphoreInternal;
|
|
|
|
class WXDLLIMPEXP_FWD_BASE wxThreadInternal;
|
2002-06-06 18:31:59 +00:00
|
|
|
|
1999-01-02 22:24:41 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-05 03:04:48 +00:00
|
|
|
// A mutex object is a synchronization object whose state is set to signaled
|
|
|
|
// when it is not owned by any thread, and nonsignaled when it is owned. Its
|
|
|
|
// name comes from its usefulness in coordinating mutually-exclusive access to
|
|
|
|
// a shared resource. Only one thread at a time can own a mutex object.
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-02 22:24:41 +00:00
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
// you should consider wxMutexLocker whenever possible instead of directly
|
|
|
|
// working with wxMutex class - it is safer
|
2003-07-04 13:00:07 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxMutex
|
1999-01-02 22:24:41 +00:00
|
|
|
{
|
1998-05-21 15:02:02 +00:00
|
|
|
public:
|
1999-01-17 22:39:58 +00:00
|
|
|
// constructor & destructor
|
2002-06-06 18:31:59 +00:00
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
// create either default (always safe) or recursive mutex
|
|
|
|
wxMutex(wxMutexType mutexType = wxMUTEX_DEFAULT);
|
|
|
|
|
|
|
|
// destroys the mutex kernel object
|
1999-01-10 00:04:07 +00:00
|
|
|
~wxMutex();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
// test if the mutex has been created successfully
|
|
|
|
bool IsOk() const;
|
|
|
|
|
|
|
|
// mutex operations
|
|
|
|
// ----------------
|
|
|
|
|
|
|
|
// Lock the mutex, blocking on it until it is unlocked by the other thread.
|
|
|
|
// The result of locking a mutex already locked by the current thread
|
|
|
|
// depend on the mutex type.
|
|
|
|
//
|
|
|
|
// The caller must call Unlock() later if Lock() returned wxMUTEX_NO_ERROR.
|
1999-01-10 00:04:07 +00:00
|
|
|
wxMutexError Lock();
|
2002-06-06 18:31:59 +00:00
|
|
|
|
2007-03-08 00:22:11 +00:00
|
|
|
// Same as Lock() but return wxMUTEX_TIMEOUT if the mutex can't be locked
|
|
|
|
// during the given number of milliseconds
|
|
|
|
wxMutexError LockTimeout(unsigned long ms);
|
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
// Try to lock the mutex: if it is currently locked, return immediately
|
|
|
|
// with an error. Otherwise the caller must call Unlock().
|
1999-01-10 00:04:07 +00:00
|
|
|
wxMutexError TryLock();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
// Unlock the mutex. It is an error to unlock an already unlocked mutex
|
|
|
|
wxMutexError Unlock();
|
1998-12-27 00:54:53 +00:00
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
protected:
|
1999-11-29 23:05:23 +00:00
|
|
|
wxMutexInternal *m_internal;
|
2002-04-02 13:15:16 +00:00
|
|
|
|
|
|
|
friend class wxConditionInternal;
|
2002-06-06 18:31:59 +00:00
|
|
|
|
2009-02-08 11:45:59 +00:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxMutex);
|
1998-05-21 15:02:02 +00:00
|
|
|
};
|
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
// a helper class which locks the mutex in the ctor and unlocks it in the dtor:
|
|
|
|
// this ensures that mutex is always unlocked, even if the function returns or
|
|
|
|
// throws an exception before it reaches the end
|
2003-07-04 13:00:07 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxMutexLocker
|
1999-01-05 03:04:48 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// lock the mutex in the ctor
|
2002-05-08 13:30:26 +00:00
|
|
|
wxMutexLocker(wxMutex& mutex)
|
2004-04-30 21:31:52 +00:00
|
|
|
: m_isOk(false), m_mutex(mutex)
|
2002-05-08 13:30:26 +00:00
|
|
|
{ m_isOk = ( m_mutex.Lock() == wxMUTEX_NO_ERROR ); }
|
1999-01-05 03:04:48 +00:00
|
|
|
|
2004-04-30 21:31:52 +00:00
|
|
|
// returns true if mutex was successfully locked in ctor
|
1999-01-22 16:21:24 +00:00
|
|
|
bool IsOk() const
|
|
|
|
{ return m_isOk; }
|
1999-01-05 03:04:48 +00:00
|
|
|
|
|
|
|
// unlock the mutex in dtor
|
1999-01-22 16:21:24 +00:00
|
|
|
~wxMutexLocker()
|
|
|
|
{ if ( IsOk() ) m_mutex.Unlock(); }
|
1999-01-05 03:04:48 +00:00
|
|
|
|
|
|
|
private:
|
1999-01-10 00:04:07 +00:00
|
|
|
// no assignment operator nor copy ctor
|
|
|
|
wxMutexLocker(const wxMutexLocker&);
|
|
|
|
wxMutexLocker& operator=(const wxMutexLocker&);
|
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
bool m_isOk;
|
1999-01-22 16:21:24 +00:00
|
|
|
wxMutex& m_mutex;
|
1999-01-05 03:04:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Critical section: this is the same as mutex but is only visible to the
|
|
|
|
// threads of the same process. For the platforms which don't have native
|
|
|
|
// support for critical sections, they're implemented entirely in terms of
|
1999-11-24 12:30:56 +00:00
|
|
|
// mutexes.
|
|
|
|
//
|
|
|
|
// NB: wxCriticalSection object does not allocate any memory in its ctor
|
|
|
|
// which makes it possible to have static globals of this class
|
1999-01-05 03:04:48 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-11-24 12:30:56 +00:00
|
|
|
// in order to avoid any overhead under platforms where critical sections are
|
|
|
|
// just mutexes make all wxCriticalSection class functions inline
|
2012-03-04 00:29:31 +00:00
|
|
|
#if !defined(__WINDOWS__)
|
1999-11-24 12:30:56 +00:00
|
|
|
#define wxCRITSECT_IS_MUTEX 1
|
2002-06-07 21:46:44 +00:00
|
|
|
|
2008-09-11 06:46:23 +00:00
|
|
|
#define wxCRITSECT_INLINE WXEXPORT inline
|
2003-08-17 23:32:00 +00:00
|
|
|
#else // MSW
|
1999-11-24 12:30:56 +00:00
|
|
|
#define wxCRITSECT_IS_MUTEX 0
|
2002-06-07 21:46:44 +00:00
|
|
|
|
|
|
|
#define wxCRITSECT_INLINE
|
1999-01-11 15:51:45 +00:00
|
|
|
#endif // MSW/!MSW
|
1999-01-17 22:39:58 +00:00
|
|
|
|
2008-09-05 08:06:07 +00:00
|
|
|
enum wxCriticalSectionType
|
|
|
|
{
|
|
|
|
// recursive critical section
|
|
|
|
wxCRITSEC_DEFAULT,
|
|
|
|
|
|
|
|
// non-recursive critical section
|
|
|
|
wxCRITSEC_NON_RECURSIVE
|
|
|
|
};
|
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
// you should consider wxCriticalSectionLocker whenever possible instead of
|
|
|
|
// directly working with wxCriticalSection class - it is safer
|
2003-07-04 13:00:07 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxCriticalSection
|
1999-01-05 03:04:48 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor & dtor
|
2008-09-05 08:06:07 +00:00
|
|
|
wxCRITSECT_INLINE wxCriticalSection( wxCriticalSectionType critSecType = wxCRITSEC_DEFAULT );
|
2002-06-07 21:46:44 +00:00
|
|
|
wxCRITSECT_INLINE ~wxCriticalSection();
|
1999-01-05 03:04:48 +00:00
|
|
|
// enter the section (the same as locking a mutex)
|
2002-06-07 21:46:44 +00:00
|
|
|
wxCRITSECT_INLINE void Enter();
|
2002-06-06 18:31:59 +00:00
|
|
|
|
2011-12-01 14:22:15 +00:00
|
|
|
// try to enter the section (the same as trying to lock a mutex)
|
|
|
|
wxCRITSECT_INLINE bool TryEnter();
|
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
// leave the critical section (same as unlocking a mutex)
|
2002-06-07 21:46:44 +00:00
|
|
|
wxCRITSECT_INLINE void Leave();
|
1999-01-05 03:04:48 +00:00
|
|
|
|
|
|
|
private:
|
1999-11-24 12:30:56 +00:00
|
|
|
#if wxCRITSECT_IS_MUTEX
|
1999-01-11 15:51:45 +00:00
|
|
|
wxMutex m_mutex;
|
2012-03-04 00:29:31 +00:00
|
|
|
#elif defined(__WINDOWS__)
|
1999-11-24 12:30:56 +00:00
|
|
|
// we can't allocate any memory in the ctor, so use placement new -
|
|
|
|
// unfortunately, we have to hardcode the sizeof() here because we can't
|
2002-06-20 22:33:22 +00:00
|
|
|
// include windows.h from this public header and we also have to use the
|
|
|
|
// union to force the correct (i.e. maximal) alignment
|
2002-06-06 18:31:59 +00:00
|
|
|
//
|
2004-10-20 01:20:58 +00:00
|
|
|
// if CRITICAL_SECTION size changes in Windows, you'll get an assert from
|
|
|
|
// thread.cpp and will need to increase the buffer size
|
2004-02-10 13:02:51 +00:00
|
|
|
#ifdef __WIN64__
|
|
|
|
typedef char wxCritSectBuffer[40];
|
|
|
|
#else // __WIN32__
|
2002-06-20 22:33:22 +00:00
|
|
|
typedef char wxCritSectBuffer[24];
|
2004-02-10 13:02:51 +00:00
|
|
|
#endif
|
2002-06-20 22:33:22 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned long m_dummy1;
|
|
|
|
void *m_dummy2;
|
|
|
|
|
|
|
|
wxCritSectBuffer m_buffer;
|
|
|
|
};
|
2014-05-15 23:56:58 +00:00
|
|
|
#endif // Unix/Win32
|
2002-06-06 18:31:59 +00:00
|
|
|
|
2009-02-08 11:45:59 +00:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxCriticalSection);
|
1999-01-05 03:04:48 +00:00
|
|
|
};
|
|
|
|
|
2008-09-11 06:46:23 +00:00
|
|
|
#if wxCRITSECT_IS_MUTEX
|
2002-06-06 18:31:59 +00:00
|
|
|
// implement wxCriticalSection using mutexes
|
2008-11-26 17:24:00 +00:00
|
|
|
inline wxCriticalSection::wxCriticalSection( wxCriticalSectionType critSecType )
|
2008-09-05 08:06:07 +00:00
|
|
|
: m_mutex( critSecType == wxCRITSEC_DEFAULT ? wxMUTEX_RECURSIVE : wxMUTEX_DEFAULT ) { }
|
2002-06-07 21:46:44 +00:00
|
|
|
inline wxCriticalSection::~wxCriticalSection() { }
|
2002-06-06 18:31:59 +00:00
|
|
|
|
2002-06-07 21:46:44 +00:00
|
|
|
inline void wxCriticalSection::Enter() { (void)m_mutex.Lock(); }
|
2011-12-01 14:22:15 +00:00
|
|
|
inline bool wxCriticalSection::TryEnter() { return m_mutex.TryLock() == wxMUTEX_NO_ERROR; }
|
2002-06-07 21:46:44 +00:00
|
|
|
inline void wxCriticalSection::Leave() { (void)m_mutex.Unlock(); }
|
2002-06-06 18:31:59 +00:00
|
|
|
#endif // wxCRITSECT_IS_MUTEX
|
1999-01-17 22:39:58 +00:00
|
|
|
|
2002-06-07 21:46:44 +00:00
|
|
|
#undef wxCRITSECT_INLINE
|
|
|
|
#undef wxCRITSECT_IS_MUTEX
|
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
// wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is
|
2004-07-04 11:19:47 +00:00
|
|
|
// to mutexes
|
2003-07-04 13:00:07 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxCriticalSectionLocker
|
1999-01-05 03:04:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2002-06-06 18:31:59 +00:00
|
|
|
wxCriticalSectionLocker(wxCriticalSection& cs)
|
|
|
|
: m_critsect(cs)
|
|
|
|
{
|
|
|
|
m_critsect.Enter();
|
|
|
|
}
|
1999-01-05 03:04:48 +00:00
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
~wxCriticalSectionLocker()
|
|
|
|
{
|
|
|
|
m_critsect.Leave();
|
|
|
|
}
|
1999-01-10 00:04:07 +00:00
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
private:
|
1999-01-09 00:28:27 +00:00
|
|
|
wxCriticalSection& m_critsect;
|
2002-06-06 18:31:59 +00:00
|
|
|
|
2009-02-08 11:45:59 +00:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxCriticalSectionLocker);
|
1999-01-05 03:04:48 +00:00
|
|
|
};
|
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2002-04-02 13:15:16 +00:00
|
|
|
// wxCondition models a POSIX condition variable which allows one (or more)
|
|
|
|
// thread(s) to wait until some condition is fulfilled
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-02 22:24:41 +00:00
|
|
|
|
2003-07-04 13:00:07 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxCondition
|
1999-01-02 22:24:41 +00:00
|
|
|
{
|
1998-05-21 15:02:02 +00:00
|
|
|
public:
|
2002-06-06 18:31:59 +00:00
|
|
|
// Each wxCondition object is associated with a (single) wxMutex object.
|
|
|
|
// The mutex object MUST be locked before calling Wait()
|
2002-04-02 15:37:57 +00:00
|
|
|
wxCondition(wxMutex& mutex);
|
2002-04-02 13:15:16 +00:00
|
|
|
|
|
|
|
// dtor is not virtual, don't use this class polymorphically
|
1999-11-29 23:05:23 +00:00
|
|
|
~wxCondition();
|
|
|
|
|
2004-04-30 21:31:52 +00:00
|
|
|
// return true if the condition has been created successfully
|
2002-06-06 18:31:59 +00:00
|
|
|
bool IsOk() const;
|
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
// NB: the associated mutex MUST be locked beforehand by the calling thread
|
2004-09-23 18:20:56 +00:00
|
|
|
//
|
2002-04-02 13:15:16 +00:00
|
|
|
// it atomically releases the lock on the associated mutex
|
|
|
|
// and starts waiting to be woken up by a Signal()/Broadcast()
|
|
|
|
// once its signaled, then it will wait until it can reacquire
|
|
|
|
// the lock on the associated mutex object, before returning.
|
2002-06-06 18:31:59 +00:00
|
|
|
wxCondError Wait();
|
1999-11-29 23:05:23 +00:00
|
|
|
|
2013-09-06 17:09:05 +00:00
|
|
|
// std::condition_variable-like variant that evaluates the associated condition
|
|
|
|
template<typename Functor>
|
|
|
|
wxCondError Wait(const Functor& predicate)
|
|
|
|
{
|
|
|
|
while ( !predicate() )
|
|
|
|
{
|
|
|
|
wxCondError e = Wait();
|
|
|
|
if ( e != wxCOND_NO_ERROR )
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
return wxCOND_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
// exactly as Wait() except that it may also return if the specified
|
2005-11-19 01:07:56 +00:00
|
|
|
// timeout elapses even if the condition hasn't been signalled: in this
|
2013-09-06 17:09:01 +00:00
|
|
|
// case, the return value is wxCOND_TIMEOUT, otherwise (i.e. in case of a
|
|
|
|
// normal return) it is wxCOND_NO_ERROR.
|
2004-09-23 18:20:56 +00:00
|
|
|
//
|
2005-11-19 01:07:56 +00:00
|
|
|
// the timeout parameter specifies an interval that needs to be waited for
|
|
|
|
// in milliseconds
|
2002-06-06 18:31:59 +00:00
|
|
|
wxCondError WaitTimeout(unsigned long milliseconds);
|
2002-04-02 13:15:16 +00:00
|
|
|
|
|
|
|
// NB: the associated mutex may or may not be locked by the calling thread
|
|
|
|
//
|
|
|
|
// this method unblocks one thread if any are blocking on the condition.
|
|
|
|
// if no thread is blocking in Wait(), then the signal is NOT remembered
|
2005-11-19 01:07:56 +00:00
|
|
|
// The thread which was blocking on Wait() will then reacquire the lock
|
2002-04-02 13:15:16 +00:00
|
|
|
// on the associated mutex object before returning
|
2002-06-06 18:31:59 +00:00
|
|
|
wxCondError Signal();
|
1998-12-27 00:54:53 +00:00
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
// NB: the associated mutex may or may not be locked by the calling thread
|
|
|
|
//
|
|
|
|
// this method unblocks all threads if any are blocking on the condition.
|
|
|
|
// if no thread is blocking in Wait(), then the signal is NOT remembered
|
2005-11-19 01:07:56 +00:00
|
|
|
// The threads which were blocking on Wait() will then reacquire the lock
|
2002-04-02 13:15:16 +00:00
|
|
|
// on the associated mutex object before returning.
|
2002-06-06 18:31:59 +00:00
|
|
|
wxCondError Broadcast();
|
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
private:
|
1999-11-29 23:05:23 +00:00
|
|
|
wxConditionInternal *m_internal;
|
2002-06-06 18:31:59 +00:00
|
|
|
|
2009-02-08 11:45:59 +00:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxCondition);
|
1998-05-21 15:02:02 +00:00
|
|
|
};
|
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2002-04-02 13:15:16 +00:00
|
|
|
// wxSemaphore: a counter limiting the number of threads concurrently accessing
|
|
|
|
// a shared resource
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2003-07-04 13:00:07 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxSemaphore
|
2002-04-02 13:15:16 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// specifying a maxcount of 0 actually makes wxSemaphore behave as if there
|
|
|
|
// is no upper limit, if maxcount is 1 the semaphore behaves as a mutex
|
|
|
|
wxSemaphore( int initialcount = 0, int maxcount = 0 );
|
|
|
|
|
|
|
|
// dtor is not virtual, don't use this class polymorphically
|
|
|
|
~wxSemaphore();
|
|
|
|
|
2004-04-30 21:31:52 +00:00
|
|
|
// return true if the semaphore has been created successfully
|
2002-06-06 18:31:59 +00:00
|
|
|
bool IsOk() const;
|
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
// wait indefinitely, until the semaphore count goes beyond 0
|
|
|
|
// and then decrement it and return (this method might have been called
|
|
|
|
// Acquire())
|
2002-06-06 18:31:59 +00:00
|
|
|
wxSemaError Wait();
|
2002-04-02 13:15:16 +00:00
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
// same as Wait(), but does not block, returns wxSEMA_NO_ERROR if
|
|
|
|
// successful and wxSEMA_BUSY if the count is currently zero
|
|
|
|
wxSemaError TryWait();
|
2002-04-02 13:15:16 +00:00
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
// same as Wait(), but as a timeout limit, returns wxSEMA_NO_ERROR if the
|
2005-11-19 01:07:56 +00:00
|
|
|
// semaphore was acquired and wxSEMA_TIMEOUT if the timeout has elapsed
|
2002-06-06 18:31:59 +00:00
|
|
|
wxSemaError WaitTimeout(unsigned long milliseconds);
|
2002-04-02 13:15:16 +00:00
|
|
|
|
|
|
|
// increments the semaphore count and signals one of the waiting threads
|
2002-06-06 18:31:59 +00:00
|
|
|
wxSemaError Post();
|
2002-04-02 13:15:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
wxSemaphoreInternal *m_internal;
|
2002-06-06 18:31:59 +00:00
|
|
|
|
2009-02-08 11:45:59 +00:00
|
|
|
wxDECLARE_NO_COPY_CLASS(wxSemaphore);
|
2002-04-02 13:15:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2004-07-04 11:19:47 +00:00
|
|
|
// wxThread: class encapsulating a thread of execution
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-02 22:24:41 +00:00
|
|
|
|
1999-11-27 22:57:06 +00:00
|
|
|
// there are two different kinds of threads: joinable and detached (default)
|
|
|
|
// ones. Only joinable threads can return a return code and only detached
|
|
|
|
// threads auto-delete themselves - the user should delete the joinable
|
|
|
|
// threads manually.
|
1999-01-17 22:39:58 +00:00
|
|
|
|
|
|
|
// NB: in the function descriptions the words "this thread" mean the thread
|
|
|
|
// created by the wxThread object while "main thread" is the thread created
|
|
|
|
// during the process initialization (a.k.a. the GUI thread)
|
1999-11-27 22:57:06 +00:00
|
|
|
|
2002-01-29 22:49:13 +00:00
|
|
|
// On VMS thread pointers are 64 bits (also needed for other systems???
|
|
|
|
#ifdef __VMS
|
|
|
|
typedef unsigned long long wxThreadIdType;
|
|
|
|
#else
|
|
|
|
typedef unsigned long wxThreadIdType;
|
|
|
|
#endif
|
|
|
|
|
2003-07-04 13:00:07 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxThread
|
1999-01-02 22:24:41 +00:00
|
|
|
{
|
1998-05-21 15:02:02 +00:00
|
|
|
public:
|
1999-01-17 22:39:58 +00:00
|
|
|
// the return type for the thread function
|
|
|
|
typedef void *ExitCode;
|
|
|
|
|
|
|
|
// static functions
|
|
|
|
// Returns the wxThread object for the calling thread. NULL is returned
|
|
|
|
// if the caller is the main thread (but it's recommended to use
|
|
|
|
// IsMain() and only call This() for threads other than the main one
|
|
|
|
// because NULL is also returned on error). If the thread wasn't
|
|
|
|
// created with wxThread class, the returned value is undefined.
|
|
|
|
static wxThread *This();
|
|
|
|
|
|
|
|
// Returns true if current thread is the main thread.
|
2009-07-11 20:46:55 +00:00
|
|
|
//
|
|
|
|
// Notice that it also returns true if main thread id hadn't been
|
|
|
|
// initialized yet on the assumption that it's too early in wx startup
|
|
|
|
// process for any other threads to have been created in this case.
|
|
|
|
static bool IsMain()
|
|
|
|
{
|
|
|
|
return !ms_idMainThread || GetCurrentId() == ms_idMainThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the main thread id
|
|
|
|
static wxThreadIdType GetMainId() { return ms_idMainThread; }
|
1999-01-17 22:39:58 +00:00
|
|
|
|
2005-11-19 01:07:56 +00:00
|
|
|
// Release the rest of our time slice letting the other threads run
|
1999-01-17 22:39:58 +00:00
|
|
|
static void Yield();
|
|
|
|
|
|
|
|
// Sleep during the specified period of time in milliseconds
|
|
|
|
//
|
2008-04-12 02:28:46 +00:00
|
|
|
// This is the same as wxMilliSleep().
|
1999-01-17 22:39:58 +00:00
|
|
|
static void Sleep(unsigned long milliseconds);
|
|
|
|
|
1999-12-06 12:31:04 +00:00
|
|
|
// get the number of system CPUs - useful with SetConcurrency()
|
|
|
|
// (the "best" value for it is usually number of CPUs + 1)
|
|
|
|
//
|
|
|
|
// Returns -1 if unknown, number of CPUs otherwise
|
|
|
|
static int GetCPUCount();
|
|
|
|
|
2002-01-16 06:28:11 +00:00
|
|
|
// Get the platform specific thread ID and return as a long. This
|
|
|
|
// can be used to uniquely identify threads, even if they are not
|
|
|
|
// wxThreads. This is used by wxPython.
|
2009-07-11 20:46:55 +00:00
|
|
|
static wxThreadIdType GetCurrentId();
|
2002-01-29 22:49:13 +00:00
|
|
|
|
1999-12-06 12:31:04 +00:00
|
|
|
// sets the concurrency level: this is, roughly, the number of threads
|
|
|
|
// the system tries to schedule to run in parallel. 0 means the
|
|
|
|
// default value (usually acceptable, but may not yield the best
|
|
|
|
// performance for this process)
|
|
|
|
//
|
2004-04-30 21:31:52 +00:00
|
|
|
// Returns true on success, false otherwise (if not implemented, for
|
1999-12-06 12:31:04 +00:00
|
|
|
// example)
|
|
|
|
static bool SetConcurrency(size_t level);
|
|
|
|
|
1999-11-27 22:57:06 +00:00
|
|
|
// constructor only creates the C++ thread object and doesn't create (or
|
|
|
|
// start) the real thread
|
|
|
|
wxThread(wxThreadKind kind = wxTHREAD_DETACHED);
|
|
|
|
|
|
|
|
// functions that change the thread state: all these can only be called
|
|
|
|
// from _another_ thread (typically the thread that created this one, e.g.
|
|
|
|
// the main thread), not from the thread itself
|
1999-01-17 22:39:58 +00:00
|
|
|
|
2002-01-03 05:53:39 +00:00
|
|
|
// create a new thread and optionally set the stack size on
|
|
|
|
// platforms that support that - call Run() to start it
|
|
|
|
wxThreadError Create(unsigned int stackSize = 0);
|
1999-01-17 22:39:58 +00:00
|
|
|
|
1999-11-27 22:57:06 +00:00
|
|
|
// starts execution of the thread - from the moment Run() is called
|
|
|
|
// the execution of wxThread::Entry() may start at any moment, caller
|
1999-01-17 22:39:58 +00:00
|
|
|
// shouldn't suppose that it starts after (or before) Run() returns.
|
|
|
|
wxThreadError Run();
|
|
|
|
|
1999-11-27 22:57:06 +00:00
|
|
|
// stops the thread if it's running and deletes the wxThread object if
|
|
|
|
// this is a detached thread freeing its memory - otherwise (for
|
|
|
|
// joinable threads) you still need to delete wxThread object
|
|
|
|
// yourself.
|
|
|
|
//
|
|
|
|
// this function only works if the thread calls TestDestroy()
|
|
|
|
// periodically - the thread will only be deleted the next time it
|
|
|
|
// does it!
|
|
|
|
//
|
|
|
|
// will fill the rc pointer with the thread exit code if it's !NULL
|
2011-03-14 11:54:32 +00:00
|
|
|
wxThreadError Delete(ExitCode *rc = NULL,
|
|
|
|
wxThreadWait waitMode = wxTHREAD_WAIT_DEFAULT);
|
1999-11-27 22:57:06 +00:00
|
|
|
|
|
|
|
// waits for a joinable thread to finish and returns its exit code
|
|
|
|
//
|
|
|
|
// Returns (ExitCode)-1 on error (for example, if the thread is not
|
|
|
|
// joinable)
|
2011-03-14 11:54:32 +00:00
|
|
|
ExitCode Wait(wxThreadWait waitMode = wxTHREAD_WAIT_DEFAULT);
|
1999-01-17 22:39:58 +00:00
|
|
|
|
|
|
|
// kills the thread without giving it any chance to clean up - should
|
2005-11-19 01:07:56 +00:00
|
|
|
// not be used under normal circumstances, use Delete() instead.
|
|
|
|
// It is a dangerous function that should only be used in the most
|
|
|
|
// extreme cases!
|
1999-11-27 22:57:06 +00:00
|
|
|
//
|
|
|
|
// The wxThread object is deleted by Kill() if the thread is
|
|
|
|
// detachable, but you still have to delete it manually for joinable
|
|
|
|
// threads.
|
1999-01-17 22:39:58 +00:00
|
|
|
wxThreadError Kill();
|
|
|
|
|
1999-11-27 22:57:06 +00:00
|
|
|
// pause a running thread: as Delete(), this only works if the thread
|
|
|
|
// calls TestDestroy() regularly
|
1999-01-17 22:39:58 +00:00
|
|
|
wxThreadError Pause();
|
|
|
|
|
|
|
|
// resume a paused thread
|
|
|
|
wxThreadError Resume();
|
|
|
|
|
|
|
|
// priority
|
2013-01-20 02:10:07 +00:00
|
|
|
// Sets the priority to "prio" which must be in 0..100 range (see
|
|
|
|
// also wxPRIORITY_XXX constants).
|
1999-01-17 22:39:58 +00:00
|
|
|
//
|
2015-01-27 11:03:08 +00:00
|
|
|
// NB: under MSW the priority can only be set after the thread is
|
|
|
|
// created (but possibly before it is launched)
|
1999-01-17 22:39:58 +00:00
|
|
|
void SetPriority(unsigned int prio);
|
|
|
|
|
|
|
|
// Get the current priority.
|
|
|
|
unsigned int GetPriority() const;
|
|
|
|
|
|
|
|
// thread status inquiries
|
|
|
|
// Returns true if the thread is alive: i.e. running or suspended
|
|
|
|
bool IsAlive() const;
|
|
|
|
// Returns true if the thread is running (not paused, not killed).
|
|
|
|
bool IsRunning() const;
|
|
|
|
// Returns true if the thread is suspended
|
1999-04-26 18:16:56 +00:00
|
|
|
bool IsPaused() const;
|
1999-01-17 22:39:58 +00:00
|
|
|
|
1999-11-27 22:57:06 +00:00
|
|
|
// is the thread of detached kind?
|
|
|
|
bool IsDetached() const { return m_isDetached; }
|
|
|
|
|
|
|
|
// Get the thread ID - a platform dependent number which uniquely
|
|
|
|
// identifies a thread inside a process
|
2002-01-29 22:49:13 +00:00
|
|
|
wxThreadIdType GetId() const;
|
2002-01-16 06:28:11 +00:00
|
|
|
|
2014-04-27 22:41:50 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
// Get the internal OS handle
|
|
|
|
WXHANDLE MSWGetHandle() const;
|
|
|
|
#endif // __WINDOWS__
|
|
|
|
|
2008-11-26 16:40:14 +00:00
|
|
|
wxThreadKind GetKind() const
|
|
|
|
{ return m_isDetached ? wxTHREAD_DETACHED : wxTHREAD_JOINABLE; }
|
|
|
|
|
2004-04-30 21:31:52 +00:00
|
|
|
// Returns true if the thread was asked to terminate: this function should
|
|
|
|
// be called by the thread from time to time, otherwise the main thread
|
|
|
|
// will be left forever in Delete()!
|
|
|
|
virtual bool TestDestroy();
|
|
|
|
|
1999-11-27 22:57:06 +00:00
|
|
|
// dtor is public, but the detached threads should never be deleted - use
|
|
|
|
// Delete() instead (or leave the thread terminate by itself)
|
|
|
|
virtual ~wxThread();
|
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
protected:
|
|
|
|
// exits from the current thread - can be called only from this thread
|
1999-11-27 22:57:06 +00:00
|
|
|
void Exit(ExitCode exitcode = 0);
|
1999-01-05 03:04:48 +00:00
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
// entry point for the thread - called by Run() and executes in the context
|
|
|
|
// of this thread.
|
|
|
|
virtual void *Entry() = 0;
|
1998-12-27 00:54:53 +00:00
|
|
|
|
2013-09-18 16:03:25 +00:00
|
|
|
// use this to call the Entry() virtual method
|
|
|
|
void *CallEntry();
|
2010-10-23 14:10:12 +00:00
|
|
|
|
|
|
|
// Callbacks which may be overridden by the derived class to perform some
|
|
|
|
// specific actions when the thread is deleted or killed. By default they
|
|
|
|
// do nothing.
|
|
|
|
|
|
|
|
// This one is called by Delete() before actually deleting the thread and
|
|
|
|
// is executed in the context of the thread that called Delete().
|
|
|
|
virtual void OnDelete() {}
|
|
|
|
|
|
|
|
// This one is called by Kill() before killing the thread and is executed
|
|
|
|
// in the context of the thread that called Kill().
|
|
|
|
virtual void OnKill() {}
|
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
private:
|
1999-01-17 22:39:58 +00:00
|
|
|
// no copy ctor/assignment operator
|
|
|
|
wxThread(const wxThread&);
|
|
|
|
wxThread& operator=(const wxThread&);
|
1998-05-21 15:02:02 +00:00
|
|
|
|
2008-11-26 17:24:00 +00:00
|
|
|
// called when the thread exits - in the context of this thread
|
|
|
|
//
|
|
|
|
// NB: this function will not be called if the thread is Kill()ed
|
|
|
|
virtual void OnExit() { }
|
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
friend class wxThreadInternal;
|
2009-07-11 20:46:55 +00:00
|
|
|
friend class wxThreadModule;
|
|
|
|
|
|
|
|
|
|
|
|
// the main thread identifier, should be set on startup
|
|
|
|
static wxThreadIdType ms_idMainThread;
|
1999-01-17 22:39:58 +00:00
|
|
|
|
|
|
|
// the (platform-dependent) thread class implementation
|
1999-11-29 23:05:23 +00:00
|
|
|
wxThreadInternal *m_internal;
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1999-01-17 22:39:58 +00:00
|
|
|
// protects access to any methods of wxThreadInternal object
|
|
|
|
wxCriticalSection m_critsect;
|
1999-11-27 22:57:06 +00:00
|
|
|
|
|
|
|
// true if the thread is detached, false if it is joinable
|
|
|
|
bool m_isDetached;
|
1998-05-21 15:02:02 +00:00
|
|
|
};
|
|
|
|
|
2003-07-10 12:36:05 +00:00
|
|
|
// wxThreadHelperThread class
|
|
|
|
// --------------------------
|
|
|
|
|
|
|
|
class WXDLLIMPEXP_BASE wxThreadHelperThread : public wxThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// constructor only creates the C++ thread object and doesn't create (or
|
|
|
|
// start) the real thread
|
2007-10-20 14:49:10 +00:00
|
|
|
wxThreadHelperThread(wxThreadHelper& owner, wxThreadKind kind)
|
|
|
|
: wxThread(kind), m_owner(owner)
|
2003-07-10 12:36:05 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// entry point for the thread -- calls Entry() in owner.
|
2014-03-30 00:02:23 +00:00
|
|
|
virtual void *Entry() wxOVERRIDE;
|
2003-07-10 12:36:05 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// the owner of the thread
|
|
|
|
wxThreadHelper& m_owner;
|
|
|
|
|
|
|
|
// no copy ctor/assignment operator
|
|
|
|
wxThreadHelperThread(const wxThreadHelperThread&);
|
|
|
|
wxThreadHelperThread& operator=(const wxThreadHelperThread&);
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxThreadHelper: this class implements the threading logic to run a
|
|
|
|
// background task in another object (such as a window). It is a mix-in: just
|
|
|
|
// derive from it to implement a threading background task in your class.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class WXDLLIMPEXP_BASE wxThreadHelper
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
void KillThread()
|
|
|
|
{
|
2008-11-28 17:56:10 +00:00
|
|
|
// If wxThreadHelperThread is detached and is about to finish, it will
|
|
|
|
// set m_thread to NULL so don't delete it then.
|
|
|
|
// But if KillThread is called before wxThreadHelperThread (in detached mode)
|
|
|
|
// sets it to NULL, then the thread object still exists and can be killed
|
2007-10-20 14:49:10 +00:00
|
|
|
wxCriticalSectionLocker locker(m_critSection);
|
2008-11-26 17:24:00 +00:00
|
|
|
|
2003-07-10 12:36:05 +00:00
|
|
|
if ( m_thread )
|
|
|
|
{
|
|
|
|
m_thread->Kill();
|
2008-11-26 17:24:00 +00:00
|
|
|
|
2007-10-20 14:49:10 +00:00
|
|
|
if ( m_kind == wxTHREAD_JOINABLE )
|
|
|
|
delete m_thread;
|
2008-11-26 17:24:00 +00:00
|
|
|
|
2007-10-20 14:49:10 +00:00
|
|
|
m_thread = NULL;
|
2003-07-10 12:36:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
// constructor only initializes m_thread to NULL
|
2007-10-20 14:49:10 +00:00
|
|
|
wxThreadHelper(wxThreadKind kind = wxTHREAD_JOINABLE)
|
|
|
|
: m_thread(NULL), m_kind(kind) { }
|
2003-07-10 12:36:05 +00:00
|
|
|
|
|
|
|
// destructor deletes m_thread
|
|
|
|
virtual ~wxThreadHelper() { KillThread(); }
|
|
|
|
|
2008-11-28 17:43:34 +00:00
|
|
|
#if WXWIN_COMPATIBILITY_2_8
|
|
|
|
wxDEPRECATED( wxThreadError Create(unsigned int stackSize = 0) );
|
|
|
|
#endif
|
|
|
|
|
2003-07-10 12:36:05 +00:00
|
|
|
// create a new thread (and optionally set the stack size on platforms that
|
|
|
|
// support/need that), call Run() to start it
|
2008-11-28 17:43:34 +00:00
|
|
|
wxThreadError CreateThread(wxThreadKind kind = wxTHREAD_JOINABLE,
|
|
|
|
unsigned int stackSize = 0)
|
2003-07-10 12:36:05 +00:00
|
|
|
{
|
|
|
|
KillThread();
|
|
|
|
|
2008-11-28 17:43:34 +00:00
|
|
|
m_kind = kind;
|
2007-10-20 14:49:10 +00:00
|
|
|
m_thread = new wxThreadHelperThread(*this, m_kind);
|
2003-07-10 12:36:05 +00:00
|
|
|
|
|
|
|
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()
|
2007-10-20 14:49:10 +00:00
|
|
|
wxThread *GetThread() const
|
|
|
|
{
|
|
|
|
wxCriticalSectionLocker locker((wxCriticalSection&)m_critSection);
|
2008-11-26 17:24:00 +00:00
|
|
|
|
2007-10-20 14:49:10 +00:00
|
|
|
wxThread* thread = m_thread;
|
2008-11-26 17:24:00 +00:00
|
|
|
|
2007-10-20 14:49:10 +00:00
|
|
|
return thread;
|
|
|
|
}
|
2003-07-10 12:36:05 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
wxThread *m_thread;
|
2007-10-20 14:49:10 +00:00
|
|
|
wxThreadKind m_kind;
|
|
|
|
wxCriticalSection m_critSection; // To guard the m_thread variable
|
2008-11-26 17:24:00 +00:00
|
|
|
|
2007-10-20 14:49:10 +00:00
|
|
|
friend class wxThreadHelperThread;
|
2003-07-10 12:36:05 +00:00
|
|
|
};
|
|
|
|
|
2008-11-28 17:43:34 +00:00
|
|
|
#if WXWIN_COMPATIBILITY_2_8
|
|
|
|
inline wxThreadError wxThreadHelper::Create(unsigned int stackSize)
|
|
|
|
{ return CreateThread(m_kind, stackSize); }
|
|
|
|
#endif
|
|
|
|
|
2003-07-10 12:36:05 +00:00
|
|
|
// call Entry() in owner, put it down here to avoid circular declarations
|
|
|
|
inline void *wxThreadHelperThread::Entry()
|
|
|
|
{
|
2007-10-20 14:49:10 +00:00
|
|
|
void * const result = m_owner.Entry();
|
2008-11-26 17:24:00 +00:00
|
|
|
|
2007-10-20 14:49:10 +00:00
|
|
|
wxCriticalSectionLocker locker(m_owner.m_critSection);
|
2008-11-26 17:24:00 +00:00
|
|
|
|
2007-10-20 14:49:10 +00:00
|
|
|
// 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;
|
2008-11-26 17:24:00 +00:00
|
|
|
|
2007-10-20 14:49:10 +00:00
|
|
|
return result;
|
2003-07-10 12:36:05 +00:00
|
|
|
}
|
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-02 22:24:41 +00:00
|
|
|
// Automatic initialization
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
// GUI mutex handling.
|
2003-07-04 13:00:07 +00:00
|
|
|
void WXDLLIMPEXP_BASE wxMutexGuiEnter();
|
|
|
|
void WXDLLIMPEXP_BASE wxMutexGuiLeave();
|
1999-01-02 22:24:41 +00:00
|
|
|
|
1999-10-18 15:14:52 +00:00
|
|
|
// macros for entering/leaving critical sections which may be used without
|
|
|
|
// having to take them inside "#if wxUSE_THREADS"
|
1999-11-27 22:57:06 +00:00
|
|
|
#define wxENTER_CRIT_SECT(cs) (cs).Enter()
|
|
|
|
#define wxLEAVE_CRIT_SECT(cs) (cs).Leave()
|
1999-12-04 22:34:54 +00:00
|
|
|
#define wxCRIT_SECT_DECLARE(cs) static wxCriticalSection cs
|
2003-06-30 01:18:10 +00:00
|
|
|
#define wxCRIT_SECT_DECLARE_MEMBER(cs) wxCriticalSection cs
|
1999-11-27 22:57:06 +00:00
|
|
|
#define wxCRIT_SECT_LOCKER(name, cs) wxCriticalSectionLocker name(cs)
|
1999-10-18 15:14:52 +00:00
|
|
|
|
2003-06-29 11:51:17 +00:00
|
|
|
// 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(); }
|
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
#else // !wxUSE_THREADS
|
1999-01-02 22:24:41 +00:00
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
// no thread support
|
2007-06-21 22:29:07 +00:00
|
|
|
inline void wxMutexGuiEnter() { }
|
|
|
|
inline void wxMutexGuiLeave() { }
|
1999-01-02 22:24:41 +00:00
|
|
|
|
1999-10-18 15:14:52 +00:00
|
|
|
// macros for entering/leaving critical sections which may be used without
|
|
|
|
// having to take them inside "#if wxUSE_THREADS"
|
2014-05-16 13:01:06 +00:00
|
|
|
// (the implementation uses dummy structs to force semicolon after the macro)
|
2007-11-22 15:17:37 +00:00
|
|
|
#define wxENTER_CRIT_SECT(cs) do {} while (0)
|
|
|
|
#define wxLEAVE_CRIT_SECT(cs) do {} while (0)
|
|
|
|
#define wxCRIT_SECT_DECLARE(cs) struct wxDummyCS##cs
|
2009-03-14 23:26:39 +00:00
|
|
|
#define wxCRIT_SECT_DECLARE_MEMBER(cs) struct wxDummyCSMember##cs { }
|
2007-11-22 15:17:37 +00:00
|
|
|
#define wxCRIT_SECT_LOCKER(name, cs) struct wxDummyCSLocker##name
|
1999-10-18 15:14:52 +00:00
|
|
|
|
2003-06-29 11:51:17 +00:00
|
|
|
// if there is only one thread, it is always the main one
|
|
|
|
inline bool wxIsMainThread() { return true; }
|
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
#endif // wxUSE_THREADS/!wxUSE_THREADS
|
1998-05-21 15:02:02 +00:00
|
|
|
|
2002-06-07 00:34:56 +00:00
|
|
|
// 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)
|
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
// automatically lock GUI mutex in ctor and unlock it in dtor
|
2003-07-04 13:00:07 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxMutexGuiLocker
|
1999-01-09 00:28:27 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxMutexGuiLocker() { wxMutexGuiEnter(); }
|
|
|
|
~wxMutexGuiLocker() { wxMutexGuiLeave(); }
|
|
|
|
};
|
|
|
|
|
1999-01-11 15:51:45 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// implementation only until the end of file
|
|
|
|
// -----------------------------------------------------------------------------
|
1999-11-09 14:32:40 +00:00
|
|
|
|
1999-01-24 14:57:09 +00:00
|
|
|
#if wxUSE_THREADS
|
1999-11-09 14:32:40 +00:00
|
|
|
|
2014-05-15 23:56:58 +00:00
|
|
|
#if defined(__WINDOWS__) || defined(__DARWIN__)
|
1999-01-11 15:51:45 +00:00
|
|
|
// 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
|
2003-07-04 13:00:07 +00:00
|
|
|
extern void WXDLLIMPEXP_BASE wxMutexGuiLeaveOrEnter();
|
1999-01-11 15:51:45 +00:00
|
|
|
|
2004-04-30 21:31:52 +00:00
|
|
|
// returns true if the main thread has GUI lock
|
2003-07-04 13:00:07 +00:00
|
|
|
extern bool WXDLLIMPEXP_BASE wxGuiOwnedByMainThread();
|
1999-01-11 15:51:45 +00:00
|
|
|
|
|
|
|
// wakes up the main thread if it's sleeping inside ::GetMessage()
|
2003-07-04 13:00:07 +00:00
|
|
|
extern void WXDLLIMPEXP_BASE wxWakeUpMainThread();
|
1999-01-17 22:39:58 +00:00
|
|
|
|
2012-07-23 15:03:42 +00:00
|
|
|
#ifndef __DARWIN__
|
2004-04-30 21:31:52 +00:00
|
|
|
// return true if the main thread is waiting for some other to terminate:
|
1999-11-09 15:24:52 +00:00
|
|
|
// wxApp then should block all "dangerous" messages
|
2003-07-04 13:00:07 +00:00
|
|
|
extern bool WXDLLIMPEXP_BASE wxIsWaitingForThread();
|
2010-09-06 13:50:12 +00:00
|
|
|
#endif
|
2008-08-01 06:40:16 +00:00
|
|
|
#endif // MSW, OS/2
|
1999-11-09 14:32:40 +00:00
|
|
|
|
1999-01-11 15:51:45 +00:00
|
|
|
#endif // wxUSE_THREADS
|
|
|
|
|
2002-06-06 18:31:59 +00:00
|
|
|
#endif // _WX_THREAD_H_
|