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