rt-thread icon indicating copy to clipboard operation
rt-thread copied to clipboard

[kernel 调度]创建3个相同优先级的线程,第1个线程永远无法得到调度

Open tujc opened this issue 2 years ago • 1 comments

工具链:stm32 + armcc rt-thread版本:5.0.1 问题:创建3个相同优先级的线程,第1个线程永远无法得到调度,如下图代码:

#include <rtthread.h>
#define THREAD_STACK_SIZE 1024
#define THREAD_PRIORITY 20
#define THREAD_TIMESLICE 10

static void thread_entry(void* parameter)
{
    rt_uint32_t value;
    rt_uint32_t count = 0;
    value = (rt_uint32_t)parameter;
    while (1)
    {
        count++;
        if(count % 1000000 == 0)
            rt_kprintf("thread %d is running ,thread %d count = %u\n", value , value, count);
    }
}

int main(void)
{
    //printf("hello!\n");
    rt_thread_t tid = RT_NULL;

    tid = rt_thread_create("thread1", 
                    thread_entry, 
                    (void*)1, 
                    THREAD_STACK_SIZE, 
                    THREAD_PRIORITY, 
                    THREAD_TIMESLICE);
    if (tid != RT_NULL)
        rt_thread_startup(tid);

    tid = rt_thread_create("thread2",
                        thread_entry, 
                        (void*)2,
                        THREAD_STACK_SIZE,
                        THREAD_PRIORITY, 
                        THREAD_TIMESLICE);
    if (tid != RT_NULL)
        rt_thread_startup(tid);

    tid = rt_thread_create("thread3",
                        thread_entry, 
                        (void*)3,
                        THREAD_STACK_SIZE,
                        THREAD_PRIORITY, 
                        THREAD_TIMESLICE);
    if (tid != RT_NULL)
        rt_thread_startup(tid);

    return 0;
}

现象如下: 企业微信截图_16950017415909 可以发现线程1一直得不到运行; 分析代码后,修改scheduler_up.c中的rt_schedule函数,修改点在代码中 //fix和//add处,

void rt_schedule(void)
{
    rt_base_t level;
    struct rt_thread *to_thread;
    struct rt_thread *from_thread;

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

    /* check the scheduler is enabled or not */
    if (rt_scheduler_lock_nest == 0)
    {
        rt_ubase_t highest_ready_priority;

        if (rt_thread_ready_priority_group != 0)
        {
            /* need_insert_from_thread: need to insert from_thread to ready queue */
            int need_insert_from_thread = 0;

            to_thread = _scheduler_get_highest_priority_thread(&highest_ready_priority);

            if ((rt_current_thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_RUNNING)
            {
                if (rt_current_thread->current_priority < highest_ready_priority)
                {
                    to_thread = rt_current_thread;
                }
                else if (rt_current_thread->current_priority == highest_ready_priority && (rt_current_thread->stat & RT_THREAD_STAT_YIELD_MASK) == 0)
                {
                    to_thread = rt_current_thread;
                }
                else
                {
                    need_insert_from_thread = 1;
                }
		//fix
                //rt_current_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
            }

            if (to_thread != rt_current_thread)
            {
                /* if the destination thread is not the same as current thread */
                rt_current_priority = (rt_uint8_t)highest_ready_priority;
                from_thread         = rt_current_thread;
                rt_current_thread   = to_thread;

                RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));

                if (need_insert_from_thread)
                {
                    rt_schedule_insert_thread(from_thread);
                }
								
	      //add
              if((from_thread->stat & RT_THREAD_STAT_YIELD_MASK) != 0)
              {
		              from_thread->stat &= ~RT_THREAD_STAT_YIELD_MASK;
              }		
                rt_schedule_remove_thread(to_thread);
                to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);

                /* switch to new thread */
                RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
                        ("[%d]switch to priority#%d "
                         "thread:%.*s(sp:0x%08x), "
                         "from thread:%.*s(sp: 0x%08x)\n",
                         rt_interrupt_nest, highest_ready_priority,
                         RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
                         RT_NAME_MAX, from_thread->parent.name, from_thread->sp));

#ifdef RT_USING_OVERFLOW_CHECK
                _scheduler_stack_check(to_thread);
#endif /* RT_USING_OVERFLOW_CHECK */

                if (rt_interrupt_nest == 0)
                {
                    extern void rt_thread_handle_sig(rt_bool_t clean_state);

                    RT_OBJECT_HOOK_CALL(rt_scheduler_switch_hook, (from_thread));

                    rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
                            (rt_ubase_t)&to_thread->sp);

                    /* enable interrupt */
                    rt_hw_interrupt_enable(level);

#ifdef RT_USING_SIGNALS
                    /* check stat of thread for signal */
                    level = rt_hw_interrupt_disable();
                    if (rt_current_thread->stat & RT_THREAD_STAT_SIGNAL_PENDING)
                    {
                        extern void rt_thread_handle_sig(rt_bool_t clean_state);

                        rt_current_thread->stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;

                        rt_hw_interrupt_enable(level);

                        /* check signal status */
                        rt_thread_handle_sig(RT_TRUE);
                    }
                    else
                    {
                        rt_hw_interrupt_enable(level);
                    }
#endif /* RT_USING_SIGNALS */
                    goto __exit;
                }
                else
                {
                    RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));

                    rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
                            (rt_ubase_t)&to_thread->sp, from_thread, to_thread);
                }
            }
            else
            {
                rt_schedule_remove_thread(rt_current_thread);
                rt_current_thread->stat = RT_THREAD_RUNNING | (rt_current_thread->stat & ~RT_THREAD_STAT_MASK);
            }
        }
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

__exit:
    return;
}

修改后现象如下: 企业微信截图_16950027311288 分析原因: 由于提前清掉了RT_THREAD_STAT_YIELD标志,导致在rt_schedule_insert_thread函数中将待插入的时间片到期的线程放在了链表头部(正常情况下,时间片到期的线程应该放在尾部),而下一次到期时获取最高优先级线程一直都是从头部获取,从而导致了只有当前线程和链表第1个线程能得到轮转,第3个永远无法得到执行; 修改方式: 修改清除RT_THREAD_STAT_YIELD标志的时机,即在rt_schedule_insert_thread函数执行后,再清除线程RT_THREAD_STAT_YIELD标志。

tujc avatar Sep 18 '23 02:09 tujc

感谢提交issue,经测试上述情况属实,解决方案可行。

Solonix-Chu avatar Oct 12 '23 07:10 Solonix-Chu