Skip to content

Instantly share code, notes, and snippets.

@tobz
Created May 30, 2024 16:29
Show Gist options
  • Save tobz/35086ba305644862f20fb7da7d5005bd to your computer and use it in GitHub Desktop.
Save tobz/35086ba305644862f20fb7da7d5005bd to your computer and use it in GitHub Desktop.
loom::model(|| {
// These lines are purely to set up the necessary data structures:
let interner = FixedSizeInterner::new(NonZeroUsize::new(1024).unwrap());
let t1_interned_s = interner.try_intern(STRING_TO_INTERN).expect("should not fail to intern");
assert_eq!(t1_interned_s.deref(), STRING_TO_INTERN);
// Now we spawn our second thread, meant to run concurrently with our "first" thread, which is the code
// that comes _after_ the call to `loom::thread::spawn`:
let t2_result = loom::thread::spawn(move || {
interner.try_intern(STRING_TO_INTERN).expect("should not fail to intern")
});
// The drop logic for the type held by `t1_interned_s` uses atomic operations/a mutex to synchronize access
// to the inner state of `FixedSizeInterner`, and is the primary portion of logic meant to be tested
// concurrently with the call to `FixedSizeInterner::try_intern`:
drop(t1_interned_s);
// Below here would be some assertion code to check that the state of the interner, and the interned strings,
// matches what we expect and that nothing is invalid:
...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment