当前位置: 首页 > web >正文

freeRTOS 互斥量优先级继承机制函数实现xQueueGenericReceive()

  一 接收到互斥量时,将互斥量的任务持有者赋值为当前任务

以下为xQueueGenericReceive()函数接收互斥量,互斥量不为空(未被别的任务持有),去除互斥量,并且将互斥量的持有者赋值为当前任务,紫色字体部分

if( uxMessagesWaiting > ( UBaseType_t ) 0 )
            {
                /* Remember the read position in case the queue is only being
                peeked. */
                pcOriginalReadPosition = pxQueue->u.pcReadFrom;

                prvCopyDataFromQueue( pxQueue, pvBuffer );

                if( xJustPeeking == pdFALSE )
                {
                    traceQUEUE_RECEIVE( pxQueue );

                    /* Actually removing data, not just peeking. */
                    pxQueue->uxMessagesWaiting = uxMessagesWaiting - 1;

                    #if ( configUSE_MUTEXES == 1 )
                    {
                        if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
                        {
                            /* Record the information required to implement
                            priority inheritance should it become necessary. */
           pxQueue->pxMutexHolder = ( int8_t * ) pvTaskIncrementMutexHeldCount(); /*lint !e961 Cast is not redundant as TaskHandle_t is a typedef. */
                        }
                        else
                        {
                            mtCOVERAGE_TEST_MARKER();
                        }
                    }
                    #endif /* configUSE_MUTEXES */

二 接收任务接收不到信号量时,提升互斥量持有者任务的优先级

还是xQueueGenericReceive()函数接收互斥量,互斥量为空(被别的任务持有),而且时间未超时时,就是第一次尝试接收时,进行优先级继承,紫色字体函数

 /* Update the timeout state to see if it has expired yet. */
        if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
        {
            if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
            {
                traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );

                #if ( configUSE_MUTEXES == 1 )
                {
                    if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
                    {
                        taskENTER_CRITICAL();
                        {
                            vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
                        }
                        taskEXIT_CRITICAL();
                    }
                    else
                    {
                        mtCOVERAGE_TEST_MARKER();
                    }
                }
                #endif

                vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
                prvUnlockQueue( pxQueue );
                if( xTaskResumeAll() == pdFALSE )
                {
                    portYIELD_WITHIN_API();
                }
                else
                {
                    mtCOVERAGE_TEST_MARKER();
                }
            }

三 最终实现函数TaskPriorityInherit()解析

void vTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
    {
    TCB_t * const pxTCB = ( TCB_t * ) pxMutexHolder;

       
        if( pxMutexHolder != NULL )
        {
            
            if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )
            {
                   /* Adjust the mutex holder state to account for its new
                priority.  Only reset the event list item value if the value is
                not    being used for anything else. */
                if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL ) //获取互斥量持有任务的节点辅助值,用于迁移到新优先级就绪链表时的排序
                {
                    listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ); //设定互斥量持有任务的节点辅助值,用于迁移到新优先级就绪链表时的排序
                }
                else
                {
                    mtCOVERAGE_TEST_MARKER();
                }

                /* If the task being modified is in the ready state it will need
                to be moved into a new list. */
                if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )//互斥量持有者任务现在处于就绪列表
                {
                    if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )//将互斥量持有者任务从其所在就绪列表移除
                    {
                        taskRESET_READY_PRIORITY( pxTCB->uxPriority );
                    }
                    else
                    {
                        mtCOVERAGE_TEST_MARKER();
                    }

                    /* Inherit the priority before being moved into the new list. */
                    pxTCB->uxPriority = pxCurrentTCB->uxPriority;//将互斥量持有者任务优先级赋值为当前任务优先级
                    prvAddTaskToReadyList( pxTCB );//将互斥量持有者任务移到相应优先级列表
                }
                else
                {
                    /* Just inherit the priority. */
                    pxTCB->uxPriority = pxCurrentTCB->uxPriority;  //将互斥量持有者任务优先级赋值为当前任务优先级,只进行优先级上升级,因为现在不在就绪列表中
                }

                traceTASK_PRIORITY_INHERIT( pxTCB, pxCurrentTCB->uxPriority );
            }
            else
            {
                mtCOVERAGE_TEST_MARKER();
            }
        }
        else
        {
            mtCOVERAGE_TEST_MARKER();
        }
    }
 

http://www.xdnf.cn/news/12768.html

相关文章:

  • C++ 信息学奥赛总复习题答案解析(第一章)
  • 电脑商城--用户注册登录
  • 步进电机调试记录(先让我的步进电机转起来)
  • 【Java学习笔记】String类(重点)
  • 沉金电路板的黑盘缺陷挑战与解决方案——高密度互连设计的关键考量
  • Jina AI 开源 node-DeepResearch
  • [面试精选] 0094. 二叉树的中序遍历
  • 【单源最短路经】Dijkstra 算法(朴素版和堆优化版)、Bellman-Ford 算法、spfa 算法 及 负环判断
  • win10环境配置-openpose pytorch版本
  • 网络协议通俗易懂详解指南
  • MyBatis 获取插入数据后的自增 ID 值
  • GoC指令测试卷 A
  • 【AI学习】wirelessGPT多任务无线基础模型摘要
  • 安卓小说阅读软件推荐
  • c++ 静态成员变量
  • JavaScript中的函数总结
  • 人工智能赋能高中学科教学的应用与前景研究
  • Macbook M3 使用 VMware Fusion 安装 openEuler24.03LTS
  • 言思集交流社区(SpringBoot)
  • leetcodeT3170
  • MIT 6.S081 Lab10 mmap
  • java报错ncapp生成主子表单据时报错,CarrierRuntimeException
  • 关于Qt阻断样式继承的解决办法
  • yolov12-区域注意力:让计算机“看见”更智能
  • Java 中 synchronized 和 ReentrantLock 的全面对比解析
  • ELK日志管理框架介绍
  • 在C语言中使用UUID作为AES加密密钥
  • python打卡第47天
  • 快速排序算法详解:从理论到实践的全方位指导
  • 从零开始制作小程序简单概述