Skip to content

OSlings Practice 37k-38k, Problem 1: Spinlocks

OSlings Practice 37k-38k, problem 1 of 3 · Length: 7:33 · Exercise: 37k_spinlocks (Week 10) · Problems: 37k-38k (PDF) · Answer key: Solutions (PDF)

Three parts, worked on the set's pages 1 and 2: (a) a try_lock that checks the lock with a load and claims it with a separate store: an order of the two steps on two CPUs in which both get a guard, what goes wrong once both hold one, and why compare_exchange fixes it; (b) 34k's NEXTPID moved into a SpinLock, with an alloc_pid that locks it twice: what happens on the first call, a fix, and why a one-statement update works; (c) the guard's DerefMut: which field of SpinLock lets a lock(&self) guard write through a shared reference, and why that is safe. The answer key's handwriting is written onto the page as each step is worked.

Download the video (MP4, 12 MB) · Captions (WebVTT) · All three problems