Queue and mailbox

Hi
1.I want to know what is the main difference between queues and mailbox,what are the advantages of them.
2.What is the advantage of mailbox over FIFO.

Thanks and regards,
Imran

A mailbox is a built-in class around a queue that uses semaphores to control access to the ends of a queue. A mailbox only has FIFO element ordering whereas you can access the head, tail, or middle elements of a queue.

You typically use a mailbox when there are multiple threads reading and writing data and you need the atomic test-and-set operation of semaphore to know when the mailbox is full or empty.

In the UVM, we use a TLM FIFO which is another wrapper around a mailbox. See How TLM Works | Advanced UVM | UVM/OVM Verification Methodology | Verification Academy

In reply to dave_59:

Thanks dave.

Hi,

I am not getting the advantage of mailbox over queues.
As what I got is that in both we can put and get the object handles or values. In queue we can get any value from the position we want. while in mailbox in build methods are there put,get,try_put, try_get etc. so how is mailbox is more efficient than queues?

Thanks

In reply to myselfprakhar:

You can say Mailbox has multiple insertion points

In reply to Subhash:

A queue is just a data structure, and a mailbox is an higher level concept that is built around a combination of queues and semaphores. If you have only one process reading and writing to the data structure, there is no need to use a mailbox. However if there are more than one thread, a mailbox is a convenient class to use. But if you have multiple threads, and need access to any position in the queue, you will need to write your own class to do that. The mailbox concept is in many other programming languages, so you should be able to search for more information about it.

In reply to dave_59:

Thanks

In reply to myselfprakhar:

Mailbox is similar to a queue, which allows only atomic operations. They can be bounded/unbounded. Bounded means it can be configured to fix size. Mailbox size can be configured to fix or unbounded while queue size is unbounded. Get/put task is used to suspend a bounded mailbox. That’s why mailbox is used more for communication between threads. FIFO can be implemented using mailbox. While FIFO or LIFO can be implemented using queue.

In reply to dave_59:

Hi Dave,

Apologies for using this thread to ask a unrelated question, you mentioned about TLM Fifo in UVM as a wrapper of a SV mailbox, I was wondering depending on the application if this can produce a big performance penalty, let’s say I’m building a reference model for a ‘complex router’ (I know early optimisations are the cause of big problems most of times), I also do understand the intent of using UVM (reusability, consistency, etc) but where to draw the line of using the methodology or not?

Cheers,

-R

In reply to rgarcia07:

The design of the UVM is based on the old-school premise that the DUT overwhelms the testbench simulation. IMHO, not enough effort is put into analyzing and optimizing the performance of the UVM Base Class Library.

If you think your testbench going to have a performance penalty, then most likely it will. But it’s now always in the place you expect it to be. You’ll need to do some careful experiments and profiling to find out.

In reply to dave_59:

In reply to rgarcia07:
The design of the UVM is based on the old-school premise that the DUT overwhelms the testbench simulation. IMHO, not enough effort is put into analyzing and optimizing the performance of the UVM Base Class Library.
If you think your testbench going to have a performance penalty, then most likely it will. But it’s now always in the place you expect it to be. You’ll need to do some careful experiments and profiling to find out.

Thank you very much for your inputs Dave :-)

In reply to dave_59:

A mailbox is a built-in class around a queue that uses semaphores to control access to the ends of a queue. A mailbox only has FIFO element ordering whereas you can access the head, tail, or middle elements of a queue.
You typically use a mailbox when there are multiple threads reading and writing data and you need the atomic test-and-set operation of semaphore to know when the mailbox is full or empty.
In the UVM, we use a TLM FIFO which is another wrapper around a mailbox. See How TLM Works | Advanced UVM

Hi Dave,

Recently I faced a racing issue with calling a callback multiple times, which use a shared variable. I remember this topic and see that the mailbox uses a semaphore to control the accessing to it shared queue among threads. So my issue could be the reason why a semaphore is used in the mailbox. Nonetheless, from this answer, and I remember you have already answered somewhere else that SystemVerilog is a single threaded program. Something similar to this. So from my understanding, the race issue for a shared variable cannot happen with functions.

Could you please help me to dig deeper to this concern? I have no idea if we can met race conditions with shared variable among functions? If yes, then how? If no, then why do a semaphore is required for the mailbox since all the queue’s accessing methods are functions.

For the callback code, it belongs to a VIP Vendor and is protected, so I cannot share it here.

Thanks you,
-An

In reply to AnPham:

As an HDL, Verilog was designed as if code could be run in parallel, even though it was implemented in a single threaded program. This allows much more flexibility in optimizations of the code, as well as eventual parallel implementation.

But a semaphore also provides FIFO thread access to shared variables. It also has queues to make sure each thread waiting for access get its access in order.

In reply to dave_59:

In reply to AnPham:
As an HDL, Verilog was designed as if code could be run in parallel, even though it was implemented in a single threaded program. This allows much more flexibility in optimizations of the code, as well as eventual parallel implementation.
But a semaphore also provides FIFO thread access to shared variables. It also has queues to make sure each thread waiting for access get its access in order.

Hi Dave, thank you for you reply. It still confuses me, but I will do further researches later.
-An