Regarding mailbox class

Hi
In Mailbox Class get(),put(),methods are of tasks
whereas try_get(),try_put(),methods are of functions why???
need an explanation
thank you

The get(), put(), and peek() are blocking methods. This means that they will block the execution of underlying code till the method is successful.

The put() method blocks until the space is available in mailbox and it has ‘put’ the transaction in the mailbox FIFO. Similarly, get() method only unblocks when mailbox has some data and it has ‘get’ that transaction.

While, try_put() and try_get() are non-blocking methods. try_put() checks whether there is space available in the mailbox FIFO. If space is available, then it ‘puts’ the transaction in FIFO and returns a positive integer. Similarly, try_get() checks whether there is space available in the mailbox FIFO. If space is available, then it ‘gets’ the transaction from FIFO and returns a positive integer.

By use of try_* methods, one component can synchronize with some other component and meanwhile do some other activity. We can use an ‘if’ condition to check whether transaction was successfully kept/fetched from the mailbox.

// Producer/Transmitter
repeat(n) begin
if(mbx.try_put(tr)) begin
// Do some operations to make new transaction
end else
// Receiver is busy. Do some other operation and wait for receiver to fetch current transaction
end
end

// Consumer/Receiver
repeat(n) begin
if(mbx.try_get(tr)) begin
// Do some operations on the new transaction
end else
// Transmitter is busy. Do some other operation and wait for transmitter to put some transaction
end
end

In a nutshell, every method with try_, can_ are non-blocking and hence they are functions.

In reply to sharvil111:

thanks shravii
i just need clarification on for all non blocking methods we need to use functions
and for all blocking methods we need to use tasks…?
explain me
thank you

In reply to sudharshan:

Since it is a “nonblocking” method, it should not block any component execution. So all the nonblocking methods are functions (since function returns in zero simulation time and doesn’t block the execution).

We can call the nonblocking methods from task and functions both.

In reply to sudharshan:

In SystemVerilog, functions have two provisions that tasks do not.

  1. A function is guaranteed not to block, therefore it must consume 0 time.
  2. A function may be defined to return value, and of so, may be used where an expression is required, or as part of a larger expression (like in the condition of an if statement).

A task never returns a value which means it never is part of an expression; it must be a stand-alone statement. A task has no guarantees whether it blocks or not. But any statement that has the potential to block is called a blocking statement. A function may not contain any blocking statements. (There is an exception to this last sentence, but not for beginners :))

In reply to dave_59:

thanks dave