Ucos2 Event task traffic and synchronization semaphore mutex message mailbox Message Queuing

Source: Internet
Author: User

This will think about it, after reviewing the material, finally do a core code analysis
Ucos uses semaphores, message mailboxes, message queues, which are used as intermediate media for communication. These data structures affect the program flow of tasks and are therefore called events.

First, the signal volume

is the most basic event for task communication

The binary signal can realize the exclusive of the shared resource, also called the mutex semaphore.

Note: When using semaphores, when a high priority task waits to receive a semaphore, a high-priority task waits for the signal to be received, and if the low-priority task does not release the semaphore, it waits until the low-priority task has no chance to run, and the system freezes. Workaround: Add a timeout wait.

when using semaphore synchronization: Whether a task can run depends on 2 conditions-----1 is the priority 2 is whether to obtain the semaphore. Equivalent to a concatenation relationship. This also raises the issue of a priority reversal.

Second, the message mailbox:

When multitasking communicates, the message mailbox is the data structure used to pass the message buffer pointer.

Third, Message Queuing

When multitasking communicates, Message Queuing is the data structure used to deliver multiple message mailboxes.

Iv. List of waiting tasks for events
For multiple tasks that are waiting, need to be logged and sorted by priority, and the maximum time to wait for tasks, avoid low-priority tasks that never run.

The next face waits for the task to be processed by the record and sort:
a int8u array of typeOseventtbl [ ] As the record table for the record waiting event task.
aint8uTypes of variables Oseventgrpto represent the group.
In fact , the same as the Ready-table principle ,This wait task table is still in order of the priority level of the task, so that each task in the system occupies a single bit in the table, and the bit is used to indicate that the corresponding task is the waiting task for the event, otherwise it is not waiting for the task.

The following is the processing of waiting time for a task:
The data is recorded in the member ostcbdly of the task control block TCB that waits for the task, and is maintained in each clock-beat interrupt service program. Whenever the waiting time limit for a task has arrived, the task is removed from the event waiting task table and put into a ready state.

V. Event control block
in order to unify the data structure of the event, the event control block ECB is set up to describe the events of the message mailbox, message queue, semaphore, and so on.



Oseventtype the desirable value

os_event_type_mutex  < Span style= "word-wrap:normal; Word-break:normal "> Indicates that the event is a mutex semaphore  

os_event_type_mbox  < Span style= "word-wrap:normal; Word-break:normal "> Indicates that the event is a message mailbox  

os_event_type_q   Indicates that the event is Message Queuing  

os_event_type_unused Empty Event control block (event control block not used)

Vi. Several operational functions of the event control block
eventwaitlistinit () event control block initialization function: Clear the data in the table is 0 called when the event is created
os_eventtaskwait()Request event does not succeed, so that a task enters the waiting state and is registered in the waiting list task and set it to    c14>                    > wait state, and finally in the ready table, set to non-ready state. Os_eventtaskrdy () causes a waiting task to enter the ready state, Os_eventto()To make a wait-time-out task enter a ready state
The event control block list is also empty and 2 different types of work, similar to the task control block, don't say much

Seven, the signal volume details
Important Composition: TypeCntSignal volume pointer is emptyControl block
1. Create a semaphore
Ossemcreat (CNT)------------setting type setting initialization count setting point to buffer null Initialize event control blockReturns the semaphore pointer
2. Request Signal Volume
Ossempend (Semaphore pointerTimeError message) The task waits when the semaphore is invalid
Ossemaccept (semaphore) signal is invalid, the task continues to run directly
3. Releasing the semaphore
Ossempost (semaphore pointer)
4. Delete the semaphore
Ossemdel (semaphore name, condition option, error message)
5. Query Semaphore status
Ossemquery ()

Eight, priority reversal and mutex
Priority reversal: AB wait state, C run, c lowest priority, in case C does not release the semaphore, a runs, that a waits, and C runs a task B before releasing the semaphore, and B has to run before a. It seems that B has a higher priority than a.
This occurs because a low-priority task is interrupted during use of the semaphore and cannot release the semaphore.
The workaround is to increase the priority of C when the semaphore is used (since the use of semaphores is higher than the priority of a), and the amount of the signal is released to restore the original priority.

Nine, mutual exclusion amount
The task can use mutexes to achieve exclusive access to resources.
Data type to be a mutex typeCNT is divided into high 8 bytes and low 8 bytes, low 8 bit 0xff means the signal is valid, and the high 8 bits record the temporary elevation priority.
1. Create Mutex
Osmutexcreat (elevation priority error message)
2. Request Mutex Amount
Osmutexpend (Mutex pointerTimeoutError message)
Osmutexaccept (Mutex pointerError message)Invalid mutex task will continue to run
3. Release the mutex amount
Osmutexpost (Mutex pointer)
4. Get the current state of the mutex
Osmutexquery (Mutex pointerstored data structures)
5. Delete Mutex amount
Osmutexdel (The mutex pointer is deleted directly or wait for the task to finish deletingError message)

Ten, the message mailbox
1.OSMboxCreat (msg)
Message mailbox: Set type to message mailbox type, set pointer to null, or point to buffer, set CNT to 0, initialize ECB
2.OSMboxPost (pointer to message mailbox, message buffer pointer)
Send Message Mailbox

Ucos2 Event task traffic and synchronization semaphore mutex message mailbox Message Queuing

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.