定时链表处理 程序 采用 链表结构 同时运行多个定时器 仿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;
}
}
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;
}
}
相关文章推荐
- MySQL数据库安全配置
- IOS项目集成ShareSDK实现第三方登录、分享、关注等功能。
- Java NIO使用及原理分析(二)
- 利用Server2008影卷复制功能快速恢复误删文件
- 黑马程序员---自学随堂笔记----线程
- Apache WEB Server WIN版下载地址,貌似新手不容易找到
- Spring使用非applicationContext.xm 默认名的配置文件的配置
- html招聘简历解析并入库测试
- android studio 安装
- MyEclipse快捷键
- JAVA项目开发中常用软件的安装配置
- uwin7下secureCRT SSH2远程登录Ubuntu系统问题解决
- javascript数据类型
- 面向对象程序设计思想 在单片机中的应用
- libevent代码阅读(3)——入门例子“hello-world.c”的学习
- 递归调用的题目-腾讯面试题
- window.open不被拦截的实现代码
- Socket编程实践(11) --UDP编程基础
- 基于java配置启动spring容器
- HDU 4756 Install Air Conditioning 块与块,非树边最短路