RT-Thread 18. 互斥量避免优先级反转

发布时间:2024年01月23日
1.代码

//mux.c

#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"

#define THREAD_PRIORITY 25
#define THREAD_TIMESLICE 10

ALIGN(RT_ALIGN_SIZE)
static struct rt_semaphore keyPress2_sem;
static char thread21_stack[500];
static struct rt_thread thread21;

ALIGN(RT_ALIGN_SIZE)
static struct rt_semaphore keyPress2_sem;
static char thread22_stack[500];
static struct rt_thread thread22;

ALIGN(RT_ALIGN_SIZE)
static struct rt_semaphore keyPress2_sem;
static char thread23_stack[500];
static struct rt_thread thread23;

static struct rt_mutex mutex1;

void out_pri(void)
{
  rt_kprintf("the priority of thread21 is: %d\n", thread21.current_priority);
  rt_kprintf("the priority of thread22 is: %d\n", thread22.current_priority);
  rt_kprintf("the priority of thread23 is: %d\n", thread23.current_priority);  
}

void MuxKeyScanCallBack(void)
{
  rt_sem_release(&keyPress2_sem); 
}
static void rt_thread21_entry(void *parameter)
{
  static rt_err_t result;
//  while(1)
  {
    rt_kprintf("t21 task start.\n");
    rt_thread_mdelay(50);
    rt_kprintf("t21 want take a static mutex.\n");
    result = rt_mutex_take(&mutex1, RT_WAITING_FOREVER);	#0
    if (result != RT_EOK)
    {
      rt_kprintf("t21 take a static mutex, failed.\n");
      return;
    }
    else
    {
      rt_kprintf("t21 take a static mutex.\n");
      rt_kprintf("t21 pri start.\n");
      out_pri();
      rt_thread_mdelay(100);
    }
    rt_kprintf("t21 release a static mutex.\n");
    rt_mutex_release(&mutex1);
    rt_kprintf("t21 task end.\n");
  }
}
static void rt_thread22_entry(void *parameter)
{
  rt_tick_t tick;
//  while(1)
  {
    rt_kprintf("t22 task start.\n");
    rt_thread_mdelay(100);
    rt_kprintf("t22 pri start.\n");
    out_pri();
    /* 做 一 个 长 时 间 的 循 环,1000ms */
    tick = rt_tick_get();
    while (rt_tick_get() - tick < (RT_TICK_PER_SECOND)) ;
    rt_kprintf("t22 task end.\n");
  }
}
static void rt_thread23_entry(void *parameter)
{
  static rt_err_t result;
//  while(1)
  {
    rt_kprintf("t23 task start.\n");
    rt_kprintf("t23 want take a static mutex.\n");
    result = rt_mutex_take(&mutex1, RT_WAITING_FOREVER);
    if (result != RT_EOK)
    {
      rt_kprintf("t23 take a static mutex, failed.\n");
      return;
    }
    else
    {
      rt_kprintf("t23 take a static mutex.\n");
      rt_kprintf("t23 pri start.\n");
      out_pri();
      rt_thread_mdelay(200);
    }
    rt_kprintf("t23 release a static mutex.\n");
    rt_mutex_release(&mutex1);
    rt_kprintf("t23 task end.\n");
  }
}

void MuxTaskInit(void)
{
  /* 初 始 化 3 个 信 号 量 */
  rt_mutex_init(&mutex1, "pri_inversion_mutex", RT_IPC_FLAG_FIFO);
  rt_thread_init(&thread21,
                "thread21",
                rt_thread21_entry,
                RT_NULL,
                &thread21_stack[0],
                sizeof(thread21_stack),
                THREAD_PRIORITY-2, THREAD_TIMESLICE);  
  rt_thread_startup(&thread21);
  rt_thread_init(&thread22,
                "thread22",
                rt_thread22_entry,
                RT_NULL,
                &thread22_stack[0],
                sizeof(thread22_stack),
                THREAD_PRIORITY-1, THREAD_TIMESLICE);  
  rt_thread_startup(&thread22); 
  rt_thread_init(&thread23,
                "thread23",
                rt_thread23_entry,
                RT_NULL,
                &thread23_stack[0],
                sizeof(thread23_stack),
                THREAD_PRIORITY, THREAD_TIMESLICE);  
  rt_thread_startup(&thread23);   
}
2.输出
t21 task start.
t22 task start.
t23 task start.
t23 want take a static mutex.
t23 take a static mutex.
t23 pri start.
the priority of thread21 is: 23
the priority of thread22 is: 24
the priority of thread23 is: 25
t21 want take a static mutex.


t22 pri start.
the priority of thread21 is: 23		//#1
the priority of thread22 is: 24
the priority of thread23 is: 23		//#2


t23 release a static mutex.
t21 take a static mutex.
t21 pri start.
the priority of thread21 is: 23
the priority of thread22 is: 24
the priority of thread23 is: 25


t21 release a static mutex.
t21 task end.


t22 task end.
t23 task end.
3.优先级反转分析

优先级,t21>t22>t23。t23先先持有互斥量,t21试图持有互斥量(#0),此时线程 3 的优 先级被提升为和线程 2 的优先级相同。通过线程t22打印出优先级提升后的,见#1,#2。t23的优先级提升到t21相同。这样中间优先级的t22执行时间不会影响t21的执行。

文章来源:https://blog.csdn.net/lljss1980/article/details/135774885
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。