DOCUMENTATION / V1.0.0 LTS
OrbitOne v0.1.13 — Preemptive multitasking
What changed
Until this release, OrbitOne had a single thread of execution. The main loop drove every subsystem in turn, and any command that took a long time froze the shell: there was nothing else for the processor to run.
The kernel now schedules real threads. Each one has its own 16 KiB stack, and the timer interrupt takes the processor away from a thread that has used up its quantum, whether or not that thread cooperates.
This is the single change that unblocks everything else on the roadmap. User mode, a block device driver and a network stack all assume something can run while something else waits.
How to see it
task # list the threads
task stats # switches, preemptions, yields
task spawn 3 # a thread that prints, sleeps 500 ms, repeats
task burn 300 # a thread that spins for 3 seconds and never yields
Run task burn 300, then keep typing. The shell answers
while the thread spins. That is preemption, observed directly. Then
task stats shows the preemption count that made it
possible.
task quantum 0 disables preemption. Run
task burn 300 again: the shell goes silent for three
seconds. Setting the quantum back to 5 restores it. The two
behaviours differ by one integer, which is the shortest available proof
that the mechanism is real.
Design
The implementation is split along a line that decides how much of it can be tested without hardware.
| Module | Responsibility | Tested |
|---|---|---|
src/sched.rs |
Which thread runs next, and the accounting. No stacks, no hardware, no allocation. | 33 host unit tests |
src/sync.rs |
Preemption counter and SpinLock. |
8 host unit tests |
src/arch/x86_64/context.rs |
The context switch, in naked assembly. | QEMU integration |
src/task.rs |
Stacks, spawn/yield/sleep/exit/reap, timer entry point. | QEMU integration |
Two invariants hold the whole thing together, and both are documented at their enforcement point rather than in this file:
- The scheduler lock is only ever taken with interrupts masked. The timer handler takes it too; if it could interrupt a holder, it would spin forever.
- The switch happens with the lock released and interrupts still masked.
Locking
Introducing preemption made every lock taken outside a critical section dangerous. Thread A takes a lock, gets preempted, thread B spins on the same lock and never yields; A never runs again.
Masking interrupts around every lock would fix it and would also
suspend the clock and the keyboard during, say, a directory walk.
Instead, holding a SpinLock increments a counter that the
timer handler reads. A refused preemption is not lost: it is recorded as
deferred and taken at the first tick after the lock is
released.
Only four subsystems needed the change — /tmp, the
ramdisk, the boot journal and the paging tables. They are the only locks
reachable from more than one thread with interrupts enabled. The rest
(serial, the allocator, the PIC, the consoles) are taken
under critical_section, and masking the timer interrupt
already forbids preemption.
Testing
scripts/test_scheduler.sh boots the kernel in QEMU and
asserts the boot self-test, the create/sleep/wake/exit cycle, a non-zero
preemption count under non-cooperative load, stack reclamation, and the
absence of any fault or reboot loop.
The assertions were counter-proved: forcing the default quantum to
zero makes the preemption assertion fail while the others still pass, so
the test is not vacuous. The structural check on
preemption_allowed was counter-proved the same way, by
renaming the symbol and confirming the check fails.
Host unit test floor raised from 145 to 186.
What this release does not do
Every thread runs in ring 0, in a single address space. There is no user mode, no system call boundary, no memory protection between threads: one of them writing through a bad pointer corrupts the others. There is no block device and no network. There is one core.
Ring 3 plus a system call entry point is the next lock to pick.