ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

rt-thread源码分析篇十八:rt_system_scheduler_start函数分析

2021-09-11 12:02:49  阅读:366  来源: 互联网

标签:rt r0 r1 thread priority 源码 ready


一、rt_system_scheduler_start源码

{
    register struct rt_thread *to_thread;
    register rt_ubase_t highest_ready_priority;

#if RT_THREAD_PRIORITY_MAX > 32
    register rt_ubase_t number;

    number = __rt_ffs(rt_thread_ready_priority_group) - 1;
    highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
#else
    highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
#endif

    /* get switch to thread */
    to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
                              struct rt_thread,
                              tlist);

    rt_current_thread = to_thread;

    /* switch to new thread */
    rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);

    /* never come back */
}

二、rt_system_scheduler_start函数分析

1、rt_thread_ready_priority_group

typedef unsigned long                   rt_uint32_t;
rt_uint32_t rt_thread_ready_priority_group;

rt_thread_ready_priority_grouprt_thread_startup函数中rt_thread_resume函数中rt_schedule_insert_thread函数调用。

rt_thread_ready_priority_group |= thread->number_mask;

thread->number_maskrt_thread_startup函数中设置。

thread->number_mask = 1L << thread->current_priority;
thread->init_priority    = priority;
thread->current_priority = thread->init_priority;

priority有调用rt_thread_create函数传入的参数。

1、main线程创建

1、priority设置为RT_MAIN_THREAD_PRIORITY

#define RT_THREAD_PRIORITY_MAX 32
#define RT_MAIN_THREAD_PRIORITY       (RT_THREAD_PRIORITY_MAX / 3)

2、main函数设置rt_thread_ready_priority_group0x400
rt_thread_ready_priority_group变量设置为或运算。

2、idle线程创建

1、priority设置为RT_THREAD_PRIORITY_MAX - 1
2、main函数设置rt_thread_ready_priority_group0x800000400

2、__rt_ffs

int __rt_ffs(int value)
{
    if (value == 0) return 0;

    if (value & 0xff)
        return __lowest_bit_bitmap[value & 0xff] + 1;

    if (value & 0xff00)
        return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9;

    if (value & 0xff0000)
        return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17;

    return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25;
}
highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;

rt_thread_ready_priority_group变量中低位代表更高优先级。

3、rt_thread_priority_table

struct rt_list_node
{
    struct rt_list_node *next;                          /**< point to next node. */
    struct rt_list_node *prev;                          /**< point to prev node. */
};
typedef struct rt_list_node rt_list_t;

#define RT_THREAD_PRIORITY_MAX 32

rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];

rt_thread_startup函数中调用rt_thread_resume中调用的rt_schedule_insert_thread函数调用的rt_list_insert_before函数完成插入列表。

1、初始化

rt_thread_priority_tablert_system_scheduler_init函数中初始化。

2、main线程插入

rt_application_init函数中创建main线程,并调用将main线程插入到rt_thread_priority_table[10]中。

3、idle线程插入

rt_thread_idle_init函数中创建idle线程,并调用将idle线程插入到rt_thread_priority_table[31]中。

4、总结

在这里插入图片描述

4、rt_list_entry

#define rt_list_entry(node, type, member) \
    rt_container_of(node, type, member)
#define rt_container_of(ptr, type, member) \
    ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
/* get switch to thread */
to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
                          struct rt_thread,
                          tlist);

to_thread保存当前就绪最高优先级的线程结构体首地址。

5、rt_current_thread

rt_current_thread = to_thread;

将下一个执行线程保存在rt_current_thread变量中。

6、rt_hw_context_switch_to

;/*
; * void rt_hw_context_switch_to(rt_uint32 to);
; * r0 --> to
; * this fucntion is used to perform the first thread switch
; */
rt_hw_context_switch_to    PROC
    EXPORT rt_hw_context_switch_to
    ; set to thread
    LDR     r1, =rt_interrupt_to_thread
    STR     r0, [r1]

    ; set from thread to 0
    LDR     r1, =rt_interrupt_from_thread
    MOV     r0, #0x0
    STR     r0, [r1]

    ; set interrupt flag to 1
    LDR     r1, =rt_thread_switch_interrupt_flag
    MOV     r0, #1
    STR     r0, [r1]

    ; set the PendSV exception priority
    LDR     r0, =NVIC_SYSPRI2
    LDR     r1, =NVIC_PENDSV_PRI
    LDR.W   r2, [r0,#0x00]       ; read
    ORR     r1,r1,r2             ; modify
    STR     r1, [r0]             ; write-back

    ; trigger the PendSV exception (causes context switch)
    LDR     r0, =NVIC_INT_CTRL
    LDR     r1, =NVIC_PENDSVSET
    STR     r1, [r0]

    ; restore MSP
    LDR     r0, =SCB_VTOR
    LDR     r0, [r0]
    LDR     r0, [r0]
    MSR     msp, r0

    ; enable interrupts at processor level
    CPSIE   F
    CPSIE   I

    ; never reach here!
    ENDP
rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);

作用:切换到下一个线程。

标签:rt,r0,r1,thread,priority,源码,ready
来源: https://blog.csdn.net/OnlyLove_/article/details/120235474

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有