tr-thread进程切换:(代入优先级计算不对,后边可以再看)

tr-thread进程切换:

原理概述:
1.进程优先级>>3, 即用8取模.就是数组号;
2.进程优先级取余,即用8取余数,就是该数组内下标;
总结:向优先级表里填写和获取都是这个原理.

1)
#if RT_THREAD_PRIORITY_MAX > 32
thread->number = thread->current_priority >> 3; 33>>3 = 4 /* 除以8就是取余数就是按8取模值,数组的组号*/
thread->number_mask = 1L << thread->number; 1<<4 = 8 /* number:线程优先级对应组掩码 */
thread->high_mask = 1L << (thread->current_priority & 0x07); 33 & 0x07 = 1, 1<<1 = 2 /* high_mask:线程优先级数组内位值 */
#else
thread->number_mask = 1L << thread->current_priority;
#endif
2)
#if RT_THREAD_PRIORITY_MAX > 32
rt_thread_ready_table[thread->number] |= thread->high_mask; xx[4] | 2 = 2
//组号 //位号
#endif
rt_thread_ready_priority_group |= thread->number_mask; x | 8 = 8 /* rt_system_scheduler_init中初始化为0, 最高优先级进程对应的组 */

3)

const rt_uint8_t __lowest_bit_bitmap[] =
{
/* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};

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

if (value & 0xff)
return __lowest_bit_bitmap[value & 0xff] + 1; //x[8] + 1 = 3+ 1 = 4

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

4)
if (rt_scheduler_lock_nest == 0)
{
register rt_ubase_t highest_ready_priority;
/* 通过__rt_ffs找出最高优先级进程 */
#if RT_THREAD_PRIORITY_MAX <= 32
highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
#else
register rt_ubase_t number;

number = __rt_ffs(rt_thread_ready_priority_group) - 1; 4-1 = 3
highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1;
/* 优先级 = (组号 << 3) + 位号 就可以计算出最高的优先级 */
#endif

 


------------------------------------------------------------------------------------
------------------------------------------------------------------------------------

1)
#if RT_THREAD_PRIORITY_MAX > 32
thread->number = thread->current_priority >> 3; /* 除以8就是取余数就是按8取模值,数组的组号*/
thread->number_mask = 1L << thread->number; /* number:线程优先级对应组掩码 */
thread->high_mask = 1L << (thread->current_priority & 0x07); /* high_mask:线程优先级数组内位值 */
#else
thread->number_mask = 1L << thread->current_priority; 1<<23 = 46
#endif
2)
#if RT_THREAD_PRIORITY_MAX > 32
rt_thread_ready_table[thread->number] |= thread->high_mask;
//组号 //位号
#endif
rt_thread_ready_priority_group |= thread->number_mask; /* rt_system_scheduler_init中初始化为0, 最高优先级进程对应的组 */
= 46
3)

const rt_uint8_t __lowest_bit_bitmap[] =
{
/* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
/* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};

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

if (value & 0xff)
return __lowest_bit_bitmap[value & 0xff] + 1; x[46] + 1 = 2

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

4)
if (rt_scheduler_lock_nest == 0)
{
register rt_ubase_t highest_ready_priority;
/* 通过__rt_ffs找出最高优先级进程 */
#if RT_THREAD_PRIORITY_MAX <= 32
highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
#else
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;
/* 优先级 = (组号 << 3) + 位号 就可以计算出最高的优先级 */
#endif

转载于:https://www.cnblogs.com/tureno/articles/8006108.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值