systemc: how to detect two events happen at the same time

21 views Asked by At

I am using systemc to model a packet-based network. There are two parallel threads, which process random size packet. After each packet, I'd like to check whether the other thread happens to be in the same status (packet boundary) to avoid conflict.

Any idea how to detect such event concurrency?

1

There are 1 answers

0
Mr Bone On

To answer the question directly about a method to detect events that happen at the same simulation time: sc_time_stamp() will return the current simulation time.

If your threads record the "last packet time" when constructing a packet, then the thread that executes second can compare its simulation time to the time of the "last packet". A match indicates the concurrent case, and the second executing thread can wait to send its packet.

sc_time last_pkt_time = sc_time(0,SC_NS);

void pktgen1_thread() {
 ...
 if (last_pkt_time != sc_time_stamp()) {
   send_pkt();
   last_pkt_time = sc_time_stamp();
 }
 else {
   wait();  // wait to send pkt in later iteration of thread loop
 }

I'm not sure what kind of system you are developing and if by 'event' you refer specifically to sc_event, but what I think you really want is arbitration based on availability of a packet. You could have a separate arbitration thread that checks 2 FIFOs for availability of a packet and selects one to process:

  void pktgen1_thread() {
    ...
    MyPkt pkt = build_pkt();
    pktgen1_fifo.write(pkt);
    ...
  }

  void arbiter_thread() {
    while (1) {
      MyPkt pkt;
      if (pktgen1_fifo.available()) {
        pkt = pktgen1_fifo.read();
        process_pkt(pkt);
      } else if (pktgen2_fifo.available()) {
        pkt = pktgen2_fifo.read();
        process_pkt(pkt);
      }
      wait();
    }
  }

The exact coding style needed will depend on your usage case for SystemC (TLM LT/AT model vs. synthesis-compatible).