Ucos Event Mailbox Semaphore queue detailed

Source: Internet
Author: User

Ucos events are divided into clocks, semaphores, mutex semaphores, message queues, and message mailboxes

First, the signal volume.

The type of semaphore in Ucos is defined as Os_event_type_sem, in the task control block ECB, the semaphore counter oseventcnt is used mainly, when there is a task request semaphore, if the semaphore oseventcnt value is greater than 0, The OSEventCnt-1 is the task to continue to run, if the oseventcnt is already 0, then the task will be suspended to the task and other representatives, when other tasks send semaphore, the suspended task to get the semaphore, and set to ready for the next schedule, If there are no waiting tasks in the task waiting table, then the semaphore counter oseventcnt is simply +1

Semaphore is a OSEVENTPTR member that does not use the event control block

Semaphores must be created before the semaphore is used and stored inside the program by variables

Ossemcreate, the parameter is the initial value of the semaphore, and the resulting semaphore returns the ECB pointer

Request Signal Volume

Ossempend, parameter is semaphore pointer, timeout event, and error code pointer, when timeout time is 0, the wait time is infinitely long

When a task requests a semaphore, you can use the function ossemaccept if you want to allow a task to continue without a semaphore if it does not enter the waiting state

Send semaphore using Ossempost, parameter is the event struct body pointer

Delete semaphore Ossemdel, parameter is semaphore ECB pointer, delete condition and error information pointer, where delete condition is selected regardless of whether the task waiting table is empty, delete and wait for empty delete

Query semaphore status Ossemquery, return error message, inside each parameter is Os_sem_data function, for storing the quantity of the semaphore of the current query

The difference between the mutual-exclusion semaphore and the semaphore is that the mutex is only 0 and 12 choices, and the semaphore can be a lot of choices (similar to what only two people use and what many people can use)

The problem of priority reversal is caused by the use of mutex semaphores, that is, low-priority tasks occupy the semaphore, causing high-priority tasks to wait for semaphores, instead of stealing low-priority tasks

Ucos in order to solve this problem, the strategy is to increase the priority of the task to the highest priority of all tasks when the task receives the semaphore, so as to run out of shared resources and release the signal as soon as possible, and restore the original priority level after releasing the signal.

The mutex semaphore is Os_event_type_mutex on the event type, and he divides the oseventcnt into high eight-bit and low eight-bit, low eight-bit storage signal value, and high eight-bit storage in order to avoid priority inversion to elevate the task to moral high-priority prio

Create a mutex semaphore osmutexcreate, parameter one is the priority level one is the error message, where the priority level identifies the high-priority semaphore to be promoted, and the low eight-bit is typically 0xff

Request the mutex semaphore, the osmutexpend, the pointer to the mutex semaphore, the wait time, and the error message pointer,

if ((int8u) (pevent->oseventcnt & os_mutex_keep_lower_8) = = os_mutex_available) {

pevent->oseventcnt &= Os_mutex_keep_upper_8;            pevent->oseventcnt |= ostcbcur->ostcbprio; Pevent->oseventptr = (void *) ostcbcur;

if (Ostcbcur->ostcbprio <= pip) {os_exit_critical ();

*perr = Os_err_pip_lower;

} else {

Os_exit_critical ();

*perr = Os_err_none;

}

Return

}

First detects the low eight bits of the current event, enters the If selection for 0xFF, sets the low eight bit to the priority of the system's current task (that is, the time request task), the event pointer pointer to the current task TCB, and then detects the priority of the task and the PIP's alignment. Setup error, larger than Pip, application successful, The source of the PIP is the high eight bits of the current event ECB counter. This is true when creating a mutex semaphore

pevent->oseventcnt = (int16u) ((int16u) prio << 8u) | os_mutex_available;

That is, the increase priority of the created mutex semaphore, the current task priority is higher than the elevation priority (the value is a little lower), you can directly get the semaphore.

If it is below, the priority of a running task is promoted later, the privilege of the task that currently occupies the semaphore is elevated, and the switch to the ready table is involved.

Send the mutex signal volume

Osmutexpost, parameter is semaphore pointer

Gets the state of the mutex semaphore

Osmutexquery, one of the parameters is the structure used to preserve the state of the mutex semaphore Os_mutex_data

Remove Mutex semaphore

Osmutexdel

About the privilege elevation issue in the mutex semaphore task process after writing a separate article discussion

Message mailbox, used to adapt to different data transmission requirements, the essence is to talk about the data buffer associated to the time control block ECB oseventprt, while the time control block Oseventtype is a constant os_event_type_mbox, the specific operation is as follows

Osmboxcreate, creating a message Mailbox

Osmboxpend Request message mailbox, return message mailbox pointer, note to see if Peerr pointer returned successfully

Osmboxpost Sending a message mailbox

Osmboxpostopt waiting for all tasks in the task table to send messages in broadcast form

Osmboxquery Querying mailbox Status

Osmboxdel Delete a mailbox

Finally, there is a message queue

Ucos Event Mailbox Semaphore queue detailed

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.