Home /Modules/cpu/SLGuestPipe.sleela

SLGuestPipe.sleela

lib/cpu/SLGuestPipe.sleela · 113 line(s) · sleela

<< lib/cpu/SLGuestPipe.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLGuestPipe — a unidirectional byte FIFO between guest
     tasks, the classic Unix pipe. One task writes bytes to the write end;
     another reads them from the read end in order. A read on an empty pipe
     signals "would block" (the kernel can block the reader); a write to a full
     pipe signals back-pressure. This is the first inter-process communication
     primitive for the guest OS.
   
   Backed by a ring buffer over SLRAM so the capacity is configurable. The guest
   syscall layer exposes pipe()/read()/write()/close() over these.
   
   Family: cpu. Composes SLRAM. >>
#sleela 2.5
material;
class SLGuestPipe {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 10235;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-06";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  SLRAM buffer;
  int capacity;
  < read index >
  int head;
  < write index >
  int tail;
  < bytes currently buffered >
  int size;
  int readerClosed;
  int writerClosed;
  int totalWritten;
  int totalRead;

  void configure(int cap) {
    buffer = new SLRAM(); buffer.configure(cap, 32); buffer.allocate();
    capacity = cap; head = 0; tail = 0; size = 0;
    readerClosed = 0; writerClosed = 0; totalWritten = 0; totalRead = 0;
  }

  boolean empty() { return size == 0; }
  boolean full() { return size == capacity; }

  < Write one byte; returns 1 on success, 0 if full (back-pressure), -1 if the >
  < read end is closed (broken pipe). >
  int writeByte(int value) {
    if (readerClosed == 1) { return -1; }
    if (full()) { return 0; }
    buffer.writeWord(tail, value);
    tail = (tail + 1) % capacity;
    size = size + 1;
    totalWritten = totalWritten + 1;
    return 1;
  }

  < Read one byte; returns the byte (>=0), -1 at end-of-stream (writer closed >
  < and empty), or -2 if empty but still open ("would block"). >
  int readByte() {
    if (empty()) {
      if (writerClosed == 1) { return -1; }
      return -2;
    }
    int v = buffer.readWord(head);
    head = (head + 1) % capacity;
    size = size - 1;
    totalRead = totalRead + 1;
    return v;
  }

  void closeReader() { readerClosed = 1; }
  void closeWriter() { writerClosed = 1; }
  boolean readable() { return size > 0; }
  int available() { return size; }
  int written() { return totalWritten; }
  int read() { return totalRead; }
}