Initial
This commit is contained in:
81
thread/THTIMER.CPP
Normal file
81
thread/THTIMER.CPP
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <thread/thtimer.hpp>
|
||||
|
||||
ThreadTimer::ThreadTimer(void)
|
||||
: mMinResolution(0), mIsOkay(FALSE), mHasTimedEvent(FALSE), mTimerID(0), mIsFirstBreak(TRUE)
|
||||
{
|
||||
if(::timeGetDevCaps(&mTimerCapability,sizeof(TIMECAPS)))return;
|
||||
mMinResolution=mTimerCapability.wPeriodMin;
|
||||
if(::timeBeginPeriod(mMinResolution))return;
|
||||
mIsOkay=TRUE;
|
||||
}
|
||||
|
||||
ThreadTimer::ThreadTimer(WORD minResolution)
|
||||
: mMinResolution(minResolution), mIsOkay(FALSE), mHasTimedEvent(FALSE), mTimerID(0), mIsFirstBreak(TRUE)
|
||||
{
|
||||
if(::timeGetDevCaps(&mTimerCapability,sizeof(TIMECAPS)))return;
|
||||
if(mMinResolution<mTimerCapability.wPeriodMin)mMinResolution=mTimerCapability.wPeriodMin;
|
||||
if(::timeBeginPeriod(mMinResolution))return;
|
||||
mIsOkay=TRUE;
|
||||
}
|
||||
|
||||
ThreadTimer::~ThreadTimer()
|
||||
{
|
||||
if(mIsOkay){stopTimer();::timeEndPeriod(mMinResolution);}
|
||||
mTimerMutex.requestMutex();
|
||||
mTimerMutex.releaseMutex();
|
||||
}
|
||||
|
||||
WORD ThreadTimer::startTimer(UINT deltaTime)
|
||||
{
|
||||
if(!mIsOkay)return FALSE;
|
||||
if(mHasTimedEvent)return FALSE;
|
||||
isFirstBreak(TRUE);
|
||||
if(0==(mTimerID=::timeSetEvent(deltaTime,mMinResolution,(LPTIMECALLBACK)timerProc,(DWORD)this,TIME_PERIODIC)))return FALSE;
|
||||
mHasTimedEvent=TRUE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
WORD ThreadTimer::stopTimer(void)
|
||||
{
|
||||
WORD returnCode(FALSE);
|
||||
|
||||
if(!mIsOkay)return returnCode;
|
||||
if(!mHasTimedEvent)return returnCode;
|
||||
if(!::timeKillEvent(mTimerID))returnCode=TRUE;
|
||||
isFirstBreak(FALSE);
|
||||
mHasTimedEvent=FALSE;
|
||||
mTimerID=FALSE;
|
||||
return returnCode;
|
||||
}
|
||||
|
||||
void CALLBACK ThreadTimer::timerProc(UINT /*wTimerID*/,UINT /*mMsg*/,DWORD dwUser,DWORD /*dummyOne*/,DWORD /*dummyTwo*/)
|
||||
{
|
||||
CallbackData callbackData;
|
||||
|
||||
if(!((ThreadTimer*)dwUser))return;
|
||||
((ThreadTimer*)dwUser)->mTimerMutex.requestMutex();
|
||||
if(((ThreadTimer*)dwUser)->isFirstBreak())
|
||||
{
|
||||
((ThreadTimer*)dwUser)->isFirstBreak(FALSE);
|
||||
((ThreadTimer*)dwUser)->timerStarted();
|
||||
}
|
||||
if(((CallbackData::ReturnType)FALSE)==((ThreadTimer*)dwUser)->mCallbackPointer.callback(callbackData))
|
||||
{
|
||||
((ThreadTimer*)dwUser)->timerStopped();
|
||||
((ThreadTimer*)dwUser)->stopTimer();
|
||||
}
|
||||
((ThreadTimer*)dwUser)->mTimerMutex.releaseMutex();
|
||||
return;
|
||||
}
|
||||
|
||||
// virtuals
|
||||
|
||||
void ThreadTimer::timerStarted(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void ThreadTimer::timerStopped(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Reference in New Issue
Block a user