Contact / Report an issue

SystemVerilog Semaphore

Semaphore


Semaphore is a SystemVerilog built-in class, used for access control to shared resources, and 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 a sufficient number of keys are returned to the bucket.

Imagine a situation where two processes try to access a shared memory area. where one process tries to write and the other process is trying to read the same memory location. this leads to an unexpected result. A semaphore can be used to overcome this situation.

Semaphore syntax


semaphore semaphore_name;

Semaphore methods


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);

  • the new method will create the semaphore with number_of_keys keys in a bucket; where number_of_keys is integer variable.
  • the default number of keys is ‘0’
  • the 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 returned 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, then the method returns and execution continues
  • If the specified number of keys are not available, then the process blocks until the keys become available
  • The default number of keys requested 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 keys are not available, the method returns 0 and execution continues
  • The default number of keys requested is 1




Semaphore examples


two processes accessing the same resource


In the example below,
semaphore sema is created with the 1 key, two processes 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,"\tCurrent Simulation Time");
    #30;
    sema.put(); //putting '1' key to sema
  endtask
endmodule

 Simulator Output

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

Semaphore with 4 keys


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 Current Simulation Time
30 Current Simulation Time
Execute the above code on 

Semaphore examples are continued in next page