采用这种宏定义方法是为了增强程序的可读性。比如if(ptcb->OSTCBStat == 0),你可以一下看不出这是判断任务是否就绪的条件,但如果写成if(ptcb->OSTCBStat == OS_STAT_RDY),马上就知道它是在干什么了。但由C语言宏定义的知识可知,这两句话是完全等价的,但后者的可读性明显更高。
任务2输出5之后,由于等待信号量被挂起,之后任务1输出3,然后发信号量,注意发信号亮并不是简单的+1,OSSemPost中有这几行代码if(pevent->OSEventGrp!=0){(void)OS_EventTaskRdy(pevent,(void*)0,OS_STAT_SEM,OS_STAT_PEND_OK)OS_EXIT_CRITICAL()OS_Sched()return(OS_ERR_NONE)}此时因为任务2的优先级较1低,所以没有立即运行,而是任务1继续输出4、0然后挂起自己,任务2就运行了,输出678OSSemPost()结束前会引发一次调度OS_Sched(),选择就绪表中优先级最高的任务运行,如果任务1的优先级高,会运行任务1,实时操作系统的任务都会被及时运行void OSSemPend (OS_EVENT *pevent, INT16U timeout, INT8U *err)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr
#endif
if (OSIntNesting >0) { /* See if called from ISR ...*/
*err = OS_ERR_PEND_ISR /* ... can't PEND from an ISR*/
return
}
#if OS_ARG_CHK_EN >0
if (pevent == (OS_EVENT *)0) {/* Validate 'pevent' */
*err = OS_ERR_PEVENT_NULL
return
}
if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */
*err = OS_ERR_EVENT_TYPE
return
}
#endif
OS_ENTER_CRITICAL()
if (pevent->OSEventCnt >0) { /* If sem. is positive, resource available ... */
pevent->OSEventCnt--/* ... decrement semaphore only if positive. */
OS_EXIT_CRITICAL()
*err = OS_NO_ERR
return
}
/* Otherwise, must wait until event occurs */
OSTCBCur->OSTCBStat |= OS_STAT_SEM /* Resource not available, pend on semaphore */
OSTCBCur->OSTCBDly = timeout /* Store pend timeout in TCB */
OS_EventTaskWait(pevent)/* Suspend task until event or timeout occurs*/
OS_EXIT_CRITICAL()
OS_Sched() /* Find next highest priority task ready */
OS_ENTER_CRITICAL()
if (OSTCBCur->OSTCBStat &OS_STAT_SEM) { /* Must have timed out if still waiting for event*/
OS_EventTO(pevent)
OS_EXIT_CRITICAL()
*err = OS_TIMEOUT /* Indicate that didn't get event within TO */
return
}
OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0
OS_EXIT_CRITICAL()
*err = OS_NO_ERR
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)