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

一、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、总结

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

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);

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

上一篇:python3学习笔记


下一篇:左偏树(可并堆)【模板】