Stopping of process management -2- thread in system Verilog

You need to create a thread in the test platform, and you also need to stop the thread.
The disable statement in verilog can be used to stop threads in system verilog.
SystemVerilog provides two types of process control methods: wait and disable.
A variety of methods commonly used in disable statements include: disable block_name,disable task_name and disable fork. Calling the disable statement will terminate the specified process.
1. Use disable block_name stops a thread

initial begin
  begin : block_0
    $display("block_0 start");
    disable block_0;
    $display("block_0 finish");
  end
  $display("initial finish");
end
//Print results
block_0 start
initial finish

2. Use disable task_name stops a thread
If you disable a task within a task, it is like a return statement of the task, but it will also stop all threads started by the task. If the task has been called by multiple threads, disabling one of them will cause them to be disabled.

task wait_for_time_out(int id);
  if(id == 0)
    fork
      begin
        #2;
        $display("@%0t:disable wait_for_time_out", $time);
        disable wait_for_time_out;
      end
    join_none
  fork: just_a_little
    begin
      $display(@%0t:%m: %0d entering thread",$time,id);
      #TIME_OUT;
      $display("@%0t:%m: %0d done",$time, id);
    end
  join
endtask
initial begin
  wait_for_time_out(0);  //Derived thread 0
  wait_for_time_out(1);  //Derived thread 1
  wait_for_time_out(2);  //Derived thread 2
end

Task wait_for_time_out is called three times, resulting in three threads. Thread 0 disabled the task after a #2 delay. Just run this code and you can see that all three threads are started. However, because of the disable statement in thread 0, these threads did not complete in the end.

3. Stop multiple threads: disable fork
systemveilog introduces the disable fork statement to stop all child threads derived from the current thread.

initial begin
  check_trans(tr0);      //Thread 0
  fork                   //Thread 1
  begin
    check_trans(tr1);    //Thread 2
    fork
      check_trans(tr2);  //Thread 3
    join
    #(TIME_OUT/2) disable fork;    // Stop threads 1-4 and leave thread 0 alone
  end
  join
end

Wait includes three sub categories: wait and wait_order,wait fork.
4.wait thread
Among them, wait is relatively simple and is not introduced.

5.wait_order thread
wait_order will block and wait for the triggering of multiple events, and the sequence of these events is required to be consistent with the setting.

initial begin
  event event_1, event_2, event_3;
  fork
    begin
      #50 -> event_1;
    end
    begin
      #30 -> event_2;
    end
    begin
      #100 -> event_3;
    end
  join_any
  wait_order(event_2, event_1, event_3);
  $display("all evnet finish, current time is %0t", $realtime);
end
//Operation results
all event finish, current time is 10000

wait_order needs to be blocked to three events in turn according to event_2,event_1,event_3 can only be executed after triggering. This is wait_ Usage of order.

6.wait_fork thread
1)wait fork will cause the calling process to block until all its child processes end;
2) The purpose of wait fork is to ensure that the execution of all sub processes ends;
3)wait fork acts as the child process under the parent process, not including the child process under the child process.

7. Process built-in class
A class is built in SystemVerilog to access and control processes. This class is process. Let's take a look at the prototype of process class:
Process is a built-in class that allows one process to access and control another process after startup. Users can declare variables of type process and safely pass them through the task or merge them into other objects. The prototype of 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

Next, we will introduce several tasks and functions in the process class in detail:
self() - get the handle of the current process. The process class cannot be actively created through new. It can only be created through initial begin... end, final begin... end, 4 always blocks, 3 fork blocks and dynamic processes.
status() - get the status of the current process. finish - the process ends, running - the process is in progress, waiting - the process waits for blocking conditions, suspended - the process stops and waits for resume. The current state can only be released through resume, and killed - the process is forcibly suspended.
kill() - abort the process and its child processes.
Await () - waits for other processes to end, and cannot invoke this method in the process. Only other processes can call this process await() to block this process.
suspend() - suspend the process.
resume() - resume the process.
srandom(int seed) - sets the random seed of the process.

module top_tb;
  process process_1;
  process process_2;
  initial begin
    process_1 = process::self();
    #100;
    $display("process_1 finish, current time is %0t", $realtime());
  end
  initial begin
    process_2 = process::self();
    #1000;
    $display("process_2 finish, current time is %0t", $realtime());
  end
  intial begin
    #100;
    $display("process_1 status is %0s, current time is %0t", process_1.status, $realtime());
    $display("process_2 status is %0s, current time is %0t", process_2.status, $realtime());
    #100;
    process_2.kill();
    $display("process_2 status is %0s, current time is %0t", process_2.status, $realtime());
  end
endmodule
//Operation results
process_1 finish, current time is 100000
process_1 status is FINISHED, current time is 100000
process_2 status is WAITING, current time is 100000
process_2 status is KILLED, current time is 200000

Tags: systemverilog

Posted on Sun, 31 Oct 2021 05:01:27 -0400 by _off_axis_