日常记录(26)fork join、semaphore、mailbox

fork join

`include "exp_inside/tbb.sv"
class ClassBase;

    virtual function void disp();
        $display("the base print");
    endfunction: disp

endclass : ClassBase

class ClassEx1 extends ClassBase;

    function void disp();
        $display("the ex1 print");
    endfunction: disp

endclass : ClassEx1

class ClassEx2 extends ClassEx1;

    function void disp();
        $display("the ex2 print");
    endfunction: disp

    extern function int calc();

endclass : ClassEx2

function int ClassEx2::calc();
    $display("this is a calc");
    return 10;
endfunction

module taa ();
    initial begin
        ClassBase cb=new;
        ClassBase ce1=new;
        ClassEx2 ce2=new;
        cb=ce2;
        cb.disp;
        ce1=ce2;
        ce1.disp;
        $display("ce2.calc() ans %d", ce2.calc());
    end

    initial begin
        event e1;
        ForkJoin fj1=new(e1);
        fj1.fork_join;
        #100
        fj1.wait_and_disable;
        #100
        fj1.event_access;
        -> e1;
    end
endmodule

  

class ForkJoin;
    // data or class properties
    event e1;
    // initialization
    function new(event e1);
        this.e1=e1;
    endfunction : new

    task event_access();
        $display("event wait.");
        fork
            @e1;
            $display("event e1 is traggerd");
        join

        $display("pass");
    endtask: event_access

    task wait_and_disable();
        fork
            #1 $display("this is a fork in wait_and_disable");
        join

        fork
            #2 $display("this is a fork in #2");
        join

        fork
            #1 $display("#1 in wait ");
        join

        $display("before");
        wait fork;
        $display("wait finished");

        fork
            #10 $display("#10");
            #1 $display("#1");
        join_none

        disable fork;
        $display("disable finished");
        
    endtask: wait_and_disable

    task automatic fork_join();

        fork
            $display("thread1 start");
            #10 $display("this is a 10 in fork join");
            #5 $display("this is a 5 in fork join");
        join

        fork
            #6 $display("this is a 6 in fork join_any");
            #3 $display("this is a 3 in fork join_any");
        join_any
        #0 $display("parent in #0 print");
        $display("this is a parent thread in 0");

        fork
            #0 $display("thread3 start");
            #1 $display("thread3 in #1 ");
        join_none

        #1 $display("this is a p2 in #1");
        #0 $display("this is a #0 in parent thread");
    endtask: fork_join

endclass

输出

the ex2 print
the ex2 print
this is a calc
ce2.calc() ans          10
thread1 start
this is a 5 in fork join
this is a 10 in fork join
this is a 3 in fork join_any
parent in #0 print
this is a parent thread in 0
thread3 start
this is a p2 in #1
thread3 in #1 
this is a #0 in parent thread
this is a 6 in fork join_any
this is a fork in wait_and_disable
this is a fork in #2
#1 in wait 
before
wait finished
disable finished
event wait.
event e1 is traggerd

  

 不要考验ForkJoin

        fork
            $display("a");
        join_none
        $display("b");
        fork
            #0
            #1 $display("a");
        join_none
        #1 
        #0 $display("b");


        fork
            #1 $display("aa");
        join_none
        #1 $display("bb");

输出

b
a
a
b
bb
aa

  

时刻性质触发

class event_test;
    // data or class properties
    event e1; 

    // initialization
    function new(event e1);
        this.e1=e1;
    endfunction : new

    task  event_test();
        $display("event test...");
        ->e1;
        fork
            event_tragger();
            event_tragger2();
        join_none
        ->e1;
        ->e1;
        #1
        ->e1;
        #1
        ->e1;
        #1
        ->e1;
        #10 $display("end...");
    endtask: event_test

    task event_tragger();
        $display("start et1");
        while(1) begin
            @(e1);
            $display("traggered!");
        end
    endtask: event_tragger

    task event_tragger2();
        $display("start et2");
        while(1) begin
            //@e1.triggered; //can't be used, please.
            wait(e1.triggered);
            $display("traggered2!");
            @e1; 
        end
    endtask: event_tragger2

endclass : event_test

module tcc ();
    initial begin
        event_test et;
        event e1;
        et=new(e1);
        et.event_test();
    end
endmodule

  

输出

event test...
start et1
start et2
traggered2!
traggered!
traggered2!
traggered!
traggered2!
traggered!
traggered2!
end...

  

旗语

new、get、put、try_get

module tdd ();
    class Semap;
        // data or class properties
        semaphore a,b[];

        // initialization
        function new();
            b=new[10];
            for (int i = 0; i < 10; i++) begin
                b[i]=new(i);
            end
            a=new(3);
            a.put(1);
        endfunction : new

        function void disp_key_all();
            disp_key(a);
            foreach (b[i]) begin
                disp_key(b[i]);
            end
        endfunction: disp_key_all

        function void disp_key(semaphore a);
            int value;
            while(1)
            begin
                value=a.try_get;
                assert(value)
                else begin
                    $display("the semaphore key of a is %d", value);
                    break;
                end
                $display("the semaphore key of a is %d", value);
            end
        endfunction: disp_key

    endclass : Semap

    initial begin
        Semap sem;
        sem=new;
        sem.disp_key_all;
    end
endmodule

  

输出

 1 the semaphore key of a is           1
 2 the semaphore key of a is           1
 3 the semaphore key of a is           1
 4 the semaphore key of a is           1
 5 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
 6     Offending 'value'
 7 the semaphore key of a is           0
 8 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
 9     Offending 'value'
10 the semaphore key of a is           0
11 the semaphore key of a is           1
12 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
13     Offending 'value'
14 the semaphore key of a is           0
15 the semaphore key of a is           1
16 the semaphore key of a is           1
17 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
18     Offending 'value'
19 the semaphore key of a is           0
20 the semaphore key of a is           1
21 the semaphore key of a is           1
22 the semaphore key of a is           1
23 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
24     Offending 'value'
25 the semaphore key of a is           0
26 the semaphore key of a is           1
27 the semaphore key of a is           1
28 the semaphore key of a is           1
29 the semaphore key of a is           1
30 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
31     Offending 'value'
32 the semaphore key of a is           0
33 the semaphore key of a is           1
34 the semaphore key of a is           1
35 the semaphore key of a is           1
36 the semaphore key of a is           1
37 the semaphore key of a is           1
38 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
39     Offending 'value'
40 the semaphore key of a is           0
41 the semaphore key of a is           1
42 the semaphore key of a is           1
43 the semaphore key of a is           1
44 the semaphore key of a is           1
45 the semaphore key of a is           1
46 the semaphore key of a is           1
47 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
48     Offending 'value'
49 the semaphore key of a is           0
50 the semaphore key of a is           1
51 the semaphore key of a is           1
52 the semaphore key of a is           1
53 the semaphore key of a is           1
54 the semaphore key of a is           1
55 the semaphore key of a is           1
56 the semaphore key of a is           1
57 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
58     Offending 'value'
59 the semaphore key of a is           0
60 the semaphore key of a is           1
61 the semaphore key of a is           1
62 the semaphore key of a is           1
63 the semaphore key of a is           1
64 the semaphore key of a is           1
65 the semaphore key of a is           1
66 the semaphore key of a is           1
67 the semaphore key of a is           1
68 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
69     Offending 'value'
70 the semaphore key of a is           0
71 the semaphore key of a is           1
72 the semaphore key of a is           1
73 the semaphore key of a is           1
74 the semaphore key of a is           1
75 the semaphore key of a is           1
76 the semaphore key of a is           1
77 the semaphore key of a is           1
78 the semaphore key of a is           1
79 the semaphore key of a is           1
80 "tdd.sv", 28: tdd.\Semap::disp_key .unnamed$$_0: started at 0s failed at 0s
81     Offending 'value'
82 the semaphore key of a is           0
View Code

 

信箱

new、put、get、peek、try_xx

https://www.wenjiangs.com/doc/shvhiu8i,搜索try_get关于信箱,队列性质get数据,但是返回值是队列长度吧?

module tee ();
    class MailTest;
        // data or class properties
        mailbox mbx;

        // initialization
        function new(mailbox mbx);
            this.mbx=mbx;
        endfunction : new

        function void mailbox_disp();
            int a;
            int has_value;
            do begin
                has_value=mbx.try_get(a);
                $display("has_value : %d", has_value);
                if(!has_value)
                    break;
                $display("this is a: %d", a);
            end
            while(1);
        endfunction: mailbox_disp

    endclass : MailTest

    initial begin
        mailbox mbx=new;
        MailTest mt=new(mbx);

        mbx.put(1);
        mbx.put(2);
        mbx.put(3);
        mbx.put(4);
        mbx.put(5);
        #2
        mt.mailbox_disp();
        
    end
endmodule

  

输出

has_value :           5
this is a:           1
has_value :           4
this is a:           2
has_value :           3
this is a:           3
has_value :           2
this is a:           4
has_value :           1
this is a:           5
has_value :           0

  

->>的非阻塞事件

据说用不到,先记着。

https://blog.csdn.net/A670449625/article/details/47657273

->> #5 ev1;   //"->>": non-blocking event, delay 5 cycle triggering the event
->> @(ev1) ev2;          //event ev1 and ev2 will triggered simutaneous,but ev1 is earlier.

  

 

posted @ 2021-12-30 14:20  大浪淘沙、  阅读(47)  评论(0)    收藏  举报