uCOS II中信号量挂起超时的时间设置为0时有的人说永远等待下去,有的说最大等待65535个周期,哪个对???

2019-07-21 03:56发布

如题:有点时间学习了一下ucosII,在看到信号量挂起函数如果超时设置为0,源码中写的是永远等待下去(If you specify 0, however, your task will wait forever)。但是在钟前辈的源码解释手册的p82和力天电子的一份手册中都写着最多等待65535个时钟。这到底是哪个对?分析了源码也没找到原因,哪位高手指点一下呢,谢谢




下面是2.92的源码中的信号量挂起函数: [mw_shl_code=c,true]********************************************************************************************************* * PEND ON SEMAPHORE * * Description: This function waits for a semaphore. * * Arguments : pevent is a pointer to the event control block associated with the desired * semaphore. * * timeout is an optional timeout period (in clock ticks). If non-zero, your task will * wait for the resource up to the amount of time specified by this argument. * If you specify 0, however, your task will wait forever at the specified * semaphore or, until the resource becomes available (or the event occurs). * * perr is a pointer to where an error message will be deposited. Possible error * messages are: * * OS_ERR_NONE The call was successful and your task owns the resource * or, the event you are waiting for occurred. * OS_ERR_TIMEOUT The semaphore was not received within the specified * 'timeout'. * OS_ERR_PEND_ABORT The wait on the semaphore was aborted. * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore. * OS_ERR_PEND_ISR If you called this function from an ISR and the result * would lead to a suspension. * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer. * OS_ERR_PEND_LOCKED If you called this function when the scheduler is locked * * Returns : none ********************************************************************************************************* */ /*$PAGE*/ void OSSemPend (OS_EVENT *pevent, INT32U timeout, INT8U *perr) { #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0u; #endif #ifdef OS_SAFETY_CRITICAL if (perr == (INT8U *)0) { OS_SAFETY_CRITICAL_EXCEPTION(); return; } #endif #if OS_ARG_CHK_EN > 0u if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */ *perr = OS_ERR_PEVENT_NULL; return; } #endif if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */ *perr = OS_ERR_EVENT_TYPE; return; } if (OSIntNesting > 0u) { /* See if called from ISR ... */ *perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */ return; } if (OSLockNesting > 0u) { /* See if called with scheduler locked ... */ *perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */ return; } OS_ENTER_CRITICAL(); if (pevent->OSEventCnt > 0u) { /* If sem. is positive, resource available ... */ pevent->OSEventCnt--; /* ... decrement semaphore only if positive. */ OS_EXIT_CRITICAL(); *perr = OS_ERR_NONE; return; } /* Otherwise, must wait until event occurs */ OSTCBCur->OSTCBStat |= OS_STAT_SEM; /* Resource not available, pend on semaphore */ OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK; 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(); switch (OSTCBCur->OSTCBStatPend) { /* See if we timed-out or aborted */ case OS_STAT_PEND_OK: *perr = OS_ERR_NONE; break; case OS_STAT_PEND_ABORT: *perr = OS_ERR_PEND_ABORT; /* Indicate that we aborted */ break; case OS_STAT_PEND_TO: default: OS_EventTaskRemove(OSTCBCur, pevent); *perr = OS_ERR_TIMEOUT; /* Indicate that we didn't get event within TO */ break; } OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Set task status to ready */ OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK; /* Clear pend status */ OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0; /* Clear event pointers */ #if (OS_EVENT_MULTI_EN > 0u) OSTCBCur->OSTCBEventMultiPtr = (OS_EVENT **)0; #endif OS_EXIT_CRITICAL(); } [/mw_shl_code]
附件为:钟高手的源码解释
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
7条回答
天时地利人和
1楼-- · 2019-07-21 08:23
回复【7楼】styleno1:
---------------------------------
哎,现在明白了,那个注释写的也太不严谨了吧。

正解:
timeout为非0时,最大等待(超时)时间为65535个节拍。
timeout为0时,将永远等待下去,直到收到合适的信号量。
天时地利人和
2楼-- · 2019-07-21 09:08
 精彩回答 2  元偷偷看……
正点原子
3楼-- · 2019-07-21 09:14
是永远等待。
ofourme
4楼-- · 2019-07-21 12:53
哈,你没看到前面有个句号吗?注释也有可能出错吧。
天时地利人和
5楼-- · 2019-07-21 18:27
回复【4楼】ofourme:
---------------------------------
句号我是看到了啊。但是后面的句子实在不知从哪里看起

而且写这句话的不止一个人,他们都是互相抄袭?
天时地利人和
6楼-- · 2019-07-21 23:16
回复【3楼】正点原子:
---------------------------------
谢谢

那这个注释和力天的那个文章都是错了。

一周热门 更多>