A semaphore allows you to control access to a resource.
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 a specific process requires a key, only a fixed number of occurrences of that process can be in progress simultaneously. All others must wait until a sufficient number of keys are returned to the bucket.
Semaphores are typically used for mutual exclusion, access control to shared resources, and basic synchronization.
Imagine that you and your spouse share a car. Obviously, only one person can drive it at a time. You can manage this situation by agreeing that whoever has the key can drive it. When you are done with the car, you give up the car so that the other person can use it. The key is the semaphore that makes sure only one person has access to the car.
In operating system terminology, this is known as “mutually exclusive access” so a semaphore is known as a “mutex” and is used to control access to a resource.
Semaphores can be used in a testbench when you have a resource, such as a bus, that may have multiple requesters from inside the testbench but, as part of the physical design, can only have one driver.
In SystemVerilog, a thread that requests a key when it is not available always blocks the execution of that particular thread. Multiple blocking threads are queued in FIFO order.
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() //Blocking in nature
- Obtain one or more keys without blocking: try_get()
The default value for Key Count is 0.
If the specified number of keys is not available, the get() method blocks process until the keys become available.
If the specified number of keys is not available, the try_get() method returns 0.
Let’s understand through example
program main; semaphore sema = new(1); // Create semaphore with 1 key. initial begin repeat(3) begin fork ////////// PROCESS 1 /////////// begin $display("1: Waiting for key, time=%0t", $time); sema.get(1); $display("1: Got the Key, time=%0t", $time); #(10);// Do some work sema.put(1); $display("1: Returning back key, time=%0t", $time); #(10); end ////////// PROCESS 2 /////////// begin #1; $display("2: Waiting for Key, time=%0t", $time); sema.get(1); $display("2: Got the Key, time=%0t", $time); #(10);//Do some work sema.put(1); $display("2: Returning back key, time=%0t", $time); #(10); end join $display(); end #1000; end endprogram