This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How can I safely pass pointers to a mailbox?

Other Parts Discussed in Thread: SYSBIOS

Hi!

Let's say I have a few producer tasks and one consumer task that communicate through a mailbox. In most cases I pass small messages so everything is easy.

But in a few cases I also need to pass a larger buffer. The buffer will be filled appropriately by each producer task and then consumed by the consumer task. I think that it is most efficient to just pass a pointer to a single large global buffer to my mailbox (i.e. the buffer is passed by reference to the consumer task).

Now I wonder what is the best way to protect the buffer so that it can be safely shared between the producer tasks and the consumer task. Should I use semaphores? If so where and how many? And do I have to limit the mailbox size to one message to prevent corruption of the buffer?

Thanks for any hints,

Anguel

  • Hi Anguel,

    Since you would like to use a global buffer, I would suggest that you don't use a mailbox, and instead use semaphores to control access to the buffer in each of your tasks. Why were you hoping to use a mailbox for this? I'm not currently seeing any benefit to using it when it is only a pointer being passed.

    Whitney

  • Hi Whitney!

    The producer and consumer tasks are part of a communication system. The consumer is a transmission task. I am using a mailbox because I am passing smaller messages most of the time. Only in certain cases I want to pass longer strings through that global buffer. So I do not want to rewrite the tasks, just to protect the buffer properly. I have found many people using mailboxes to pass buffer pointers. Just could not find a good example how to properly protect the buffer when it is passed as a pointer through the mailbox.

    Anguel

  • Hi Anguel,

    One idea I have is that you could use the Mailbox_pend and Mailbox_post to protect the buffer.  By passing the pointer to the buffer through the mailbox, you could make your code so that only the task who has received the pointer to the global buffer via the mailbox will be allowed to access it.

    I'm not sure how your code is, but it may work to surround any reads/writes to the global buffer by the post/pend calls.

    For example, something like the following pseudo code:

    Void mytask(Void)
    {

        while (<not done>) {

            /* wait for access to the global buffer pointer */
            Mailbox_pend(<msg>)

            if (<msg passed was pointer to the global buffer>) {
        
                <access global buffer>
     
                /* post pointer to global buffer to signal that we're done */
                Mailbox_post(<message containing global buffer pointer>);
            }
        }
    }

    Steve

  • Steven,

    Thank you very much for the idea. Actually, yesterday I had a similar idea: using the mailbox to enter the "critical" section and then signal back with a semaphore that the consumer is done.

    On the other hand I thought more about Whitney's idea to use only semaphores to protect the global buffer and some additional info I pass. This will be probably much more efficient than using a mailbox. Is this correct?

    Anguel

  • Hi Anguel,

    Yes, that's correct. It would be more efficient because you would avoid copying the buffer pointer through the mailbox, and I would think that protecting the buffer would be easier.

    Steve and I discussed this a little, and you may want to consider just using one or the other instead of trying to use mailbox in certain cases and semaphores in others. All of these solutions should work, and you know the details of your application better than we do, but a semaphore solutions sounds like it could be more efficient.

    Whitney

  • Thank you both once again.

    Is there any approximate information on how long a mailbox post/pend (assuming a small message of 1 byte) vs semaphore post/pend takes in SYSBIOS, e.g. in cycles?

    Anguel

  • Hi Anguel,

    If you look at the release notes for your version of SYS/BIOS, you should be able to find a benchmarks section that will give you some info about time in cycles for semaphore use. Mailbox isn't on the list, but mailbox actually calls semaphore pend and post in its own pend/post functions.

    Whitney