FreeRTOS Mutex 意外行为

问题描述

#include "main.h"
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
void Task1(void *argument);
void Task2(void *argument);
void PrintMsg(char *data);
/* USER CODE BEGIN PFP */

SemaphoreHandle_t hMutex = NULL;
TaskHandle_t hTask1 = NULL;
TaskHandle_t hTask2 = NULL;
UART_HandleTypeDef huart2;

int main(void) {
    /* MCU Configuration--------------------------------------------------------*/
    /* Reset of all peripherals,Initializes the Flash interface and the Systick. */
    HAL_Init();
    /* Configure the system clock */
    SystemClock_Config();
    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_USART2_UART_Init();

    /* USER CODE BEGIN 2 */
    xTaskCreate(Task1,"Task 1",configMINIMAL_STACK_SIZE,NULL,2,&hTask1);
    xTaskCreate(Task2,"Task 2",&hTask2);
    /* USER CODE END 2 */

    hMutex = xSemaphoreCreateMutex();
    if (hMutex == NULL) {
        PrintMsg("Mutex not created\r\n");
    } else {
        PrintMsg("Mutex created\r\n");
    }

    vTaskStartScheduler();

    /* USER CODE BEGIN WHILE */
    while (1) {
        /* USER CODE END WHILE */
        /* USER CODE BEGIN 3 */
    }
    /* USER CODE END 3 */
}


void Task1(void *argument) {
    /* USER CODE BEGIN 5 */
    /* Infinite loop */
    for (;;) {
        if (xSemaphoreTake(hMutex,2000) == pdTRUE) {
            PrintMsg("Shared Resource Start and Executing Task 1\r\n");
            xSemaphoreGive(hMutex);
            PrintMsg("Shared Resource End and Executing Task 1\r\n");
            vTaskDelay(100);
        } else {
            PrintMsg("Task 1 Didn't get access to shared resource\r\n");
        }
    }
    /* USER CODE END 5 */
}


void Task2(void *argument) {
    /* USER CODE BEGIN 5 */
    /* Infinite loop */
    for (;;) {
        if (xSemaphoreTake(hMutex,2000) == pdTRUE) {
            PrintMsg("Shared Resource Start and Executing Task 2\r\n");
            //xSemaphoreGive(hMutex);
            PrintMsg("Shared Resource End and Executing Task 2\r\n");
            vTaskDelay(100);
        } else {
            PrintMsg("Task 2 Didn't get access to shared resource\r\n");
        }
    }
    /* USER CODE END 5 */
}


void PrintMsg(char *data) {
    int i = 0;
    while (*(data + i) != '\0') {
        HAL_UART_Transmit(&huart2,(uint8_t*) &data[i],1,0xFF);
        i++;
    }
}

使用的硬件:STM32F446RE

当我运行这段代码时,我得到如下输出

//输出开始

创建互斥锁

共享资源启动并执行任务1

共享资源结束并执行任务1

任务 2 无法访问共享资源

Task 1 DiTask 2 无法访问共享资源

任务 1 无法访问共享资源

任务 2 无法访问共享资源 . .

//输出结束

问题 1)

考虑到 Task1 被首先调度,因为 Task1 和 2 的优先级相同。 Task1 执行正确。

在此之后,Task2 已安排但无法获取互斥锁,因此我得到的输出为“Task 2 无法访问共享资源”。为什么会这样?

问题 2 )

“Task 1 DiTask 2 did not get access to shared resource”在这一行中,似乎 Task1 正在执行但它被 Task2 抢占,这本不应该发生,因为两个任务具有相同的优先级??

解决方法

问题 1:这里可能的解释是,您被相互竞争的任务覆盖了彼此缓冲的 UART 输出所愚弄。 UART 慢,任务切换快 :)

Task1 打印“Shared Resource Start and Executing Task 1”并第一次释放互斥锁后,scheduler 立即切换到Task2(可能是因为Task1 已经用完了它的时间槽?)。 Task2 获取互斥锁,快速将它的两个消息都吐到 UART 缓冲区中,然后进入睡眠状态 100 个滴答声。调度程序立即切换回 Task1,但不幸的是,来自 Task2 的缓冲消息尚未到达 UART 输出。在打印单个字节的 Task2-s 消息之前,UART 缓冲区被消息“Shared Resource End and Executing Task 1”覆盖。

之后整个过程停止,因为您还没有释放 Task2 中的互斥锁。

Q2:当调度器决定时(通常是当它们进入睡眠或用完它们的时间段时),具有相同优先级的任务会被彼此抢占

我会推荐这本tutorial/book,第 3 章对任务的工作原理有很好的解释。

,

USART 是一个资源,所以它不能被没有同步机制的两个任务共享,比如互斥锁。您对 PrintMsg() 的使用违反了此规则。

调度程序以 configTICK_RATE_HZ 频率运行。如果 configUSE_TIME_SLICING1(或未定义),调度程序会在同等优先级的任务之间切换。如果您不想要这种默认行为,请将 configUSE_TIME_SLICING 设置为 0

请记住,您的 configTICK_RATE_HZ 设置 1000 最多可为每个任务提供约 1 毫秒的运行时间,除非没有其他任务可以运行。更高优先级的任务也可以在 1 毫秒过去之前抢占它。使用 115200 波特率,您可以在这个 ~1 毫秒的时间段内发送 ~10 个字节左右。