[FreeRTOS 内部实现] 互斥访问与回环队列

文章目录

    • 基础知识
    • 队列结构体
    • 创建队列 xQueueCreate 解析
    • 队列读数据 xQueueReceive 解析
    • 队列写数据 xQueueGenericSend 解析
    • 互斥访问与回环队列 内部实现框图


基础知识

[FreeRTOS 基础知识] 互斥访问与回环队列 概念


队列结构体

typedef struct QueueDefinition
{
    int8_t *pcHead;                    /*< Points to the beginning of the queue storage area. */
    int8_t *pcTail;            /*< Points to the byte at the end of the queue storage area.  Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
    int8_t *pcWriteTo;            /*< Points to the free next place in the storage area. */

    
     union                /* Use of a union is an exception to the coding standard to ensure two mutually exclusive structure members don't appear simultaneously (wasting RAM). */
    {
        int8_t *pcReadFrom;        /*< Points to the last place that a queued item was read from when the structure is used as a queue. */
        UBaseType_t uxRecursiveCallCount;/*< Maintains a count of the number of times a recursive mutex has been recursively 'taken' when the structure is used as a mutex. */
    } u;

    List_t xTasksWaitingToSend;        /*< List of tasks that are blocked waiting to post onto this queue.  Stored in priority order. */
    List_t xTasksWaitingToReceive;        /*< List of tasks that are blocked waiting to read from this queue.  Stored in priority order. */

    
     volatile UBaseType_t uxMessagesWaiting;/*< The number of items currently in the queue. */
             UBaseType_t uxLength;        /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
         UBaseType_t uxItemSize;        /*< The size of each items that the queue will hold. */

    volatile int8_t cRxLock;        /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */
    volatile int8_t cTxLock;        /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */

    #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
    uint8_t ucStaticallyAllocated;    /*< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. */
    #endif

    #if ( configUSE_QUEUE_SETS == 1 )
    struct QueueDefinition *pxQueueSetContainer;
    #endif

    #if ( configUSE_TRACE_FACILITY == 1 )
    UBaseType_t uxQueueNumber;
    uint8_t ucQueueType;
    #endif
} xQUEUE;

在结构体中主要成员介绍:
List_t xTasksWaitingToSend; 等待发布到此队列的阻塞任务列表。按优先级顺序存储。
List_t xTasksWaitingToReceive; 阻塞等待从此队列读取的任务列表。按优先级顺序存储。
int8_t *pcWriteTo; 指向存储区的下一个空闲位置。
int8_t *pcReadFrom; 指向将该结构用作队列时最后从其中读取队列项的位置。


创建队列 xQueueCreate 解析

xQueueCreate( uxQueueLength, uxItemSize )
    -> xQueueGenericCreate
        -> xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); 
        -> pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes );

uxQueueLength 指 队列长度
uxItemSize 指 每段长度的大小
总长度 = uxQueueLength * uxItemSize + Queue_t结构体大小

在内存中分配如下
在这里插入图片描述


队列读数据 xQueueReceive 解析

BaseType_t xQueueReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait )
->
    for( ;; )
    {
        taskENTER_CRITICAL();            // 关中断 portDISABLE_INTERRUPTS();
        {
            const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting;        // 当前队列中值的总数量

            
            if( uxMessagesWaiting > ( UBaseType_t ) 0 )            // 队列中有数值
            {                            
                prvCopyDataFromQueue( pxQueue, pvBuffer );        // 将队列中pcReadFrom指向的值取出放到pvBuffer中,pcReadFrom指向下一个位置
                traceQUEUE_RECEIVE( pxQueue );
                pxQueue->uxMessagesWaiting = uxMessagesWaiting - ( UBaseType_t ) 1;    // 当前队列中值的总数量 -1

  
                if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )    // xTasksWaitingToSend链表中有没等待要写的任务
                {
                    if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )    // 1、将要写的任务从xTasksWaitingToSend移除;2、将要写的任务从DelayList移动到ReadyList
                    {
                        queueYIELD_IF_USING_PREEMPTION();    // 当前任务让出CPU
                    }
                    else
                    {
                        mtCOVERAGE_TEST_MARKER();            
                    }
                }
                else
                {
                    mtCOVERAGE_TEST_MARKER();
                }
                taskEXIT_CRITICAL();            // 打开中断
                return pdPASS;                   // 返回成功
            }
            else
            {
                if( xTicksToWait == ( TickType_t ) 0 )
                {
                    // 队列是空的,没有指定阻塞时间(或者阻塞时间已经过期),所以现在离开。
                    taskEXIT_CRITICAL();         // 打开中断
                    traceQUEUE_RECEIVE_FAILED( pxQueue );
                    return errQUEUE_EMPTY;        // 返回队列为空
                }
                else if( xEntryTimeSet == pdFALSE )
                {
                    // 队列为空,阻塞时间被指定,所以配置超时结构。        
                    vTaskInternalSetTimeOutState( &xTimeOut );
                    xEntryTimeSet = pdTRUE;
                }
                else
                {
                /* Entry time was already set. */
                mtCOVERAGE_TEST_MARKER();
                }
            }
        }
        taskEXIT_CRITICAL();
        
        vTaskSuspendAll();
        prvLockQueue( pxQueue );


        if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
        {

            if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
            {
                traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
                // 1、当前的任务加入到队列的xTasksWaitingToReceive链表中;
                // 2、当前的任务从ReadyList移动到DelayList
                vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
                prvUnlockQueue( pxQueue );
                if( xTaskResumeAll() == pdFALSE )
                {
                    portYIELD_WITHIN_API();
                }
                else
                {
                    mtCOVERAGE_TEST_MARKER();
                }
            }
            else
            {
                prvUnlockQueue( pxQueue );
                ( void ) xTaskResumeAll();
            }
        }
        else
        {
            prvUnlockQueue( pxQueue );
            ( void ) xTaskResumeAll();
            
            if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
            {
                traceQUEUE_RECEIVE_FAILED( pxQueue );
                return errQUEUE_EMPTY;
            }
            else
            {
                mtCOVERAGE_TEST_MARKER();
            }
        }                                                                                                            
}

队列写数据 xQueueGenericSend 解析

BaseType_t xQueueGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, const BaseType_t xCopyPosition )
-> 
    for( ;; )
    {
    taskENTER_CRITICAL();    // 关中断 portDISABLE_INTERRUPTS();
    {

        if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) )    // 判断是否有空间写数据
        {
        // 有空间写数据
            traceQUEUE_SEND( pxQueue );
            xYieldRequired = prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
       // 写数据 

            if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )          // 判断xTasksWaitingToReceive队列里是否有等待的任务
            {
                if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE ) // 1、将要写的任务从xTasksWaitingToReceive移除;2、将要写的任务从DelayList移动到ReadyList
                {

                    queueYIELD_IF_USING_PREEMPTION();    //让出CPU使用权
                }
                else
                {
                    mtCOVERAGE_TEST_MARKER();
                }
            }                                

            else if( xYieldRequired != pdFALSE )
            {

                queueYIELD_IF_USING_PREEMPTION();
            }
            else
            {
                mtCOVERAGE_TEST_MARKER();
            }
        }
        
        
        taskEXIT_CRITICAL();    // 开中断
        return pdPASS;          // 返回成功
    }
    else
    {
        if( xTicksToWait == ( TickType_t ) 0 )
        {
        
            taskEXIT_CRITICAL();
                
            traceQUEUE_SEND_FAILED( pxQueue );
            return errQUEUE_FULL;         // 返回队列已满
        }
        else if( xEntryTimeSet == pdFALSE )
        {
       
            vTaskInternalSetTimeOutState( &xTimeOut ); // 阻塞时间被指定,所以配置超时结构。 
            xEntryTimeSet = pdTRUE;
        }
        else
        {
        
        mtCOVERAGE_TEST_MARKER();
        }
    }
}
taskEXIT_CRITICAL();    // 开中断


vTaskSuspendAll();
prvLockQueue( pxQueue );

        
if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
{
    if( prvIsQueueFull( pxQueue ) != pdFALSE )
    {
        traceBLOCKING_ON_QUEUE_SEND( pxQueue );
        // 1、当前的任务加入到队列的xTasksWaitingToSend链表中;
        // 2、当前的任务从ReadyList移动到DelayList                
        vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
        prvUnlockQueue( pxQueue );
    
        if( xTaskResumeAll() == pdFALSE )
        {
            portYIELD_WITHIN_API();
        }
    }
    else
    {

        prvUnlockQueue( pxQueue );
        ( void ) xTaskResumeAll();
    }
}
else
{

    prvUnlockQueue( pxQueue );
    ( void ) xTaskResumeAll();
    
    traceQUEUE_SEND_FAILED( pxQueue );
    return errQUEUE_FULL;
}
}

互斥访问与回环队列 内部实现框图

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/734664.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Golang三色标记法

简介 在JVM中&#xff0c;GC采用可达性分析法来判断对象是否死亡&#xff1b;在python虚拟机中&#xff0c;GC采用引用计数法加循环检测器来判断对象是否死亡&#xff0c;而在golang中&#xff0c;使用的是三色表记法来判断对象是否死亡。 什么是三色抽象 总所周知在GC时&am…

keepalived高可用,nginx+keepalived+apache架构的实现

目 录 一、概述&#xff1a; 二、实验架构图如图所示&#xff1a; 三、实验环境&#xff1a; 四、实现效果&#xff1a; 五、实验解析及步骤&#xff1a; 六、具体实现&#xff1a; 6.1 先关闭防火墙和核心防护&#xff1a;两条命令&#xff1a; 6.2 后端apache服务…

2024最新1小时零基础编写uniapp和小程序管理后台,基于uniadmin和vue3实现uniapp小程序的网页管理后台

一&#xff0c;创建uniAdmin项目 打开开发者工具Hbuilder,然后点击左上角的文件&#xff0c;点新建&#xff0c;点项目。如下图。 选择uniadmin&#xff0c;编写项目名&#xff0c;然后使用vue3 记得选用阿里云服务器&#xff0c;因为最便宜 点击创建&#xff0c;等待项目创…

53【场景作图】纵深感

1 想清楚什么是前 什么是后 如果背景虚化,就不要处理地很平面,如果很平面,就留一个清晰的边缘 2 重叠 遮挡 被遮挡的物体会更远

动态ARP

定义 动态ARP表项由ARP协议通过ARP报文自动生成和维护&#xff0c;可以被老化&#xff0c;可以被新的ARP报文更新&#xff0c;可以被静态ARP表项覆盖。 动态ARP适用于拓扑结构复杂、通信实时性要求高的网络。 ARP地址解析过程 动态ARP通过广播ARP请求和单播ARP应答这两个过…

前端监控实现(node+vue)

前端监控 项目地址 git clone https://gitee.com/childe-jia/monitor–front-end.git 背景 思考一下&#xff0c;我们的项目代码在上线之后是不是就不用管了呢&#xff1f;并不是&#xff0c;作为前端开发工程师&#xff0c;我们是直接跟用户打交道的&#xff0c;一个应用的用…

合并有序链表

合并有序链表 图解代码如下 图解 虽然很复杂&#xff0c;但能够很好的理解怎么使用链表&#xff0c;以及对链表的指针类理解 代码如下 Node* merge_list_two_pointer(List& list1, List& list2) {Node* new_head1 list1.head;Node* new_head2 list2.head;Node* s…

华为---理解OSPF Route-ID(五)

9.5 理解OSPF Route-ID 9.5.1 原理概述 一些动态路由协议要求使用Router-ID作为路由器的身份标示&#xff0c;如果在启动这些路由协议时没有指定Router-ID,则默认使用路由器全局下的路由管理Router-ID。 Router-ID选举规则为&#xff0c;如果通过Router-ID命令配置了Router-…

【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 局域网中的服务器个数(200分) - 三语言AC题解(Python/Java/Cpp)

&#x1f36d; 大家好这里是清隆学长 &#xff0c;一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-C/D卷的三语言AC题解 &#x1f4bb; ACM银牌&#x1f948;| 多次AK大厂笔试 &#xff5c; 编程一对一辅导 &#x1f44f; 感谢大家的订阅➕ 和 喜欢&#x1f497; &#x1f…

【Flutter 专题】120 Flutter 腾讯移动通讯 TPNS~

1.2 方法使用 小菜按照官网的介绍尝试了一些常用的 API 方式&#xff0c;主要分为应用类&#xff0c;账号类和标签类三种 API&#xff0c;小菜业务中没有应用账号和标签模块&#xff0c;暂未深入研究&#xff1b; 应用接口 API a. 注册推送服务 对于服务的注册初始化&#x…

【嵌入式Linux】i.MX6ULL 时钟树——理论分析

文章目录 0. 时钟树结构0.1 参考手册 Chapter 18​: Clock Controller Module (CCM)0.2 时钟信号路径 1. 时钟源——晶振1.1 外部低频时钟 - CKIL1.1.1 CKIL 同步到 IPG_CLK 解释 1.2 外部高频时钟 - CKIH 和 内部振荡器1.3 总结1.4 缩写补充 2. PLL时钟2.1 i.MX6U 芯片 PLL 时…

不用写一行代码,deepseek结合腾讯云语音识别来批量转录Mp3音频

首先&#xff0c;打开window系统中的cmd命令行工具&#xff0c;或者powershell&#xff0c;安装腾讯云tencentcloud的Python库 pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python 然后&#xff0c;开通腾讯云的对象存储COS服务&…

关于DrawTools的分析- 一个优秀的C#开源绘图软件

国外大佬&#xff0c;曾经写过两个关于DrawTools相关的开源绘图软件。 我更新了一个优化的版本如下图&#xff0c;稍后会发布更新给大家。 需要的用户可发邮件给我 448283544qq.com 应用于AGV地图编辑器如下&#xff1a; 那么这个优于很多普通的画布软件&#xff0c;包含点、…

Android进程间通信 Messenger详解

//这里服务端Service是运行在单独的进程中的 android:process“:other” class MessengerService : Service() { private lateinit var mMessenger: Messenger override fun onBind(intent: Intent): IBinder { log(TAG, “onBind~”) //传入Handler实例化Messenger mMes…

Redis数据库的删除和安装

Redis数据库的删除和安装 1、删除Redis数据库2、下载Redis数据库 1、删除Redis数据库 没有下载过的&#xff0c;可以直接跳到下面的安装过程↓ 我们电脑中如果有下载过Redis数据库&#xff0c;要更换版本的话&#xff0c;其实Redis数据库的删除是比较简单的&#xff0c;打开我…

leetcode 二分查找·系统掌握 第一个错误版本

题意&#xff1a; 题解&#xff1a; 就是经典的~01~泛型查找&#xff0c;而且一定存在这样错误的版本所以查找不会"失败"&#xff0c;返回每次查找结果即可。 int firstBadVersion(int n) {long l1,rn,mid;while(l<r){mid(lr)>>1;if(isBadVersion(mid))r…

微积分-导数1(导数与变化率)

切线 要求与曲线 C C C相切于 P ( a , f ( a ) ) P(a, f(a)) P(a,f(a))点的切线&#xff0c;我们可以在曲线上找到与之相近的一点 Q ( x , f ( x ) ) Q(x, f(x)) Q(x,f(x))&#xff0c;然后求出割线 P Q PQ PQ的斜率&#xff1a; m P Q f ( x ) − f ( a ) x − a m_{PQ} \…

csdn上传源码资源卖钱能买房买车吗?每天最高收入200-500?

csdn上传源码卖钱能买房买车吗,最高收入200-500&#xff1f; 作者收入日榜 不***孩 收益617.32元 程***妍 收益534.56元 s***n 收益323.71元 盈***客 收益315.05元 极***计 收益284.17元

[第五空间2019 决赛]PWN5

参考文章: 格式化字符串漏洞原理及其利用&#xff08;附带pwn例题讲解&#xff09;_格式化字符串攻击教程-CSDN博客 格式化字符串漏洞原理详解_静态编译 格式化字符串漏洞-CSDN博客 BUU pwn [第五空间2019 决赛]PWN5 //格式化字符串漏洞 - Nemuzuki - 博客园 (cnblogs.com) …

如果申请小程序地理位置接口权限之前刷到这一篇就好了

小程序地理位置接口有什么功能&#xff1f; 通常情况下&#xff0c;我们在开发小程序时&#xff0c;可能会用到获取用户地理位置信息的功能。小程序开发者开放平台的新规定指出&#xff0c;如果没有申请开通微信小程序地理位置接口&#xff08;getLocation&#xff09;&#xf…