Process class inside forever loop

Hi,

I am trying to use process class inside forever loop. in below snippet code, i use two processors p[0] and p[1].
whenever thread 1 or thread 2 finishes, it will kill un-finished thread.
And in next iteration of forever loop, it will create two new threads and continue monitor new thread 1 and thread 2.
Is this correct usage of process inside forever loop?

Thank you!


process p[2];

forever begin
 fork
  begin //thread 1
   p[0] = process::self();
   //do something 
  end
  begin //thread 2
   p[1] = process::self();
   uvm_event.wait_trigger(); 
  end
 join_any
 if ((p[0] != null) && (p[0].status != process::FINISHED)) p[0].kill();
 if ((p[1] != null) && (p[1].status != process::FINISHED)) p[1].kill();
 #1;
end


In reply to jasonko:

Maybe you could explain why you are trying to do this?

Otherwise, why not just use “disable fork”?

In reply to warnerrs:

I know i can also use “disable fork” but i need to add extra “fork being end join” which i would like to learn other options so i don’t need to put two fork in my code to avoid disable fork kills sub-child thread, so i would like to understand more on how to proper use process class.

Another follow up question I have that can’t used “disable fork” when using “fork join_none”.
In below pseudo code, when condition is true inside task proc1, i want to kill current thread of task proc2 forever loop.
But once p[5] is killed, it seems like entire forever loop is killed inside proc2. how do i fix it so i only kill the current thread of proc2 but it will create a new thread again in next forever iteration?


class monitor;
process p[5];

task run_phase(uvm_phase phase);

fork
 proc1();
 proc2()
join_none

endtask

task proc1();
 forever begin
  //do something 
  if (condition)
  begin
    if ((p[5] !=null) && p[5].status !=process:: FINISHED) p[5].kill();
  end
 end
endtask

task proc2();
forever
begin
  p[5] = processs:self();
  //do something
  #1;
end
endtask
endclass


In reply to jasonko:

But once p[5] is killed, it seems like entire forever loop is killed inside proc2. how do i fix it so i only kill the current thread of proc2 but it will create a new thread again in next forever iteration?

You only have 3 threads in your example. The first thread is the one that calls run_phase. The fork in run_phase creates 2 new threads.

So, there is no “current thread” inside proc2. It’s just a single thread running in a loop. When you kill the thread, you kill the loop.

You’d need to introduce a new thread on each iteration of the loop.

class monitor;
    process p[5];

    task run_phase(uvm_phase phase);

        fork
            proc1();
            proc2();
        join_none

    endtask

    task proc1();
        forever begin
            //do something
            if (condition)
            begin
                if ((p[5] !=null) && p[5].status !=process:: FINISHED) p[5].kill();
            end
        end
    endtask

    task proc2();
        forever
            fork // <- new thread every iteration
                begin
                    p[5] = processs::self();
                    //do something
                    #1;
                end
            join
    endtask
endclass

In reply to jasonko:

just a quick question here. when does process::self() return null ? Why we always need to code like this :



 if ((p[0] != null) && (p[0].status != process::FINISHED)) p[0].kill();
 if ((p[1] != null) && (p[1].status != process::FINISHED)) p[1].kill();


In reply to zz8318:

In this contrived example, it is possible that the (p[n] != null) expression gets evaluated before the p[n] = processs::self(); assignment executes.

In reply to dave_59:

So it’s possible to get null the process::self() returned if this process is not started yet. Thank you.

In reply to zz8318:

In reply to dave_59:
So it’s possible to get null the process::self() returned if this process is not started yet. Thank you.

That is not correct. It’s possible process::self() has not been called yet. so p[0] or p[1] might still be null.