SV Interprocess Synchronization and Communication PDF
SV Interprocess Synchronization and Communication PDF
and communication
Introduction:
The basic synchronization mechanism is the
named event type, along with the event trigger
and event control constructs (i.e., -> and @).
This type of control is limited to static objects.
System Verilog also provides a powerful and easy-
to-use set of synchronization and communication
mechanisms that can be created and reclaimed
dynamically.
Semaphores
Mailboxes
Introduction:
Semaphore:
A semaphore built-in class, which can be used for
Initial begin
#3 sema.put();
end
Initial #5 $finish;
endmodule
Example2:
module semaphore_ex;
semaphore sema; //declaring semaphore sema
initial begin
sema=new(4); //creating sema with '4' keys
fork
display(); //process-1
display(); //process-2
display(); //process-3
join
end
task automatic display();
sema.get(2); //getting '2' keys from sema
$display($time,"\tCurent Simulation Time");
#30;
sema.put(2); //putting '2' keys to sema
endtask
endmodule
Example3:
module semaphore_ex;
semaphore sema; //declaring semaphore sema
initial begin
sema=new(1); //creating sema with '4' keys
sema.get(1);
$display(“GOT KEY at : %0t ”, $realtime);
#1;
if(sema.try_get(1))
$display(“GOT KEY at : %0t ”, $realtime);
else
$display(“Didn’t GET KEY at : %0t ”, $realtime);
end
Initial #5 $finish;
endmodule
Mailboxes:
A mailbox is a communication mechanism that
allows messages to be exchanged between
processes.
Data can be sent to a mailbox by one process and
retrieved by another.
Mailboxes are created as having either a bounded
or unbounded queue size.
A process that attempts to place a message into a
full mailbox shall be suspended until enough room
becomes available in the mailbox queue.
Unbounded mailboxes never suspend a thread in a
send operation.
Mailbox Methods:
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()
Mailbox Methods:
New()
Mailboxes are created with the new() method.
Prototype::
function new(int bound = 0);
If the bound argument is 0, then the mailbox is
unbounded (the default) and a put() operation shall
never block.
If bound is nonzero, it represents the size of the
mailbox queue.
NOTE: Negative bounds are illegal and can result in indeterminate
behavior, but implementations can issue a warning.
Num():
The number of messages in a mailbox can be
obtained via the num() method.
Prototype:
function int 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.
Put():
The put() method places a message in a mailbox.
prototype:
task put( singular message);
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 room in the queue.
Try_put():
The try_put() method attempts to place a
message in a mailbox.
prototype:
function int try_put( singular message);
This method is meaningful only for bounded
mailboxes.
If the mailbox is not full, then the specified
message is placed in the mailbox, and the
function returns a positive integer.
If the mailbox is full, the method returns 0.
Get():
The get() method retrieves a message from a
mailbox.
prototype:
task get( ref singular message );
The get() method retrieves one message from the
mailbox, that is, removes one message from the
mailbox queue.
If the mailbox is empty, then the current process
blocks until a message is placed in the mailbox.
Try_get():
The try_get() method attempts to retrieves a
message from a mailbox without blocking.
prototype:
function int try_get( ref singular message );
The try_get() method tries to retrieve one message
from the mailbox.
If the mailbox is empty, then the method returns 0.
If a message is available and type is equivalent, the
message is retrieved, and method returns a positive
integer.
If the type of the message is not equivalent, the
method returns negative integer.
Peek():
The peek() method copies a message from a
mailbox without removing the message from the
queue.
prototype:
task peek( ref singular message );
The peek() method copies one message from the
mailbox without removing the message from the
mailbox queue.
If the mailbox is empty, then the current process
blocks until a message is placed in the mailbox.
Try_peek():
The try_peek() method attempts to copy a message
from a mailbox without blocking.
prototype:
function int try_peek( ref singular message );
The try_peek() method tries to copy one message
from the mailbox without removing the message
from the mailbox queue.
If the mailbox is empty, then the method returns 0.
Parameterized mailbox:
The default mailbox is typeless, that is, a single
mailbox can send and receive any type of data.
prototype:
mailbox #(type = dynamic_type)
where dynamic_type represents a special type that
enables run-time type checking (the default).
The only difference between a generic (dynamic)
mailbox and a parameterized mailbox is that for a
parameterized mailbox, the compiler verifies that the
calls to put, try_put, peek, try_peek, get, and try_get
methods use argument types equivalent to the mailbox
type so that all type mismatches are caught by the
compiler and not at run time.
Example1:
module mailbox_ex;
Mailbox mb;
Int int_var;
Initial
Begin
mb = new(); // unbounded mailbox
$display(“Number of elements in mailbox: %0d”,mb.num);
mb.put(111);
$display(“Number of elements in mailbox: %0d”,mb.num);
mb.get(int_var);
$display(“Number of elements in mailbox: %0d”,mb.num);
endmodule
Example2:
module mailbox_ex;
Mailbox mb; Int int_var;
Initial begin
mb = new(2); // bounded mailbox of size 2
mb.put(111); mb.put(222);
$display(“Number of elements in mailbox: %0d”,mb.num);
if(mb.try_put(333))
$display(“Entering into if statement”);
else $display(“Entering into else statement”);
mb.get(int_var);
mb.get(int_var);
if(mb.try_get(int_var))
$display(“GET: Entering into if statement”);
else
$display(“GET: Entering into else statement”);
endmodule
module mailbox_ex;
Example 3
Mailbox mb; Int int_var;
Initial begin
mb = new(2); // bounded mailbox of size 2
mb.put(111);
mb.put(222);
$display(“Number of elements in mailbox: %0d”,mb.num);
mb.peek(int_var);
$display(“int_var value is: %0d”,int_var);
mb.peek(int_var);
$display(“int_var value is: %0d”,int_var);
$display(“Number of elements in mailbox: %0d”,mb.num);
mb.get(int_var);
mb.get(int_var);
if(mb.try_peek(int_var))
$display(“GET: Entering into if statement”);
else
$display(“GET: Entering into else statement”);
endmodule