{ 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 */ }
typedef unsigned long rt_uint32_t; rt_uint32_t rt_thread_ready_priority_group;
rt_thread_ready_priority_group
在rt_thread_startup
函数中rt_thread_resume
函数中rt_schedule_insert_thread
函数调用。
rt_thread_ready_priority_group |= thread->number_mask;
thread->number_mask
在rt_thread_startup
函数中设置。
thread->number_mask = 1L << thread->current_priority;
thread->init_priority = priority; thread->current_priority = thread->init_priority;
priority
有调用rt_thread_create
函数传入的参数。
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_group
为0x400
。
rt_thread_ready_priority_group
变量设置为或运算。
1、priority
设置为RT_THREAD_PRIORITY_MAX - 1
。
2、main
函数设置rt_thread_ready_priority_group
为0x800000400
。
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
变量中低位代表更高优先级。
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
函数完成插入列表。
rt_thread_priority_table
在rt_system_scheduler_init
函数中初始化。
rt_application_init
函数中创建main
线程,并调用将main
线程插入到rt_thread_priority_table[10]
中。
rt_thread_idle_init
函数中创建idle
线程,并调用将idle
线程插入到rt_thread_priority_table[31]
中。
#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
保存当前就绪最高优先级的线程结构体首地址。
rt_current_thread = to_thread;
将下一个执行线程保存在rt_current_thread
变量中。
;/* ; * 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);
作用:切换到下一个线程。