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:
|
|
|
|
// constructor & destructor
|
1998-06-14 12:15:13 +00:00
|
|
|
wxMutex();
|
|
|
|
~wxMutex();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
|
|
|
// Lock the mutex.
|
1998-06-14 12:15:13 +00:00
|
|
|
wxMutexError Lock();
|
1998-05-21 15:02:02 +00:00
|
|
|
// Try to lock the mutex: if it can't, returns immediately with an error.
|
1998-06-14 12:15:13 +00:00
|
|
|
wxMutexError TryLock();
|
1998-05-21 15:02:02 +00:00
|
|
|
// Unlock the mutex.
|
1998-06-14 12:15:13 +00:00
|
|
|
wxMutexError Unlock();
|
1998-05-21 15:02:02 +00:00
|
|
|
|
|
|
|
// Returns true if the mutex is locked.
|
1998-06-14 12:15:13 +00:00
|
|
|
bool IsLocked() const { return (m_locked > 0); }
|
1998-12-27 00:54:53 +00:00
|
|
|
|
1998-05-21 15:02:02 +00:00
|
|
|
protected:
|
|
|
|
friend class wxCondition;
|
|
|
|
|
|
|
|
int m_locked;
|
|
|
|
wxMutexInternal *p_internal;
|
|
|
|
};
|
|
|
|
|
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:
|
|
|
|
bool m_isOk;
|
|
|
|
wxMutex *m_mutex;
|
|
|
|
};
|
|
|
|
|
1999-01-06 21:09:17 +00:00
|
|
|
#ifdef __WXMSW__
|
|
|
|
|
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
|
|
|
|
// mutexes
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// you should consider wxCriticalSectionLocker whenever possible instead of
|
|
|
|
// directly working with wxCriticalSection class - it is safer
|
|
|
|
class WXDLLEXPORT wxCriticalSectionInternal;
|
|
|
|
class WXDLLEXPORT wxCriticalSection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor & dtor
|
|
|
|
wxCriticalSection();
|
|
|
|
~wxCriticalSection();
|
|
|
|
|
|
|
|
// enter the section (the same as locking a mutex)
|
|
|
|
void Enter();
|
|
|
|
// leave the critical section (same as unlocking a mutex)
|
|
|
|
void Leave();
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxCriticalSectionInternal *m_critsect;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-09 00:28:27 +00:00
|
|
|
wxCriticalSection& m_critsect;
|
1999-01-05 03:04:48 +00:00
|
|
|
};
|
|
|
|
|
1999-01-06 21:09:17 +00:00
|
|
|
#endif
|
|
|
|
|
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-09 00:28:27 +00:00
|
|
|
// implementation only
|
|
|
|
#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
|
|
|
|
inline bool WXDLLEXPORT wxGuiOwnedByMainThread();
|
|
|
|
|
|
|
|
// wakes up the main thread if it's sleeping inside ::GetMessage()
|
|
|
|
inline void WXDLLEXPORT wxWakeUpMainThread();
|
|
|
|
#endif // MSW
|
|
|
|
|
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(); }
|
|
|
|
};
|
|
|
|
|
1998-12-27 00:54:53 +00:00
|
|
|
#endif // __THREADH__
|