Search code examples
hardwarehdlsystemcregister-transfer-level

Proper way to reset a SC_THREAD in SystemC from another process


If I have two threads in SystemC, A and B (both SC_THREAD), and I want thread A to stop executing (be reset) if a variable or event in B gets asserted, what is the proper way to accomplish this.

Here is a more illustrative example:

// Thread A: Data transfer
SC_THREAD(rx_tx_transfer);
  dont_initialize();
  sensitive << clk.pos();

// Thread B: If the value of the control registers changes 
// during a transfer, the transfer should be aborted
  SC_THREAD(check_reg_write_during_tx);
    sensitive << x_event << y_event;

So, I want Thread A to stop what it is doing as soon as I fire an event or signal in Thread B. Maybe this could be accomplished if in A I put in every wait the event in question and the regular clk.pos(), and then do an if-else like this:

void rx_tx_transfer()
{
  // code
  wait(clk.pos() | event_from_b);
  if (clk.read() == SC_LOGIC_1 & event_from_b_var)
  {
    //...
  }
  else if (clk.read() == SC_LOGIC_1)
  {
    //...
  }
  else
  {
    //...
  }

  // other code
  wait(clk.pos() | event_from_b);
  // repeated if-else code from above
}

I don't like this solution, and hope there is a better way I am unfamiliar with. Perhaps I should have used the async_reset_signal_is() in the SC_THREAD, like so:

// Thread A: Data transfer
SC_THREAD(rx_tx_transfer);
   dont_initialize();
   sensitive << clk.pos();
   async_reset_signal_is(sig_form_b, true);

Would this stop thread A execution and go to reset state when sig_form_b is asseted, no matter the current state of execution of rx_tx_transfer function (i.e, it has not reached the next wait())?

If there is a better or more proper way to do this, please share.


Solution

  • I'm not sure what you really want to achieve: stopping thread and resetting it are two different things.

    You can reset a thread with a reset signal if it was configured with async_reset_signal_is. If a reset signal is asserted thread will start from the beginning.

    SystemC threads are not real threads, but a co-routines executed sequentially in non-preemptive manner. So there is only one thread executing in each moment of time and no one can really stop it. Thread can stop itself and pass control to scheduler by calling wait() method.