#include <sys/time.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include <termios.h> /*PPSIX终端控制定义*/
#include <errno.h> /*错误号定义*/
#define SYS_TIMER_NUMBER 120
pthread_mutex_t g_Timermutex = PTHREAD_MUTEX_INITIALIZER;
/*timer structure*/
struct timer_struct
{
int currentTicks; /*current ticks */
int defaulTicks; /*default ticks*/
int timerId; /*timer ID*/
unsigned char useState; /*0: don't used 1: used*/
unsigned char timerType; /*0: no timer function ,1 have timer function */
void *timeParams;
int (*timerProcess)(int timerId); /*timer function*/
};
struct timer_struct g_sysTimerArray[SYS_TIMER_NUMBER];
/*************************************************
Function: secondsTimerSevice
Description:
Calls:
Called By:
Table Accessed:
Table Updated:
Input:
Output:
Return:
Others:
*************************************************/
void secondsTimerSevice(int s)
{
int index;
for(index = 0 ; index <SYS_TIMER_NUMBER ;index++)
{
if((g_sysTimerArray[index].useState ==1) &&(g_sysTimerArray[index].currentTicks != 0))
{
g_sysTimerArray[index].currentTicks--;
}
}
}
/*************************************************
Function: timerThread
Description: manage the timer of this system
Input:
Output: none
Return: 0 failure 1 success
*************************************************/
void timerThread(void)
{
int iIndex,iReturn,iMoniterTimerId;
struct itimerval tick;
float ftest;
char uBCD;
uBCD = 0x30;
/*initialize system timer*/
memset(&tick, 0, sizeof(tick));
tick.it_value.tv_sec = 0;
tick.it_value.tv_usec = 100000;
tick.it_interval.tv_sec = 0;
tick.it_interval.tv_usec = 100000;
signal(SIGALRM, secondsTimerSevice);
/*starting the real timer*/
iReturn = setitimer(ITIMER_REAL, &tick, NULL);
if(iReturn == -1)
{
printf("timerThread: set timer failed!!\n");
}
//moinitorStates
iMoniterTimerId = getSecondsTimer(g_sysTimerArray,SYS_TIMER_NUMBER, 1,100,NULL, moinitorStates);
//备注:moinitorStates 为一个定时器执行的函数;此处为声明
while (1)
{
pthread_mutex_lock(&g_Timermutex);
for(iIndex = 0; iIndex<SYS_TIMER_NUMBER; iIndex++)
{
if(g_sysTimerArray[iIndex].useState == 1 && g_sysTimerArray[iIndex].timerType ==1)
{
if(g_sysTimerArray[iIndex].currentTicks == 0 && g_sysTimerArray[iIndex].timerProcess != NULL)
{
g_sysTimerArray[iIndex].timerProcess(g_sysTimerArray[iIndex].timerId);
g_sysTimerArray[iIndex].currentTicks = g_sysTimerArray[iIndex].defaulTicks;
}
}
}
pthread_mutex_unlock(&g_Timermutex);
sleep(1);
}
}