107 lines
2.6 KiB
C++
107 lines
2.6 KiB
C++
#ifndef _THREAD_PURETHREAD_HPP_
|
|
#define _THREAD_PURETHREAD_HPP_
|
|
#ifndef _COMMON_WINDOWS_HPP_
|
|
#include <common/windows.hpp>
|
|
#endif
|
|
#ifndef _THREAD_CONTEXT_HPP_
|
|
#include <thread/context.hpp>
|
|
#endif
|
|
|
|
class PureThread
|
|
{
|
|
public:
|
|
typedef DWORD (WINAPI *LPFNTHREADPROC)(LPVOID lpInstanceData);
|
|
enum Status{InSuspend,InTerminate,InRun,InStop,InNothing};
|
|
enum ThreadPriority{ThreadPriorityAboveNormal=THREAD_PRIORITY_ABOVE_NORMAL,
|
|
ThreadPriorityBelowNormal=THREAD_PRIORITY_BELOW_NORMAL,
|
|
ThreadPriorityHighest=THREAD_PRIORITY_HIGHEST,
|
|
ThreadPriorityIdle=THREAD_PRIORITY_IDLE,
|
|
ThreadPriorityLowest=THREAD_PRIORITY_LOWEST,
|
|
ThreadPriorityNormal=THREAD_PRIORITY_NORMAL,
|
|
ThreadPriorityTimeCritical=THREAD_PRIORITY_TIME_CRITICAL};
|
|
PureThread(HANDLE hThread=GetCurrentThread());
|
|
PureThread(LPFNTHREADPROC lpfnThreadProc);
|
|
virtual ~PureThread();
|
|
WORD start(LPVOID lpCreationData=0);
|
|
WORD fatalThreadExit(void);
|
|
DWORD wait(DWORD timeout=INFINITE,bool alertable=false)const;
|
|
DWORD resume(void);
|
|
DWORD suspend(void);
|
|
DWORD exitCode(void)const;
|
|
WORD context(Context &contextThread)const;
|
|
ThreadPriority getThreadPriority(void)const;
|
|
ThreadPriority setThreadPriority(ThreadPriority threadPriority)const;
|
|
WORD isOkay(void)const;
|
|
String getThreadName(void)const;
|
|
private:
|
|
enum{InitialStack=8192};
|
|
PureThread &operator=(const PureThread &somePureThread);
|
|
|
|
HANDLE mhThread;
|
|
DWORD mThreadID;
|
|
LPFNTHREADPROC mlpfnThreadProc;
|
|
Status mThreadStatus;
|
|
};
|
|
|
|
inline
|
|
PureThread::PureThread(HANDLE hThread)
|
|
: mhThread(hThread), mThreadID(0), mlpfnThreadProc(0), mThreadStatus(InRun)
|
|
{
|
|
}
|
|
|
|
inline
|
|
PureThread::PureThread(LPFNTHREADPROC lpfnThreadProc)
|
|
: mhThread(0), mThreadID(0), mlpfnThreadProc(lpfnThreadProc), mThreadStatus(InNothing)
|
|
{
|
|
}
|
|
|
|
inline
|
|
PureThread::~PureThread()
|
|
{
|
|
}
|
|
|
|
inline
|
|
DWORD PureThread::suspend(void)
|
|
{
|
|
if(!isOkay()||InRun!=mThreadStatus)return FALSE;
|
|
mThreadStatus=InSuspend;
|
|
return ::SuspendThread(mhThread);
|
|
}
|
|
|
|
inline
|
|
DWORD PureThread::resume(void)
|
|
{
|
|
if(!isOkay()||InSuspend!=mThreadStatus)return FALSE;
|
|
mThreadStatus=InRun;
|
|
return ::ResumeThread(mhThread);
|
|
}
|
|
|
|
inline
|
|
DWORD PureThread::wait(DWORD timeout,bool alertable)const
|
|
{
|
|
if(!isOkay()||InRun!=mThreadStatus)return WAIT_ABANDONED;
|
|
return ::WaitForSingleObjectEx(mhThread,timeout,alertable);
|
|
}
|
|
|
|
inline
|
|
WORD PureThread::context(Context &contextThread)const
|
|
{
|
|
if(!isOkay())return FALSE;
|
|
return contextThread.getThreadContext(mhThread);
|
|
}
|
|
|
|
inline
|
|
WORD PureThread::isOkay(void)const
|
|
{
|
|
return (0==mhThread?FALSE:TRUE);
|
|
}
|
|
|
|
inline
|
|
String PureThread::getThreadName(void)const
|
|
{
|
|
String strThreadName;
|
|
::sprintf(strThreadName.str(),"%08lx",mThreadID);
|
|
return strThreadName;
|
|
}
|
|
#endif
|