函数sys_arch_timeouts返回对应于当前任务的指向定时事件链表的起始指针.该指针存在lwip_timeouts[MAX_LWIP_TASKS]中.
struct sys_timeouts null_timeouts;
struct sys_timeouts * sys_arch_timeouts(void)
{
u8_t curr_prio;
s16_t err,offset;
OS_TCB curr_task_PCB;
null_timeouts.next = NULL;
//获取当前任务的优先级
err = OSTaskQuery(OS_PRIO_SELF,&curr_task_pcb);
curr_prio = curr_task_pcb.OSTCBPrio;
offset = curr_prio - LWIP_START_PRIO;
//判断当前任务优先级是不是tcp/ip相关任务,优先级5-9
if(offset < 0 || offset >= LWIP_TASK_MAX)
{
return &null_timeouts;
}
return &lwip_timeouts[offset];
}
注意:杨晔大侠移植的代码在本函数有一个bug.杨晔大侠的移植把上面函数中的OS_TCB curr_task_tcb定义成了全局变量,使本函数成为了一个不可重入函数.我也是在进行如下测试时发现了这个bug.我的开发板上设置的ip地址是192.168.1.95.我在windows的dos窗口内运行
PINg 192.168.1.95 –l 2000 –t,不间断用长度为2000的数据报进行ping测试,同时使用tftp客户端软件给192.168.1.95下载一个十几兆程序,同时再使用telnet连接192.168.1.95端口7(echo端口),往该端口写数测试echo功能.
在运行一段时间以后,开发板进入不再响应.我当时也是经过长时间的分析才发现是因为在低优先级任务运行ys_arch_timeouts()时被高优先级任务打断改写了curr_task_tcb的值,从而使sys_arch_timeouts返回的指针错误,进而导致系统死锁.函数sys_timeout给当前任务增加一个定时事件:
void sys_timeout(u32_t msecs, sys_timeout_handler h, void *arg)
{
struct sys_timeouts *timeouts;
struct sys_timeout *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) {//如果链表为空直接增加该定时事件
timeouts->next = timeout;
return;
}
//如果链表不为空,对定时事件进行排序.注意定时事件中的time存储的是本事件
//时间相对于前一事件的时间的差值
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;
}
}
}
}
函数sys_untimeout从当前任务定时事件链表中删除一个定时事件
void sys_untimeout(sys_timeout_handler h, void *arg)
{
struct sys_timeouts *timeouts;
struct sys_timeout *prev_t, *t;
timeouts = sys_arch_timeouts();//返回当前任务定时事件链表起始指针
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))
{
/* We have a match */
/* Unlink from previous in list */
if (prev_t == NULL)
timeouts->next = t->next;
else
prev_t->next = t->next;
/* If not the last one, add time of this one back to next */
if (t->next != NULL)
t->next->time += t->time;
memp_free(MEMP_SYS_TIMEOUT, t);
return;
}
}
return;
}
2.2.3 “mbox”的实现:
(1)mbox的创建
sys_mbox_t sys_mbox_new(void)
{
u8_t ucErr;
PQ_DESCR pQDesc;
//从消息队列内存分区中得到一个内存块
pQDesc = OSMemGet( pQueueMem, &ucErr );
本文关键字:暂无联系方式嵌入式系统-技术,单片机-工控设备 - 嵌入式系统-技术
上一篇:基于RTOS的智能交通灯设计方法