1998-05-21 15:02:02 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: thread.h
|
|
|
|
// Purpose: Thread API
|
|
|
|
// Author: Guilhem Lavaux
|
|
|
|
// Modified by:
|
|
|
|
// Created: 04/13/98
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Guilhem Lavaux
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-08-18 17:22:15 +00:00
|
|
|
#ifndef __THREADH__
|
|
|
|
#define __THREADH__
|
1998-05-21 15:02:02 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma interface "thread.h"
|
|
|
|
#endif
|
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 15:02:02 +00:00
|
|
|
#include "wx/setup.h"
|
1999-01-05 03:04:48 +00:00
|
|
|
|
|
|
|
#if wxUSE_THREADS
|
1999-01-02 22:24:41 +00:00
|
|
|
#include "wx/module.h"
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-01-02 22:24:41 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
1998-12-27 00:54:53 +00:00
|
|
|
wxMUTEX_NO_ERROR = 0,
|
|
|
|
wxMUTEX_DEAD_LOCK, // Mutex has been already locked by THE CALLING thread
|
|
|
|
wxMUTEX_BUSY, // Mutex has been already locked by ONE thread
|
|
|
|
wxMUTEX_UNLOCKED,
|
|
|
|
wxMUTEX_MISC_ERROR
|
1998-05-21 15:02:02 +00:00
|
|
|
} wxMutexError;
|
|
|
|
|
1999-01-02 22:24:41 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
1998-12-27 00:54:53 +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_MISC_ERROR // Some other error
|
1998-05-21 15:02:02 +00:00
|
|
|
} wxThreadError;
|
|
|
|
|
1999-01-02 22:24:41 +00:00
|
|
|
/* defines the interval of priority. */
|
1998-12-27 00:54:53 +00:00
|
|
|
#define WXTHREAD_MIN_PRIORITY 0
|
1998-05-21 15:02:02 +00:00
|
|
|
#define WXTHREAD_DEFAULT_PRIORITY 50
|
1998-12-27 00:54:53 +00:00
|
|
|
#define WXTHREAD_MAX_PRIORITY 100
|
1998-05-21 15:02:02 +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
|
1998-08-18 17:22:15 +00:00
|
|
|
class WXDLLEXPORT wxMutexInternal;
|
1999-01-02 22:24:41 +00:00
|
|
|
class WXDLLEXPORT wxMutex
|
|
|
|
{
|
1998-05-21 15:02:02 +00:00
|
|
|
public:
|
1999-01-10 00:04:07 +00:00
|
|
|
// constructor & destructor
|
|
|
|
wxMutex();
|
|
|
|
~wxMutex();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1999-01-10 00:04:07 +00:00
|
|
|
// Lock the mutex.
|
|
|
|
wxMutexError Lock();
|
|
|
|
// Try to lock the mutex: if it can't, returns immediately with an error.
|
|
|
|
wxMutexError TryLock();
|
|
|
|
// Unlock the mutex.
|
|
|
|
wxMutexError Unlock();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1999-01-10 00:04:07 +00:00
|
|
|
// Returns true if the mutex is locked.
|
|
|
|
bool IsLocked() const { return (m_locked > 0); }
|
1998-12-27 00:54:53 +00:00
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
protected:
|
1999-01-10 00:04:07 +00:00
|
|
|
friend class wxCondition;
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1999-01-10 00:04:07 +00:00
|
|
|
// no assignment operator nor copy ctor
|
|
|
|
wxMutex(const wxMutex&);
|
|
|
|
wxMutex& operator=(const wxMutex&);
|
|
|
|
|
|
|
|
int m_locked;
|
|
|
|
wxMutexInternal *p_internal;
|
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
|
|
|
|
class WXDLLEXPORT wxMutexLocker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// lock the mutex in the ctor
|
|
|
|
wxMutexLocker(wxMutex *mutex)
|
|
|
|
{ m_isOk = mutex && ((m_mutex = mutex)->Lock() == wxMUTEX_NO_ERROR); }
|
|
|
|
|
|
|
|
// returns TRUE if mutex was successfully locked in ctor
|
|
|
|
bool IsOk() const { return m_isOk; }
|
|
|
|
|
|
|
|
// unlock the mutex in dtor
|
|
|
|
~wxMutexLocker() { if ( IsOk() ) m_mutex->Unlock(); }
|
|
|
|
|
|
|
|
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;
|
|
|
|
wxMutex *m_mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// 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
|
|
|
|
// mutexes
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// you should consider wxCriticalSectionLocker whenever possible instead of
|
|
|
|
// directly working with wxCriticalSection class - it is safer
|
1999-01-11 15:51:45 +00:00
|
|
|
#ifdef __WXMSW__
|
|
|
|
class WXDLLEXPORT wxCriticalSectionInternal;
|
|
|
|
#define WXCRITICAL_INLINE
|
|
|
|
#else // !MSW
|
|
|
|
#define WXCRITICAL_INLINE inline
|
|
|
|
#endif // MSW/!MSW
|
1999-01-05 03:04:48 +00:00
|
|
|
class WXDLLEXPORT wxCriticalSection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor & dtor
|
1999-01-11 15:51:45 +00:00
|
|
|
WXCRITICAL_INLINE wxCriticalSection();
|
|
|
|
WXCRITICAL_INLINE ~wxCriticalSection();
|
1999-01-05 03:04:48 +00:00
|
|
|
|
|
|
|
// enter the section (the same as locking a mutex)
|
1999-01-11 15:51:45 +00:00
|
|
|
void WXCRITICAL_INLINE Enter();
|
1999-01-05 03:04:48 +00:00
|
|
|
// leave the critical section (same as unlocking a mutex)
|
1999-01-11 15:51:45 +00:00
|
|
|
void WXCRITICAL_INLINE Leave();
|
1999-01-05 03:04:48 +00:00
|
|
|
|
|
|
|
private:
|
1999-01-10 00:04:07 +00:00
|
|
|
// no assignment operator nor copy ctor
|
|
|
|
wxCriticalSection(const wxCriticalSection&);
|
|
|
|
wxCriticalSection& operator=(const wxCriticalSection&);
|
|
|
|
|
1999-01-11 15:51:45 +00:00
|
|
|
#ifdef __WXMSW__
|
1999-01-05 03:04:48 +00:00
|
|
|
wxCriticalSectionInternal *m_critsect;
|
1999-01-11 15:51:45 +00:00
|
|
|
#else // !MSW
|
|
|
|
wxMutex m_mutex;
|
|
|
|
#endif // MSW/!MSW
|
1999-01-05 03:04:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is
|
|
|
|
// to th mutexes
|
|
|
|
class WXDLLEXPORT wxCriticalSectionLocker
|
|
|
|
{
|
|
|
|
public:
|
1999-01-09 00:28:27 +00:00
|
|
|
wxCriticalSectionLocker(wxCriticalSection& critsect) : m_critsect(critsect)
|
|
|
|
{ m_critsect.Enter(); }
|
1999-01-05 03:04:48 +00:00
|
|
|
~wxCriticalSectionLocker()
|
1999-01-09 00:28:27 +00:00
|
|
|
{ m_critsect.Leave(); }
|
1999-01-05 03:04:48 +00:00
|
|
|
|
|
|
|
private:
|
1999-01-10 00:04:07 +00:00
|
|
|
// no assignment operator nor copy ctor
|
|
|
|
wxCriticalSectionLocker(const wxCriticalSectionLocker&);
|
|
|
|
wxCriticalSectionLocker& operator=(const wxCriticalSectionLocker&);
|
|
|
|
|
1999-01-09 00:28:27 +00:00
|
|
|
wxCriticalSection& m_critsect;
|
1999-01-05 03:04:48 +00:00
|
|
|
};
|
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 15:02:02 +00:00
|
|
|
// Condition handler.
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-02 22:24:41 +00:00
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
class wxConditionInternal;
|
1999-01-02 22:24:41 +00:00
|
|
|
class WXDLLEXPORT wxCondition
|
|
|
|
{
|
1998-05-21 15:02:02 +00:00
|
|
|
public:
|
|
|
|
// constructor & destructor
|
1998-06-14 12:15:13 +00:00
|
|
|
wxCondition();
|
|
|
|
~wxCondition();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1998-06-14 12:15:13 +00:00
|
|
|
// Waits indefinitely.
|
1998-05-21 15:02:02 +00:00
|
|
|
void Wait(wxMutex& mutex);
|
|
|
|
// Waits until a signal is raised or the timeout is elapsed.
|
|
|
|
bool Wait(wxMutex& mutex, unsigned long sec, unsigned long nsec);
|
|
|
|
// Raises a signal: only one "Waiter" is released.
|
1998-06-14 12:15:13 +00:00
|
|
|
void Signal();
|
1998-05-21 15:02:02 +00:00
|
|
|
// Broadcasts to all "Waiters".
|
1998-06-14 12:15:13 +00:00
|
|
|
void Broadcast();
|
1998-12-27 00:54:53 +00:00
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
private:
|
|
|
|
wxConditionInternal *p_internal;
|
|
|
|
};
|
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 15:02:02 +00:00
|
|
|
// Thread management class
|
1998-12-27 00:54:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-02 22:24:41 +00:00
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
class wxThreadInternal;
|
1999-01-02 22:24:41 +00:00
|
|
|
class WXDLLEXPORT wxThread
|
|
|
|
{
|
1998-05-21 15:02:02 +00:00
|
|
|
public:
|
|
|
|
// constructor & destructor.
|
1998-06-14 12:15:13 +00:00
|
|
|
wxThread();
|
|
|
|
virtual ~wxThread();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
|
|
|
// Create a new thread, this method should check there is only one thread
|
|
|
|
// running by object.
|
1998-06-14 12:15:13 +00:00
|
|
|
wxThreadError Create();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1998-06-14 12:15:13 +00:00
|
|
|
// Destroys the thread immediately if the defer flag isn't true.
|
|
|
|
wxThreadError Destroy();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1998-08-18 17:22:15 +00:00
|
|
|
// Pause a running thread
|
|
|
|
wxThreadError Pause();
|
|
|
|
|
|
|
|
// Resume a paused thread
|
|
|
|
wxThreadError Resume();
|
|
|
|
|
1998-06-14 12:15:13 +00:00
|
|
|
// Switches on the defer flag.
|
1998-05-21 15:02:02 +00:00
|
|
|
void DeferDestroy(bool on);
|
|
|
|
|
|
|
|
// Waits for the termination of the thread.
|
1998-06-14 12:15:13 +00:00
|
|
|
void *Join();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
|
|
|
// Sets the priority to "prio". (Warning: The priority can only be set before
|
|
|
|
// the thread is created)
|
|
|
|
void SetPriority(int prio);
|
|
|
|
// Get the current priority.
|
1998-06-14 12:15:13 +00:00
|
|
|
int GetPriority() const;
|
1998-05-21 15:02:02 +00:00
|
|
|
|
|
|
|
// Get the thread ID
|
1998-06-14 12:15:13 +00:00
|
|
|
unsigned long GetID() const;
|
1998-05-21 15:02:02 +00:00
|
|
|
|
|
|
|
// Returns true if the thread is alive.
|
1998-06-14 12:15:13 +00:00
|
|
|
bool IsAlive() const;
|
1998-08-18 17:22:15 +00:00
|
|
|
// Returns true if the thread is running (not paused, not killed).
|
|
|
|
bool IsRunning() const;
|
1998-12-27 00:54:53 +00:00
|
|
|
// Returns true if the thread is suspended
|
|
|
|
bool IsPaused() const { return IsAlive() && !IsRunning(); }
|
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
// Returns true if current thread is the main thread (aka the GUI thread)
|
1998-06-14 12:15:13 +00:00
|
|
|
static bool IsMain();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
|
|
|
// Called when thread exits.
|
1998-06-14 12:15:13 +00:00
|
|
|
virtual void OnExit();
|
1998-08-18 17:22:15 +00:00
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
protected:
|
1999-01-05 03:04:48 +00:00
|
|
|
// Returns TRUE if the thread was asked to terminate
|
|
|
|
bool TestDestroy();
|
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
// Exits from the current thread.
|
|
|
|
void Exit(void *status = NULL);
|
1998-12-27 00:54:53 +00:00
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
private:
|
|
|
|
// Entry point for the thread.
|
1998-06-14 12:15:13 +00:00
|
|
|
virtual void *Entry() = 0;
|
1998-05-21 15:02:02 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class wxThreadInternal;
|
|
|
|
|
|
|
|
wxThreadInternal *p_internal;
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
void WXDLLEXPORT wxMutexGuiEnter();
|
|
|
|
void WXDLLEXPORT wxMutexGuiLeave();
|
1999-01-02 22:24:41 +00:00
|
|
|
|
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
|
|
|
|
inline void WXDLLEXPORT wxMutexGuiEnter() { }
|
|
|
|
inline void WXDLLEXPORT wxMutexGuiLeave() { }
|
1999-01-02 22:24:41 +00:00
|
|
|
|
1999-01-05 03:04:48 +00:00
|
|
|
#endif // wxUSE_THREADS
|
1998-05-21 15:02:02 +00:00
|
|
|
|
1999-01-09 00:28:27 +00:00
|
|
|
// automatically unlock GUI mutex in dtor
|
|
|
|
class WXDLLEXPORT wxMutexGuiLocker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxMutexGuiLocker() { wxMutexGuiEnter(); }
|
|
|
|
~wxMutexGuiLocker() { wxMutexGuiLeave(); }
|
|
|
|
};
|
|
|
|
|
1999-01-11 15:51:45 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// implementation only until the end of file
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
#ifdef wxUSE_THREADS
|
|
|
|
#ifdef __WXMSW__
|
|
|
|
// 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
|
|
|
|
void WXDLLEXPORT wxMutexGuiLeaveOrEnter();
|
|
|
|
|
|
|
|
// returns TRUE if the main thread has GUI lock
|
1999-01-11 16:05:33 +00:00
|
|
|
bool WXDLLEXPORT wxGuiOwnedByMainThread();
|
1999-01-11 15:51:45 +00:00
|
|
|
|
|
|
|
// wakes up the main thread if it's sleeping inside ::GetMessage()
|
1999-01-11 16:05:33 +00:00
|
|
|
void WXDLLEXPORT wxWakeUpMainThread();
|
1999-01-11 15:51:45 +00:00
|
|
|
#else // !MSW
|
|
|
|
// implement wxCriticalSection using mutexes
|
|
|
|
inline wxCriticalSection::wxCriticalSection() { }
|
|
|
|
inline wxCriticalSection::~wxCriticalSection() { }
|
|
|
|
|
|
|
|
inline void wxCriticalSection::Enter() { (void)m_mutex.Lock(); }
|
|
|
|
inline void wxCriticalSection::Leave() { (void)m_mutex.Unlock(); }
|
|
|
|
#endif // MSW/!MSW
|
|
|
|
#endif // wxUSE_THREADS
|
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
#endif // __THREADH__
|