找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2341|回复: 0
打印 上一主题 下一主题
收起左侧

RT_THREAD系统中的位图算法

[复制链接]
跳转到指定楼层
楼主
ID:204746 发表于 2017-5-25 15:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<stdio.h>
unsigned char rt_ready_stable[32];//have 8bit per array,all 256bbit
unsigned long rt_thread_priority_group;//
unsigned char highest_priority_thread;
//unsigned char thread_mask;
//unsigned char thread_high_mask;

/*
biggest priority is 8,and the map is belowing
rt_lowest_bitmap[index]

the biggest priority is 32,and the map is belowing
[31......0] 32bit in all
index=priority_group&0xff,......
rt_lowest_bitmap[index]

the biggest priority is 256,and the map is belowing
[31.........0] 32bit in all
[7...0] 8bit in all
index1=rt_lowest_bitmap[index]
result=rt_lowest_bitmap[index1]
*/
const unsigned char rt_lowest_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  
};  
/**
* @function get the lowest bit of one byte
*/
unsigned char GetLowBit(unsigned char byte)
{
  unsigned char i;
  unsigned char c;
  c=0;//init the number

  for(i=0;i<8;i++)
  {
    if(byte&0x01)
                return c;
        c=c+1;
        byte=byte>>1;
  }
  return 0;
}
/**
*@functon print all the lowest bit arrange 0 to 255
*/
void PrintTheMap(void)
{
   unsigned short i;
   for(i=0;i<256;i++)
   {
      printf("%d,",GetLowBit(i));
          if((i+1)%16==0)
           printf("\n");
   }
}

/**
* @function create one thread
@param num is the priority
*/
void CreateOneThread(unsigned char num)
{
   unsigned char current_priority;
   unsigned long priority_byte;
   unsigned char priority_bit;
   unsigned char priority_number;
   current_priority=num;
   priority_number=num>>3;//the integer of 8
   priority_byte=1<<priority_number; //sure the bit of 32bit number
   priority_bit=1<<(current_priority&0x07);//the rest of 8
#if 1
   rt_ready_stable[priority_number] |=priority_bit;
#endif
   rt_thread_priority_group |=priority_byte;
}
/**
* @functon find the highest_priority;
*/
unsigned char Find_Highest_priority()
{
#if RT_HTREAD_PRIORITY_MAX==8
        highest_priority_thread=rt_lowest_bitmap[rt_thread_priority_group];
        return highest_priority_thread;
#endif
        unsigned char number;
        if(rt_thread_priority_group&0xff)
        {
          number=rt_lowest_bitmap[(rt_thread_priority_group)&0xff];
        }
        else if(rt_thread_priority_group&0xff00)
        {
           number=rt_lowest_bitmap[(rt_thread_priority_group>>8)&0xff]+8;
        }
        else if(rt_thread_priority_group&0xff0000)
        {
                number=rt_lowest_bitmap[(rt_thread_priority_group>>16)&0xff]+16;
        }
        else
        {
           number=rt_lowest_bitmap[(rt_thread_priority_group>>24)&0xff]+24;
        }

        highest_priority_thread=(number<<3)+rt_lowest_bitmap[rt_ready_stable[number]];
       
        return highest_priority_thread;
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表