【 uC/OS II 】uC/OS II 源代码阅读(os_mutex.c)互斥信号量

前言

这个关于互斥信号量的代码,最核心的就是优先级反转。先说一下优先级反转吧。

在创建互斥信号量的时候,需要传入反转的优先级,也就是当发生优先级反转的时候,将当前拥有信号量的任务的优先级提升为反转的优先级。

优先级反转情景:

假设有两个任务A,任务B,任务A的优先级小于任务B。此时A拥有互斥信号量,但是此时B来了,B的优先级高于A,但是因为B也需要互斥信号量,所以按照常理来说,B应该等待信号量,但是这是一个RTOS,本身就是基于优先级抢占的,所以此时为了缩短B的等待时间,需要B任务来主动的将A任务的优先级提升为反转的优先级,然后再将A以新的反转优先级存在于就绪表中,反转的优先级理论上应该大于所有任务,这样的话,只要优先级一反转,任务A就会立即进行。从而B的等待时间就会缩短。上述操作为A在就绪状态下。但是如果A除了等待CPU外,还在等待其他事件的发生,任务B同样需要修改A的优先级,然后再修改A对应事件等待表的优先级,然后B修改自己的TCB状态,将自己修改为Pend信号量状态,然后进入此信号量的等待表,最后由于修改了A的优先级,需要进行一次调度。

核心结构体:

#if (OS_EVENT_EN) && (OS_MAX_EVENTS > 0u)
typedef struct os_event {
    INT8U    OSEventType;                   /* Type of event control block (see OS_EVENT_TYPE_xxxx)    */
    void    *OSEventPtr;                    /* Pointer to message or queue structure                   */
    INT16U   OSEventCnt;                    /* Semaphore Count (not used if other EVENT type)          */
    OS_PRIO  OSEventGrp;                    /* Group corresponding to tasks waiting for event to occur */
    OS_PRIO  OSEventTbl[OS_EVENT_TBL_SIZE]; /* List of tasks waiting for event to occur                */

#if OS_EVENT_NAME_EN > 0u
    INT8U   *OSEventName;
#endif
} OS_EVENT;
#endif

只不过此时的(OSEventPtr)用来指示当前拥有此信号量的TCB,(OSEventCnt)高8位为反转优先级,低8位为拥有此信号量的TCB的优先级。

	/*互斥信号量(已看)
*********************************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*                                  MUTUAL EXCLUSION SEMAPHORE MANAGEMENT
*
*                           (c) Copyright 1992-2017; Micrium, Inc.; Weston; FL
*                                           All Rights Reserved
*
* File    : OS_MUTEX.C
* By      : Jean J. Labrosse
* Version : V2.92.13
*
* LICENSING TERMS:
* ---------------
*   uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research.
* If you plan on using  uC/OS-II  in a commercial product you need to contact Micrium to properly license
* its use in your product. We provide ALL the source code for your convenience and to help you experience
* uC/OS-II.   The fact that the  source is provided does  NOT  mean that you can use it without  paying a
* licensing fee.
*
* Knowledge of the source code may NOT be used to develop a similar product.
*
* Please help us continue to provide the embedded community with the finest software available.
* Your honesty is greatly appreciated.
*
* You can find our product's user manual, API reference, release notes and
* more information at https://doc.micrium.com.
* You can contact us at www.micrium.com.
*********************************************************************************************************
*/

#define  MICRIUM_SOURCE


//采用预编译引入头文件
#ifndef  OS_MASTER_FILE
#include <ucos_ii.h>
#endif

//如果允许使用互斥信号量
#if OS_MUTEX_EN > 0u
/*
*********************************************************************************************************
*                                           LOCAL CONSTANTS(本地常量)
*********************************************************************************************************
*/

#define  OS_MUTEX_KEEP_LOWER_8   ((INT16U)0x00FFu)//获取低8位
#define  OS_MUTEX_KEEP_UPPER_8   ((INT16U)0xFF00u)//获取高8位

#define  OS_MUTEX_AVAILABLE      ((INT16U)0x00FFu)//获取低8位

/*
*********************************************************************************************************
*                                           LOCAL CONSTANTS(本地常量)
*********************************************************************************************************
*/

/**
 * 所有未加 static 前缀的全局变量和函数都具有全局可见性,其它的源文件也能访问。
 * 如果加了 static,就会对其它源文件隐藏。仅在当前文件可见。
 * 利用这一特性可以在不同的文件中定义同名函数和同名变量,而不必担心命名冲突。
 * static 可以用作函数和变量的前缀,1)对于函数来讲,static 的作用仅限于隐藏,2)而对于变量,static 还有下面两个作用。
 * 对于变量,还分有:静态全局变量、静态局部变量
 * 其次就是对于变量,静态变量存储在静态存储区,在静态数据区,内存中所有的字节默认值都是 0x00,这样就省去了初始化置零的操作
*/
static  void  OSMutex_RdyAtPrio(OS_TCB *ptcb, INT8U prio);//定义本文件可见的函数


/*
*********************************************************************************************************
*                                  ACCEPT MUTUAL EXCLUSION SEMAPHORE(无等待请求互斥信号量)
*
* Description: This  function checks the mutual exclusion semaphore to see if a resource is available.
*              Unlike OSMutexPend(), OSMutexAccept() does not suspend the calling task if the resource is
*              not available or the event did not occur.
*这个函数用来检查互斥信号量资源是否可用,不像OSMutexPend(),如果资源不可用,或者说事件尚未发生,OSMutexAccept()并不会挂起任务
* Arguments  : pevent     is a pointer to the event control block
*指向ECB的指针
*              perr       is a pointer to an error code which will be returned to your application:
*                            OS_ERR_NONE         if the call was successful.
*                            OS_ERR_EVENT_TYPE   if 'pevent' is not a pointer to a mutex
*                            OS_ERR_PEVENT_NULL  'pevent' is a NULL pointer
*                            OS_ERR_PEND_ISR     if you called this function from an ISR
*                            OS_ERR_PCP_LOWER    If the priority of the task that owns the Mutex is
*                                                HIGHER (i.e. a lower number) than the PCP.  This error
*                                                indicates that you did not set the PCP higher (lower
*                                                number) than ALL the tasks that compete for the Mutex.
*                                                Unfortunately, this is something that could not be
*                                                detected when the Mutex is created because we don't know
*                                                what tasks will be using the Mutex.
//PCP:优先级反转中的反转优先级
//如果拥有互斥信号量的任务的优先级比PCP要高。这个错误说明你并没有给PCP设置更高的优先级,这个优先级要比所有竞争此互斥信号量的任务的优先级都要高。不幸的是,我们在创建互斥信号量的时候并不知道有哪些任务会使用这个互斥信号量。
*
* Returns    : == OS_TRUE    if the resource is available, the mutual exclusion semaphore is acquired
*如果资源可用,互斥信号量可以被获得
*              == OS_FALSE   a) if the resource is not available
*                            b) you didn't pass a pointer to a mutual exclusion semaphore
*                            c) you called this function from an ISR
*
* Warning(s) : This function CANNOT be called from an ISR because mutual exclusion semaphores are
*              intended to be used by tasks only.
*注意:这个函数不能在ISR中被调用,因为互斥信号量仅仅只能被任务使用。(我的理解就是从ISR中调用此函数,说明ISR这个程序要使用信号量,这是错误的,因为ISR不能使用信号量)
*********************************************************************************************************
*/

//检查是否允许无等待请求互斥信号量
#if OS_MUTEX_ACCEPT_EN > 0u
BOOLEAN  OSMutexAccept (OS_EVENT  *pevent,
                        INT8U     *perr)
{
    //优先级反转中的反转优先级
    INT8U      pcp;                                    /* Priority Ceiling Priority (PCP)              */
#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 (OS_FALSE);
    }
#endif

//参数检查
#if OS_ARG_CHK_EN > 0u
    if (pevent == (OS_EVENT *)0) {                     /* Validate 'pevent'                            */
        *perr = OS_ERR_PEVENT_NULL;
        return (OS_FALSE);
    }
#endif
//检查ECB类型
    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) {  /* Validate event block type                    */
        *perr = OS_ERR_EVENT_TYPE;
        return (OS_FALSE);
    }
    //ISR不能调用此函数
    if (OSIntNesting > 0u) {                           /* Make sure it's not called from an ISR        */
        *perr = OS_ERR_PEND_ISR;
        return (OS_FALSE);
    }
    //进入临界区,关中断
    OS_ENTER_CRITICAL();                               /* Get value (0 or 1) of Mutex                  */
    //在普通信号量中,OSEventCnt存储的是信号量的资源数目,
    //在互斥信号量这里,存储的是两个优先级,高8位是反转的优先级,低8位是当前占有信号量的任务的优先级
    //获取反转的优先级
    pcp = (INT8U)(pevent->OSEventCnt >> 8u);           /* Get PCP from mutex                           */
    //如果低8位为0xFF,说明资源可用
    //查看互斥信号量是否可用
    if ((pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8) == OS_MUTEX_AVAILABLE) {
        //保持高8位,将低8位置0,用于低8位或上即将占有此信号量的任务的优先级
        pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8;   /*      Mask off LSByte (Acquire Mutex)         */
        //占有互斥信号量
        pevent->OSEventCnt |= OSTCBCur->OSTCBPrio;     /*      Save current task priority in LSByte    */
        //将ECB连接到TCB
        pevent->OSEventPtr  = (void *)OSTCBCur;        /*      Link TCB of task owning Mutex           */
        //#define  OS_PRIO_MUTEX_CEIL_DIS      0xFFu              /* Disable mutex priority ceiling promotion    */
        //查看当前信号量是否禁止优先级反转,以及当前申请资源的优先级数值是否小于PCP
        if ((pcp != OS_PRIO_MUTEX_CEIL_DIS) &&
            (OSTCBCur->OSTCBPrio <= pcp)) {            /*      PCP 'must' have a SMALLER prio ...      */
             //如果允许优先级反转,但是反转的优先级小于了TCB的优先级,则报错
             OS_EXIT_CRITICAL();                       /*      ... than current task!                  */
            *perr = OS_ERR_PCP_LOWER;
        } else {
            /**
             * 进入这里有两种情况:
             * 1. 不允许优先级反转:优先级反转只是可选的,如果禁用了,无所谓的,修改了OSEventCnt的低8位,OSEventPtr也指向了TCB。申请已经成功了
             * 2. 允许修改优先级,并且TCB的优先级数值大于PCP,这是成立的,允许优先级反转,资源申请成功
            */
             OS_EXIT_CRITICAL();
            *perr = OS_ERR_NONE;
        }
        return (OS_TRUE);
    }
    //如果资源被占用,请求失败,但是并不阻塞,所以不发生错误,只是返回请求失败而已
    OS_EXIT_CRITICAL();
    *perr = OS_ERR_NONE;
    return (OS_FALSE);
}
#endif


/*
*********************************************************************************************************
*                                 CREATE A MUTUAL EXCLUSION SEMAPHORE(创建一个互斥信号量)
*
* Description: This function creates a mutual exclusion semaphore.
*这个函数创建了一个互斥信号量
* Arguments  : prio          is the priority to use when accessing the mutual exclusion semaphore.  In
*                            other words, when the semaphore is acquired and a higher priority task
*                            attempts to obtain the semaphore then the priority of the task owning the
*                            semaphore is raised to this priority.  It is assumed that you will specify
*                            a priority that is LOWER in value than ANY of the tasks competing for the
*                            mutex. If the priority is specified as OS_PRIO_MUTEX_CEIL_DIS, then the
*                            priority ceiling promotion is disabled. This way, the tasks accessing the
*                            semaphore do not have their priority promoted.
*prio是当访问互斥信号量的时候需要使用的优先级,换句话说,当信号量被获得后,又一个优先级更高的任务尝试去获得这个信号量,那么当前拥有信号量的任务将会被提升到这个用于优先级反转的优先级。这种做法的前提是,你需要传递一个比任何竞争此资源的任务的优先级都高的优先级,也就是数值上要是最小的,如果这个优先级被打上了OS_PRIO_MUTEX_CEIL_DIS,那么优先级反转是被禁止了的。如果这样的话,那么当任务请求互斥资源的时候,就不会升级优先级
*              perr          is a pointer to an error code which will be returned to your application:
*                               OS_ERR_NONE                     if the call was successful.
*                               OS_ERR_CREATE_ISR               if you attempted to create a MUTEX from an
*                                                               ISR
*                               OS_ERR_ILLEGAL_CREATE_RUN_TIME  if you tried to create a mutex after
*                                                               safety critical operation started.
*                               OS_ERR_PRIO_EXIST               if a task at the priority ceiling priority
*                                                               already exist.
*                               OS_ERR_PEVENT_NULL              No more event control blocks available.
*                               OS_ERR_PRIO_INVALID             if the priority you specify is higher that
*                                                               the maximum allowed (i.e. > OS_LOWEST_PRIO)
*
* Returns    : != (void *)0  is a pointer to the event control clock (OS_EVENT) associated with the
*                            created mutex.(如果返回的不是0指针,那么就返回一个指向与此互斥信号量有关的ECB指针)
*              == (void *)0  if an error is detected.(如果发生了错误,那么就会返回0指针)
*
* Note(s)    : 1) The LEAST significant 8 bits of '.OSEventCnt' hold the priority number of the task
*                 owning the mutex or 0xFF if no task owns the mutex.
*OSEventCnt的低8位,用来保存拥有此互斥资源的任务的优先级,如果没有任务获得这个资源,那么低8位为全1,即0xFF
*              2) The MOST  significant 8 bits of '.OSEventCnt' hold the priority number used to
*                 reduce priority inversion or 0xFF (OS_PRIO_MUTEX_CEIL_DIS) if priority ceiling
*                 promotion is disabled.
OSEventCnt的高8位,用来保存优先级反转的优先级,如果优先级反转被禁用了的话,那么高8位就是0xFF(OS_PRIO_MUTEX_CEIL_DIS)
*********************************************************************************************************
*/

OS_EVENT  *OSMutexCreate (INT8U   prio,
                          INT8U  *perr)
{
    OS_EVENT  *pevent;
#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 ((OS_EVENT *)0);
    }
#endif

#ifdef OS_SAFETY_CRITICAL_IEC61508
    if (OSSafetyCriticalStartFlag == OS_TRUE) {
        OS_SAFETY_CRITICAL_EXCEPTION();
        *perr = OS_ERR_ILLEGAL_CREATE_RUN_TIME;
        return ((OS_EVENT *)0);
    }
#endif

//参数检查
#if OS_ARG_CHK_EN > 0u
    if (prio != OS_PRIO_MUTEX_CEIL_DIS) {
        if (prio >= OS_LOWEST_PRIO) {                      /* Validate PCP                             */
           *perr = OS_ERR_PRIO_INVALID;
            return ((OS_EVENT *)0);
        }
    }
#endif
//ISR程序不能创建互斥信号量
    if (OSIntNesting > 0u) {                               /* See if called from ISR ...               */
        *perr = OS_ERR_CREATE_ISR;                         /* ... can't CREATE mutex from an ISR       */
        return ((OS_EVENT *)0);
    }
    OS_ENTER_CRITICAL();//关中断,进入临界区
    //检查此信号量是否禁用优先级反转,如果没有禁用,则进入循环
    //如果禁用了优先级反转,那么根本就不会用到优先级列表中的占位操作
    if (prio != OS_PRIO_MUTEX_CEIL_DIS) {
        //检查,用于优先级反转的优先级是否已经被使用
        if (OSTCBPrioTbl[prio] != (OS_TCB *)0) {           /* Mutex priority must not already exist    */
            OS_EXIT_CRITICAL();                            /* Task already exist at priority ...       */
           *perr = OS_ERR_PRIO_EXIST;                      /* ... ceiling priority                     */
            return ((OS_EVENT *)0);
        }
        //如果不是的话,就占用此优先级
        OSTCBPrioTbl[prio] = OS_TCB_RESERVED;              /* Reserve the table entry                  */
    }

    //从事件控制块空闲列表中获取一个新的ECB
    pevent = OSEventFreeList;                              /* Get next free event control block        */
    //如果不存在可用的ECB
    if (pevent == (OS_EVENT *)0) {                         /* See if an ECB was available              */
        //再次检查优先级反转是否被禁用
        if (prio != OS_PRIO_MUTEX_CEIL_DIS) {
            //因为ECB申请不成功,所以将优先级列表中的占位去掉
            OSTCBPrioTbl[prio] = (OS_TCB *)0;              /* No, Release the table entry              */
        }
        OS_EXIT_CRITICAL();
       *perr = OS_ERR_PEVENT_NULL;                         /* No more event control blocks             */
        return (pevent);
    }
    //成功申请ECB
    //空闲列表指向下一个ECB
    OSEventFreeList     = (OS_EVENT *)OSEventFreeList->OSEventPtr; /* Adjust the free list             */
    OS_EXIT_CRITICAL();//开中断,退出临界区
    pevent->OSEventType = OS_EVENT_TYPE_MUTEX;
    //优先级左移8位,或上OS_MUTEX_AVAILABLE,构造16位的OSEventCnt,其中高8位为优先级反转的优先级,低8位为全1,表示当前信号量未被占用
    pevent->OSEventCnt  = (INT16U)((INT16U)prio << 8u) | OS_MUTEX_AVAILABLE; /* Resource is avail.     */
    //初始化操作,表示目前没有任何任务拥有此信号量
    pevent->OSEventPtr  = (void *)0;                       /* No task owning the mutex                 */
    //检查是否ECB允许使用名字
#if OS_EVENT_NAME_EN > 0u
    pevent->OSEventName = (INT8U *)(void *)"?";
#endif
//初始化等待组/表
    OS_EventWaitListInit(pevent);
    OS_TRACE_MUTEX_CREATE(pevent, pevent->OSEventName);
   *perr = OS_ERR_NONE;
    return (pevent);
}


/*
*********************************************************************************************************
*                                           DELETE A MUTEX(删除一个互斥信号量)
*
* Description: This function deletes a mutual exclusion semaphore and readies all tasks pending on the it.
*(这个函数删除一个互斥信号量,并且使所有在等待此信号量的任务都变为就绪状态)
* Arguments  : pevent        is a pointer to the event control block associated with the desired mutex.
*是一个指向与互斥信号量相关的ECB的指针
*              opt           determines delete options as follows:
*                            opt == OS_DEL_NO_PEND   Delete mutex ONLY if no task pending
*                            opt == OS_DEL_ALWAYS    Deletes the mutex even if tasks are waiting.
*                                                    In this case, all the tasks pending will be readied.
*
*              perr          is a pointer to an error code that can contain one of the following values:
*                            OS_ERR_NONE                  The call was successful and the mutex was deleted
*                            OS_ERR_DEL_ISR               If you attempted to delete the MUTEX from an ISR
*                            OS_ERR_INVALID_OPT           An invalid option was specified
*                            OS_ERR_ILLEGAL_DEL_RUN_TIME  If you tried to delete a mutex after safety
*                                                         critical operation started.
*                            OS_ERR_TASK_WAITING          One or more tasks were waiting on the mutex
*                            OS_ERR_EVENT_TYPE            If you didn't pass a pointer to a mutex
*                            OS_ERR_PEVENT_NULL           If 'pevent' is a NULL pointer.
*
* Returns    : pevent        upon error (如果发生错误返回pevent的指针)
*              (OS_EVENT *)0 if the mutex was successfully deleted.(如果互斥信号量被正确删除,则返回0指针)
*
* Note(s)    : 1) This function must be used with care.  Tasks that would normally expect the presence of
*                 the mutex MUST check the return code of OSMutexPend().
*
*              2) This call can potentially disable interrupts for a long time.  The interrupt disable
*                 time is directly proportional to the number of tasks waiting on the mutex.
*
*              3) Because ALL tasks pending on the mutex will be readied, you MUST be careful because the
*                 resource(s) will no longer be guarded by the mutex.
*
*              4) IMPORTANT: In the 'OS_DEL_ALWAYS' case, we assume that the owner of the Mutex (if there
*                            is one) is ready-to-run and is thus NOT pending on another kernel object or
*                            has delayed itself.  In other words, if a task owns the mutex being deleted,
*                            that task will be made ready-to-run at its original priority.
注意:
1)必须谨慎使用此功能。 通常期望互斥量存在的任务必须检查OSMutexPend()的返回码。
2)此调用可能会长时间禁用中断。 中断禁用时间与互斥锁上等待的任务数成正比。 
3)因为将准备好在互斥体上挂起的所有任务,所以您必须小心,因为资源将不再由互斥体保护。 
4)重要提示:在“ OS_DEL_ALWAYS”情况下,我们假设互斥对象的所有者(如果有)已经可以运行,因此不会在另一个内核对象上挂起或已延迟自身。 换句话说,如果一个任务拥有要删除的互斥锁,则该任务将以其原始优先级准备就绪。
*********************************************************************************************************
*/

//如果允许进行互斥量的删除
#if OS_MUTEX_DEL_EN > 0u
OS_EVENT  *OSMutexDel (OS_EVENT  *pevent,
                       INT8U      opt,
                       INT8U     *perr)
{
    BOOLEAN    tasks_waiting;
    OS_EVENT  *pevent_return;
    INT8U      pcp;                                        /* Priority ceiling priority                */
    INT8U      prio;
    OS_TCB    *ptcb;
#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 ((OS_EVENT *)0);
    }
#endif

#ifdef OS_SAFETY_CRITICAL_IEC61508
    if (OSSafetyCriticalStartFlag == OS_TRUE) {
        OS_SAFETY_CRITICAL_EXCEPTION();
        *perr = OS_ERR_ILLEGAL_DEL_RUN_TIME;
        return ((OS_EVENT *)0);
    }
#endif

//参数检查
#if OS_ARG_CHK_EN > 0u
    if (pevent == (OS_EVENT *)0) {                         /* Validate 'pevent'                        */
        *perr = OS_ERR_PEVENT_NULL;
        return (pevent);
    }
#endif

    OS_TRACE_MUTEX_DEL_ENTER(pevent, opt);
//检查等待ECB类型
    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) {      /* Validate event block type                */
        *perr = OS_ERR_EVENT_TYPE;
        OS_TRACE_MUTEX_DEL_EXIT(*perr);
        return (pevent);
    }
    //ISR中不能删除互斥信号量
    if (OSIntNesting > 0u) {                               /* See if called from ISR ...               */
        *perr = OS_ERR_DEL_ISR;                            /* ... can't DELETE from an ISR             */
        OS_TRACE_MUTEX_DEL_EXIT(*perr);
        return (pevent);
    }
    OS_ENTER_CRITICAL();//关中断,进入临界区
    if (pevent->OSEventGrp != 0u) {                        /* See if any tasks waiting on mutex        */
        tasks_waiting = OS_TRUE;                           /* Yes                                      */
    } else {
        tasks_waiting = OS_FALSE;                          /* No                                       */
    }
    //查看是否有任务在等待,并根据传入的opt决定如何处理
    switch (opt) {
    //如果没有任务在等待,也就是说目前不会涉及到优先级反转的相关操作,只需要操作ECB即可。对于就绪组/表啥的无操作
        case OS_DEL_NO_PEND:                               /* DELETE MUTEX ONLY IF NO TASK WAITING --- */
             if (tasks_waiting == OS_FALSE) {
//是否允许ECB使用名字
#if OS_EVENT_NAME_EN > 0u
                 pevent->OSEventName   = (INT8U *)(void *)"?";
#endif
                //获取优先级反转的优先级
                 pcp                   = (INT8U)(pevent->OSEventCnt >> 8u);
                //如果优先级反转没有被禁用,则释放由于优先级反转所占用的优先级
                 if (pcp != OS_PRIO_MUTEX_CEIL_DIS) {
                     OSTCBPrioTbl[pcp] = (OS_TCB *)0;      /* Free up the PCP                          */
                 }
                 //清空ECB,将其插入到ECB空闲列表
                 pevent->OSEventType   = OS_EVENT_TYPE_UNUSED;
                 pevent->OSEventPtr    = OSEventFreeList;  /* Return Event Control Block to free list  */
                 pevent->OSEventCnt    = 0u;
                 OSEventFreeList       = pevent;
                 OS_EXIT_CRITICAL();
                 *perr                 = OS_ERR_NONE;
                 pevent_return         = (OS_EVENT *)0;    /* Mutex has been deleted                   */
             } else {
                 OS_EXIT_CRITICAL();
                 *perr                 = OS_ERR_TASK_WAITING;
                 pevent_return         = pevent;
             }
             break;

        case OS_DEL_ALWAYS:                                /* ALWAYS DELETE THE MUTEX ---------------- */
            //获取优先级反转的优先级
             pcp  = (INT8U)(pevent->OSEventCnt >> 8u);                       /* Get PCP of mutex       */
             //如果优先级反转未被禁用,就要恢复拥有互斥信号量的任务的优先级
             if (pcp != OS_PRIO_MUTEX_CEIL_DIS) {
                 //获取低8位
                 prio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8); /* Get owner's orig prio  */
                 //获取拥有此互斥信号量的TCB指针
                 ptcb = (OS_TCB *)pevent->OSEventPtr;
                 //检查是否有任务拥有此信号量
                 if (ptcb != (OS_TCB *)0) {                /* See if any task owns the mutex           */
                     //检查是否进行了优先级反转
                     if (ptcb->OSTCBPrio == pcp) {         /* See if original prio was changed         */
                         OS_TRACE_MUTEX_TASK_PRIO_DISINHERIT(OSTCBCur, prio);
                         //将TCB以指定优先级就绪
                         OSMutex_RdyAtPrio(ptcb, prio);    /* Yes, Restore the task's original prio    */
                     }
                 }
             }
             //如果有任务在等待信号量,那么使所有任务恢复就绪态
             while (pevent->OSEventGrp != 0u) {            /* Ready ALL tasks waiting for mutex        */
                 (void)OS_EventTaskRdy(pevent, (void *)0, OS_STAT_MUTEX, OS_STAT_PEND_ABORT);
             }
#if OS_EVENT_NAME_EN > 0u
             pevent->OSEventName   = (INT8U *)(void *)"?";
#endif
            //再次获得优先级反转的优先级
             pcp                   = (INT8U)(pevent->OSEventCnt >> 8u);
             if (pcp != OS_PRIO_MUTEX_CEIL_DIS) {
                 OSTCBPrioTbl[pcp] = (OS_TCB *)0;          /* Free up the PCP                          */
             }//释放优先级反转所占用的优先级
             //清空TCB
             pevent->OSEventType   = OS_EVENT_TYPE_UNUSED;
             pevent->OSEventPtr    = OSEventFreeList;      /* Return Event Control Block to free list  */
             pevent->OSEventCnt    = 0u;
             OSEventFreeList       = pevent;               /* Get next free event control block        */
             OS_EXIT_CRITICAL();//退出临界区,开中断
             if (tasks_waiting == OS_TRUE) {               /* Reschedule only if task(s) were waiting  */
                 //无论是将拥有互斥信号量的任务恢复,还是释放了等待的任务,只要有任务就绪了,就需要进行一次调度,选择当前优先级最高的任务执行
                 OS_Sched();                               /* Find highest priority task ready to run  */
             }
             *perr         = OS_ERR_NONE;
             pevent_return = (OS_EVENT *)0;                /* Mutex has been deleted                   */
             break;

        default:
             OS_EXIT_CRITICAL();
             *perr         = OS_ERR_INVALID_OPT;
             pevent_return = pevent;
             break;
    }

    OS_TRACE_MUTEX_DEL_EXIT(*perr);
    
    return (pevent_return);
}
#endif


/*
*********************************************************************************************************
*                                 PEND ON MUTUAL EXCLUSION SEMAPHORE(等待一个互斥信号量)
*
* Description: This function waits for a mutual exclusion semaphore.
*这个函数用来等待一个互斥信号量
* Arguments  : pevent        is a pointer to the event control block associated with the desired
*                            mutex.
*指向与互斥信号量相关的ECB的指针
*              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
*                            mutex or, until the resource becomes available.
*是可选的超时时间(以时钟滴答为单位)。 如果不为零,则您的任务将等待资源直至此参数指定的时间。 但是,如果指定0,则您的任务将在指定的互斥信号量上永远等待,或者直到资源可用为止。
*              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 mutex
*                               OS_ERR_TIMEOUT     The mutex was not available within the specified 'timeout'.
*                               OS_ERR_PEND_ABORT  The wait on the mutex was aborted.
*                               OS_ERR_EVENT_TYPE  If you didn't pass a pointer to a mutex
*                               OS_ERR_PEVENT_NULL 'pevent' is a NULL pointer
*                               OS_ERR_PEND_ISR    If you called this function from an ISR and the result
*                                                  would lead to a suspension.
*                               OS_ERR_PCP_LOWER   If the priority of the task that owns the Mutex is
*                                                  HIGHER (i.e. a lower number) than the PCP.  This error
*                                                  indicates that you did not set the PCP higher (lower
*                                                  number) than ALL the tasks that compete for the Mutex.
*                                                  Unfortunately, this is something that could not be
*                                                  detected when the Mutex is created because we don't know
*                                                  what tasks will be using the Mutex.
*                               OS_ERR_PEND_LOCKED If you called this function when the scheduler is locked
*
* Returns    : none
*
* Note(s)    : 1) The task that owns the Mutex MUST NOT pend on any other event while it owns the mutex.
*注意:拥有此互斥信号量的任务,在拥有此互斥信号量的期间内,不能同时等待其他任何任务
*              2) You MUST NOT change the priority of the task that owns the mutex
在任务拥有此信号量期间内,你不能修改它的原始优先级
*********************************************************************************************************
*/

void  OSMutexPend (OS_EVENT  *pevent,
                   INT32U     timeout,
                   INT8U     *perr)
{
    INT8U      pcp;                                        /* Priority Ceiling Priority (PCP)          */
    INT8U      mprio;                                      /* Mutex owner priority                     */
    BOOLEAN    rdy;                                        /* Flag indicating task was ready           */
    OS_TCB    *ptcb;
    OS_EVENT  *pevent2;
    INT8U      y;
#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

    OS_TRACE_MUTEX_PEND_ENTER(pevent, timeout);
    //检查ECB的类型是否为互斥信号量
    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) {      /* Validate event block type                */
        *perr = OS_ERR_EVENT_TYPE;
        OS_TRACE_MUTEX_PEND_EXIT(*perr);
        return;
    }
    //ISR不能请求信号量
    if (OSIntNesting > 0u) {                               /* See if called from ISR ...               */
        *perr = OS_ERR_PEND_ISR;                           /* ... can't PEND from an ISR               */
        OS_TRACE_MUTEX_PEND_EXIT(*perr);
        return;
    }
    //调度器如果上锁了,那么就不能请求信号量,因为如果等待的话,就会涉及到调度问题,优先级反转调度问题
    if (OSLockNesting > 0u) {                              /* See if called with scheduler locked ...  */
        *perr = OS_ERR_PEND_LOCKED;                        /* ... can't PEND when locked               */
        OS_TRACE_MUTEX_PEND_EXIT(*perr);
        return;
    }

    OS_ENTER_CRITICAL();//进入临界区,关中断
    //获取优先级反转的优先级
    pcp = (INT8U)(pevent->OSEventCnt >> 8u);               /* Get PCP from mutex                       */
                                                           /* Is Mutex available?                      */
    //如果当前信号量确实可用
    if ((INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8) == OS_MUTEX_AVAILABLE) {
        //获得此信号量,将任务的优先级放到低8位上
        pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8;       /* Yes, Acquire the resource                */
        pevent->OSEventCnt |= OSTCBCur->OSTCBPrio;         /*      Save priority of owning task        */
        pevent->OSEventPtr  = (void *)OSTCBCur;            /*      Point to owning task's OS_TCB       */
        //如果允许优先级反转,但是任务的优先级比反转的优先级还要高,那么返回错误,pcp必须是最高的
        if ((pcp != OS_PRIO_MUTEX_CEIL_DIS) &&
            (OSTCBCur->OSTCBPrio <= pcp)) {                /*      PCP 'must' have a SMALLER prio ...  */
             OS_EXIT_CRITICAL();                           /*      ... than current task!              */
            *perr = OS_ERR_PCP_LOWER;
        } else {
             //如果请求成功,则返回
             OS_EXIT_CRITICAL();
            *perr = OS_ERR_NONE;
        }
        OS_TRACE_MUTEX_PEND_EXIT(*perr);
        return;
    }
    //如果允许优先级反转
    if (pcp != OS_PRIO_MUTEX_CEIL_DIS) {
        //获取拥有此互斥信号量的任务的优先级
        mprio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8); /*  Get priority of mutex owner   */
        //获取拥有此互斥信号量的TCB的指针
        ptcb  = (OS_TCB *)(pevent->OSEventPtr);                   /*     Point to TCB of mutex owner   */
        //如果拥有互斥信号量的TCB的优先级小于反转的优先级,才拥有反转的可能
        if (ptcb->OSTCBPrio > pcp) {                              /*     Need to promote prio of owner?*/
            //如果拥有互斥信号量的TCB的优先级小于当前请求互斥信号量的任务的优先级(即拥有互斥信号量的TCB的优先级数值上大于当前请求互斥信号量的任务的优先级)
            //这样的话,就是表示为,需要进行优先级反转,将互斥信号量拥有者的优先级进行升级,来超过请求此信号量的任务的优先级
            if (mprio > OSTCBCur->OSTCBPrio) {
                y = ptcb->OSTCBY;
                //查看互斥信号量拥有者是否就绪了,也就是它是否是因为被抢占了,才变为就绪态
                //如果就绪了,就取消他的就绪标志
                if ((OSRdyTbl[y] & ptcb->OSTCBBitX) != 0u) {      /*     See if mutex owner is ready   */
                    OSRdyTbl[y] &= (OS_PRIO)~ptcb->OSTCBBitX;     /*     Yes, Remove owner from Rdy ...*/
                    if (OSRdyTbl[y] == 0u) {                      /*          ... list at current prio */
                        OSRdyGrp &= (OS_PRIO)~ptcb->OSTCBBitY;
                    }
                    //标志拥有互斥信号量的任务一开始的时候是否处于就绪态
                    rdy = OS_TRUE;
                } else {
                    //获取拥有此信号量的TCB的等待其他事件的事件ECB指针
                    pevent2 = ptcb->OSTCBEventPtr;
                    //如果拥有此信号量的TCB还在等待其他事件,则将其从对应的时间等待表中移除
                    if (pevent2 != (OS_EVENT *)0) {               /* Remove from event wait list       */
                        y = ptcb->OSTCBY;
                        pevent2->OSEventTbl[y] &= (OS_PRIO)~ptcb->OSTCBBitX;
                        if (pevent2->OSEventTbl[y] == 0u) {
                            pevent2->OSEventGrp &= (OS_PRIO)~ptcb->OSTCBBitY;
                        }
                    }
                    //标志拥有此信号量的任务,在之前还在等待其他事件
                    rdy = OS_FALSE;                        /* No                                       */
                }
                //将TCB的优先级进行升级
                ptcb->OSTCBPrio = pcp;                     /* Change owner task prio to PCP            */
                
                OS_TRACE_MUTEX_TASK_PRIO_INHERIT(ptcb, pcp);
//根据不同的版本重设TCB信息
#if OS_LOWEST_PRIO <= 63u
                ptcb->OSTCBY    = (INT8U)( ptcb->OSTCBPrio >> 3u);
                ptcb->OSTCBX    = (INT8U)( ptcb->OSTCBPrio & 0x07u);
#else
                ptcb->OSTCBY    = (INT8U)((INT8U)(ptcb->OSTCBPrio >> 4u) & 0xFFu);
                ptcb->OSTCBX    = (INT8U)( ptcb->OSTCBPrio & 0x0Fu);
#endif
                ptcb->OSTCBBitY = (OS_PRIO)(1uL << ptcb->OSTCBY);
                ptcb->OSTCBBitX = (OS_PRIO)(1uL << ptcb->OSTCBX);

                //如果TCB原来就是就绪态,那么现在仍然将其设置为就绪态
                if (rdy == OS_TRUE) {                      /* If task was ready at owner's priority ...*/
                    OSRdyGrp               |= ptcb->OSTCBBitY; /* ... make it ready at new priority.   */
                    OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
                } else {
                    //如果TCB原来就是等待态,那么再次让其以不同的优先级进行等待原来的事件
                    pevent2 = ptcb->OSTCBEventPtr;
                    if (pevent2 != (OS_EVENT *)0) {        /* Add to event wait list                   */
                        pevent2->OSEventGrp               |= ptcb->OSTCBBitY;
                        pevent2->OSEventTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
                    }
                }
                OSTCBPrioTbl[pcp] = ptcb;//在优先级列表中,新的优先级处进行优先级处理,原来的优先级不动
            }
        }
        //如果,反转的优先级小于任务所具有的优先级,即反转的优先级在数值上大于任务所具有的优先级,那么不做任何处理,直接当前运行任务将自己阻塞即可
    }
    //在将拥有信号量的任务的优先级进行提升之后,开始处理自己的情况,自己需要进行等待信号量
    OSTCBCur->OSTCBStat     |= OS_STAT_MUTEX;         /* Mutex not available, pend current task        */
    OSTCBCur->OSTCBStatPend  = OS_STAT_PEND_OK;
    OSTCBCur->OSTCBDly       = timeout;               /* Store timeout in current task's 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 getting mutex        */
             break;

        case OS_STAT_PEND_TO:
        default:
             OS_EventTaskRemove(OSTCBCur, pevent);
             *perr = OS_ERR_TIMEOUT;                  /* Indicate that we didn't get mutex 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();

    OS_TRACE_MUTEX_PEND_EXIT(*perr);
}


/*
*********************************************************************************************************
*                                POST TO A MUTUAL EXCLUSION SEMAPHORE(提交一个互斥信号量)
*
* Description: This function signals a mutual exclusion semaphore
*此函数用来提交一个互斥信号量
* Arguments  : pevent              is a pointer to the event control block associated with the desired
*                                  mutex.
*与相关互斥信号量有关的ECB的指针
* Returns    : OS_ERR_NONE             The call was successful and the mutex was signaled.
*              OS_ERR_EVENT_TYPE       If you didn't pass a pointer to a mutex
*              OS_ERR_PEVENT_NULL      'pevent' is a NULL pointer
*              OS_ERR_POST_ISR         Attempted to post from an ISR (not valid for MUTEXes)
*              OS_ERR_NOT_MUTEX_OWNER  The task that did the post is NOT the owner of the MUTEX.
*              OS_ERR_PCP_LOWER        If the priority of the new task that owns the Mutex is
*                                      HIGHER (i.e. a lower number) than the PCP.  This error
*                                      indicates that you did not set the PCP higher (lower
*                                      number) than ALL the tasks that compete for the Mutex.
*                                      Unfortunately, this is something that could not be
*                                      detected when the Mutex is created because we don't know
*                                      what tasks will be using the Mutex.
*********************************************************************************************************
*/

INT8U  OSMutexPost (OS_EVENT *pevent)
{
    INT8U      pcp;                                   /* Priority ceiling priority                     */
    INT8U      prio;
#if OS_CRITICAL_METHOD == 3u                          /* Allocate storage for CPU status register      */
    OS_CPU_SR  cpu_sr = 0u;
#endif

//ISR中不允许提交一个信号量
    if (OSIntNesting > 0u) {                          /* See if called from ISR ...                    */
        return (OS_ERR_POST_ISR);                     /* ... can't POST mutex from an ISR              */
    }
    //参数检查
#if OS_ARG_CHK_EN > 0u
    if (pevent == (OS_EVENT *)0) {                    /* Validate 'pevent'                             */
        return (OS_ERR_PEVENT_NULL);
    }
#endif

    OS_TRACE_MUTEX_POST_ENTER(pevent);
//检查ECB类型
    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) { /* Validate event block type                     */
        OS_TRACE_MUTEX_POST_EXIT(OS_ERR_EVENT_TYPE);
        return (OS_ERR_EVENT_TYPE);
    }
    OS_ENTER_CRITICAL();//关中断,进入临界区
    //获取反转优先级
    pcp  = (INT8U)(pevent->OSEventCnt >> 8u);         /* Get priority ceiling priority of mutex        */
    //获取低8位,即拥有互斥信号量的TCB的优先级
    prio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8);  /* Get owner's original priority      */
    //如果提交信号量的任务不是拥有信号量的任务,就退出
    if (OSTCBCur != (OS_TCB *)pevent->OSEventPtr) {   /* See if posting task owns the MUTEX            */
        OS_EXIT_CRITICAL();
        OS_TRACE_MUTEX_POST_EXIT(OS_ERR_NOT_MUTEX_OWNER);
        return (OS_ERR_NOT_MUTEX_OWNER);
    }
    //如果优先级反转没有被禁用
    if (pcp != OS_PRIO_MUTEX_CEIL_DIS) {
        //查看当前TCB的优先级是否位反转的优先级
        if (OSTCBCur->OSTCBPrio == pcp) {             /* Did we have to raise current task's priority? */
            OS_TRACE_MUTEX_TASK_PRIO_DISINHERIT(OSTCBCur, prio);
            //如果是反转的优先级,则让任务以原来的优先级就绪
            OSMutex_RdyAtPrio(OSTCBCur, prio);        /* Restore the task's original priority          */
        }
        //反转的优先级位置,置保留位
        OSTCBPrioTbl[pcp] = OS_TCB_RESERVED;          /* Reserve table entry                           */
    }
    //如果目前有任务在等待信号量
    if (pevent->OSEventGrp != 0u) {                   /* Any task waiting for the mutex?               */
                                                      /* Yes, Make HPT waiting for mutex ready         */
        //唤醒最高优先级,置最高优先级为就绪态
        prio                = OS_EventTaskRdy(pevent, (void *)0, OS_STAT_MUTEX, OS_STAT_PEND_OK);
        //最高优先级的TCB获得信号量
        pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8;  /*      Save priority of mutex's new owner       */
        pevent->OSEventCnt |= prio;
        pevent->OSEventPtr  = OSTCBPrioTbl[prio];     /*      Link to new mutex owner's OS_TCB         */
        if ((pcp  != OS_PRIO_MUTEX_CEIL_DIS) &&
            (prio <= pcp)) {                          /*      PCP 'must' have a SMALLER prio ...       */
            OS_EXIT_CRITICAL();                       /*      ... than current task!                   */
            //即使此时产生错误,但是当前的信号量确实已经被分配给了最高优先级的TCB,并且有任务就绪,就必须得调度,只不过后期涉及到优先级反转的时候不做处理
            OS_Sched();                               /*      Find highest priority task ready to run  */
            OS_TRACE_MUTEX_POST_EXIT(OS_ERR_PCP_LOWER);
            return (OS_ERR_PCP_LOWER);
        } else {
            //正常调度
            OS_EXIT_CRITICAL();
            OS_Sched();                               /*      Find highest priority task ready to run  */
            OS_TRACE_MUTEX_POST_EXIT(OS_ERR_NONE);
            return (OS_ERR_NONE);
        }
    }
    //如果没有任务等待,就置低8位为全1,表示信号量有效
    pevent->OSEventCnt |= OS_MUTEX_AVAILABLE;         /* No,  Mutex is now available                   */
    pevent->OSEventPtr  = (void *)0;
    OS_EXIT_CRITICAL();
    OS_TRACE_MUTEX_POST_EXIT(OS_ERR_NONE);
    return (OS_ERR_NONE);
}


/*
*********************************************************************************************************
*                                 QUERY A MUTUAL EXCLUSION SEMAPHORE(查询一个互斥信号量)
*
* Description: This function obtains information about a mutex
*这个函数用来获得一个互斥信号量
* Arguments  : pevent          is a pointer to the event control block associated with the desired mutex
*指向与互斥信号量有关系的ECB的指针
*              p_mutex_data    is a pointer to a structure that will contain information about the mutex
*指向存储互斥信号量信息的结构体的指针
* Returns    : OS_ERR_NONE          The call was successful and the message was sent
*              OS_ERR_QUERY_ISR     If you called this function from an ISR
*              OS_ERR_PEVENT_NULL   If 'pevent'       is a NULL pointer
*              OS_ERR_PDATA_NULL    If 'p_mutex_data' is a NULL pointer
*              OS_ERR_EVENT_TYPE    If you are attempting to obtain data from a non mutex.
*********************************************************************************************************
*/

//是否允许查询互斥信号量
#if OS_MUTEX_QUERY_EN > 0u
INT8U  OSMutexQuery (OS_EVENT       *pevent,
                     OS_MUTEX_DATA  *p_mutex_data)
{
    INT8U       i;
    OS_PRIO    *psrc;
    OS_PRIO    *pdest;
#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */
    OS_CPU_SR   cpu_sr = 0u;
#endif


    //ISR中禁止查询互斥信号量
    if (OSIntNesting > 0u) {                               /* See if called from ISR ...               */
        return (OS_ERR_QUERY_ISR);                         /* ... can't QUERY mutex from an ISR        */
    }
    //参数检查
#if OS_ARG_CHK_EN > 0u
    if (pevent == (OS_EVENT *)0) {                         /* Validate 'pevent'                        */
        return (OS_ERR_PEVENT_NULL);
    }
    if (p_mutex_data == (OS_MUTEX_DATA *)0) {              /* Validate 'p_mutex_data'                  */
        return (OS_ERR_PDATA_NULL);
    }
#endif
//检查ECB的类型,是否为互斥信号量类型
    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) {      /* Validate event block type                */
        return (OS_ERR_EVENT_TYPE);
    }
    OS_ENTER_CRITICAL();//进入临界区,关中断
    p_mutex_data->OSMutexPCP  = (INT8U)(pevent->OSEventCnt >> 8u);//获得反转优先级
    p_mutex_data->OSOwnerPrio = (INT8U)(pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8);//获得互斥信号量拥有者的优先级
    if (p_mutex_data->OSOwnerPrio == 0xFFu) {
        p_mutex_data->OSValue = OS_TRUE;
    } else {
        p_mutex_data->OSValue = OS_FALSE;
    }//标识互斥信号量是否有效
    //复制就绪组/表信息
    p_mutex_data->OSEventGrp  = pevent->OSEventGrp;        /* Copy wait list                           */
    psrc                      = &pevent->OSEventTbl[0];
    pdest                     = &p_mutex_data->OSEventTbl[0];
    for (i = 0u; i < OS_EVENT_TBL_SIZE; i++) {
        *pdest++ = *psrc++;
    }
    OS_EXIT_CRITICAL();//退出临界区,开中断
    return (OS_ERR_NONE);
}
#endif                                                     /* OS_MUTEX_QUERY_EN                        */


/*
*********************************************************************************************************
*                            RESTORE A TASK BACK TO ITS ORIGINAL PRIORITY(将一个任务的优先级恢复到其原有的优先级)
*
* Description: This function makes a task ready at the specified priority
*这个函数使一个任务以一个特定的优先级就绪
* Arguments  : ptcb            is a pointer to OS_TCB of the task to make ready
*指向需要就绪的TCB的指针
*              prio            is the desired priority
*期待的优先级
* Returns    : none
*********************************************************************************************************
*/

//此函数是一个静态函数,静态函数的定义域仅仅局限于本文件内
static  void  OSMutex_RdyAtPrio (OS_TCB  *ptcb,
                                 INT8U    prio)
{
    INT8U  y;

    //获取TCB中任务的高位
    y            =  ptcb->OSTCBY;                          /* Remove owner from ready list at 'pcp'    */
    //从就绪表中去掉对应任务优先级的就绪标志
    OSRdyTbl[y] &= (OS_PRIO)~ptcb->OSTCBBitX;
    OS_TRACE_TASK_SUSPENDED(ptcb);
    //如果就绪表变成0了,那么清空就绪组的对应位置
    if (OSRdyTbl[y] == 0u) {
        OSRdyGrp &= (OS_PRIO)~ptcb->OSTCBBitY;
    }
    //重设TCB的优先级,以及表示当前任务的优先级全局变量
    ptcb->OSTCBPrio         = prio;
    OSPrioCur               = prio;                        /* The current task is now at this priority */
//重设TCB中的表示就绪组表需要用到的属性
#if OS_LOWEST_PRIO <= 63u
    ptcb->OSTCBY            = (INT8U)((INT8U)(prio >> 3u) & 0x07u);
    ptcb->OSTCBX            = (INT8U)(prio & 0x07u);
#else
    ptcb->OSTCBY            = (INT8U)((INT8U)(prio >> 4u) & 0x0Fu);
    ptcb->OSTCBX            = (INT8U) (prio & 0x0Fu);
#endif
    ptcb->OSTCBBitY         = (OS_PRIO)(1uL << ptcb->OSTCBY);
    ptcb->OSTCBBitX         = (OS_PRIO)(1uL << ptcb->OSTCBX);
    //在就绪组表上,将任务的优先级按照传进的优先级进行就绪操作
    OSRdyGrp               |= ptcb->OSTCBBitY;             /* Make task ready at original priority     */
    OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
    OSTCBPrioTbl[prio]      = ptcb;//就该优先级列表
    OS_TRACE_TASK_READY(ptcb);
}


#endif                                                     /* OS_MUTEX_EN                              */

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值