Pre-RFC: Pinned synchronization primitives


Create a new module std::sync::pinned for OS synchronization primitives which make use of the Pin dialect instead of relying on boxing.


The current synchronization primitives at std::sync box the OS primitives for some OSes, including all Posix-compliant. This is suboptimal as it increases the possibility of cache misses, and adds overhead to creation of these primitives.

With the new Pin type, we can expose the primitives directly and have the functions simply take self: Pin<&Self>.

This gives more flexibility as it is possible to have, for example, Arc<Mutex<T>> without double-boxing the Mutex.

Guide-level explanation

For the following types:

  • Barrier

  • Condvar

  • Mutex<T>

  • RwLock<T>

Pinned versions are available at the module std::sync::pinned. These versions are all !Unpin.

In all methods of these pinned versions a self: Pin<&Self> argument is taken, therefore the type must be constructed with, for example, Arc::pin or Box::pin.

In order to initialize a pinned::Mutex, for example, one of these can be used:

// Directly initialize using ergonomic constructors
let boxed_mutex: Pin<Box<Mutex<T>>> = Mutex::boxed(...);
let arc_mutex: Pin<Arc<Mutex<T>>> = Mutex::arc(...);

// Explicitly initialize after creation with a custom constructor
// Nothing here is unsafe, the library checks for initialization
let mut mutex: Pin<Box<Mutex<T>>> = Box::pin(Mutex::uninit(...));

Reference-level explanation

The std sys_common already has a pretty good infrastructure for implementing this.

  • The explicit initialization is done by first creating the structure using uninit(value: T) -> Self, then calling init(self: Pin<&mut Self>).
  • The library adds the necessary assertions to make this pattern safe. Using before init causes a panic!. In particular, the implementation can use an Option to wrap the OS primitive. The assertions can be placed such that they are inlined, so if one method is used after another, the assertion can be optimized away for the second method.


  • Adds further complexity to the std library.

Rationale and alternatives

  • Alternative for the explicit initialization: have unsafe uninit(). This can remove assertion overhead but can lead to some hard to detect undefined behaviour, as in POSIX - for example - this would cause mutexes to work as normal until a double lock happens, which would cause undefined behaviour.
  • Replace the primitives with parking_lot. This has been proposed in the past and has its fair share of drawbacks, such as non-trivial space overhead which can be a deal breaker for some applications.

Prior art

Unresolved questions

  • Should Once be moved to pinned for consistency? The current implementation does not box any primitives, however if we were to use pthread_once_t in the future, it would be necessary.

Future possibilities

  • Lints for double boxing (suggesting Pin<Arc<pinned::Mutex<T>>> when using Arc<sync::Mutex<T>>).

How do you intend for Mutex::new(T) to work, or is the intent to only offer Mutex::new_boxed(T), Mutex::new_arc(T), etc?

FWIW, as additional prior art you may be interested in the sync package we've created for the Linux kernel: linux/ at rust · Rust-for-Linux/linux · GitHub All the locks use Pin, but we haven't found a better solution than making new() unsafe and requiring you to init once you have the lock in the pinned location.

It could use PTHREAD_MUTEX_INITIALIZER, which is just a constant bit pattern and thus necessarily safe to move around before any operations are performed on it.

If/when parking_lot becomes the basis for the synchronization primitives, it will be unnecessary to implement this proposal as parking_lot natively allows moving of all it's types.

Other possible alternatives:

  • Lobby OS vendors (somehow) to guarantee that these primitives are safe to move when not in use (which is of course the only circumstance where Rust would allow moving). After all, this is almost always true in practice. A mutex may have pointers to it in some global data structure while it has waiters, or even when it's locked without waiters, but usually not when it's just sitting there unlocked. As far as I can tell, Windows already guarantees the relevant primitives (or at least some of them) are save to move, e.g. SRWLOCK.

  • Have std switch away from OS native primitives entirely in favor of parking_lot primitives, which don't have this problem and are often more efficient in other ways. This has been proposed and partially implemented before, but has stalled.

    Though, parking_lot primitives have disadvantages such as not supporting priority inheritance.

We discussed exactly this interface in Pin + Arc + Mutex - #5 by burdges - The Rust Programming Language Forum It could be implemented outside std using nightly with #![feature(libstd_sys_internals)] btw.

We cannot re-implement the old types as Pin<Box<T>> with T being new types. There exist platforms where Mutex involves no allocation. See - source and - source

I've no idea why RwLock does not benefit from this optimization yet - source and maybe Condvar does so. I'd think Barrier should remain implemented in terms of these so that it benefits.

I'd assume the system primitives should always be exposed, even if parking_lot becomes the default. I like the std::sync::pinned module name, maybe the current primitives should live in std::sync::sys, parking_lot should become core::sync::spin, and core::sync::.. should be type aliases, for which the default can later be changed.

Thanks for all the responses!

PTHREAD_MUTEX_INITIALIZER sadly can not be used, as locking it twice causes undefined behaviour.

I really forgot about this when writing. I will suggest having a Mutex::uninit constructor and initializing it using init(self: Pin<&mut Self>). Using an uninitialized Mutex should not cause undefined behaviour, just panic. Hopefully on sequential operations the checks for initialization can be optimized away after the first check.

My understanding is that parking_lot has been rejected for standard library because it has non-trivial overhead memory-wise (the static hashmap). Also linking directly to system libraries has some benefits: it the system is updated to use more efficient syscalls, the program can benefit from it without needing to be rebuilt and check for support to the new features of the system.

That said, if this ever becomes the case, the new module could simply be deprecated, or just delegate to the sync types.

(editted) I just missed the:

An unlocked SRW lock with no waiting threads is in its initial state and can be copied, moved, and forgotten

I'm pretty sure this is new and was not documented before, and that's why the std did not use this in the past. I could be wrong tho.

Coincidentally a PR with that optimization of RwLock was submitted yesterday: #84687

std uses the fact that SRW locks can be moved: on Windows the movable mutex is not boxed:

1 Like

My mistake, I've corrected the post