WEBVTT

00:00.000 --> 00:04.915
Week 10: The Guard Holds It, for exercise 37k_spinlocks.

00:05.515 --> 00:14.567
A script steps this explainer, it was built with Claude, and its numbers come from the given test and the reference kernel's build, not from the lecture.

00:15.080 --> 00:17.478
After the October 27 lecture:

00:18.078 --> 00:24.673
on Thursday, October 29, you write lock and try_lock, the two calls that hand out a guard.

00:25.273 --> 00:28.833
No solution code here: only what the machine holds.

00:29.360 --> 00:33.124
COUNTER: data at +0, the lock word at +8.

00:33.724 --> 00:41.830
A guard, g, is the only way in. It names the lock; through Deref and DerefMut, star g reaches the data.

00:42.430 --> 00:45.905
Its Drop calls unlock: the word reads false again.

00:46.440 --> 00:53.969
Up top is the test, run_checks, with a band on the line each step is at; beside it, its checks, in order.

00:54.569 --> 00:57.830
Below, COUNTER's two words, and the guards on the stack.

00:58.360 --> 01:02.509
No guard is alive, and the word reads false: the lock is free.

01:03.509 --> 01:11.820
The first try, a call to try_lock, leaves the word true, a claim on the lock. It answers a guard, which g holds.

01:12.420 --> 01:13.763
It names COUNTER.

01:15.280 --> 01:19.395
g holds a guard, so the check at line 57 passes.

01:20.395 --> 01:24.905
While g is alive, the word reads true: line 62 passes.

01:26.400 --> 01:32.532
Check 2: a second try, while g is alive. It finds the word true and answers no guard.

01:33.132 --> 01:40.777
Its answer is a temporary, with no name, made in the condition. It holds no guard: line 67 passes.

01:42.280 --> 01:47.212
drop(g) ends g, and its Drop calls unlock: the word reads false.

01:48.212 --> 01:51.527
With no guard alive, line 72 passes.

01:52.040 --> 01:53.490
The word reads false.

01:54.490 --> 02:03.090
Check 3 calls lock(): it leaves the word true and answers a guard. held, a local of the block, keeps it until the block ends.

02:04.090 --> 02:07.107
The write goes through held: the data reads 1.

02:08.640 --> 02:13.124
While held is alive, the word reads true: line 81 passes.

02:14.124 --> 02:18.175
A try now answers no guard: line 85 passes.

02:19.680 --> 02:26.633
The block ends at its closing brace. held drops, and its Drop calls unlock: the word reads false.

02:27.233 --> 02:30.187
Three checks have passed, and no guard is alive.

02:31.720 --> 02:39.068
The fourth check is in words only: many locked updates, each through a temporary guard alive for one statement.

02:40.068 --> 02:44.931
Every check passes. The test prints its [ok] line, then OSLINGS:PASS.

02:45.440 --> 02:49.709
Now the skeleton: the lock as you receive it, run through this test.

02:50.309 --> 02:51.877
Which check fails first?

02:53.377 --> 02:55.210
Watch the word and the guards.

02:56.210 --> 02:59.697
The first try answers no guard, though the lock is free.

03:01.200 --> 03:06.692
g holds no guard, so the check at line 57 fails, and the test stops.

03:07.292 --> 03:11.035
The test prints: [fail] try_lock failed on a free lock.

03:11.635 --> 03:14.151
Expect this line before you write anything.

03:14.680 --> 03:17.228
A broken case: claims without asking.

03:17.828 --> 03:19.779
Check 1 passes, as before.

03:20.779 --> 03:26.389
At check 2 the second try answers a guard too: two guards are alive on one lock.

03:27.920 --> 03:30.606
As the condition is asked, check 2 fails.

03:32.106 --> 03:38.743
Then the temporary drops, and its Drop calls unlock: the word reads false, though g is still alive.

03:39.343 --> 03:43.897
The test prints: [fail] try_lock succeeded on an already-held lock.

03:44.400 --> 03:46.681
One more case: a try that waits.

03:47.681 --> 03:51.359
The second try, at line 67, has not answered.

03:52.359 --> 03:53.754
It never answers.

03:54.754 --> 03:57.016
The test prints its banner, then nothing.

03:57.520 --> 04:07.468
In a correct run, this test drops its guards in three places: drop(g), the end of the block, and, in the fourth check, the end of each update's statement.

04:08.068 --> 04:10.349
Each drop is one call to unlock.

04:10.880 --> 04:14.368
In spinlock.rs you write lock and try_lock.

04:14.968 --> 04:20.092
When lock returns, the word reads true, and its guard is the only one alive.

04:20.692 --> 04:26.397
try_lock answers at once: a guard when the lock was free, no guard when it was held.

04:26.920 --> 04:33.033
Each row is a check and its line, with what the test prints if it fails, and the picture to look at.

04:33.633 --> 04:40.375
If it prints [fail] lock not marked held while a guard is alive, look at the case guard without the claim.

04:40.880 --> 04:53.533
A try that waits has no fail line: after the timeout, oslings, the course's test runner, prints a line that begins: QEMU timed out without printing OSLINGS:PASS.

04:54.040 --> 05:04.219
What the test cannot see: on one hart, RISC-V's word for a hardware thread, run_checks is the only flow, the only code, that ever takes the lock.

05:04.819 --> 05:08.864
A lock that waits and a lock that tries once look the same to it.

05:09.360 --> 05:17.325
A guard alive is the lock held: the word reads true exactly while one lives, and every drop is one call to unlock.

05:17.925 --> 05:20.768
Next: the explainer Two Counts on One Page.

05:21.368 --> 05:24.388
Then Thursday's sessions, October 29.
