Remix.run Logo
tialaramex 3 days ago

Note that the diagram of a Mutex is not how a Mutex works today, at least on reasonable platforms.

First, Mara changed it to be something much less silly, on Linux and similar it's a Futex, while on Windows it was an SRWLock. However, more recently (last year IIRC) the Windows Mutex is also basically a Futex, albeit Microsoft doesn't call their analogous feature a Futex.

In either case this futex-based design means there's no "inner" pointer, and nothing for it to point to, instead there's some sort of atomic integer type, when we're contended we go to sleep waiting on the integer via futex or similar OS feature.

Edited to add:: Also, the niche trick in the bottom right is somewhat broader than this suggests. If the type doesn't need every bit pattern then Rust might and in some cases is guaranteed to see a niche it can use for this memory optimisation.

Option<&T> is the same size as &T but also Option<NonZeroU16> is the same size as u16, Option<OwnedFd> is the same size as OwnedFd (and thus same size as a C integer by definition), Option<Ordering> is the same size as the Ordering (either of them) and Option<bool> is of course the same size as a bool.