Ucos ii event control block learning

Source: Internet
Author: User
Ucos ii event control block learning time: Source: Author: Mike Zhong CLICK: 1940

I. Data Structure of the Event Control Block ECB:

Typedef struct
{
Int8u oseventtype; // event type
Int8u oseventgrp; // the group in which the waiting task is located
Int16u oseventcnt; // counter when the event is a semaphore
Void * oseventptr; // pointer to a message or message queue
Int8u oseventtbl [OS _event_tbl_size]; // wait for the task list
} OS _event;

II. Key Algorithms:

1. Insert a task to the task list waiting for the event:

When the OS _eventtaskwait () function is called to wait for a task to wait for an event, this algorithm is required to insert the task to the task list waiting for the event.

Pevent-> oseventgrp | = osmaptbl [PRIO> 3];

Pevent-> oseventtbl [PRIO> 3] = osmaptbl [PRIO & 0x07];

2. Remove the task from the task list of the wait event:

When the OS _eventtaskrdy () function is called to bring a task into the ready state, you need to call this algorithm so that the waiting task enters the ready state from the waiting state.

If (pevent-> oseventtbl [PRIO> 3] & = ~ Osmaptbl [PRIO & 0x07]) = 0)
{
Pevent-> oseventgrp & = ~ Osmaptbl [PRIO> 3];
}

3. Find the task with the highest priority in the task list of the waiting event:

When the OS _eventtaskrdy () function is called to bring a task into the ready state, you need to call this algorithm to first find the task with the highest priority in the list of waiting event tasks to enter the ready state.

Y = osunmaptbl [pevent-> oseventgrp];

X = osunmaptbl [pevent-> oseventtbl [y];

Prio = (Y <3) + X;

III. Basic operations on the event control block ECB

1. Initialize an event control block:

When the ossemcreate (), osmutexcreate (), osmboxcreate (), and osqcreate () functions are created, you must call this function for initialization and initialize an empty wait list, the table does not have any tasks waiting for events.

OS _eventwaitlistinit ();

2. Make a task ready:

When an event occurs, you need to move the task with the highest priority in the event wait task list to the ready state. The functions include ossempost, osmutexpost (), osmboxpost (), and osqpost () you must call this function to make a task ready.

OS _eventtaskrdy ();

3. Enable a task to wait for an event:

When a task waits for an event, the semaphore, mutex semaphore, mailbox, and Message Queue call this function through the corresponding pend function.

OS _eventtaskwait ();

4. Set the task to ready due to wait Timeout:

If the task wait event does not occur within the specified waiting time in advance, the pned type function calls this function to enter the ready state of the task waiting for timeout.

OS _eventto ();

4. Analysis of the basic operation code of the Event Control Block ECB.

1. OS _eventwaitlistinit ();

Void OS _eventwaitlistinit (OS _event * pevent)
{
Int8u * ptbl; // defines the pointer variable ptbl
Pevent-> oseventgrp = 0x00; // clear the group in which the task resides

OS _event_tbl_size is defined in UCOS _ II. H.
Ptbl = & pevent-> oseventtbl [0];

# If OS _event_tbl_size> 0
* Ptbl ++ = 0x00; // clear the waiting task list. The for loop is not used here to save system overhead.
# Endif

# If OS _event_tbl_size> 1
* Ptbl ++ = 0x00;
# Endif

# If OS _event_tbl_size> 2
* Ptbl ++ = 0x00;
# Endif

# If OS _event_tbl_size> 3
* Ptbl ++ = 0x00;
# Endif

# If OS _event_tbl_size> 4
* Ptbl ++ = 0x00;
# Endif

# If OS _event_tbl_size> 5
* Ptbl ++ = 0x00;
# Endif

# If OS _event_tbl_size> 6
* Ptbl ++ = 0x00;
# Endif

# If OS _event_tbl_size> 7
* Ptbl ++ = 0x00;
# Endif
}

2. OS _eventtaskrdy ();

Int8u OS _eventtaskrdy (OS _event * pevent, void * MSG, int8u MSK)
{
OS _tcb * ptcb;
Int8u X;
Int8u y;

According to key algorithm 3, find the task with the highest priority in the task list of the waiting event and determine its priority.

Int8u bitx;
Int8u bity;
Int8u PRIO;

Y = osunmaptbl [pevent-> oseventgrp];
Bity = osmaptbl [y];
X = osunmaptbl [pevent-> oseventtbl [y];

Use algorithm 2 to delete the task with the highest priority from the waiting task list.

Bitx = osmaptbl [x];
Prio = (int8u) (Y <3) + x );
If (pevent-> oseventtbl [y] & = ~ Bitx) = 0x00)
{Pevent-> oseventgrp & = ~ Bity ;}

Ptcb = ostcbpriotbl [PRIO]; // you know the task priority and find the pointer to the task control block.
Ptcb-> ostcbdly = 0; // The task is cleared directly because it no longer waits for the event to occur.
Ptcb-> ostcbeventptr = (OS _event *) 0; // the pointer to the Event Control Block points to null because the event is no longer waiting.

# If (OS _q_en> 0) & (OS _max_qs> 0) | (OS _mbox_en> 0)
Ptcb-> ostcbmsg = MSG; // if this function is called by the mailbox or queue post function, you need to put the passed parameters in its task control block.

# Else
MSG = MSG;
# Endif

Ptcb-> ostcbstat & = ~ MSK;
If (ptcb-> ostcbstat = OS _stat_rdy)]
{
Osrdygrp | = bity; --- Insert the task with the highest priority to the ready task list, in the subsequent post Function
Osrdytbl [y] | = bitx; --- schedule the task by using the ossched () function. For details, refer to the readiness table in Chapter 3.
}
Return (PRIO );
}

3. OS _eventtaskwait ();

Void OS _eventtaskwait (OS _event * pevent)
{
Ostcbcur-> ostcbeventptr = pevent;
If (osrdytbl [ostcbcur-> ostcby] & = ~ Ostcbcur-> ostcbbitx) = 0x00)
{Osrdygrp & = ~ Ostcbcur-> ostcbbity;} // if no valid semaphore is received in the Pend function,
// This function will be called to enter the sleep state. Here, the task will be deleted from the task readiness list.

Pevent-> oseventtbl [ostcbcur-> ostcby] | = ostcbcur-> ostcbbitx;
Pevent-> oseventgrp | = ostcbcur-> ostcbbity; // place the task to the task list of the ECB waiting for the event.
}

4. OS _eventto ();
Void OS _eventto (OS _event * pevent)
{
If (pevent-> oseventtbl [ostcbcur-> ostcby] & = ~ Ostcbcur-> ostcbbitx) = 0x00)
{Pevent-> oseventgrp & = ~ Ostcbcur-> ostcbbity;} // Delete the expired task in the waiting for the task list
Ostcbcur-> ostcbstat = OS _stat_rdy; // set the task to ready.
Ostcbcur-> ostcbeventptr = (OS _event *) 0;
}

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.