Giter Club home page Giter Club logo

usync's Introduction

usync

Crates.io Documentation MSRV: 1.59.0

This library provides implementations of Mutex, RwLock, Condvar, Barrier and Once that are word-sized and generally fast as those in parking_lot. It also provides a ReentrantMutex type which supports recursive locking.

Features

The primitives provided by this library have several advantages over those in the Rust standard library:

  1. All types require only 1 word of storage (unlike stdlib which stores more state for poison detection).
  2. Static initializers for all types (stdlib doesn't yet have this for Barrier).
  3. Inline uncontested paths and micro-contention handled with bounded, adaptive spinning.
  4. Condvar::notify_all will generally only wake up a single thread and requeue the rest to wait on the associated Mutex. This avoids a thundering herd problem where all threads try to acquire the lock at the same time.
  5. Mutex and RwLock allow raw locking and unlocking without a RAII guard object.
  6. A ReentrantMutex type which supports recursive locking.
  7. Lock guards can be sent to other threads when the send_guard feature is enabled.

Userspace queues

To keep these primitives word sized, their state is multiplexed between counters, queues of threads, and combinations of both. This draws similarities to Windows' Slim Synchronization Primitives. No external locking of global queues as seen in Linux futex or parking_lot is employed. The queues are all embedded in each primitive and interacted with lock-free operations to decrease worst-case contention latency.

Having to juggle around queues with the synchronization state unfortunately means that "no spurious wakeups" cannot be guaranteed for Condvar and that extreme read-only workflows for RwLock can't use optimized atomics to improve throughput. These perf limits shouldn't matter in practice though, even more so when other cache effects come into play. On the bright side, writer/exclusive heavy workloads scale much better than existing solutions and are heavily optimized for micro-contention.

Nightly vs stable

There are a few restrictions when using this library on stable Rust:

  • You will have to use the const_* functions (e.g. const_mutex(val)) to statically initialize the locking primitives. Using e.g. Mutex::new(val) does not work on stable Rust yet.

To enable nightly-only functionality, you need to enable the nightly feature in Cargo (see below).

Usage

Add this to your Cargo.toml:

[dependencies]
usync = "0.2.1"

To enable nightly-only features, add this to your Cargo.toml instead:

[dependencies]
usync = { version = "0.2.1", features = ["nightly"] }

To allow sending MutexGuards and RwLock*Guards to other threads, enable the send_guard option.

License

Licensed under MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT).

usync's People

Contributors

ibraheemdev avatar joboet avatar kprotty avatar palfrey avatar thomcc avatar yjhn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

usync's Issues

MSRV?

What's the minimum stable Rust version supported by this library? I've dug through the docs a bit and hadn't spotted any mention of a version.

Questions about detecting panics in peer mutexes

Question for the author about detecting panics that occurred in a prior mutex region. Is this detection the responsibility of client caller?

I thought a MutexGuard might not span the entire mutex region to provide reliable panic detection, since it only tracks access point of the embedded data; is this correct?

Unrolled Spin Loops

Hey,

Great project, really interesting! I've been having a play with unrolling the spin loops to try and make it faster, and seem to have got there!

In one test it went from 1421 -> 2046 kHz! I have attached a godbolt link here to show the approach for unrolling. My tests had the cpu check in just removed it to clean up the outputted assembly.

https://rust.godbolt.org/z/MrrnqfoPP

I had to increase the number of pauses as the loop is now faster. The downsides are that the assembly is now slightly more, however if run with optimisation level S it removes the unrolling.

A couple of questions:

  1. What benchmark parameters are you mainly testing with?
  2. Would you be interested in me creating a PR with it?

Thanks Jack

Fairness

Interesting project, got here from Reddit.

Some questions after reading the README:

  1. Is the mutex fair or eventually fair, i.e. does it guarantee that no thread can permanently starve another?
  2. What are the disadvantages and advantages compared to parking_lot? Quickly skimming the README of parking_lot suggests that their synchronization structures are also โ‰ค 1 word large, but even that information would be interesting in the README IMO.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.