相关: 线程间同步和通信,event semaphore mailbox
wait fork
语句来等待所有子线程结束task run_threads;
fork
check_trans(tr1); // 线程1
check_trans(tr2); // 线程2
check_trans(tr3); // 线程3
join_none
...
// 等待所有fork中的线程结束再退出task
wait fork;
endtask
注意:
parameter TIME_OUT = 1000;
task check_trans(Transaction tr);
fork
begin
// 等待回应,或者达到某个最大时延
fork: time_block
begin
wait (bus.cb.addr == tr.addr);
$display("@%0t: Addr match %d", $time, tr.addr);
end
#TIME_OUT $display("@%0t: Error: timeout", $time);
join_any
disable time_block
end
join_none
endtask
initial begin
check_trans(tr0); // 线程0
// 创建一个线程来限制disable fork的作用范围
fork // 线程1
begin
check_trans(tr1); // 线程2
fork // 线程3
check_trans(tr2); // 线程4
join
// 停止线程1-4,单独保留线程0
#(TIME_OUT/2) disable fork;
end
join
end
`timescale 1ns/1ns
module tb;
task automatic child_t(int t, string name);
forever #(t*1ns) $display("@%0t: child thread [%s] say hello", $time, name);
endtask
task automatic parent_t(int t = 3, string name = "parent_thread", int loop = 10);
fork: child_thread
child_t(4, "child_thread_A");
child_t(5, "child_thread_B");
child_t(6, "child_thread_C");
join_none
repeat(loop) #(t*1ns) $display("@%0t: parent thread [%s] say hello", $time, name);
$display("@%0t: finish %s", $time, name);
endtask
initial begin: parent_thread
parent_t();
end
initial begin
#25ns;
disable parent_thread;
// $display("@%0t: disable parent_thread", $time);
#50ns;
$display("@%0t: finish current test", $time);
$finish;
end
endmodule
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_none
endtask
initial begin
wait_for_time_out(0); // Spawn thread 0
wait_for_time_out(1); // Spawn thread 1
wait_for_time_out(2); // Spawn thread 2
#(TIME_OUT*2) $display("@%0t: All done", $time);
end
->
操作符来触发事件,结束对第一个线程的阻塞event e1, e2;
initial begin
$display("@%0t: 1: before trigger", $time);
-> e1;
@e2;
$display("@%0t: 1: after trigger", $time);
end
initial begin
$display("@%0t: 2: before trigger", $time);
-> e2;
@e1;
$display("@%0t: 2: after trigger", $time);
end
@0: 1: before trigger
@0: 2: before trigger
@0: 1: after trigger
event e1, e2;
initial begin
$display("@%0t: 1: before trigger", $time);
-> e1;
wait(e2.triggered());
$display("@%0t: 1: after trigger", $time);
end
initial begin
$display("@%0t: 2: before trigger", $time);
-> e2;
wait(e1.triggered());
$display("@%0t: 2: after trigger", $time);
end
wait(e1.triggered())
来代替边沿敏感的阻塞语句@e1