[SV]SystemVerilog Semaphore

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

1、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

2、put( );

    The semaphore put() method is used to return key/keys to a semaphore.

semaphore_name.put(number_of_keys); or semaphore_name.put();

3、get( );


    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.

    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

4、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

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

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

元直数字电路验证

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值