3 ms·
Edit: Rust 1.62 uses futexes directly; ParkingLot is a thing but not backing mutex on Linux. Thanks to ibraheemdev for the correction. I'm working to understan
by ridiculous_fish 4y ago
Edit: Rust 1.62 uses futexes directly; ParkingLot is a thing but not backing mutex on Linux. Thanks to ibraheemdev for the correction.
I'm working to understand how Rust 1.62 avoid allocations for futex-based locks. I think the summary is this:
1. A mutex is a u8. The mutex fast path is locking via atomic ops to set a bit.
2. Contention is resolved by adding the blocked thread into a wait queue. The queue is allocated/discovered via a global hash table, keyed by the address of the mutex. The mutex keeps a stable address while locked, as the lock holds shared ownership and so the mutex cannot be moved for the duration.
I ported a POSIX semaphore wrapper from C++ to Rust, and needed an extra allocation for the same reason, to get an int with a stable address. Unfortunately this technique won't work for semaphores, they need to be async-signal safe. So I'm still stuck with `Pin<Box<UnsafeCell<sem_t>>>` where in C++ it's just `sem_t` and a deleted move constructor.
Anyways this is a lot of hoops to jump through to avoid non-movable types, hope it's worth it!
- cwzwarich 4y ago> 2. Contention is resolved by adding the blocked thread into a wait queue. The queue is allocated/discovered via a global hash table, keyed by the address of the mutex. The mutex keeps a stable address while locked, as the lock holds shared ownership and so the mutex cannot be moved for the duration. Wouldn't this break any platform-specific priority/importance donation mechanisms, or does Linux have a way to work around this?
- ridiculous_fish 4y agoActually upon reflection why wasn't this just trivial for futex? A Linux futex requires only user-space initialization, and it has a stable address while locked for the same reason, so you can get the same zero-allocations without the use of parking lot.
- ibraheemdev 4y agoIt uses the linux futex api directly, not a user space parking lot as used by the parking-lot crate.