You are on page 1of 22

Mailboxes

&
Semaphores
Mailboxes
A Mailbox is a communication mechanism that allows messages to be exchanged between processes.

The process which wants to talk to another process posts the message to a mailbox (which stores the messages
temporarily in a system defined memory object) to pass it to the desired process.

Based on the sizes mailboxes are categorized as,

• Bounded mailbox

• Unbounded mailbox
Built-in Functions of Mailboxes
Mailbox is a built-in class that provides the following methods:

--Create a mailbox: new()

--Place a message in a mailbox: put()

--Try to place a message in a mailbox without blocking: try_put()

--Retrieve a message from a mailbox: get() or peek()

--Try to retrieve a message from a mailbox without blocking: try_get() or try_peek()

--Retrieve the number of messages in the mailbox: num()


Built-in Functions of Mailboxes
Mailbox is a built-in class that provides the following methods:

--Create a mailbox: new()


Mailboxes are created with the new() method
The new() function returns the mailbox handle or, if the mailbox cannot be created, null.
If the bound argument is 0, then the mailbox is unbounded (the default) and
a put() operation shall never block

--Place a message in a mailbox: put()

--Try to place a message in a mailbox without blocking: try_put()

--Retrieve a message from a mailbox: get() or peek()

--Try to retrieve a message from a mailbox without blocking: try_get() or try_peek()

--Retrieve the number of messages in the mailbox: num()


Built-in Functions of Mailboxes
Mailbox is a built-in class that provides the following methods:

--Create a mailbox: new()

--Place a message in a mailbox: put()


The put() method places a message in a mailbox.
The message is any singular expression, including object handles.
The put() method stores a message in the mailbox in strict FIFO order.
If the mailbox was created with a bounded queue, the process shall be suspended
until there is enough space in the queue.

--Try to place a message in a mailbox without blocking: try_put()

--Retrieve a message from a mailbox: get() or peek()

--Try to retrieve a message from a mailbox without blocking: try_get() or try_peek()

--Retrieve the number of messages in the mailbox: num()


Built-in Functions of Mailboxes
Mailbox is a built-in class that provides the following methods:

--Create a mailbox: new()

--Place a message in a mailbox: put()

--Try to place a message in a mailbox without blocking: try_put()


This is like put() but it doesn’t suspend the process, even if the mailbox is full.
Simply it returns ‘0’ when the mailbox is full

--Retrieve a message from a mailbox: get() or peek()

--Try to retrieve a message from a mailbox without blocking: try_get() or try_peek()

--Retrieve the number of messages in the mailbox: num()


Built-in Functions of Mailboxes
Mailbox is a built-in class that provides the following methods:

--Create a mailbox: new()

--Place a message in a mailbox: put()

--Try to place a message in a mailbox without blocking: try_put()

--Retrieve a message from a mailbox: get() or peek()


The get() method retrieves a message from a mailbox
The get() method removes one message from queue
If the mailbox is empty, then the current process blocks until a message is placed in the mailbox.
If the type of the message variable is not equivalent to the type of the message in the mailbox,
a run-time error is generated
The peek() method copies a message from a mailbox without removing the message from the queue,
except it everything is same as get()

--Try to retrieve a message from a mailbox without blocking: try_get() or try_peek()

--Retrieve the number of messages in the mailbox: num()


Built-in Functions of Mailboxes
Mailbox is a built-in class that provides the following methods:

--Create a mailbox: new()

--Place a message in a mailbox: put()

--Try to place a message in a mailbox without blocking: try_put()

--Retrieve a message from a mailbox: get() or peek()

--Try to retrieve a message from a mailbox without blocking: try_get() or try_peek()


This is like get() but it doesn’t suspend the process, even if the mailbox is empty.
Simply it returns ‘0’ when the mailbox is empty.

--Retrieve the number of messages in the mailbox: num()


Built-in Functions of Mailboxes
Mailbox is a built-in class that provides the following methods:

--Create a mailbox: new()

--Place a message in a mailbox: put()

--Try to place a message in a mailbox without blocking: try_put()

--Retrieve a message from a mailbox: get() or peek()

--Try to retrieve a message from a mailbox without blocking: try_get() or try_peek()

--Retrieve the number of messages in the mailbox: num()


The num() method returns the number of messages currently in the mailbox.
The returned value should be used with care,
because it is valid only until the next get() or put() is executed on the mailbox.
Simple Mailbox example https://edaplayground.com/x/6M_w
Simple Mailbox example https://edaplayground.com/x/6M_w
Simple Mailbox example https://edaplayground.com/x/6M_w
Mailbox example using Tasks
https://edaplayground.com/x/eEm
Mailbox example using Tasks
https://edaplayground.com/x/eEm
Mailbox example using Tasks
https://edaplayground.com/x/eEm
Mailbox example using Tasks
https://edaplayground.com/x/eEm
Semaphores
Semaphore is a SystemVerilog built-in class, used for access control to shared resources, & for basic synchronization.

A semaphore is like a bucket with the number of keys. processes using semaphores must first procure a key
from the bucket before they can continue to execute, All other processes must wait until enough keys are returned
to the bucket.

Example: Three brothers in a house with a single bike.

Semaphore is a built-in class that provides the following methods:

new(); Create a semaphore with a specified number of keys as argument (default: 1 key)

get();   Obtain one or more keys from the bucket

put();   Return one or more keys into the bucket

try_get(); Try to obtain one or more keys without blocking


1.Example -Semaphores
https://edaplayground.com/x/5prd
1.Example -Semaphores
https://edaplayground.com/x/5prd
1.Example -Semaphoreshttps://edaplayground.com/x/5prd
2.Example – Semaphores with Tasks
https://edaplayground.com/x/26mv
2.Example – Semaphores with Tasks
https://edaplayground.com/x/26mv

You might also like