1 unstable release

0.1.0-preview Feb 25, 2019

#7 in #stm

MIT license

185KB
5K SLoC

swym

License Cargo Documentation Rust Nightly

swym is an experimental STM that can be used to implement concurrent data structures with performance not far from lock-free data structures.

See the docs for more information.

Initial benchmarks can be found here.


lib.rs:

A low level transaction memory library.

swym is an experimental STM that can be used to implement concurrent data structures with performance not far from lock-free data structures.

Examples

Getting a handle to swym's thread local state:

use swym::thread_key;

let thread_key = thread_key::get();

Creating new transactional memory cells:

use swym::tcell::TCell;

static A: TCell<i32> = TCell::new(0);
let b = TCell::new(42);

Performing a transaction to swap the two values:

thread_key.rw(|tx| {
    let temp = A.get(tx, Default::default())?;
    A.set(tx, b.get(tx, Default::default())?)?;
    b.set(tx, temp)?;
    Ok(())
});

Features

  • Behaves as though a single global lock were held for the duration of every transaction. This can be relaxed a bit using Ordering::Read for loads.
  • Highly optimized for read mostly data structures and modern caches. TCell stores all of its data inline. Read only transactions don't modify any global state, and read write transactions only modify global state on commit.
  • The number of allocations imposed by swym per transaction should average 0 through reuse of read logs/write logs/garbage bags.
  • Support for building recursive data structures using TPtr is still experimental but looks promising - see examples on github.
  • Backed by a custom epoch based reclaimation style garbage collector (still lots of optimization work to do there).
  • Support for nested transactions is planned, but not yet implemented.

Shared Memory

  • TCell, a low level transactional memory location - does not perform any heap allocation.
  • TPtr, a low level transactional pointer for building heap allocated data structures.

Running Transactions

  • rw, starts a read write transaction.
  • read, starts a read only transaction.
  • try_rw, starts a read write transaction returning an error if the transaction could not be started.
  • try_read, starts a read only transaction returning an error if the transaction could not be started.

Dependencies