Ucos Another mechanism of communication between tasks is message (mbox), the personal feeling is that it is a special case of only one message in the queue, it can be clearly seen from the code, because there are previous learning notes about the queue, so talk about mbox. Why is there a queue mechanism to use mbox, as long as the set of the queue of MSG only one is not OK? In fact very simple, is to save resources, because the use of queue words need to specifically describe the body of the queue os_q, but also need to allocate a memory to store MSG, and if directly using the mbox mechanism, it is much better, save .....
First, starting with the creation of mbox, the function that mbox creates is osmboxcreate, simplifying the code as follows:
Os_event *osmboxcreate (void*pmsg) {os_event*pevent; if(Osintnesting >0) {/*See if called from ISR ...*/ return((Os_event *)0);/*... can ' t ' CREATE from an ISR*/} os_enter_critical (); Pevent= Oseventfreelist;/*Get Next free event control block*/ if(Oseventfreelist! = (Os_event *)0) {/*See if pool of free ECB pool is empty*/oseventfreelist= (Os_event *) oseventfreelist->oseventptr; } os_exit_critical (); if(Pevent! = (Os_event *)0) {pevent->oseventtype =Os_event_type_mbox; Pevent->oseventcnt =0; Pevent->oseventptr = pmsg;/*Deposit Message in event control block*/Os_eventwaitlistinit (pevent); } return(pevent);/*Return Pointer to event control block*/}
MBOX uses the same event mechanism, which differs from the SEM in that the SEM uses the OSEVENTCNT variable in the event to hold the semaphore, while Mbox uses OSEVENTPTR to store the MSG address at the time of creation, which can be compared to the create code of the SEM. The create code for both is so similar.
void*osmboxpend (os_event *pevent, int16u timeout, int8u *Perr) { void*pmsg; if(Pevent->oseventtype! = Os_event_type_mbox) {/*Validate Event block type*/*perr =Os_err_event_type; return((void*)0); } if(Osintnesting >0) {/*See if called from ISR ...*/*perr = OS_ERR_PEND_ISR;/*... can ' t PEND from an ISR*/ return((void*)0); } if(Oslocknesting >0) {/*See if called with Scheduler locked ...*/*perr = os_err_pend_locked;/*... can ' t PEND when locked*/ return((void*)0); } os_enter_critical (); pmsg = pevent->oseventptr; if(PMsg! = (void*)0) {/*See If there is already a message*/pevent->oseventptr = (void*)0;/*Clear the Mailbox*/os_exit_critical (); *perr =Os_err_none; return(pmsg);/*Return the message received (or NULL)*/ } ostcbcur->ostcbstat |= Os_stat_mbox;/*Message not available, task would pend*/Ostcbcur->ostcbstatpend =OS_STAT_PEND_OK; Ostcbcur->ostcbdly = timeout;/*Load Timeout in TCB*/os_eventtaskwait (pevent); /*Suspend task until event or timeout occurs*/os_exit_critical (); Os_sched (); /*Find Next highest priority task ready to run*/os_enter_critical (); Switch(Ostcbcur->ostcbstatpend) {/*See if we timed-out or aborted*/ Caseos_stat_pend_ok:pmsg= ostcbcur->ostcbmsg; *perr =Os_err_none; Break; Caseos_stat_pend_abort:pmsg= (void*)0; *perr = Os_err_pend_abort;/*indicate that we aborted*/ Break; Caseos_stat_pend_to:default: Os_eventtaskremove (Ostcbcur, pevent); PMsg= (void*)0; *perr = Os_err_timeout;/*indicate that we didn ' t get the event within to*/ Break; } ostcbcur->ostcbstat = Os_stat_rdy;/*Set task status to Ready*/Ostcbcur->ostcbstatpend = OS_STAT_PEND_OK;/*Clear pend Status*/Ostcbcur->ostcbeventptr = (Os_event *)0;/*Clear Event Pointers*/Ostcbcur->ostcbmsg = (void*)0;/*Clear received message*/os_exit_critical (); return(pmsg);/*Return received message*/}
Because better than the other mechanism code comparison, so the entire code is pasted out, in fact, the main difference in the yellow part, because when the MSG is created, or at the time of the post MSG, will be put msg to oseventptr, so directly from the MSG, to determine whether the current MSG exists, If present, the MSG will be returned and the MsgBox is oseventptr clear 0 operation; If MSG is not directly suspend the current task in the waiting list of the event, task scheduling is the yellow section below the processing process, do not do too much to repeat.
int8u Osmboxpost (Os_event *pevent,void*pmsg) { if(Pevent->oseventtype! = Os_event_type_mbox) {/*Validate Event block type*/ return(Os_err_event_type); } os_enter_critical ();
(1) =================================================================================================== = if(Pevent->oseventgrp! =0) {/*See if the task pending on mailbox*/ /*Ready HPT Waiting on event*/ (void) Os_eventtaskrdy (pevent, pmsg, Os_stat_mbox, OS_STAT_PEND_OK); Os_exit_critical (); Os_sched (); /*Find Highest priority task ready to run*/ return(Os_err_none); }
(2) =================================================================================================== = if(Pevent->oseventptr! = (void*)0) {/*Make sure mailbox doesn ' t already has a msg*/os_exit_critical (); return(Os_err_mbox_full); } pevent->oseventptr = pmsg;/*Place message in mailbox*/os_exit_critical (); return(Os_err_none);
(3) =================================================================================================== =}
Mbox post function code as shown above, in fact, the processing process and SEM and so on, post is actually the function of filling, first of all to determine whether there is a task waiting for the current event, if there is a direct msg etc to wait for the task, if not then into the third part, Determine if there is a msg in the current oseventptr, if there is no task to remove the MSG, this will return an error, if there is no MSG in oseventptr, then put MSG in Oseventptr, waiting for the task to take out.
Through learning Sem,mutex,queue and mbox kernel implementation can generally understand how the communication mechanism between operating system tasks is implemented, but also with other operating systems do not, through a simple kernel implementation to understand the complex operating system of the implementation of the Mechanism, such as the Linux operating system, this is a good way to learn. Here is a brief summary of the functions of the four communication mechanisms:
1. Signal Volume: The signal is a kind of digital size to achieve the restriction of the use of resources a mechanism, set the signal volume is actually set the maximum number of resources can be allowed to access the same resource, through the semaphore pend and post operations that is the semaphore variable addition and subtraction to achieve task control, when special circumstances, There is a bit of mutual exclusion when there is only one semaphore.
2. Mutex: The mutex is only one task can occupy resources at the same time, when there are other tasks to access the resources will be suspended, put on the waiting list of the event, when the task of the resources to release the lock, wait for the task can occupy resources and locked, in order to prevent priority rollover, The mechanism by which priority inheritance is used is to elevate the priority of the task that occupies the resource to a higher priority than the task to use the resource.
3. Queue: Queue is to take a piece of memory to hold the message, this message is an address, the real message content is stored in this address, so that can achieve real inter-mission communication, the data from one task to another task, rather than the semaphore and mutex lock is only a limiting role. Queue usage be aware that if multiple tasks are waiting for different messages, there may be different tasks that get the information that is not what you want and remove the message from the queue, so use it with care.
4. Message: The message is actually a special case of the queue, in order to save resources, also mentioned before, if the number of messages must be at the same time only one message to use, then the use of the message mechanism is simpler, the same implementation of the data transmission function. The use of the message is also important to note, Pend and post use, because if there are multiple tasks at the same time, there will be the current task is the desired information, if not it is possible to the other task of the message to the place and released, so use need to pay attention.
Ucos Real-time Operating system learning notes-inter-mission communication (message)