📄 osapi_timer.c
字号:
/*****************************************************************************
******************************************************************************
** **
** Copyright (c) 2006 Videon Central, Inc. **
** All rights reserved. **
** **
** The computer program contained herein contains proprietary information **
** which is the property of Videon Central, Inc. The program may be used **
** and/or copied only with the written permission of Videon Central, Inc. **
** or in accordance with the terms and conditions stipulated in the **
** agreement/contract under which the programs have been supplied. **
** **
******************************************************************************
*****************************************************************************/
/**
* @file osapi_timer.cpp
*
* $Revision: 1.12 $
*
* Operating System API (OSAPI) source file.
* Provides timer support on Windows XP
*
*/
#include <windows.h>
#include <time.h>
#include "osapi.h"
#include "dbgprint.h"
/********************************************************************************
CONSTANTS
********************************************************************************/
#define DEBUG_ERROR 1
#define DEBUG_TRACE 2
#define DEBUG_INFO 4
#define DEBUG_SETTINGS (DEBUG_ERROR)
#define DEBUG_ON(x) (DEBUG_SETTINGS & x)
#define OS_TIMER_MAGIC_NUMBER 0x12345000
/********************************************************************************
DATA TYPES
********************************************************************************/
typedef struct tagTimerData
{
#if DEBUG
uint32 magicNumber;
#endif
OS_FUNCPTR_PARAM pvFunction;
PVOID pvParam;
HANDLE hTimerHandle;
HANDLE hCancelTimerSemaphoreHandle;
BOOL bEnabled;
BOOL bThreadRunning;
clock_t startTime;
ULONG ulPeriodInMilliseconds;
} TimerData;
/********************************************************************************
LOCAL ROUTINES
********************************************************************************/
static OS_STATUS OS_TimerSetup ( OS_TIMER_ID pTimerId, ULONG ulPeriodInMilliseconds, BOOL bRepeat );
static int OS_TimerThread ( PVOID pvTimerData );
static OS_STATUS OS_TimerValidateHandle ( OS_TIMER_ID pTimerId );
/**
*******************************************************************************
* OS_TimerThread Timer's worker thread
*
* @param pvTimerData pointer to timer handle
*
* @return thread exit code (0)
*******************************************************************************/
static int OS_TimerThread(PVOID pvTimerData)
{
TimerData* timer;
HANDLE handleArray[2];
timer = (TimerData*)pvTimerData;
handleArray[0] = timer->hTimerHandle;
handleArray[1] = timer->hCancelTimerSemaphoreHandle;
if (( timer->hTimerHandle != NULL ) && ( timer->bEnabled == TRUE ))
{
do
{
WaitForMultipleObjects (2,handleArray,FALSE,INFINITE);
if (( timer->pvFunction != NULL ) && ( timer->bEnabled == TRUE ))
{
if (NULL != timer->pvParam)
{
timer->pvFunction(timer->pvParam);
}
else
{
timer->pvFunction(NULL);
}
}
} while ( (timer->hTimerHandle != NULL ) && ( timer->bEnabled == TRUE ));
}
timer->bThreadRunning = FALSE;
return ( 0 );
}
/**
*******************************************************************************
* OS_TimerValidateHandle Validates timer handle
*
* @param pTimerId pointer to timer handle
*
* @return OS_OK if handle is valid, OS_FAILURE otherwise
*******************************************************************************/
static OS_STATUS OS_TimerValidateHandle( OS_TIMER_ID pTimerId )
{
TimerData* pTimer = (TimerData *)pTimerId;
if ( pTimerId == 0 )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerValidateHandle: invalid timer handle (NULL pointer)"));
return ( OS_FAILURE );
}
#if DEBUG
// validate pointer is timer pointer
if ( pTimer->magicNumber != OS_TIMER_MAGIC_NUMBER )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerValidateHandle: invalid timer handle (pointer corruption)"));
return ( OS_FAILURE );
}
#endif
return ( OS_OK );
}
/**
*******************************************************************************
* GetTimerFunc Get timer function
*
* @param pTimerId pointer to timer handle
*
* @return timer function if successful, NULL otherwise
*******************************************************************************/
OS_FUNCPTR GetTimerFunc(OS_TIMER_ID pTimerId)
{
TimerData* pTimer = (TimerData *)pTimerId;
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("GetTimerFunc"));
// validate timer handle
if ( OS_TimerValidateHandle(pTimerId) != OS_OK )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("GetTimerFunc: invalid timer handle"));
return ( NULL );
}
return ((OS_FUNCPTR)pTimer->pvFunction);
}
/**
*******************************************************************************
* OS_TimerCreate Create timer
*
* @param ptimerId pointer to timer handle
* @param func function to be called when timer expires
*
* @return OS_OK if successful, OS_FAILURE or OS_NO_MEMORY otherwise
*******************************************************************************/
OS_STATUS OS_TimerCreate(OS_TIMER_ID *ptimerId, OS_FUNCPTR func)
{
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerCreate"));
return (OS_TimerCreateParam(ptimerId, (OS_FUNCPTR_PARAM)func, NULL));
}
/**
*******************************************************************************
* OS_TimerCreateParam Create timer
*
* @param ptimerId pointer to timer handle
* @param func function to be called when timer expires
* @param pvParam parameter to be passed to timer function executes
*
* @return OS_OK if successful, OS_FAILURE or OS_NO_MEMORY otherwise
*******************************************************************************/
OS_STATUS OS_TimerCreateParam(OS_TIMER_ID *pTimerId, OS_FUNCPTR_PARAM func, PVOID pvParam)
{
TimerData* pTimer = NULL;
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerCreateParam"));
pTimer = (TimerData*)OS_MemAlloc(sizeof(TimerData));
if (pTimer == NULL)
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerCreateParam: OS_MemAlloc failed\n"));
return ( OS_NO_MEMORY );
}
memset( pTimer, 0, sizeof(TimerData) );
#if DEBUG
pTimer->magicNumber = OS_TIMER_MAGIC_NUMBER;
#endif
pTimer->hTimerHandle = NULL;
pTimer->hCancelTimerSemaphoreHandle = NULL;
pTimer->pvFunction = func;
pTimer->pvParam = pvParam;
pTimer->bEnabled = FALSE;
pTimer->bThreadRunning = FALSE;
pTimer->hTimerHandle = CreateWaitableTimer(
NULL, // pointer to security attributes
FALSE, // flag for manual reset state
NULL // pointer to timer object name
);
if ( pTimer->hTimerHandle == NULL )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerCreateParam: CreateWaitableTimer failed\n"));
OS_MemFree(pTimer);
return ( OS_FAILURE );
}
*pTimerId = (OS_TIMER_ID)pTimer;
return ( OS_OK );
}
/**
*******************************************************************************
* OS_TimerDelete Delete timer
*
* @param ptimerId pointer to timer handle
*
* @return OS_OK if successful, OS_FAILURE otherwise
*******************************************************************************/
OS_STATUS OS_TimerDelete(OS_TIMER_ID pTimerId)
{
OS_STATUS status = OS_OK;
TimerData* pTimer = (TimerData *)pTimerId;
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerDelete"));
// validate timer handle
if ( OS_TimerValidateHandle(pTimerId) != OS_OK )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerDelete: invalid timer handle"));
return ( OS_FAILURE );
}
// stop timer before deleting it
if ( pTimer->bEnabled == TRUE )
{
status = OS_TimerStop(pTimerId);
if ( status != OS_OK )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerDelete: OS_TimerStop failed"));
return ( status );
}
}
// close timer
if ( CloseHandle( pTimer->hTimerHandle ) == 0 )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerDelete: CloseHandle failed"));
status = OS_FAILURE;
}
OS_MemFree(pTimer);
DBGPRINT((status != OS_OK), ("OS_TimerDelete: FAILED\n"));
return (status);
}
/**
*******************************************************************************
* OS_TimerStop Stop timer
*
* @param ptimerId pointer to timer handle
*
* @return OS_OK if successful, OS_FAILURE otherwise
*******************************************************************************/
OS_STATUS OS_TimerStop(OS_TIMER_ID pTimerId)
{
TimerData* pTimer = (TimerData *)pTimerId;
DBGPRINT(DEBUG_ON(DEBUG_TRACE), ("OS_TimerStop"));
// validate timer handle
if ( OS_TimerValidateHandle(pTimerId) != OS_OK )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerStop: invalid timer handle"));
return ( OS_FAILURE );
}
pTimer->bEnabled = FALSE;
if ( CancelWaitableTimer(pTimer->hTimerHandle) == 0 )
{
DBGPRINT(DEBUG_ON(DEBUG_ERROR), ("OS_TimerStop: CancelWaitableTimer failed\n"));
return ( OS_FAILURE );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -