Mailbox

Gün 4: Süreçler Arası İletişim (IPC) | Bileşenler arası veri aktarımı: put, get, peek, try_get

Kaynak Kod

// =============================================================================
// GUN 4 - Konu 4: Mailbox - Bilesenler Arasi Veri Aktarimi
// =============================================================================

class Transaction;
  int id;
  int data;
  string source;

  function new(int id, int data, string source = "unknown");
    this.id = id;
    this.data = data;
    this.source = source;
  endfunction

  function void display();
    $display("      TXN #%0d: data=%0d, source=%s", id, data, source);
  endfunction
endclass

module mailbox_ornek;

  mailbox #(Transaction) mbx_bounded;    // Sinirli mailbox
  mailbox #(Transaction) mbx_unbounded;  // Sinirsiz mailbox
  mailbox #(int)         mbx_simple;     // Basit tip mailbox

  // --- Basit Mailbox: put ve get ---
  initial begin
    $display("=== Mailbox Kullanimi ===\n");

    $display("--- Basit Mailbox (int) ---");
    mbx_simple = new();  // Sinirsiz
    
    // put: Veri koy
    mbx_simple.put(10);
    mbx_simple.put(20);
    mbx_simple.put(30);
    $display("  3 eleman eklendi, boyut = %0d", mbx_simple.num());
    
    // get: Veri al (FIFO)
    begin
      int val;
      mbx_simple.get(val);
      $display("  get() = %0d, boyut = %0d", val, mbx_simple.num());
      mbx_simple.get(val);
      $display("  get() = %0d, boyut = %0d", val, mbx_simple.num());
    end
    
    // peek: Almadan bak
    begin
      int val;
      mbx_simple.peek(val);
      $display("  peek() = %0d, boyut = %0d (degismedi)", val, mbx_simple.num());
    end

    // try_get: Bloklama yapmadan al
    begin
      int val;
      if (mbx_simple.try_get(val))
        $display("  try_get() = %0d", val);
      if (!mbx_simple.try_get(val))
        $display("  try_get() basarisiz (bos mailbox)");
    end
  end

  // --- Sinirli Mailbox ile Producer-Consumer ---
  initial begin
    #50;
    $display("\n--- Sinirli Mailbox (bounded=3) ---");
    mbx_bounded = new(3);  // Maks 3 eleman

    fork
      // Producer
      begin : producer
        for (int i = 0; i < 6; i++) begin
          Transaction t = new(i, $urandom_range(100,999), "Producer");
          $display("  [%0t] Producer: #%0d gonderiyor... (kuyruk=%0d)",
                   $time, i, mbx_bounded.num());
          mbx_bounded.put(t);  // Dolu ise bekler (blocking)
          $display("  [%0t] Producer: #%0d gonderildi", $time, i);
          #5;
        end
      end

      // Consumer (yavas)
      begin : consumer
        Transaction t;
        repeat (6) begin
          #15;  // Yavas tuketim
          mbx_bounded.get(t);
          $display("  [%0t] Consumer: alindi ->", $time);
          t.display();
        end
      end
    join

    $display("  [%0t] Producer-Consumer tamamlandi", $time);
  end

  // --- Sinirsiz Mailbox ile coklu tuketici ---
  initial begin
    #250;
    $display("\n--- Coklu Tuketici ---");
    mbx_unbounded = new();

    fork
      // Uretici
      begin
        for (int i = 0; i < 10; i++) begin
          Transaction t = new(i, i*100, "Gen");
          mbx_unbounded.put(t);
          #3;
        end
      end

      // Tuketici 1
      begin
        Transaction t;
        repeat (5) begin
          mbx_unbounded.get(t);
          $display("  [%0t] Tuketici-1: TXN#%0d (data=%0d)", $time, t.id, t.data);
          #7;
        end
      end

      // Tuketici 2
      begin
        Transaction t;
        repeat (5) begin
          mbx_unbounded.get(t);
          $display("  [%0t] Tuketici-2: TXN#%0d (data=%0d)", $time, t.id, t.data);
          #10;
        end
      end
    join

    $display("  [%0t] Coklu tuketici tamamlandi", $time);
    #10;
    $display("\n=== Mailbox Sonu ===");
    $finish;
  end
endmodule