home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1997 October
/
Chip_1997-10_cd.bin
/
tema
/
sybase
/
powerj
/
hpp.z
/
WTHREAD.HPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-01-22
|
16KB
|
509 lines
/********************************************************************
*
* Threads
*
* Two classes are available:
*
* WCurrentThread -- Always refers to the current thread.
*
* WThread -- Refers to a specific thread in the process.
*
* If you want to do something related to the current thread,
* use an object of type WCurrentThread. Otherwise you need
* an object of class WThread, which can only be had in the
* following manner:
*
* 1) Call the Create method.
*
* 2) Assignment from another WThread.
*
* 3) Assignment from a WCurrentThread. (This makes a WThread
* object which can then be used in other threads to refer
* to the current thread.)
*
********************************************************************/
#ifndef _WTHREAD_HPP_INCLUDED
#define _WTHREAD_HPP_INCLUDED
#ifndef _WNO_PRAGMA_PUSH
#pragma pack(push,8);
#pragma enum int;
#endif
#ifndef _WDEF_HPP_INCLUDED
# include "wdef.hpp"
#endif
#ifndef _WOBJECT_HPP_INCLUDED
# include "wobject.hpp"
#endif
#ifndef _WSEMAPHORE_HPP_INCLUDED
# include "wsemaphr.hpp"
#endif
#ifndef _WMESSAGE_HPP_INCLUDED
# include "wmessage.hpp"
#endif
#ifndef _WIN16
#undef PostMessage
#undef DispatchMessage
#if defined( _UNICODE )
#define PostMessage PostMessageW
#define DispatchMessage DispatchMessageW
#else
#define PostMessage PostMessageA
#define DispatchMessage DispatchMessageA
#endif
#endif
class WThread;
class WProcess;
class WThreadReference;
class WFilterEntry;
enum WThreadHandle { NULLHTHREAD = 0, LASTHTHREAD = LAST_16TO32BIT };
//
// Thread callback functions. You can call back to either a
// static function or a member function.
//
typedef WDWord WCMDEF (*WStaticThreadRoutine)( void *userData );
typedef WDWord WCMDEF (WObject::*WThreadRoutine)( void *userData );
//
// Message filter callback functions.
//
struct WMessageFilterData {
WMessage message;
WULong filterCode;
void *userData;
void *reserved1; // Reserved for internal use
void *reserved2; // Reserved for internal use
};
typedef WBool WCMDEF (*WStaticMessageFilter)( WMessageFilterData *data );
typedef WBool WCMDEF (WObject::*WMessageFilter)( WMessageFilterData *data );
// Thread priority...
enum WThreadPriority {
WThreadPriorityFirst,
WThreadPriorityLowest = WThreadPriorityFirst,
WThreadPriorityIdle,
WThreadPriorityBelowNormal,
WThreadPriorityNormal,
WThreadPriorityAboveNormal,
WThreadPriorityTimeCritical,
WThreadPriorityHighest,
WThreadPriorityLast = WThreadPriorityHighest,
WThreadPriorityError
};
//
// WThreadBase -- An abstract base class.
//
class WCMCLASS WThreadBase : public WObject {
WDeclareSubclass( WThreadBase, WObject );
/***********************************************************
* Constructors
***********************************************************/
private:
WThreadBase& operator=( const WThreadBase & );
WThreadBase( const WThreadBase & );
protected:
WThreadBase();
public:
~WThreadBase();
/***********************************************************
* Properties
***********************************************************/
public:
// Active
//
// TRUE if the thread is still running.
WBool GetActive() const;
// ExitCode
//
// Returns the exit code for the thread. If the thread
// is still active the result is undefined.
WDWord GetExitCode() const;
// Handle
//
// Returns the thread handle.
virtual WThreadHandle GetHandle() const = 0;
// ID
//
// Returns the thread ID.
virtual WDWord GetID() const = 0;
// Priority
//
// Set/get the thread priority.
WThreadPriority GetPriority() const;
WBool SetPriority( WThreadPriority priority );
// Valid
//
// TRUE if the thread represents (or represented)
// a valid thread.
WBool GetValid() const;
/***********************************************************
* Methods
***********************************************************/
// AttachInputTo
//
// Synchronizes the thread's input status with another
// thread.
WBool AttachInputTo( WDWord threadID );
// DetachInputFrom
//
// Detaches the thread from another thread.
WBool DetachInputFrom( WDWord threadID );
// Resume
//
// Let a thread resume after it has been suspended.
WBool Resume();
// Suspend
//
// Suspend a thread.
WBool Suspend();
// Terminate
WBool Terminate( WDWord exitCode=0 );
// Wait
//
// Wait for the thread to terminate, with a timeout in
// milliseconds. By default the timeout is infinite.
// If blockMessageProcessing is TRUE, then no messages will be
// responded to by this thread until the call to Wait() finishes.
WBool Wait(); // 0xFFFFFFFF, TRUE
WBool Wait( WDWord timeout ); // blocks processing
WBool Wait( WDWord timeout, WBool blockMessageProcessing );
};
//
// WCurrentThread -- Represents the currently-running thread.
//
class WCMCLASS WCurrentThread : public WThreadBase {
WDeclareSubclass( WCurrentThread, WThreadBase );
public:
/***********************************************************
* Constructors and Destructors
***********************************************************/
WCurrentThread();
WCurrentThread( const WCurrentThread & thd );
~WCurrentThread(); // does not terminate thread!
/***********************************************************
* Operators
***********************************************************/
WCurrentThread& operator=( const WCurrentThread & thd );
/***********************************************************
* Properties
***********************************************************/
// ErrorCode
//
// Per-thread error code.
static WDWord GetErrorCode();
static WBool SetErrorCode( WDWord errCode );
// ErrorMessage
//
// Per-thread error message. If the give error code is 0,
// uses the value of the ErrorCode property. If no error,
// an empty string is returned.
static WString GetErrorMessage( WDWord errCode=0 );
static WBool GetErrorMessage( WString & str, WDWord errCode=0 );
// Name
//
// Per-thread user-settable name (for debugging).
static WString GetName();
static WBool SetName( const WString & name );
// UserData
//
// Per-thread userdata.
static void *GetUserData();
static WBool SetUserData( void *userData );
/***********************************************************
* Methods
***********************************************************/
// CallFilter
//
// Calls the chain of message filters. Returns TRUE
// if the message was processed and should not be
// dispatched. This function can also be used within
// a filter to call the next filter in the chain by
// passing in a non-NULL data value.
#define WFILTER_REMOVE 0
#define WFILTER_PEEK 1
static WBool CallFilter( WMessage & message,
WULong filterCode=WFILTER_REMOVE,
WMessageFilterData *data=NULL,
WBool callHookFilters=TRUE );
// Create
//
// Creates a new thread and returns the thread object.
static WThread Create( WObject *object, WThreadRoutine handler,
void *userData=NULL,
WBool createSuspended=FALSE,
WULong stackSize=0 );
static WThread Create( WStaticThreadRoutine handler,
void *userData=NULL,
WBool createSuspended=FALSE,
WULong stackSize=0 );
// DispatchMessage
//
// Translates and dispatches a low-level window message.
static void DispatchMessage( const WMessage & message );
// FetchMessage
//
// Returns the next message from the queue. Returns 1
// if a message was retrieved and the message was not
// a WM_QUIT, 0 if a message was retrieved and it was
// a WM_QUIT, and -1 if an error occurred.
static WInt FetchMessage( WMessage & message,
WBool waitForMessage=TRUE,
WBool removeMessage=TRUE,
WWindowHandle rootWindow=NULLHWND,
WULong firstMessage=0,
WULong lastMessage=0,
WBool filter=TRUE,
WBool * dispatch=NULL );
// FreeThreadData
//
// Frees any private (per-thread) data that might have been
// allocated for this thread by the class library.
static void FreeThreadData();
// InstallFilter
//
// Installs a message filter. User-installed filters
// should have a priority > WFP_SYSTEM.
#define WFP_USER 64
#define WFP_SYSTEM 32
#define WFP_DEFAULT 0
static WBool InstallFilter( WObject *object,
WMessageFilter filter,
void *userData=NULL,
WULong priority=WFP_USER,
WBool asHookFilter=TRUE );
static WBool InstallFilter( WStaticMessageFilter filter,
void *userData=NULL,
WULong priority=WFP_USER,
WBool asHookFilter=TRUE );
// PostMessage
//
// Posts a message for the current thread. Returns TRUE
// if the message was posted.
static WBool PostMessage( const WMessage & message );
// PostQuitMessage
//
// Posts a WM_QUIT message to the current thread,
// which will then cause a future FetchMessage
// to return 0.
static WBool PostQuitMessage( WInt exitCode=0 );
// ProcessMessages
//
// Enters a loop calling FetchMessage/DispatchMessage
// until FetchMessage returns FALSE (WM_QUIT) or an
// error occurs. Returns TRUE if successful, FALSE
// otherwise. Optional WMessage value returns the
// last message processed.
static WBool ProcessMessages( WMessage * lastMessage=NULL );
// RemoveFilter
//
// Remove a filter that was installed using InstallFilter.
/// Returns TRUE if a filter was found and removed.
static WBool RemoveFilter( WObject *object,
WMessageFilter filter,
WBool fromHook=TRUE );
static WBool RemoveFilter( WStaticMessageFilter filter,
WBool fromHook=TRUE );
// Sleep
//
// Sleeps for the given number of milliseconds, or
// forever if time=0xffffffff. If time=0 then the
// thread merely gives up its time slice.
static WBool Sleep( WDWord time );
// WaitForMessage
//
// Waits until a message arrives in the queue.
static void WaitForMessage();
/***********************************************************
* Other
***********************************************************/
virtual WThreadHandle GetHandle() const;
virtual WDWord GetID() const;
};
//
// WThread -- Represents an arbitrary thread.
//
class WCMCLASS WThread : public WThreadBase {
WDeclareSubclass( WThread, WThreadBase );
public:
/***********************************************************
* Constructors
***********************************************************/
WThread();
WThread( WThreadHandle handle, WDWord threadId,
WBool closeIt=TRUE, WBool makeCopy=FALSE );
WThread( const WThread & thd );
WThread( const WCurrentThread & curr );
~WThread(); // does not terminate thread!
/***********************************************************
* Operators
***********************************************************/
WThread& operator=( const WThread & thd );
WThread& operator=( const WCurrentThread & thd );
/***********************************************************
* Properties
***********************************************************/
/***********************************************************
* Methods
***********************************************************/
// Clear
//
// Resets the object, closing the low-level kernel
// handle if no one else is referencing the thread.
void Clear();
// Create
//
// Resets the object, then creates a new thread.
WBool Create( WObject *object, WThreadRoutine handler,
void *userData=NULL,
WBool createSuspended=FALSE,
WULong stackSize=0 );
WBool Create( WStaticThreadRoutine handler,
void *userData=NULL,
WBool createSuspended=FALSE,
WULong stackSize=0 );
// PostMessage
//
// Posts a message for the current thread. Returns TRUE
// if the message was posted.
WBool PostMessage( const WMessage & message ) const;
// PostQuitMessage
//
// Posts a WM_QUIT message to the given thread,
// which will then cause a future FetchMessage
// to return 0.
WBool PostQuitMessage( WInt exitCode=0 ) const;
/***********************************************************
* Other
***********************************************************/
virtual WThreadHandle GetHandle() const;
virtual WDWord GetID() const;
private:
WThreadReference *_threadRef;
};
#ifndef _WNO_PRAGMA_PUSH
#pragma enum pop;
#pragma pack(pop);
#endif
#endif // _WTHREAD_HPP_INCLUDED