关于μC/OS-II原任务调度算法猛戳此处。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
#include <stdio.h>
typedef unsigned char INT8U; /* Unsigned 8 bit quantity */ INT8U OSRdyGrp; INT8U OSRdyTbl[ 8]; //即将要运行的任务列表 //OSMapTbl[]:就绪任务表;对应OSRdyGrp和OSRbyTbl[i]的位值(0~7) INT8U const OSMapTbl[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; //******************************************************************************************************* //最高优先级任务查找表(PRIORITY RESOLUTION TABLE) //注意: 变址索引表是位模式,找出就绪态最高优先级别任务,给定值应符合高优先级位位值(0~7) //OSUnMapTbl[]:最高优先级任务查找表;对应OSRdy Grp和OSRbyTbl[i]的位值(0~7) INT8U const OSUnMapTbl[] = { 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x00 to 0x0F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x10 to 0x1F */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x20 to 0x2F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x30 to 0x3F */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x40 to 0x4F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x50 to 0x5F */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x60 to 0x6F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x70 to 0x7F */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x80 to 0x8F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x90 to 0x9F */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xA0 to 0xAF */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xB0 to 0xBF */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xC0 to 0xCF */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xD0 to 0xDF */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xE0 to 0xEF */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /* 0xF0 to 0xFF */ }; int main( void) { INT8U x,y; INT8U prio; //使任务进入就绪状态 prio = 5; OSRdyGrp |= OSMapTbl[prio >> 3]; OSRdyTbl[prio >> 3] |= OSMapTbl[prio & 0x07]; prio = 15; OSRdyGrp |= OSMapTbl[prio >> 3]; OSRdyTbl[prio >> 3] |= OSMapTbl[prio & 0x07]; prio = 25; OSRdyGrp |= OSMapTbl[prio >> 3]; OSRdyTbl[prio >> 3] |= OSMapTbl[prio & 0x07]; prio = 35; OSRdyGrp |= OSMapTbl[prio >> 3]; OSRdyTbl[prio >> 3] |= OSMapTbl[prio & 0x07]; prio = 45; OSRdyGrp |= OSMapTbl[prio >> 3]; OSRdyTbl[prio >> 3] |= OSMapTbl[prio & 0x07]; prio = 5; //使任务脱离就绪状态 if((OSRdyTbl[prio >> 3] &= ~OSMapTbl[prio & 0x07]) == 0) OSRdyGrp &= ~OSMapTbl[prio >> 3]; y = OSUnMapTbl[OSRdyGrp]; x = OSUnMapTbl[OSRdyTbl[y]]; prio = (y << 3) + x; printf( "High Priority = %d \n",prio); printf( "x = %d \n",x); printf( "y = %d \n",y); getchar(); return 0; } |
支持的任务数 | 额外开辟的内存空间 |
原版:64 | 256-OSUnMapTbl[256]+8-OSMapTbl[]+1-OSRdyGrp |
512 | 256-OSUnMapTbl[256]+8-OSMapTbl[]+1-OSRdyGrpX+8-OSRdyGrpY8[] |
4096 | 256-OSUnMapTbl[256]+8-OSMapTbl[]+1-OSRdyGrpX+8-OSRdyGrpY[8]+64-OSRdyGrpZ[8][8] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
#include <stdio.h> typedef unsigned char INT8U; /* Unsigned 8 bit quantity */ INT8U OSRdyGrpX; INT8U OSRdyGrpY[8]; INT8U OSRdyTbl[8][8]; //即将要运行的任务列表 //OSMapTbl[]:就绪任务表;对应OSRdyGrp和OSRbyTbl[i]的位值(0~7) INT8U const OSMapTbl[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; //******************************************************************************************************* //最高优先级任务查找表(PRIORITY RESOLUTION TABLE) //注意: 变址索引表是位模式,找出就绪态最高优先级别任务,给定值应符合高优先级位位值(0~7) //OSUnMapTbl[]:最高优先级任务查找表;对应OSRdy Grp和OSRbyTbl[i]的位值(0~7) INT8U const OSUnMapTbl[] = { 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x00 to 0x0F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x10 to 0x1F */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x20 to 0x2F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x30 to 0x3F */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x40 to 0x4F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x50 to 0x5F */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x60 to 0x6F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x70 to 0x7F */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x80 to 0x8F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x90 to 0x9F */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xA0 to 0xAF */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xB0 to 0xBF */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xC0 to 0xCF */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xD0 to 0xDF */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xE0 to 0xEF */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /* 0xF0 to 0xFF */ }; #define t1 (prio >> 6) #define t2 ((prio >> 3) & 0x07) #define t3 (prio & 0x07) int main(void) { INT8U x,y,z; int prio; //使任务进入就绪状态 prio = 257; OSRdyGrpX |= OSMapTbl[t1]; OSRdyGrpY[t1] |= OSMapTbl[t2]; OSRdyTbl[t1][t2] |= OSMapTbl[t3]; prio = 113; OSRdyGrpX |= OSMapTbl[t1]; OSRdyGrpY[t1] |= OSMapTbl[t2]; OSRdyTbl[t1][t2] |= OSMapTbl[t3]; prio = 14; OSRdyGrpX |= OSMapTbl[t1]; OSRdyGrpY[t1] |= OSMapTbl[t2]; OSRdyTbl[t1][t2] |= OSMapTbl[t3]; prio = 11; OSRdyGrpX |= OSMapTbl[t1]; OSRdyGrpY[t1] |= OSMapTbl[t2]; OSRdyTbl[t1][t2] |= OSMapTbl[t3]; prio = 11; //使任务脱离就绪状态 if((OSRdyTbl[t1][t2] &= ~OSMapTbl[t3]) == 0) { OSRdyGrpY[t2] &= ~OSMapTbl[t2]; if((OSRdyGrpY[t1] &= ~OSMapTbl[t2]) == 0) OSRdyGrpX &= ~OSMapTbl[t1]; } z = OSUnMapTbl[OSRdyGrpX]; y = OSUnMapTbl[OSRdyGrpY[z]]; x = OSUnMapTbl[OSRdyTbl[z][y]]; prio = (z<<6)+(y<<3) + x; printf("High Priority = %d \n",prio); printf("x = %d \n",x); printf("y = %d \n",y); printf("z = %d \n",z); getchar(); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
#include <stdio.h> typedef unsigned char INT8U; /* Unsigned 8 bit quantity */ typedef unsigned short INT16U; INT16U OSRdyGrp; INT8U OSRdyTbl[16]; //即将要运行的任务列表 //OSMapTbl[]:就绪任务表;对应OSRdyGrp和OSRbyTbl[i]的位值(0~7) INT16U const OSMapTbl[] = { 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000}; //******************************************************************************************************* //最高优先级任务查找表(PRIORITY RESOLUTION TABLE) //注意: 变址索引表是位模式,找出就绪态最高优先级别任务,给定值应符合高优先级位位值(0~7) //OSUnMapTbl[]:最高优先级任务查找表;对应OSRdy Grp和OSRbyTbl[i]的位值(0~7) INT8U const OSUnMapTbl[] = { 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x00 to 0x0F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x10 to 0x1F */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x20 to 0x2F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x30 to 0x3F */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x40 to 0x4F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x50 to 0x5F */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x60 to 0x6F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x70 to 0x7F */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x80 to 0x8F */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x90 to 0x9F */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xA0 to 0xAF */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xB0 to 0xBF */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xC0 to 0xCF */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xD0 to 0xDF */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xE0 to 0xEF */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /* 0xF0 to 0xFF */ }; int main(void) { INT8U x,y; INT8U prio; //使任务进入就绪状态 prio = 5; OSRdyGrp |= OSMapTbl[prio >> 3]; OSRdyTbl[prio >>3] |= OSMapTbl[prio & 0x07]; prio = 100; OSRdyGrp |= OSMapTbl[prio >> 3]; OSRdyTbl[prio >>3] |= OSMapTbl[prio & 0x07]; prio = 111; OSRdyGrp |= OSMapTbl[prio >> 3]; OSRdyTbl[prio >>3] |= OSMapTbl[prio & 0x07]; prio = 120; OSRdyGrp |= OSMapTbl[prio >> 3]; OSRdyTbl[prio >>3] |= OSMapTbl[prio & 0x07]; prio = 125; OSRdyGrp |= OSMapTbl[prio >> 3]; OSRdyTbl[prio >>3] |= OSMapTbl[prio & 0x07]; prio = 5; //使任务脱离就绪状态 if((OSRdyTbl[prio >> 3] &= ~OSMapTbl[prio & 0x07]) == 0) OSRdyGrp &= ~OSMapTbl[prio >> 3]; if((OSRdyGrp & 0xff) != 0) { y = OSUnMapTbl[OSRdyGrp & 0xff]; x = OSUnMapTbl[OSRdyTbl[y]]; prio = (y << 3) + x; } else { y = OSUnMapTbl[OSRdyGrp >> 8]; x = OSUnMapTbl[OSRdyTbl[y + 8]]; prio = 64 + (y << 3) + x; } printf("High Priority = %d \n",prio); printf("x = %d \n",x); printf("y = %d \n",y); getchar(); return 0; } |