<< lib/cpu/SLGuestScheduler.sleela SLeeLa Standard Library Definition. Definition: Defines SLGuestScheduler — the GUEST kernel's task scheduler, driven by the guest timer device. It holds the guest's runnable tasks and, on each timer IRQ (the preemption source), rotates the CPU to the next ready task (round-robin). This is what gives the guest OS preemptive multitasking on its own virtual hardware: the SLGuestTimer heartbeat is the scheduler tick. Up to 8 guest tasks (SLeeLa surface has no arrays). The scheduler is a guest- side analogue of the host SLScheduler, but it runs inside the VM and is clocked by the guest timer rather than the host timer. Family: cpu. Composes SLGuestTask; clocked by SLGuestTimer via the device bus. >> #sleela 2.5 material; class SLGuestScheduler { < ---- 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 = 10237; String metaAuthor = "Kiro Agent"; String metaDate = "2026-10-16"; String metaCopyright = "SLeeLa project"; String metaTrademark = "none"; String metaCorporation = "none"; < metaOrigin: National | | 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 = 8; SLGuestTask t0; SLGuestTask t1; SLGuestTask t2; SLGuestTask t3; SLGuestTask t4; SLGuestTask t5; SLGuestTask t6; SLGuestTask t7; int count; int current; < timer ticks per slice > int quantum; < ticks since the last switch > int tickAccum; int switches; void configure(int timerTicksPerSlice) { count = 0; current = -1; quantum = timerTicksPerSlice; tickAccum = 0; switches = 0; } < Place a task into a specific slot (no bookkeeping beyond the slot write). > void putAt(int idx, SLGuestTask t) { if (idx == 0) { t0 = t; } if (idx == 1) { t1 = t; } if (idx == 2) { t2 = t; } if (idx == 3) { t3 = t; } if (idx == 4) { t4 = t; } if (idx == 5) { t5 = t; } if (idx == 6) { t6 = t; } if (idx == 7) { t7 = t; } } < Find a slot occupied by a reaped-and-DONE task (a zombie a parent has > < already collected) that can be recycled for a new task, or -1 if none. > < The scheduler's current running slot is never recycled, so reusing a slot > < can't leave `current` pointing at a fresh task that never went RUNNING. > int freeReapedSlot() { int i = 0; while (i < count) { SLGuestTask t = at(i); if (i != current && t != null && t.taskState() == t.DONE && t.reapedYet()) { return i; } i = i + 1; } return -1; } int admit(SLGuestTask t) { int idx = count; if (idx >= MAX) { < Table full: recycle a slot whose task was reaped (its TCB is free). > idx = freeReapedSlot(); < genuinely out of slots > if (idx < 0) { return -1; } putAt(idx, t); t.markReady(); < reused slot: count is unchanged > return idx; } putAt(idx, t); t.markReady(); count = count + 1; return idx; } SLGuestTask at(int idx) { if (idx == 0) { return t0; } if (idx == 1) { return t1; } if (idx == 2) { return t2; } if (idx == 3) { return t3; } if (idx == 4) { return t4; } if (idx == 5) { return t5; } if (idx == 6) { return t6; } if (idx == 7) { return t7; } return null; } < Find the scheduler slot holding the task with the given tid, or -1. > int slotOfTid(int tid) { int i = 0; while (i < count) { SLGuestTask t = at(i); if (t != null && t.taskId() == tid) { return i; } i = i + 1; } return -1; } < fork(): clone a running parent task into a fresh ready task that resumes at > < the SAME guest instruction pointer as the parent (classic Unix fork — both > < parent and child return from the syscall at the same point, distinguished > < only by the return value the kernel pushes to each). Because the guest > < substrate is a shared single code store, parent and child share text and > < resume IP; the child gets its own TCB, tid, and scheduling slot. Returns the > < admitted child task, or null if the task table is full. > SLGuestTask fork(SLGuestTask parent, int childTid) { if (count >= MAX) { return null; } SLGuestTask child = new SLGuestTask(); child.configure(childTid, parent.taskName(), parent.resumeIP()); child.setParent(parent.taskId()); < child resumes where the parent forked > child.saveIP(parent.resumeIP()); admit(child); return child; } < wait()-style reap: find a finished-but-unreaped (zombie) child of the given > < parent tid, reap it, and return it so the kernel can hand the parent the > < child's tid + exit status. Matches any zombie whose parent is the given tid > < (models wait(NULL) — reap whichever child is ready). Returns null if the > < parent has no reapable child yet (the parent would block / retry). > SLGuestTask reapChild(int parentTid) { int i = 0; while (i < count) { SLGuestTask t = at(i); if (t != null && t.parent() == parentTid && t.zombie()) { t.reap(); return t; } i = i + 1; } return null; } < True if the given parent still has at least one live (not-yet-DONE) child — > < i.e. a wait() could still eventually succeed. > boolean hasLiveChild(int parentTid) { int i = 0; while (i < count) { SLGuestTask t = at(i); if (t != null && t.parent() == parentTid && t.alive()) { return true; } i = i + 1; } return false; } int pickNext() { int tries = 0; int idx = current; while (tries < count) { idx = (idx + 1) % count; SLGuestTask t = at(idx); if (t != null && t.runnable()) { return idx; } tries = tries + 1; } return -1; } < Called on each guest timer IRQ. Charge the running task; when its quantum > < expires, preempt it and switch to the next ready task. Returns the task now > < selected to run (or null if none). > SLGuestTask onTimerTick() { tickAccum = tickAccum + 1; if (current >= 0) { SLGuestTask run = at(current); if (run != null) { run.chargeTick(); } } if (tickAccum < quantum && current >= 0) { SLGuestTask cur = at(current); < keep running > if (cur != null && cur.runnable()) { return cur; } } < Quantum expired (or nothing running): preempt and rotate. > tickAccum = 0; if (current >= 0) { SLGuestTask prev = at(current); if (prev != null && prev.taskState() == prev.RUNNING) { prev.markReady(); } } int next = pickNext(); if (next < 0) { return null; } current = next; SLGuestTask sel = at(next); sel.markRunning(); switches = switches + 1; return sel; } < Force the running slot to a specific task (used to model a deterministic > < context switch between a parent and its fork()'d child when demonstrating > < the fork/exec/wait cycle outside the timer-driven path). Marks the selected > < task RUNNING and the previously-running task READY. > void markRunningSlot(int slot) { if (slot < 0 || slot >= count) { return; } if (current >= 0 && current != slot) { SLGuestTask prev = at(current); if (prev != null && prev.taskState() == prev.RUNNING) { prev.markReady(); } } current = slot; SLGuestTask sel = at(slot); if (sel != null && sel.alive()) { sel.markRunning(); } } int taskCount() { return count; } int contextSwitches() { return switches; } int quantumTicks() { return quantum; } int currentSlot() { return current; } SLGuestTask running() { if (current < 0) { return null; } return at(current); } }