linux c timer 线程,自己通过线程实现的简易定时器(Timer)

#include

#include

#define OK (0)

#define ERROR (-1)

typedef enum{

TIMER_ID_NULL = -1,

TIMER_ID_GUI = 0,

//add new TIMER after this line

TIMER_ID_COUNT

}TIMER_ID_ENUM;

typedef  Int (*TimerCallback)(void *param);

typedef struct timer {

Bool  running; /*Timer state*/

UInt32 interval;

UInt32 starttime; /*Timer start time*/

TimerCallback cb; /*Timer callback function*/

void *param;  /*callback function param*/

}Timer;

static Timer Timers[TIMER_ID_COUNT];

Int timer_init(void);

void timer_exit(void);

Int32  add_timer(Int32 timer_id, UInt32 interval, TimerCallback callback, void *param);

Int32 remove_timer(Int32 timer_id);

typedef struct timer_context{

pthread_t thread_id;

pthread_mutex_t  *p_timer_mutex;

Bool abort;

}Timer_Context;

static Timer_Context  timer_context={

.thread_id = 0,

.p_timer_mutex = NULL,

};

void ThreadedTimerCheck(void)

{

UInt32 now;

Int cb_return,i;

do{

for(i=0; i{

pthread_mutex_lock(timer_context.p_timer_mutex);

if(Timers[i].running && Timers[i].cb != NULL)

{

now = ipanel_porting_time_ms();

if(now > Timers[i].starttime+ Timers[i].interval)

{

/*先注销Timer在执行回调函数,防止注销掉回调函数内部添加新的Timer*/

Timers[i].starttime = 0;

Timers[i].interval = 0;

Timers[i].running = FALSE;

pthread_mutex_unlock(timer_context.p_timer_mutex);

cb_return = Timers[i].cb(Timers[i].param); /*回调函数不属于互斥操作,否则无法完成回调函数内部添加新的Timer的工作*/

continue;

}

}

pthread_mutex_unlock(timer_context.p_timer_mutex);

}

if(timer_context.abort)

{

printf( "ThreadedTimerCheck: abort .\n");

break;

}

}while(1);

printf( "ThreadedTimerCheck: return .\n");

}

Int timer_init(void)

{

Int i;

timer_context.p_timer_mutex= malloc(sizeof(pthread_mutex_t));

if(!timer_context.p_timer_mutex)

{

printf("[%s]---malloc faile for  p_timer_mutex  \n",__FUNCTION__);

return ERROR;

}

pthread_mutex_init(timer_context.p_timer_mutex, NULL);

for(i=0;i++;i{

Timers[i].running = FALSE;

Timers[i].starttime = 0;

Timers[i].interval = 0;

Timers[i].cb = NULL;

Timers[i].param = NULL;

}

if( pthread_create(&timer_context.thread_id, NULL, ThreadedTimerCheck, (void *)NULL))

{

printf("[timer_init]-->pthread_create  ThreadedTimerCheck failed!\n");

return ERROR;

}

return OK;

}

void timer_exit(void)

{

timer_context.abort = TRUE;

pthread_mutex_destroy(timer_context.p_timer_mutex);

free(timer_context.p_timer_mutex);

timer_context.p_timer_mutex = NULL;

pthread_exit(timer_context.thread_id);

return;

}

/* ==========================================================

*     开发人员:woodsjiang

*     编写时间:2008-7-4

*     函数名称:AddTimer

*     参数说明:Int32 timer_id, TIMER_ID_ENUM中定义的ID

*                         UInt32 interval, 定时器的时间,计时单位为ms

*                         TimerCallback callback, 定时器触发后的回调函数

*                         void *param,定时器回调函数的参数

*

*                         成功返回TimerID,失败返回-1

*     功能说明:添加某一模块使用的定时器,目前浏览器中只在GUI模块中需要一个定时器

*                         ;其他木块如果需要,只需在TIMER_ID_ENUM中添加一个新的ID

*/

Int32  add_timer(Int32 timer_id, UInt32 interval, TimerCallback callback, void *param)

{

if(timer_id>=TIMER_ID_COUNT || timer_id<=TIMER_ID_NULL\

|| interval <= 0 || callback==NULL)

{

printf("[add_timer]---failed  timer_id:%d ,interval:%d!\n",timer_id ,interval );

return TIMER_ID_NULL;

}

pthread_mutex_lock(timer_context.p_timer_mutex);

Timers[timer_id].running = TRUE;

Timers[timer_id].interval = interval;

Timers[timer_id].starttime = ipanel_porting_time_ms();

Timers[timer_id].cb = callback;

Timers[timer_id].param = param;

pthread_mutex_unlock(timer_context.p_timer_mutex);

return timer_id;

}

/* ==========================================================

*     开发人员:woodsjiang

*     编写时间:2008-7-4

*     函数名称:RemoveTimer

*     参数说明:Int32 timer_id,要删除的TIMER_ID

*                      &nb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值