Home /Modules/cpu/SLScheduler.sleela

SLScheduler.sleela

lib/cpu/SLScheduler.sleela · 121 line(s) · sleela

<< lib/cpu/SLScheduler.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLScheduler — the OS component that decides which ready
     process runs next and for how long. This models a round-robin scheduler
     with a fixed time quantum: the timer interrupt preempts the running process
     at the end of its quantum and the scheduler rotates to the next ready one.
   
   Holds up to 8 processes (SLeeLa surface has no arrays); enough to demonstrate
   multiprogramming on the modeled machine.
   
   Family: cpu. Composes SLProcess. >>
#sleela 2.5
material;
class SLScheduler {
  < ---- 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 = 10278;
  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; }
  static final int MAX_PROC = 8;
  SLProcess p0; SLProcess p1; SLProcess p2; SLProcess p3;
  SLProcess p4; SLProcess p5; SLProcess p6; SLProcess p7;
  int count;
  < index of the running process >
  int current;
  < ticks per time slice >
  int quantum;
  int switches;

  void configure(int timeQuantum) { count = 0; current = -1; quantum = timeQuantum; switches = 0; }

  int admit(SLProcess p) {
    int idx = count;
    if (idx >= MAX_PROC) { return -1; }
    if (idx == 0) { p0 = p; } if (idx == 1) { p1 = p; }
    if (idx == 2) { p2 = p; } if (idx == 3) { p3 = p; }
    if (idx == 4) { p4 = p; } if (idx == 5) { p5 = p; }
    if (idx == 6) { p6 = p; } if (idx == 7) { p7 = p; }
    p.markReady();
    count = count + 1;
    return idx;
  }

  SLProcess at(int idx) {
    if (idx == 0) { return p0; } if (idx == 1) { return p1; }
    if (idx == 2) { return p2; } if (idx == 3) { return p3; }
    if (idx == 4) { return p4; } if (idx == 5) { return p5; }
    if (idx == 6) { return p6; } if (idx == 7) { return p7; }
    return null;
  }

  < Pick the next runnable process after `current`, round-robin. >
  int pickNext() {
    int tries = 0;
    int idx = current;
    while (tries < count) {
      idx = (idx + 1) % count;
      SLProcess p = at(idx);
      if (p != null && p.runnable()) { return idx; }
      tries = tries + 1;
    }
    return -1;
  }

  < Preempt the current process and dispatch the next onto the given core. >
  SLProcess schedule(SLCore core) {
    if (count == 0) { return null; }
    if (current >= 0) { SLProcess running = at(current); if (running != null && running.alive()) { running.saveContext(core); if (running.processState() == running.RUNNING) { running.markReady(); } } }
    int next = pickNext();
    if (next < 0) { return null; }
    current = next;
    SLProcess sel = at(next);
    sel.markRunning();
    sel.restoreContext(core);
    sel.chargeQuantum();
    switches = switches + 1;
    return sel;
  }

  int quantumTicks() { return quantum; }
  int processCount() { return count; }
  int contextSwitches() { return switches; }
  SLProcess running() { if (current < 0) { return null; } return at(current); }
}