1998-05-21 11:02:02 -04: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 13:22:15 -04:00
|
|
|
#ifndef __THREADH__
|
|
|
|
#define __THREADH__
|
1998-05-21 11:02:02 -04:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma interface "thread.h"
|
|
|
|
#endif
|
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 11:02:02 -04:00
|
|
|
#include "wx/object.h"
|
|
|
|
#include "wx/setup.h"
|
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1998-05-21 11:02:02 -04:00
|
|
|
typedef enum {
|
1998-12-26 19:54:53 -05: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 11:02:02 -04:00
|
|
|
} wxMutexError;
|
|
|
|
|
|
|
|
typedef enum {
|
1998-12-26 19:54:53 -05: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 11:02:02 -04:00
|
|
|
} wxThreadError;
|
|
|
|
|
|
|
|
// defines the interval of priority.
|
1998-12-26 19:54:53 -05:00
|
|
|
#define WXTHREAD_MIN_PRIORITY 0
|
1998-05-21 11:02:02 -04:00
|
|
|
#define WXTHREAD_DEFAULT_PRIORITY 50
|
1998-12-26 19:54:53 -05:00
|
|
|
#define WXTHREAD_MAX_PRIORITY 100
|
1998-05-21 11:02:02 -04:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 11:02:02 -04:00
|
|
|
// Mutex handler
|
1998-12-26 19:54:53 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-08-18 13:22:15 -04:00
|
|
|
class WXDLLEXPORT wxMutexInternal;
|
1998-05-21 11:02:02 -04:00
|
|
|
class WXDLLEXPORT wxMutex {
|
|
|
|
public:
|
|
|
|
// constructor & destructor
|
1998-06-14 08:15:13 -04:00
|
|
|
wxMutex();
|
|
|
|
~wxMutex();
|
1998-05-21 11:02:02 -04:00
|
|
|
|
|
|
|
// Lock the mutex.
|
1998-06-14 08:15:13 -04:00
|
|
|
wxMutexError Lock();
|
1998-05-21 11:02:02 -04:00
|
|
|
// Try to lock the mutex: if it can't, returns immediately with an error.
|
1998-06-14 08:15:13 -04:00
|
|
|
wxMutexError TryLock();
|
1998-05-21 11:02:02 -04:00
|
|
|
// Unlock the mutex.
|
1998-06-14 08:15:13 -04:00
|
|
|
wxMutexError Unlock();
|
1998-05-21 11:02:02 -04:00
|
|
|
|
|
|
|
// Returns true if the mutex is locked.
|
1998-06-14 08:15:13 -04:00
|
|
|
bool IsLocked() const { return (m_locked > 0); }
|
1998-12-26 19:54:53 -05:00
|
|
|
|
1998-05-21 11:02:02 -04:00
|
|
|
protected:
|
|
|
|
friend class wxCondition;
|
|
|
|
|
|
|
|
int m_locked;
|
|
|
|
wxMutexInternal *p_internal;
|
|
|
|
};
|
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 11:02:02 -04:00
|
|
|
// Condition handler.
|
1998-12-26 19:54:53 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 11:02:02 -04:00
|
|
|
class wxConditionInternal;
|
|
|
|
class WXDLLEXPORT wxCondition {
|
|
|
|
public:
|
|
|
|
// constructor & destructor
|
1998-06-14 08:15:13 -04:00
|
|
|
wxCondition();
|
|
|
|
~wxCondition();
|
1998-05-21 11:02:02 -04:00
|
|
|
|
1998-06-14 08:15:13 -04:00
|
|
|
// Waits indefinitely.
|
1998-05-21 11:02:02 -04: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 08:15:13 -04:00
|
|
|
void Signal();
|
1998-05-21 11:02:02 -04:00
|
|
|
// Broadcasts to all "Waiters".
|
1998-06-14 08:15:13 -04:00
|
|
|
void Broadcast();
|
1998-12-26 19:54:53 -05:00
|
|
|
|
1998-05-21 11:02:02 -04:00
|
|
|
private:
|
|
|
|
wxConditionInternal *p_internal;
|
|
|
|
};
|
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 11:02:02 -04:00
|
|
|
// Thread management class
|
1998-12-26 19:54:53 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 11:02:02 -04:00
|
|
|
class wxThreadInternal;
|
|
|
|
class WXDLLEXPORT wxThread {
|
|
|
|
public:
|
|
|
|
// constructor & destructor.
|
1998-06-14 08:15:13 -04:00
|
|
|
wxThread();
|
|
|
|
virtual ~wxThread();
|
1998-05-21 11:02:02 -04:00
|
|
|
|
|
|
|
// Create a new thread, this method should check there is only one thread
|
|
|
|
// running by object.
|
1998-06-14 08:15:13 -04:00
|
|
|
wxThreadError Create();
|
1998-05-21 11:02:02 -04:00
|
|
|
|
1998-06-14 08:15:13 -04:00
|
|
|
// Destroys the thread immediately if the defer flag isn't true.
|
|
|
|
wxThreadError Destroy();
|
1998-05-21 11:02:02 -04:00
|
|
|
|
1998-08-18 13:22:15 -04:00
|
|
|
// Pause a running thread
|
|
|
|
wxThreadError Pause();
|
|
|
|
|
|
|
|
// Resume a paused thread
|
|
|
|
wxThreadError Resume();
|
|
|
|
|
1998-06-14 08:15:13 -04:00
|
|
|
// Switches on the defer flag.
|
1998-05-21 11:02:02 -04:00
|
|
|
void DeferDestroy(bool on);
|
|
|
|
|
|
|
|
// Waits for the termination of the thread.
|
1998-06-14 08:15:13 -04:00
|
|
|
void *Join();
|
1998-05-21 11:02:02 -04: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 08:15:13 -04:00
|
|
|
int GetPriority() const;
|
1998-05-21 11:02:02 -04:00
|
|
|
|
|
|
|
// Get the thread ID
|
1998-06-14 08:15:13 -04:00
|
|
|
unsigned long GetID() const;
|
1998-05-21 11:02:02 -04:00
|
|
|
|
|
|
|
// Returns true if the thread is alive.
|
1998-06-14 08:15:13 -04:00
|
|
|
bool IsAlive() const;
|
1998-08-18 13:22:15 -04:00
|
|
|
// Returns true if the thread is running (not paused, not killed).
|
|
|
|
bool IsRunning() const;
|
1998-12-26 19:54:53 -05:00
|
|
|
// Returns true if the thread is suspended
|
|
|
|
bool IsPaused() const { return IsAlive() && !IsRunning(); }
|
|
|
|
|
1998-05-21 11:02:02 -04:00
|
|
|
// Returns true if the thread is the main thread (aka the GUI thread).
|
1998-06-14 08:15:13 -04:00
|
|
|
static bool IsMain();
|
1998-05-21 11:02:02 -04:00
|
|
|
|
|
|
|
// Called when thread exits.
|
1998-06-14 08:15:13 -04:00
|
|
|
virtual void OnExit();
|
1998-08-18 13:22:15 -04:00
|
|
|
|
1998-05-21 11:02:02 -04:00
|
|
|
protected:
|
|
|
|
// In case, the DIFFER flag is true, enables another thread to kill this one.
|
1998-06-14 08:15:13 -04:00
|
|
|
void TestDestroy();
|
1998-05-21 11:02:02 -04:00
|
|
|
// Exits from the current thread.
|
|
|
|
void Exit(void *status = NULL);
|
1998-12-26 19:54:53 -05:00
|
|
|
|
1998-05-21 11:02:02 -04:00
|
|
|
private:
|
|
|
|
// Entry point for the thread.
|
1998-06-14 08:15:13 -04:00
|
|
|
virtual void *Entry() = 0;
|
1998-05-21 11:02:02 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class wxThreadInternal;
|
|
|
|
|
|
|
|
wxThreadInternal *p_internal;
|
|
|
|
};
|
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Global functions and variables
|
|
|
|
// ----------------------------------------------------------------------------
|
1998-05-21 11:02:02 -04:00
|
|
|
|
1998-10-14 13:36:50 -04:00
|
|
|
// GUI mutex handling.
|
|
|
|
void WXDLLEXPORT wxMutexGuiEnter();
|
|
|
|
void WXDLLEXPORT wxMutexGuiLeave();
|
1998-05-21 11:02:02 -04:00
|
|
|
|
1998-12-26 19:54:53 -05:00
|
|
|
#endif // __THREADH__
|