Why use mailbox?

User avatar
kreyl
Posts: 59
Joined: Sun Jan 13, 2013 11:46 pm
Been thanked: 4 times
Contact:

Why use mailbox?

Postby kreyl » Sun Feb 03, 2013 7:06 pm

First of all - I beg your pardon for such a topic, but I am searching knowledge, not scandal :)
I need to implement command queue in my application, nothing special. Several threads can put commands to queue, one dedicated thread dispatches them; if queue is overflowed - thread willing to put command should wait.
I analyzed code of ChibiOS-GFX (great work!), and I found that mailbox functionality used there - to send commands like DrawChar. All the putting and fetching is wrapped with semaphores and mutexes.
As far as I understand (please correct me if I am wrong), to use mailbox, one should implement an array for messages msg_t, and an array for commands themselves. To put command to queue, one should find empty slot in command array, get pointer and put it into mailbox. To fetch command, one should fetch pointer from mailbox.
What is the advantage of mailbox over simple circular buffer of commands? It seems to me that wrapping mailbox and it's methods requires more code (and memory), than implementing circular buffer from scratch. Most probably, I do not know something (maybe obvious). Would you comment please?

User avatar
Giovanni
Site Admin
Posts: 12584
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 647 times
Been thanked: 543 times
Contact:

Re: Why use mailbox?

Postby Giovanni » Sun Feb 03, 2013 7:47 pm

Following this line of reasoning mailboxes are never required, a circular queue can be implemented from scratch in any possible mailbox use case.

Moved in the right forum.

Giovanni

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: Why use mailbox?

Postby Tectu » Sun Feb 03, 2013 8:13 pm

As Giovanni already said, using the mailboxes / message queues from the RTOS is not neccessary. The RTOS does just provide them because they are often needed. You can also implement your own.

A message queue does not take a command and a real parameter, it can only take a defined message. The message, in the case of ChibiOS/RT is a type of msg_t which is itself defined as uint32_t (when I remember correctly). The trick used to pass a command + multiple arguments (for example to draw a line, you need at least the command to draw a line, the start- and end point as well as the color) is to use struct. The client thread does create such a struct (allocate it dynamically, for example), fill in the stuff and then simply pass the pointer to that struct into the message queue. The worker thread receiving the message will simply take a look what's inside the struct and will do what he's supposed to do.
An important thing to notice is that you don't change the content of the struct members while the thread didn't read or still is reading it. You must also make sure the worker thread can access the memory area (scope).

I hope this helps somehow.

@Giovanni: I don't think this is the right forum. The question is not really ChibiOS/GFX related.


~ Tectu


Return to “LCD Driver and Graphic Framework”

Who is online

Users browsing this forum: No registered users and 1 guest