您的位置:首页 > 其它

定时链表处理 程序 采用 链表结构 同时运行多个定时器 仿PLC定时器结构实现

2015-11-06 09:43 344 查看
定时链表:

typedef void (* sys_timeout_handler)(void *arg);

struct sys_timeo

{

struct sys_timeo *next;

u32_t time;

sys_timeout_handler h;

void *arg;

};

//定时链表首部结构

struct sys_timeouts

{

//指向第一个定时结构指针

struct sys_timeo *next;

};

struct sys_timeouts lwip_timeouts[4];

定时链表初始化

定时链表添加节点

void sys_timeout(u32_t msecs,sys_timeout_handler h,void*arg)

{

struct sys_timeouts *timeouts;

struct sys_timeo *timeout,*t;

timeout=memp_malloc(MEMP_SYS_TIMEOUT);

if(timeout == NULL)

{

return;

}

timeout->next=NULL;

timeout->h=h;

timeout->arg=arg;

timeout->time=msecs;

//获得定时链表首部结构指针

timeouts=sys_arch_timeouts();

if(timeouts->next == NULL)

{

return;

}

if(timeouts->next == NULL)

{

timeouts->next==timeout;

return;

}

//将定时事件插入到定时链表中

//新事件比链表上第一个定时事件的定时时间短

if(timeouts->next->time >msecs)

{

//调整第一个定时事件的定时时间

timeouts->next->time -= msecs;

//新事件成为链表上的第一个节点

timeout->next =timeouts->next;

timeouts->next =timeout;

}

else

{

for(t=timeouts->next;t!=NULL;t=t->next)

{

timeout->time -= t->time;

if(t->next == NULL || t->next->time > timeout->time)

{

if(t->next != NULL)

{

t->next->time -= timeout->time;

}

timeout ->next =t->next;

t->next =timeout

break;

}

}

}

}

定时链表删除

void sys_untimeout(sys_timeout_handler h, void *arg)

{

struct sys_timeouts *timeouts;

struct sys_timeo *prev_t;

struct sys_timeo *t;

timeouts = sys_arc_timeouts();

if(timeouts == NULL)

{

LWIP_ASSERT("sys_untimeout: timeouts != NULL", timeouts != NULL);

return;

}

if(timeouts->next == NULL)

{

return;

}

for(t= timeouts-> next,prev_t = NULL; t!= NULL; prev_t = t, t = t->next)

{

if((t-> h == h)&&(t->arg == arg))

{

if(prev_t ==NULL)

{

timeouts-> next = t->next;

}

else

{

prev_t->next = t->next;

}

if (t->next != NULL)

{

t->next->time += t->time;

}

memp_free(MEMP_SYS_TIMEOUT, t);

return;

}

}

return;

}

定时链表查询

struct sys_timeouts *

sys_arch_timeouts(void)

{

u8_t curr_prio;

s8_t ubldx;

OS_TCB curr_task_pcb;

null_timeouts.next = NULL;

OSTaskQuery(OS_PRIO_SELF,&curr_task_pcb);

curr_prio = curr_task_pcb.OSTCBPrio;

ubldx = (u8_t)(curr_prio - LWIP_START_PRIO);

if((ubldx>=0) && (ubldx<LWIP_TASK_MAX))

{

/* printf("\nlwip_timeouts[%d],prio=%d!!! \n",ubldx,curr_prio); */

return &lwip_timeouts[ubldx];

}

else

{

/* printf("\nnull_timeouts,prio=%d!!! \n",curr_prio); */

return &null_timeouts;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: