# Wait for signal value in a task with timeout

**URL:** <https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, timeout-value\
**Created:** [July 19, 2018, 5:48am UTC](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702 "2018-07-19T05:48:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaygueta](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@shaygueta](https://verificationacademy.com/forums/u/shaygueta)\
**Post date:** [July 19, 2018, 5:48am UTC](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/1 "2018-07-19T05:48:15Z")

</div>

Hi,  
I would like to write **a task** that receives a signal by reference and waits for its posedge or negedge. If the signal is not set to the desired value within a specified timeout, an error message should be set.  
I wrote something like this:

```verilog
task automatic timeout(ref logic signal,input logic value,input time timeout_value);
      if(signal !== value) begin
           fork
               begin
                  #timeout_value;
                  `uvm_error(....
               end
               begin
                  if (signal !== signal_value)
                     @(signal === signal_value);
               end
           join_any
           disable fork;
endtask

```

This code does not compile because it violates Verilog LRM section 9.3.2(parallel blocks):it is illegal to refer to an argument passed by reference in fork - join\_any block.  
Do you have any suggestions? Maybe there is a solution for this in UVM? I could not find any.  
Thanks

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [July 19, 2018, 2:06pm UTC](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/2 "2018-07-19T14:06:57Z")

</div>

_In reply to [shaygueta](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702):_

Just fork the timeout delay.

```verilog
task automatic timeout(ref logic signal,input logic value,input time timeout_value);
   bit timed_out;
           fork begin
              fork
                begin
                  #timeout_value;
                  `uvm_error(....
                  timed_out = '1;
                end
             join_none
             wait(signal === value || timed_out);
             disable fork;
           end join
endtask

```

Note the enclosing fork/join is needed to prevent the disable fork from killing other child threads that may have been spawned before calling this task.

---

<div class="post-metadata">

**Author:** ![Elisha\_K](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@Elisha\_K](https://verificationacademy.com/forums/u/Elisha_K)\
**Post date:** [March 29, 2022, 7:54pm UTC](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/3 "2022-03-29T19:54:10Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/2):_

Why add another bit “timed\_out” instead of using join\_any?

```verilog
task automatic timeout(ref logic signal,input logic value,input time timeout_value);
           fork begin
              fork
                begin
                  #timeout_value;
                  `uvm_error(....
                end
                begin
                  wait(signal === value);
                end
             join_any
             disable fork;
           end join
endtask

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 29, 2022, 9:29pm UTC](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/4 "2022-03-29T21:29:01Z")

</div>

_In reply to [Elisha K](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/3):_

Because the reference to **signal** inside a fork/join\_any or join\_none is not legal.

---

<div class="post-metadata">

**Author:** ![VE1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/54ee81/32.png) [@VE1](https://verificationacademy.com/forums/u/VE1)\
**Post date:** [April 4, 2022, 6:31pm UTC](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/5 "2022-04-04T18:31:42Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/4):_

> _In reply to [shaygueta](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/4):_  
> Just fork the timeout delay.

```verilog
task automatic timeout(ref logic signal,input logic value,input time timeout_value);
bit timed_out;
fork begin
fork
begin
#timeout_value;
`uvm_error(....
timed_out = '1;
end
join_none
wait(signal === value || timed_out);
disable fork;
end join
endtask

```

Note the enclosing fork/join is needed to prevent the disable fork from killing other child threads that may have been spawned before calling this task.

HI Dave :  
1 can I use disable {name of fork} instead of the guard fork/join here ? i.e  
wait(signal === value || timed\_out);  
disable \< name of fork process\>; // assume there is name of fork/join\_none  
2. Do you recommend always use a guard fork/join when there is disable fork ? for example  
in run\_phase(uvm\_phase phase);

Thanks

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [April 4, 2022, 9:36pm UTC](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/6 "2022-04-04T21:36:36Z")

</div>

_In reply to [VE](https://verificationacademy.com/forums/t/wait-for-signal-value-in-a-task-with-timeout/34702/5):_

1. Using   
disable _name\_of\_fork_ has problems if there are multiple concurrent instances of the fork. Since you are allowed to use   
disable _name\_of\_fork_ from anywhere, it has no way of knowing which instance you mean and it kills all instances.
2. I do recommend using guard fork/join whenever there is disable fork as a good habit. But you can avoid it if you are sure no other processes would get disabled accidentally. All of the UVM phase tasks are forked as a separate process so there are no other child process that could get disabled accidentally.
