Everyone Wants the Sensors
Stardate 55155.5 ยท UES Magnanimous
Briefing
Welcome to Engineering. Six science teams have queued for the long-range array, and each request runs at the same time as the others. Two teams have received identical timeslots. Lt. Skree reports this is "arithmetically impossible, and yet". Commander Raghunathan has stopped sighing, which everybody agrees is the bad sign.
The scheduler looks at the first free slot, then goes off to talk to the array, then
takes the slot. Between looking and taking, it lets go, and another
request looks at the same slot. That gap is the whole bug. In Python the gap is an
await. In Rust it is the moment between two separate locks, and the fix is
to hold one lock across the choice and the take. Ensign Tannenbaum wrote the gap. He
says it "seemed polite".
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขEvery team receives a slot
- โขNo slot is issued twice
- โขWhen teams equal slots, every slot is used and none is left free
- โขA seventh team, with six slots, gets None instead of a crash
- โขThe requests can still run concurrently
Objective
Fix Scheduler.request so that concurrent requests never receive the same
slot. Scheduler(slots) holds free slots; await
scheduler.request(team) gives that team a free slot (recording it in
assigned) or None if none remain. It must stay an
async method: the requests are gathered concurrently.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Fix allocate so that concurrent threads never take the same slot.
allocate(teams, slots) spawns one thread per team; each takes a slot from
the shared pool, or None if the pool is empty. It returns
(team, slot) pairs in team order. The threads and the shared
Arc<Mutex<..>> must stay: the point is to make them correct.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Python: take the slot before the await, in one step (pop), so nothing can slip in between. Rust: lock once, choose and remove inside that single lock, then let go.
Debrief
Six teams, six slots, no repeats, and the seventh team gets a polite nothing instead of a crash. Commander Raghunathan has resumed sighing, which is a relief to everyone. Lt. Skree has the array for the next hour and has pointed it at the other ship.