about MailBox In ucos

Here is uCOS about Mailbox post code:

    OS_ENTER_CRITICAL();
    if (pevent->OSEventGrp != 0x00) {                 /* See if any task pending on mailbox            */
        OS_EventTaskRdy(pevent, msg, OS_STAT_MBOX);   /* Ready highest priority task waiting on event  */
        OS_EXIT_CRITICAL();
        OS_Sched();                                   /* Find highest priority task ready to run       */
        return (OS_NO_ERR);
    }
    if (pevent->OSEventPtr != (void *)0) {            /* Make sure mailbox doesn''''t already have a msg  */
        OS_EXIT_CRITICAL();
        return (OS_MBOX_FULL);
    }
    pevent->OSEventPtr = msg;                         /* Place message in mailbox                      */
    OS_EXIT_CRITICAL();
    return (OS_NO_ERR);

 

 I want to say, what Mailbox post is an pointor .

Internal a function ,private variables will be free at the end of the function.If we send a private variables by OSMBoxPost,the target may get a wrong data.

Here is a test code:

///Test1.c
void task1(void *pd)

{


char buf[16];
char cnt;
pd=pd;

OSMboxCreate();
while(1)

{

sprintf(buf,"Send:%2d/r/n",cnt++);
OSMboxPost(TestMbox,buf);
OSTimeDly(10);

}

}


void task2(void *pd)
{
char *p;
pd =pd;
for(;;)
{

p=OSMboxPend(TestMbox,0,err);
printf("%s",p); //here we say ,the addr of p is the still under  used

}

}
//end of Test1.c

Another example:

///Test2.c
void function(void)
{
char buf[16];
char cnt;
sprintf(buf,"Send:%2d/r/n",cnt++);
OSMboxPost(TestMbox,buf);
}

void task1(void *pd)
{

pd=pd;

OSMboxCreate();
while(1)

{

function();

OSTimeDly(10);

}

}

void task2(void *pd)
{
char *p;
pd =pd;
for(;;)
{

p=OSMboxPend(TestMbox,0,err);
printf("%s",p); //here we say ,the addr of p is the still under  used

}

}
//end of Test2.c

Both of the codes were not been tested.

It must be casefull when we use an API.

投 票

觉得本文不错,投一票   

评 论


验证码: 看不清?换一张