14 releases

0.1.13 Oct 10, 2023
0.1.11 Aug 14, 2023
0.1.10 Apr 24, 2023
0.1.9 Jan 3, 2023
0.1.0 Jan 3, 2017

#40 in Concurrency

Download history 29568/week @ 2024-08-08 27652/week @ 2024-08-15 30277/week @ 2024-08-22 29423/week @ 2024-08-29 32943/week @ 2024-09-05 27993/week @ 2024-09-12 28162/week @ 2024-09-19 32002/week @ 2024-09-26 28108/week @ 2024-10-03 29751/week @ 2024-10-10 33097/week @ 2024-10-17 31493/week @ 2024-10-24 29968/week @ 2024-10-31 26865/week @ 2024-11-07 28164/week @ 2024-11-14 25286/week @ 2024-11-21

115,836 downloads per month
Used in 523 crates (70 directly)

Apache-2.0/MIT

21KB
365 lines

atomic_refcell

Threadsafe RefCell for Rust.


lib.rs:

Implements a container type providing RefCell-like semantics for objects shared across threads.

RwLock is traditionally considered to be the |Sync| analogue of RefCell. However, for consumers that can guarantee that they will never mutably borrow the contents concurrently with immutable borrows, an RwLock is overkill, and has key disadvantages:

  • Performance: Even the fastest existing implementation of RwLock (that of parking_lot) performs at least two atomic operations during immutable borrows. This makes mutable borrows significantly cheaper than immutable borrows, leading to weird incentives when writing performance-critical code.
  • Features: Implementing AtomicRefCell on top of RwLock makes it impossible to implement useful things like AtomicRef{,Mut}::map.

As such, we re-implement RefCell semantics from scratch with a single atomic reference count. The primary complication of this scheme relates to keeping things in a consistent state when one thread performs an illegal borrow and panics. Since an AtomicRefCell can be accessed by multiple threads, and since panics are recoverable, we need to ensure that an illegal (panicking) access by one thread does not lead to undefined behavior on other, still-running threads.

So we represent things as follows:

  • Any value with the high bit set (so half the total refcount space) indicates a mutable borrow.
  • Mutable borrows perform an atomic compare-and-swap, swapping in the high bit if the current value is zero. If the current value is non-zero, the thread panics and the value is left undisturbed.
  • Immutable borrows perform an atomic increment. If the new value has the high bit set, the thread panics. The incremented refcount is left as-is, since it still represents a valid mutable borrow. When the mutable borrow is released, the refcount is set unconditionally to zero, clearing any stray increments by panicked threads.

There are a few additional purely-academic complications to handle overflow, which are documented in the implementation.

The rest of this module is mostly derived by copy-pasting the implementation of RefCell and fixing things up as appropriate. Certain non-threadsafe methods have been removed. We segment the concurrency logic from the rest of the code to keep the tricky parts small and easy to audit.

Dependencies

~165KB