Saturday, July 16, 2016

fine gain process control

A process is a built-in class in System Verilog that allows one process to access and control another process once it has started. Users can declare variables of type process and safely pass them through tasks or incorporate them into other objects. The prototype for the process class is as follows:

class process;
typedef enum { FINISHED, RUNNING, WAITING, SUSPENDED, KILLED } state;
static function process self();
function state status();
function void kill();
task await();
function void suspend();
function void resume();
function void srandom( int seed );
function string get_randstate();
function void set_randstate( string state );
endclass

Sometimes we encounter the situations where we need to spawn off threads and if anyone of the thread is done, we might want to kill other threads. One straightward solution is using disable statement but that is not scalable if number of threads are more.
If you look at the following example, we spawn off 10 threads and we want to wait for 2nd thread to finish and by that time, if the other threads are not done, we want to kill it.
module process_test;
bit clk;
initial begin
   forever begin
      #10 clk = ~clk;
   end
end

task automatic spawn_threads(int N);
process job[]= new[N];
foreach(job[j])
  fork
    automatic int k=j;
    begin  
      job[k]=process::self();
      $display("Process %0d started %t",k, $time);
      repeat(10*k) @(posedge clk);
      $display("Process %0d finished %t",k, $time);
    end
  join_none

foreach (job[i])
   wait(job[i]!=null);

job[2].await();

foreach (job[i]) begin
  if(job[i].status != process::FINISHED)
     job[i].kill();
end
endtask

initial begin
  spawn_threads(10);
  $finish();
end
endmodule

Result:-
Compiler version K-2015.09-SP2-1; Runtime version K-2015.09-SP2-1;  Jul 16 14:44 2016
Process 0 started                    0
Process 0 finished                    0
Process 1 started                    0
Process 2 started                    0
Process 3 started                    0
Process 4 started                    0
Process 5 started                    0
Process 6 started                    0
Process 7 started                    0
Process 8 started                    0
Process 9 started                    0
Process 1 finished                  190
Process 2 finished                  390
$finish called from file "test_process.sv", line 38.
$finish at simulation time                  390
           V C S   S i m u l a t i o n   R e p o r t 


This is useful not just for killing the threads but also we can suspend and resume threads based on our own conditions.




No comments:

Post a Comment