SystemVerilog Semaphore

 Semaphore 


Imagine a situation where two processes try to access a shared memory area where one process tries to write to a memory location when the other process is trying to read. this leads to an unexpected results.

Semaphores are used for shared resources access control.

Conceptually, a semaphore is a bucket. When a semaphore is allocated, it contains a fixed number of keys is created. 

The process which wants to access the shared resource can first acquire the key/keys, once the process finishes the access to resource, key/keys shall be put back to the bucket. 

If any other process tries to access the same resource it must wait until a sufficient number of keys is returned to the bucket.

creating a semaphore is:
        semaphore semaphore_name;

Semaphore is a built-in class that provides the following methods:
  • new() - Create a semaphore with a specified number of keys
  • 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


 new( ); 

The new() method is used to create the Semaphore.
       semaphore_name = new(numbers_of_keys);

new method will create the semaphore with number_of_keys keys in bucket, where number_of_keys is integer variable.
the default number of keys is ‘0’,new() method will return the semaphore handle or null if the semaphore cannot be created.

 put( ); 


The semaphore put() method is used to return key/keys to a semaphore.
       semaphore_name.put(number_of_keys);  or semaphore_name.put();

When the semaphore_name.put() method is called, the specified number of keys are returned to the semaphore.  The default number of keys is 1.

 get( ); 


The semaphore get() method is used to get key/keys from a semaphore.
       semaphore_name.get(number_of_keys);  or semaphore_name.get();

When the semaphore_name.get() method is called, If the specified number of keys are available, the method returns and execution continues. If the specified number of key are not available, the process blocks until the keys become available. The default number of keys is 1.

 try_get(); 


The semaphore try_get() method is used to procure a specified number of keys from a semaphore, but without blocking.
        semaphore_name.try_get(number_of_keys); or semaphore_name.try_get();

When the semaphore_name.try_get() method is called, If the specified number of keys are available, the method returns 1 and execution continues. If the specified number of key are not available, the method returns 0. The default number of keys is 1.

 Example-1: 


In the example below,
  semaphore sema is created with the 1 key,two process are accessing the display method at the same time, but only one process will get the semaphore key and the other process will wait till it gets the key.

module semaphore_ex;
  semaphore sema; //declaring semaphore sema
  initial begin
    sema=new(1); //creating sema with '1' key
    fork
      display(); //process-1
      display(); //process-2
    join
  end

  //display method
  task automatic display();
    sema.get(); //getting '1' key from sema
    $display($time,"\tCurent Simulation Time");
    #30;
    sema.put(); //putting '1' key to sema
  endtask
endmodule

 Simulator Output  


 0 Curent Simulation Time
30 Curent Simulation Time
Execute the above code on 

 Example-2: 


In the example below,
  Creating semaphore with '4' keys.

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
    join
  end
  //display method
  task automatic display();
    sema.get(4); //getting '4' keys from sema
    $display($time,"\tCurent Simulation Time");
    #30;
    sema.put(4); //putting '4' keys to sema
  endtask
endmodule

 Simulator Output  


0 Curent Simulation Time
30 Curent Simulation Time
Execute the above code on 


Semaphore examples are continued in next page