0% found this document useful (0 votes)
97 views

SV Interprocess Synchronization and Communication PDF

This document discusses interprocess synchronization and communication in SystemVerilog using semaphores and mailboxes. It provides an overview of semaphores and mailboxes, describing them as synchronization and communication mechanisms. It details the various methods available for semaphores like new(), get(), put(), and try_get(). Similarly, it outlines the methods for mailboxes such as new(), put(), get(), try_put(), and try_get(). Examples are also given to demonstrate the usage of semaphores and mailboxes.

Uploaded by

k k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

SV Interprocess Synchronization and Communication PDF

This document discusses interprocess synchronization and communication in SystemVerilog using semaphores and mailboxes. It provides an overview of semaphores and mailboxes, describing them as synchronization and communication mechanisms. It details the various methods available for semaphores like new(), get(), put(), and try_get(). Similarly, it outlines the methods for mailboxes such as new(), put(), get(), try_put(), and try_get(). Examples are also given to demonstrate the usage of semaphores and mailboxes.

Uploaded by

k k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Interprocess synchronization

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

synchronization and mutual exclusion to shared


resources.
Mailbox:
 A mailbox built-in class, which can be used as a

communication channel between processes.


Semaphores:
 Conceptually, a semaphore is a bucket.
 When a semaphore is allocated, a bucket that
contains a fixed number of keys is created.
 Processes using semaphores must first procure a
key from the bucket before they can continue to
execute.
 If semaphore keys are not available, all others
must wait until a sufficient number of keys is
returned to the bucket.
 Semaphores are typically used for mutual
exclusion, access control to shared resources,
and basic synchronization.
Semaphore Methods:
Semaphore is a built-in class that provides the
following methods:
 Create a semaphore with a specified number of
keys: new()
 Obtain one or more keys from the bucket: get()

 Return one or more keys into the bucket: put()

 Try to obtain one or more keys without blocking:


try_get()
Semaphore Methods:
 New()
semaphores are created with new() method.
syntax:
new(int keyCount = 0 );
The keyCount specifies the number of keys
initially allocated to the semaphore bucket.
The number of keys in the bucket can increase
beyond keyCount when more keys are put into the
semaphore than are removed.
Semaphore Methods:
Put() : (void function)
The semaphore put() method is used to return keys
to a semaphore.
When the semaphore.put() function is called, the
specified number of keys is returned to the
semaphore.
Get() : (task )
The semaphore get() method is used to procure a
specified number of keys from a semaphore.
If the specified number of keys is available, the
method returns and execution continues.
If the specified number of keys is not available, the
process blocks until the keys become available.
Semaphore Methods:
Try_get(): (function)
The semaphore try_get() method is used to
procure a specified number of keys from a
semaphore, but without blocking.
function int try_get(int keyCount = 1);
If the specified number of keys is available, the
method returns a positive integer and execution
continues.
If the specified number of keys is not available,
the method returns 0.
Example1:
module semaphore_ex;
  semaphore sema; //declaring semaphore sema
 
  initial begin
    sema=new(1); //creating sema with '4' keys
$display(“waiting to get key from semaphore”);
sema.get(1);
$display(“GOT KEY at : %0t ”, $realtime);
#1;
$display(“waiting to get key from semaphore”);
sema.get(1); 
$display(“GOT KEY at : %0t ”, $realtime);
   end

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

You might also like