Before writing any implementation, write down what must always be true about the system. These are your invariants. Then build the implementation to enforce them — not to hope they hold.
Cerberus-OS invariants (explicit, written before first line of code):
- The highest-priority runnable task is always executing
- A task in U-mode can never access memory outside its PMP regions
- A mutex holder's priority is always >= any waiter's priority
- The ready bitmap and task state are always consistent
- Interrupt handlers never allocate
Each invariant maps to an enforcement mechanism:
- (1) → O(1) scheduler with priority bitmap
- (2) → PMP configuration on every context switch
- (3) → Priority inheritance mutex implementation
- (4) → Atomic update of bitmap and state together
- (5) →
#[no_alloc]lint (planned, via SafetyMacro)
When the borrow checker rejects your implementation, the question is: which invariant does this design violate? In all four redesigns of the priority inheritance mutex, the borrow checker was pointing at a real inconsistency in the invariants — not a language limitation to work around.