In SystemVerilog, one can use the disable construct to terminate named blocks or tasks. But I found today that disabling a task within a class instance affects all copies of the task in all class instances.
Consider a class which has a few tasks defined within it. If, internal to the class, there is a call to disable <taskname>, this will disable all copies of the task across all instances of the class. This is not the behavior I expected, but it is correct according to the LRM:
If a task is enabled more than once, then disabling such a task shall disable all activations of the task.
Is there a mechanism or workaround to disable only the task within a specific instance of a class?
The code below highlights the problem I am facing. Basically, I have a timer task which I want to disable due to some other event. This is all contained within a class. In my verification environment I have multiple instances of this class and they operate entirely independently. So disabling the timer within one instance should not affect the others. But the way disable works it is affecting the other instances.
I tried disable this.timer but that did not compile.
In this example, I have two instances of class c which contains a timer task. There is a disabler task which is started in parallel which makes the call to disable timer. The intent in this example is for timer to get disabled half-way through its count. So c1 would get disabled at time 5, and c2 would get disabled at time 10. However, they both are disabled at time 5, because the disable timer call originating in c1 disables the task in both c1 and c2.
module top();
class c;
string name;
int count;
function new(string _name, int _count);
name = _name;
count = _count;
endfunction
task t1();
timer();
$display("%t %s timer completed", $time, name);
endtask
task run();
fork
t1();
disabler();
join
endtask
task timer();
$display("%t %s starting timer with count=%0d", $time, name, count);
repeat(count) #1;
endtask
task disabler();
repeat(count/2) #1;
disable timer;
endtask
endclass
class ex;
c c1;
c c2;
function new();
c1 = new("c1", 10);
c2 = new("c2", 20);
endfunction
task run();
fork
c1.run();
c2.run();
join_none
endtask
endclass
ex e = new;
initial begin
e.run();
end
endmodule
Output:
0 c1 starting timer with count=10
0 c2 starting timer with count=20
5 c2 timer completed
5 c1 timer completed
I know I could rewrite this to get it to work without using disable, but it’s such an easy way to terminate a task early that I’m hoping there is something I’m missing.
I don’t think this is possible. Even using disable on named blocks will still stop all instances of that task.
You could try to use a process class. I’ve never used them myself so this code likely contains errors.