linux应用层定时器实现

源文件

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/queue.h>
#include <termios.h> 
#include <time.h>
 
#include "errcode.h"
#include "timer.h"


/**************************************************************************
    函数名称: timer_get_now_tick
    功能描述: 获取当前系统的时间,精确到毫秒 
    输入参数: 
    输出参数: 
    注 意 点: 
***************************************************************************/
unsigned long long timer_get_now_tick(void)
{
    unsigned long long value = 0;;
    struct timespec stTimespec;

    memset(&stTimespec, 0 ,sizeof(stTimespec));
    
    clock_gettime(CLOCK_MONOTONIC, &stTimespec);
    
    value = stTimespec.tv_sec * 1000 + stTimespec.tv_nsec / 1000000;
    return value;
}

/**************************************************************************
    函数名称: Timer_Golab_Init
    功能描述: 定时器初始化 
    输入参数: 
    输出参数: 
    注 意 点: 
***************************************************************************/
void Timer_Golab_Init(void)
{
    int index = 0;
    
    for (index = 0; index < OS_TIMER_MAX; index++)
    {
        memset(&gastTimerList[index], 0, sizeof(Timer_Node_t));
        gastTimerList[index].pfun = NULL;
        gastTimerList[index].pParam = NULL;
    }
}

/**************************************************************************
    函数名称: timer_add
    功能描述: 添加定时器 
    输入参数: IN int run_once               是否运行一次 0:永久运行 1:运行一次
              INOUT int *pTimer_ID      返回的定时器ID
              IN int intv_ms            定时器的时间 ms
              IN void *func             定时器的回调函数
              IN void *ptr              定时器的回调函数的入参
    输出参数: INOUT int *pTimer_ID      返回的定时器ID
    注 意 点: pTimer_ID入参时需要赋值为-1 对应宏定义 INVALID_TIMERID
***************************************************************************/
void timer_add(IN int run_once, INOUT int *pTimer_ID, IN int intv_ms, IN void *func, IN void *ptr)
{
    int i = 0;
    
    *pTimer_ID = -1;
    
    while ((gastTimerList[i].pfun != NULL) && (i < OS_TIMER_MAX))
    {
        i++;
    }

    if (OS_TIMER_MAX == i)
    {
        printf("timer is full\n");
        return;
    }
    
    gastTimerList[i].run_once = run_once;
    gastTimerList[i].intv_ms = intv_ms;
    gastTimerList[i].last_tick = timer_get_now_tick();
    gastTimerList[i].pfun = func;
    gastTimerList[i].pParam = ptr;
    
    *pTimer_ID = i;
}

/**************************************************************************
    函数名称: Timer_Add
    功能描述: 添加一个无限循环的定时器 
    输入参数: INOUT int *pTimer_ID          返回的定时器ID
              IN int intv_ms            定时器的时间 ms
              IN void *func             定时器的回调函数
              IN void *ptr              定时器的回调函数的入参
    输出参数: INOUT int *pTimer_ID      返回的定时器ID
    注 意 点: pTimer_ID入参时需要赋值为-1 对应宏定义 INVALID_TIMERID
***************************************************************************/
void Timer_Add(INOUT int *pTimer_ID, IN int intv_ms, IN void *func, IN void *ptr)
{
    timer_add(0, pTimer_ID, intv_ms, func, ptr);
}

/**************************************************************************
    函数名称: Timer_Add_once
    功能描述: 添加一个只执行一次的定时器 
    输入参数: INOUT int *pTimer_ID          返回的定时器ID
              IN int intv_ms            定时器的时间 ms
              IN void *func             定时器的回调函数
              IN void *ptr              定时器的回调函数的入参
    输出参数: INOUT int *pTimer_ID      返回的定时器ID
    注 意 点: pTimer_ID入参时需要赋值为-1 对应宏定义 INVALID_TIMERID
***************************************************************************/
void Timer_Add_once(INOUT int *pTimer_ID, IN int intv_ms, IN void *func, IN void *ptr)
{
    timer_add(1, pTimer_ID, intv_ms, func, ptr);
}

/**************************************************************************
    函数名称: Timer_Destroy
    功能描述: 销毁定时器 
    输入参数: INOUT int *pTimer_ID          返回的定时器ID
              IN int intv_ms            定时器的时间 ms
              IN void *func             定时器的回调函数
              IN void *ptr              定时器的回调函数的入参
    输出参数: 
    注 意 点: 
***************************************************************************/
void Timer_Destroy(int Timer_ID)
{
    if ((Timer_ID < 0) || (Timer_ID > OS_TIMER_MAX))
    {
        return;
    }
    
    gastTimerList[Timer_ID].run_once = 0;
    gastTimerList[Timer_ID].intv_ms = 0;
    gastTimerList[Timer_ID].last_tick = 0;
    gastTimerList[Timer_ID].pfun = NULL;
    gastTimerList[Timer_ID].pParam = NULL;
}

/**************************************************************************
    函数名称: Timer_Check_env
    功能描述: 定时器超时检测及相关事件执行 
    输入参数: 
    输出参数: 
    注 意 点: 该函数需要放到一个循环里面反复执行
***************************************************************************/
void Timer_Check_env(void)
{
    int i = 0;
    unsigned long long nowtick = 0;
    
    for (i = 0; i < OS_TIMER_MAX; i++)
    {
        nowtick = timer_get_now_tick();
        
        if ((0 == gastTimerList[i].intv_ms) || (0 == gastTimerList[i].last_tick))
        {
            continue;
        }
        
        if ((nowtick - gastTimerList[i].last_tick) >= gastTimerList[i].intv_ms)
        {
            gastTimerList[i].last_tick = nowtick;
            if (NULL != gastTimerList[i].pfun)
            {
                gastTimerList[i].pfun(gastTimerList[i].pParam);
                
                if (0 == gastTimerList[i].run_once)
                {
                    Timer_Destroy(i);
                }
            }
        }
    }
    
}

/**************************************************************************
    函数名称: Timer_Detect_Thread
    功能描述: 定时器检测处理线程 
    输入参数: IN void *arg
    输出参数: 
    注 意 点: 
***************************************************************************/
void *Timer_Detect_Thread(IN void *arg)
{
    while (1)
    {
        Timer_Check_env();
        usleep(10*1000);
    }

    return NULL;
}


头文件

#ifndef _TIMER_H
#define _TIMER_H

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/queue.h>
#include <termios.h> 
#include <time.h>

#define  OS_TIMER_MAX  32
#define  INVALID_TIMERID  (-1)



/* 一个定时器对应一个节点 */
typedef struct timer_node_s
{
    unsigned char run_once;
    unsigned long long intv_ms;
    unsigned long long last_tick;
    void *pParam;
    void (*pfun)(void *pParam);
}Timer_Node_t;



Timer_Node_t gastTimerList[OS_TIMER_MAX];


void Timer_Golab_Init(void);
void Timer_Add(int *pTimer_ID, int intv_ms, void *func, void *ptr);/* 添加一个无限循环的定时器 */
void Timer_Add_once(int *pTimer_ID, int intv_ms, void *func, void *ptr);/* 添加一个只执行一次的定时器 */
void Timer_Check_env(void);/* 定时器超时检测及相关事件执行 */
void *Timer_Detect_Thread(IN void *arg);

#endif//_TIMER_H


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值