UC/OS-II how to make a task in the ready table into the ready state and exit the ready state

Source: Internet
Author: User

 

Program list: make the task ready

Osrdygrp | = osmaptbl [PRIO> 3]; (1)

Osrdytbl [PRIO> 3] | = osmaptbl [PRIO & 0x07]; (2)

The two lines of code serve to add a given priority (PRIO) task to the ready table;

To thoroughly understand these two lines of code, we must first have a knowledge of PRIO.

Prio priority, from 0 ~ 63, that is, from 0x00 ~ 0x3f. It only takes 8 characters to 6 characters in length. The lower six places can be divided into the lower three places and the higher three places:

0x3f = 00 111 111

Three green bits, namely PRIO> 3; three red bits, namely, PRIO & 0x07, are y coordinates and X coordinates in the ready table, respectively, osrdygrp is a bit operable, and each of its bits corresponds to the Y coordinate from 0 ~ 7, indicates the row of the ready table. If any task with a priority is ready in this row, the corresponding position is 1;

Similarly, osrdytbl [] indicates the content of each row of the ready table, and each element of the table is also operable. Each subscript represents the Y coordinate, that is, the number of rows, each subscript corresponds to each bit of the element, corresponding to the eight elements of the row, that is, the eight columns of the ready table. In this way, constitute the 8x8 readiness table of uC/OS-II, indicating 0 ~ 63.

Let's take a look at the osmaptbl array, which has been defined. Its eight elements are:

Osmaptbl [0] = 00000001;

Osmaptbl [1] = 00000010;

Osmaptbl [2] = 00000100;

Osmaptbl [3] = 00001000;

Osmaptbl [4] = 00010000;

Osmaptbl [5] = 00100000;

Osmaptbl [6] = 01000000;

Osmaptbl [7] = 10000000;

Now, the two lines of code are easy to understand:

(1): Move PRIO three places to the right, remove the last three digits occupied by the X coordinate, take only the Y coordinate, and fill in the osmaptbl []. The return value is a bit or same as that of osrdygrp, place osrdygrp in the corresponding position 1, indicating that a task in the row enters the ready state. For example, if PRIO> 3 obtains y coordinate = 3, osmaptbl [3] = 00001000, bitwise with osrdygrp or, place the fourth position 1;

(2): Set PRIO and 0x07 = 00000111 as bitwise and keep the three lower positions. Set the other positions to 0 to obtain the X coordinate, and enter the X coordinate into osmaptbl []. the returned value is bitwise to osrdytbl [Y], and the corresponding location of osrdytbl [y] is 1, indicating that the X-th bit of the row has a task in the ready state. Note that the X-th bit should be counted from the low end, that is, the right end of the table starts to count. For example, if y coordinate = 3 has been calculated just now, assume PRIO & 0x07 = x coordinate = 4, osmaptbl [4] = 00010000, bitwise the returned value with osmaptbl [3] or the fifth position 1, indicating that the task of this bit enters the ready state;

Calculate the task priority PRIO based on X and Y:

Simply reverse the above operation: PRIO = [Y <3] + X;

For example, in the above example, y = 3, x = 4, then:

Prio = [Y <3] + x = [3 <3] + 4 = 28;

Therefore, the priority of a ready task is 28.

 

/*************************************** **************************************** **********************

In fact, the osrdytbl [] elements (a total of 8 elements, each of which is 8 bits) constitute an 8x8 ready table. osrdygrp only indicates the Y axis (ROW) of the ready table ), that is to say, each bit in the osrdygrp indicates whether there are ready tasks in each group (ROW) of eight (ROW) tasks.

Note that the elements of osrdygrp and osrdytbl [] can be bitwise.

1. Enable the task to enter the ready table (use osmaptbl [] to set 1 for the corresponding rows and columns of the ready table ):

Osrdygrp | = osmaptbl [PRIO> 3];
Osrdytbl [PRIO> 3] | = osmaptbl [PRIO & 0x07];

2. delete a task from the ready table (use osmaptbl [] to set the corresponding row and column to 0 in the ready table ):

If (osrdytbl [PRIO> 3] & = ~ Osmaptbl [PRIO & 0x07]) = 0)
Osrdygrp & = ~ Osmaptbl [PRIO> 3];

Note: osrdytbl [PRIO> 3] When all bits are zero, the corresponding bits of osrdygrp are cleared. Therefore, you must make a judgment.

3. Find the task with the highest priority in the ready state (by determining the priority table osunmaptbl ):

The search logic is from top to bottom, from right to left, but this requires a lot of judgment, therefore, as long as the osunmaptbl [] Table is determined based on Y = osrdygrp and osrdytbl [Y], the task with the highest priority can be found by entering the subscript operation and changing the time with space.

Y = osunmaptbl [osrdygrp];
X = osunmaptbl [osrdytbl [y];

Prio = (Y <3) + X;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.