#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 = get_system_time_ms();//根据不同操作系统提供的API来调用
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 = get_system_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
*
* 成功返回TimerID,失败返回-1
* 功能说明:删除已经添加的定时器*/
Int32 browser_remove_timer(Int32 timer_id)
{
if(timer_id>=TIMER_ID_COUNT || timer_id<=TIMER_ID_NULL)
{
printf("[browser_remove_timer]---timer_id:%d invalid!\n",timer_id );
return TIMER_ID_NULL;
}
if(STB_sem_take(timer_context.timer_sem,-1))
{
printf( "browser_remove_timer:STB_sem_take failed.\n");
return ERROR;
}
Timers[timer_id].running = FALSE;
if(STB_sem_give(timer_context.timer_sem))
{
printf("browser_remove_timer:STB_sem_give failed.\n");
return ERROR;
}
return timer_id;
}
阅读(4900) | 评论(6) | 转发(0) |