📄 sys.c
字号:
/* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels <adam@sics.se> * */#include "lwip/sys.h"#include "lwip/opt.h"#include "lwip/def.h"#include "lwip/memp.h"#if (NO_SYS == 0)struct sswt_cb{ s16_t timeflag; sys_sem_t *psem;};voidsys_mbox_fetch(sys_mbox_t mbox, void **msg){ u32_t time; struct sys_timeouts *timeouts; struct sys_timeout *tmptimeout; sys_timeout_handler h; void *arg; again: timeouts = sys_arch_timeouts(); //返回当前任务定时事件链表起始指针 if (!timeouts || !timeouts->next) { //如果定时事件链表为空 sys_arch_mbox_fetch(mbox, msg, 0); //无超时等待消息 } else { if (timeouts->next->time > 0) { //如果超时事件链表不为空,而且第一个超时事件的time !=0//带超时等待消息队列,超时时间等于超时事件链表中第一个超时事件的time, time = sys_arch_mbox_fetch(mbox, msg, timeouts->next->time); //在后面分析中可以看到sys_arch_mbox_fetch调用了ucos中的OSQPend系统调//用从消息队列中读取消息.//如果”mbox”消息队列不为空,任务立刻返回,否则任务进入阻塞态.//需要重点说明的是sys_arch_mbox_fetch的返回值time:如果sys_arch_mbox_fetch//因为超时返回,time=SYS_ARCH_TIMEOUT,//如果sys_arch_mbox_fetch因为收到消息而返回,//time = 收到消息时刻的时间-执行sys_arch_mbox_fetch时刻的时间,单位是毫秒//由于在ucos中任务调用OSQPend系统调用进入阻塞态,到收到消息重新开始执行//这段时间没有记录下来,所以我们要简单修改ucos的源代码.(后面我们会看到). } else { //如果定时事件链表不为空,而且第一个定时事件的time ==0,表示该事件的定时//时间到 time = SYS_ARCH_TIMEOUT; } if (time == SYS_ARCH_TIMEOUT) { /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message could be fetched. We should now call the timeout handler and deallocate the memory allocated for the timeout. */ //一个定时事件的定时时间到 tmptimeout = timeouts->next; timeouts->next = tmptimeout->next; h = tmptimeout->h; arg = tmptimeout->arg; memp_free(MEMP_SYS_TIMEOUT, tmptimeout); //从内存中释放该定时事件,并执行该定时事件中的函数 if (h != NULL) { LWIP_DEBUGF(SYS_DEBUG, ("smf calling h=%p(%p)\n", (void *)h, (void *)arg)); h(arg); } //因为定时事件中的定时时间到或者是因为sys_arch_mbo_fetch超时到而执行到//这里,返回本函数开头重新等待mbox的消息 /* We try again to fetch a message from the mbox. */ goto again; } else { //如果sys_arch_mbox_fetch无超时收到消息返回//则刷新定时事件链表中定时事件的time值. /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout occured. The time variable is set to the number of milliseconds we waited for the message. */ if (time <= timeouts->next->time) { timeouts->next->time -= time; } else { timeouts->next->time = 0; } } }}voidsys_sem_wait(sys_sem_t sem){ u32_t time; struct sys_timeouts *timeouts; struct sys_timeout *tmptimeout; sys_timeout_handler h; void *arg; /* while (sys_arch_sem_wait(sem, 1000) == 0); return;*/ again: timeouts = sys_arch_timeouts(); if (!timeouts || !timeouts->next) { sys_arch_sem_wait(sem, 0); } else { if (timeouts->next->time > 0) { time = sys_arch_sem_wait(sem, timeouts->next->time); } else { time = SYS_ARCH_TIMEOUT; } if (time == SYS_ARCH_TIMEOUT) { /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message could be fetched. We should now call the timeout handler and deallocate the memory allocated for the timeout. */ tmptimeout = timeouts->next; timeouts->next = tmptimeout->next; h = tmptimeout->h; arg = tmptimeout->arg; memp_free(MEMP_SYS_TIMEOUT, tmptimeout); if (h != NULL) { LWIP_DEBUGF(SYS_DEBUG, ("ssw h=%p(%p)\n", (void *)h, (void *)arg)); h(arg); } /* We try again to fetch a message from the mbox. */ goto again; } else { /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout occured. The time variable is set to the number of milliseconds we waited for the message. */ if (time <= timeouts->next->time) { timeouts->next->time -= time; } else { timeouts->next->time = 0; } } }}//函数sys_timeout给当前任务增加一个定时事件:voidsys_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(); //返回当前任务定时事件链表起始指针 LWIP_DEBUGF(SYS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" h=%p arg=%p\n", (void *)timeout, msecs, (void *)h, (void *)arg)); LWIP_ASSERT("sys_timeout: timeouts != NULL", timeouts != NULL); 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; } } }}/* Go through timeout list (for this task only) and remove the first matching entry, even though the timeout has not triggered yet.*///函数sys_untimeout从当前任务定时事件链表中删除一个定时事件voidsys_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;}static voidsswt_handler(void *arg){ struct sswt_cb *sswt_cb = (struct sswt_cb *) arg; /* Timeout. Set flag to TRUE and signal semaphore */ sswt_cb->timeflag = 1; sys_sem_signal(*(sswt_cb->psem));}/* Wait for a semaphore with timeout (specified in ms) *//* timeout = 0: wait forever *//* Returns 0 on timeout. 1 otherwise */intsys_sem_wait_timeout(sys_sem_t sem, u32_t timeout){ struct sswt_cb sswt_cb; sswt_cb.psem = &sem; sswt_cb.timeflag = 0; /* If timeout is zero, then just wait forever */ if (timeout > 0) /* Create a timer and pass it the address of our flag */ sys_timeout(timeout, sswt_handler, &sswt_cb); sys_sem_wait(sem); /* Was it a timeout? */ if (sswt_cb.timeflag) { /* timeout */ return 0; } else { /* Not a timeout. Remove timeout entry */ sys_untimeout(sswt_handler, &sswt_cb); return 1; }}voidsys_msleep(u32_t ms){ sys_sem_t delaysem = sys_sem_new(0); sys_sem_wait_timeout(delaysem, ms); sys_sem_free(delaysem);}#endif /* NO_SYS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -