📄 osapi_timer.c
字号:
if ( pTimer->hCancelTimerSemaphoreHandle != NULL )
{
if ( ReleaseSemaphore(pTimer->hCancelTimerSemaphoreHandle, +1, NULL) == 0)
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerStop: ReleaseSemaphore failed: %x\n",GetLastError()));
return ( OS_FAILURE );
}
}
// Wait for timer thread to exit
while ( pTimer->bThreadRunning == TRUE )
{
Sleep(1);
}
if ( pTimer->hCancelTimerSemaphoreHandle != NULL )
{
if ( CloseHandle(pTimer->hCancelTimerSemaphoreHandle) == 0 )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerStop: CloseHandle failed\n"));
return ( OS_FAILURE );
}
}
pTimer->startTime = 0;
pTimer->ulPeriodInMilliseconds=0;
pTimer->hCancelTimerSemaphoreHandle = NULL;
return ( OS_OK );
}
/**
*******************************************************************************
* OS_TimerSetup Set Timer firing period, repeat mode
*
* @param pTimerId pointer to timer handle
* @param ulPeriodInMilliseconds timer period in milliseconds
*
* @return OS_OK if successful, OS_FAILURE otherwise
*******************************************************************************/
static OS_STATUS OS_TimerSetup(OS_TIMER_ID pTimerId, ULONG ulPeriodInMilliseconds, BOOL repeat )
{
DWORD threadId = 0;
TimerData* pTimer = (TimerData *)pTimerId;
LARGE_INTEGER dueTime;
long period;
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerSetup"));
// validate timer handle
if ( OS_TimerValidateHandle(pTimerId) != OS_OK )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerSetup: invalid timer handle"));
return ( OS_FAILURE );
}
// stop the timer if it's currently running
if ( OS_TimerStop(pTimerId) != OS_OK )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerSetup: OS_TimerStop failed"));
}
// create semaphore so it's possible to cancel timer
pTimer->hCancelTimerSemaphoreHandle = CreateSemaphore( NULL, // security attributes
0, // initial count
1, // maximum count
NULL ); // name
if ( pTimer->hCancelTimerSemaphoreHandle == NULL )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerSetup: CreateSemaphore failed\n"));
return ( OS_FAILURE );
}
// due time is relative time from now (means negative)
dueTime.QuadPart = -(LONGLONG)ulPeriodInMilliseconds * 10000;
if ( repeat == TRUE )
{
period = ulPeriodInMilliseconds;
}
else
{
period = 0;
}
pTimer->bEnabled = TRUE;
pTimer->startTime = clock();
pTimer->ulPeriodInMilliseconds = ulPeriodInMilliseconds;
if ( SetWaitableTimer( pTimer->hTimerHandle,
&dueTime, // when timer will become signaled
period, // periodic timer interval (repeat)
NULL, // completion routine
NULL, // data for completion routine
FALSE // wake up system (power resume)
) == 0 )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerSetup: SetWaitableTimer FAILED\n"));
return ( OS_FAILURE );
}
pTimer->bThreadRunning = TRUE;
if ( CreateThread(
NULL, // security attributes
0,
(LPTHREAD_START_ROUTINE)OS_TimerThread,
pTimer,
EVENT_ALL_ACCESS | MUTEX_ALL_ACCESS | SEMAPHORE_ALL_ACCESS | TIMER_ALL_ACCESS,
&threadId) == 0 )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerSetup: CreateThread failed\n"));
return ( OS_FAILURE );
}
return (OS_OK);
}
/**
*******************************************************************************
* OS_TimerSet Set Timer firing period in milliseconds
*
* @param pTimerId pointer to timer handle
* @param ulPeriodInMilliseconds timer period in milliseconds
*
* @return OS_OK if successful, OS_FAILURE otherwise
*******************************************************************************/
OS_STATUS OS_TimerSetMsec(OS_TIMER_ID pTimerId, ULONG ulPeriodInMilliseconds)
{
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerSetMsec %u(ms)",ulPeriodInMilliseconds));
return ( OS_TimerSetup(pTimerId, ulPeriodInMilliseconds, FALSE ) );
}
/**
*******************************************************************************
* OS_TimerSet Set Timer firing period in ticks
*
* @param pTimerId pointer to timer handle
* @param ulPeriodInTicks timer period in ticks
*
* @return OS_OK if successful, OS_FAILURE otherwise
*******************************************************************************/
OS_STATUS OS_TimerSet(OS_TIMER_ID pTimerId, ULONG ulPeriodInTicks)
{
uint64 periodInMilliseconds;
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerSet %u(ticks)",ulPeriodInTicks));
periodInMilliseconds = (uint64)ulPeriodInTicks;
periodInMilliseconds = periodInMilliseconds * 1000;
periodInMilliseconds = periodInMilliseconds / OS_GetTicksPerSecond();
return ( OS_TimerSetMsec(pTimerId, (ULONG)periodInMilliseconds));
}
/**
*******************************************************************************
* OS_TimerSetRepeatMsec Set timer firing period in milliseconds and set to repeat
*
* @param pTimerId pointer to timer handle
* @param ulPeriodInMilliseconds timer period in milliseconds
*
* @return OS_OK if successful, OS_FAILURE otherwise
*******************************************************************************/
OS_STATUS OS_TimerSetRepeatMsec(OS_TIMER_ID pTimerId, ULONG ulPeriodInMilliseconds)
{
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerSetRepeatMsec"));
return ( OS_TimerSetup(pTimerId, ulPeriodInMilliseconds, TRUE ) );
}
/**
*******************************************************************************
* OS_TimerSetRepeat Set timer firing period in ticks and set to repeat
*
* @param pTimerId pointer to timer handle
* @param ulPeriodInTicks timer period in ticks
*
* @return OS_OK if successful, OS_FAILURE otherwise
*******************************************************************************/
OS_STATUS OS_TimerSetRepeat(OS_TIMER_ID pTimerId, ULONG ulPeriodinTicks)
{
uint64 periodInMilliseconds;
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerSetRepeat"));
periodInMilliseconds = (uint64)ulPeriodinTicks;
periodInMilliseconds = periodInMilliseconds * 1000;
periodInMilliseconds = periodInMilliseconds / OS_GetTicksPerSecond();
return ( OS_TimerSetRepeatMsec(pTimerId, (ULONG)periodInMilliseconds ) );
}
/**
*******************************************************************************
* OS_TimerGetTime Get time remaining in ticks
*
* @param pTimerId pointer to timer handle
* @param pulTimeRemainingTicks time remaining in ticks
*
* @return OS_OK if successful, OS_FAILURE otherwise
*******************************************************************************/
OS_STATUS OS_TimerGetTime(OS_TIMER_ID pTimerId, ULONG* pulTimeRemainingTicks)
{
TimerData* pTimer = (TimerData *)pTimerId;
clock_t currentTime;
ULONG elapsedTime;
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerGetTime"));
// validate timer handle
if ( OS_TimerValidateHandle(pTimerId) != OS_OK )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerGetTime: invalid timer handle"));
return ( OS_FAILURE );
}
currentTime = clock();
if ( currentTime >= pTimer->startTime )
{
elapsedTime = ( currentTime - pTimer->startTime );
}
else
{
elapsedTime = ( currentTime + (0xFFFFFFFF - pTimer->startTime + 1));
}
*pulTimeRemainingTicks = (pTimer->ulPeriodInMilliseconds*(OS_GetTickRate()/1000)) - elapsedTime;
return ( OS_OK );
}
/**
*******************************************************************************
* OS_TimerGetTimeMsec Get time remaining in milliseconds
*
* @param pTimerId pointer to timer handle
* @param pulTimeRemainingMsec time remaining in milliseconds
*
* @return OS_OK if successful, OS_FAILURE otherwise
*******************************************************************************/
OS_STATUS OS_TimerGetTimeMsec(OS_TIMER_ID pTimerID, ULONG* pulTimeRemainingMsec)
{
ULONG timeRemainingInTicks;
uint64 timeRemainingInMsec;
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerGetTimeMsec"));
if ( OS_TimerGetTime( pTimerID, &timeRemainingInTicks ) != OS_OK )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerGetTime failed"));
return ( OS_FAILURE );
}
timeRemainingInMsec = (uint64)timeRemainingInTicks;
timeRemainingInMsec = timeRemainingInMsec * 1000;
timeRemainingInMsec = timeRemainingInMsec / OS_GetTicksPerSecond();
*pulTimeRemainingMsec = (ULONG)timeRemainingInMsec;
return ( OS_OK );
}
/**
*******************************************************************************
* OS_GetTickRate Get ticks per second on this system
*
* @return ticks per second
*******************************************************************************/
ULONG OS_GetTickRate(void)
{
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_GetTickRate %u\n",CLOCKS_PER_SEC));
return (CLOCKS_PER_SEC);
}
/**
*******************************************************************************
* OS_GetTicks Get current tick count
*
* @return current tick count
*******************************************************************************/
ULONG OS_GetTicks(void)
{
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerCreateParam"));
return ((ULONG) clock() );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -