One of the common complaints I hear from systems programmers who try Rust is
about mutexes, and specifically about the Rust Mutex API. The complaints
usually go something like this:
They don’t want the mutex to contain data, just a lock.
They don’t want to have to manage a “guard” value that unlocks the mutex on
drop – often, more specifically, they just want to call an unlock operation
because they feel like that’s more explicit.
These changes would make the Rust mutex API equivalent to the C/Posix mutex API.
In one case I’ve seen someone try to use Mutex<()> and trickery to fake it.
There’s a problem with this, though: these two aspects of Mutex’s design are
inextricably linked to one another, and to Rust’s broader safety guarantees –
changing either or both of them will open the door to subtle bugs and
corruption due to data races.
A C-style mutex API consisting of some bundle of implicitly guarded data, plus
lock and unlock functions, isn’t wise in Rust because it allows safe code to
easily commit errors that break memory safety and create data races.
Perhaps controversially, I’d argue that this is also true in C. It’s just more
obvious in Rust, because Rust rigorously distinguishes between the notion of
“safe” code that cannot commit such errors, and “unsafe” code that can commit
such errors if it wishes. C does not make this distinction, and as a result, any
code using a mutex in C can trivially produce serious, potentially exploitable,
bugs.
In the rest of this post I’ll walk through a typical C mutex API, compare with a
typical Rust mutex API, and look at what happens if we change the Rust API to
resemble C in various ways.
At some point in the past… I dunno, two years or so, it appears that my RSS
feeds broke.
I use Zola to generate this site, and they don’t have much in the way of a
cross-version compatibility guarantee – minor version updates routinely break
my templates. (I’m currently stuck on an older version because of this
bug.) They appear to have changed
the names of the RSS-related settings, causing my detection for generate_rss
to always return false (because they also seem to default any typo’d
configuration key to false). Whee.
Anyway, should be back on now – thanks to all the folks who have asked about
this.
Last week I gave a talk at the Open Source Firmware Conference about some of the
work I’m doing at Oxide Computer, entitled On Hubris and Humility. There is a
video of the talk if you’d like to
watch it in video form. It came out pretty alright!
The conference version of the talk has a constantly animated background that
makes the video hard for some people to watch. OSFC doesn’t appear to be
bothering with either captions or transcripts, so my friends who don’t hear as
well as I do (or just don’t want to turn their speakers on!) are kind of out of
luck.
And so, here’s a transcript with my slides inlined. The words may not exactly
match the audio because this is written from my speaker’s notes. And, yes, my
slides are all character art. The browser rendering is imperfect.
I’ve also written an epilogue at the end after the initial response to the talk.
(I’ve updated this pattern, since a lot has changed since 2020. The
recommendations here should be ready for the Rust 2024 edition, and are closer
to correct in a post-pointer-provenance world.)
Here’s another useful Rust pattern. Like the Typestate Pattern
before it, I wrote this because I haven’t seen the sort of obsessively nerdy
writeup that I wanted to read. And, as with the Typestate Pattern, I didn’t
invent this — I’m merely documenting and generalizing it.
Since it looks like some folks have been actually reading my blog, I’ve made a
pass over the site, looking for accessibility problems. I have increased visual
contrast and made links within articles slightly more obvious. The comments in
code samples are still under the WCAG recommended constrast level, but they’re
generated by a third party syntax highlighting library, so fixing them is more
involved.
Please let me know if you have any difficulty using the site!