1. 程式人生 > >uC/OS II 函式說明之與訊號量操作相關的六個函式

uC/OS II 函式說明之與訊號量操作相關的六個函式

    if (OSIntNesting > 0) {                           /* ISR中,不允許此操作                           */
        *err = OS_ERR_PEND_ISR;                     
        return;
    }
#if OS_ARG_CHK_EN > 0
    if (pevent == (OS_EVENT *)0) {                    /* 引數驗證                                      */
        *err = OS_ERR_PEVENT_NULL;
        return;
    }
    if (pevent->OSEventType != OS_EVENT_TYPE_SEM) {   /* 引數驗證                                      */
        *err = OS_ERR_EVENT_TYPE;
        return;
    }
#endif
    OS_ENTER_CRITICAL();
    if (pevent->OSEventCnt > 0) {                     /* 如果訊號量代表的資源至少有一個可以使用        */
        pevent->OSEventCnt–;                         /* 訊號量減1,從函式返回,繼續執行下面的程式     */
        OS_EXIT_CRITICAL();
        *err = OS_NO_ERR;
        return;
    }
                                                      /* 訊號量為0,則必須等待該訊號量被別的任務釋放   */
    OSTCBCur->OSTCBStat |= OS_STAT_SEM;               /* 訊號量型別                                    */
    OSTCBCur->OSTCBDly   = timeout;                   /* 等待超時時間                                  */
    OS_EventTaskWait(pevent);                         /* 任務掛起,詳見《與ECB操作相關的四個函式》     */
    OS_EXIT_CRITICAL();
    OS_Sched();                                       /* 該優先順序的任務不可用,尋找下一優先順序任務      */
    OS_ENTER_CRITICAL();
    if (OSTCBCur->OSTCBStat & OS_STAT_SEM) {          /* 任務等待超時                                  */
        OS_EventTO(pevent);
        OS_EXIT_CRITICAL();
        *err = OS_TIMEOUT;                            /* 返回超時標誌                                  */
        return;
    }
    OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0;
    OS_EXIT_CRITICAL();
    *err = OS_NO_ERR;
}