cooptex provides deadlock-free Mutexes.

Overview

Maintenance

cooptex

cooptex provides deadlock-free Mutexes. The [CoopMutex::lock] method wraps the [std::sync::Mutex] return value with a Result that will request the caller to drop other held locks so another thread could make progress. This behavior is easily accomplished by using the [retry_loop] function.

use cooptex::*;
let a = CoopMutex::new(42);
let b = CoopMutex::new(43);

retry_loop(|| {
  let a_lock = a.lock()?.unwrap();
  let b_lock = b.lock()?.unwrap();
  assert_eq!(*a_lock + *b_lock, 85);
  Ok(())
});

Guarantees

This crate aims to guarantee that multiple threads cannot possibly deadlock by acquiring locks.

This crate also will prefer threads that have lived the "longest" without completing work. Meaning, when [retry_loop] successfully completes, it will move that thread to the end of the queue for acquiring future locks. This provides an approximately fair scheduler.

The crate is still in early development, so there may be cases that aren't covered. Please open an issue if you can reproduce a deadlock.

Non-Guarantees

This crate explicitly allows the following potentially undesired behavior:

  • [CoopMutex::lock] may return [Retry] when it could wait and acquire the lock without a deadlock.
  • [CoopMutex::lock] may wait arbitrarily long before returning [Retry].

Incomplete

  • We have not fully analyzed the behavior during panics. There is no unsafe code, so we could only possibly deadlock.

License: MIT

You might also like...
Provides a wrapper to deserialize clap app using serde.

clap-serde Provides a wrapper to deserialize clap app using serde. API Reference toml const CLAP_TOML: &'static str = r#" name = "app_clap_serde" vers

A (mostly) drop-in replacement for Rust's Result that provides backtrace support

Errant A (mostly) drop-in replacement for Rust's Result that provides backtrace support. Please note that Errant is still very early in development an

This repository provides an emulator for iterated prisoner's dilemma.

Iterated Prisoner's Dilemma Emulator Name This repository provides an emulator for iterated prisoner's dilemma. Description You can run the program by

Memory.lol - a tiny web service that provides historical information about social media accounts

memory.lol Overview This project is a tiny web service that provides historical information about social media accounts. It can currently be used to l

Deadlock freedom

with_lock Deadlock freedom Docs Example Say you have this code: use std::sync::Mutex; fn main() { let a = Mutex::new(2); let b = Mutex::new(3

Use free svg icons in your Dioxus projects easily with dioxus-free-icons.

dioxus-free-icons Use free svg icons in your Dioxus projects easily with dioxus-free-icons. More information about this crate can be found in the crat

A lock-free, partially wait-free, eventually consistent, concurrent hashmap.
A lock-free, partially wait-free, eventually consistent, concurrent hashmap.

A lock-free, partially wait-free, eventually consistent, concurrent hashmap. This map implementation allows reads to always be wait-free on certain pl

Provides a single TUI-based registry for drm-free, wine and steam games on linux, accessed through a rofi launch menu.
Provides a single TUI-based registry for drm-free, wine and steam games on linux, accessed through a rofi launch menu.

eidolon A conversion of steam_suite to rust with additional features. Provides a single TUI-based registry for drm-free, wine and steam games on linux

Extra iterator adaptors, iterator methods, free functions, and macros.

Itertools Extra iterator adaptors, functions and macros. Please read the API documentation here How to use with cargo: [dependencies] itertools = "0.1

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies

Dear ImGui (This library is available under a free and permissive license, but needs financial support to sustain its continued improvements. In addit

A fast, boilerplate free, web framework for Rust

Tower Web A web framework for Rust with a focus on removing boilerplate. API Documentation Tower Web is: Fast: Fully asynchronous, built on Tokio and

Local blockchain for Free TON DApp development and testing.

TON OS Startup Edition Local blockchain for Free TON DApp development and testing. Have a question? Get quick help in our channel: TON OS Startup Edit

A dependency-free chess engine library built to run anywhere.
A dependency-free chess engine library built to run anywhere.

♔chess-engine♚ A dependency-free chess engine library built to run anywhere. Demo | Docs | Contact Me Written in Rust 🦀 💖 Why write a Chess engine?

transmute-free Rust library to work with the Arrow format

Arrow2: Transmute-free Arrow This repository contains a Rust library to work with the Arrow format. It is a re-write of the official Arrow crate using

Alternative Free Identity System
Alternative Free Identity System

Alfis Alternative Free Identity System This project represents a minimal blockchain without cryptocurrency, capable of sustaining any number of domain

Fastest and safest Rust implementation of parquet. `unsafe` free. Integration-tested against pyarrow

Parquet2 This is a re-write of the official parquet crate with performance, parallelism and safety in mind. The five main differentiators in compariso

Bloat-Free Browser Game in Rust (rustc-only challenge)
Bloat-Free Browser Game in Rust (rustc-only challenge)

Bloat-Free Browser Game in Rust (rustc-only challenge) Don't blame me if this code breaks in 1 year The idea is to make a simple game in Rust as bloat

 The Bloat-Free Browser Game in Rust but in C and in UEFI
The Bloat-Free Browser Game in Rust but in C and in UEFI

rust-browser-game but in UEFI instead of browser quick start deps rust gnu-efi gcc make build process $ make running after building everything you wil

Uindex is a data store, for data that can be parsed as sentences in some context-free language.
Uindex is a data store, for data that can be parsed as sentences in some context-free language.

Uindex - Universal index Uindex is a data store, for data that can be parsed as sentences in some context-free language.

Comments
  • Possibility of RwLock version of CoopMutex?

    Possibility of RwLock version of CoopMutex?

    Hello! This library looks quite useful, and I'll be trying it out over the next couple days in my project (which is currently suffering from occasional deadlocks).

    Currently in my project I am using RwLock locks for my data, as most of the time there are not writes occurring, so allowing multiple read-locks simultaneously is beneficial. Naturally, I was wondering if a RwLock is possible for the approach cooptex is using to handle deadlocks.

    If not, that's fine! I will try to rework using CoopMutex as written.

    I just figured it was worth asking:

    1. Whether a CoopRwLock struct were possible, given cooptex's deadlock resolution design.
    2. How much work it would be to create such a struct. (as if it were straightforward enough, I may look into trying to develop the variant myself)

    Anyway, thanks for releasing the library, as it gets the thought process going on other possibilities for handling deadlocks. :)

    EDIT: For now, I am using this wrapper around RwLock that keeps track of all "living lock-guards", so that at least when a deadlock occurs, it is easy to see the name/location of any lock-guards it is waiting-for/deadlocked-with: https://gist.github.com/Venryx/64f822a9468bb6d76a8f6d63f0c3ae23

    opened by Venryx 1
Owner
Shelby Doolittle
Shelby Doolittle
This is a Telegram bot I'm working on in my free time to learn Rust.

Maldness Bot This is a Telegram bot I'm working on in my free time to learn Rust. Building docker build -t . should be enough.

Sergey Kislyakov 10 May 13, 2022
🦜 A hassle-free, highly performant, host it yourself Discord music bot built with Serenity in Rust. Powered by youtube-dl and Genius.

?? A hassle-free, highly performant and fast evolving Discord music bot built with Serenity in Rust. Deployment Usage Just create a bot account, copy

Miguel Mano 82 Dec 14, 2022
A lock-free thread-owned queue whereby tasks are taken by stealers in entirety via buffer swapping

Swap Queue A lock-free thread-owned queue whereby tasks are taken by stealers in entirety via buffer swapping. This is meant to be used [thread_local]

Thomas Sieverding 20 Sep 9, 2022
Quinine is a Rust library that implements atomic, lock-free, but write-once versions of containers like `Box` or `Arc`

Quinine is a Rust library that implements atomic, lock-free, but write-once versions of containers like `Box` or `Arc`

Paul Khuong 4 Feb 19, 2022
This crate provides a convenient macro that allows you to generate type wrappers that promise to always uphold arbitrary invariants that you specified.

prae This crate provides a convenient macro that allows you to generate type wrappers that promise to always uphold arbitrary invariants that you spec

null 96 Dec 4, 2022
todo-or-die provides procedural macros that act as checked reminders.

todo-or-die provides procedural macros that act as checked reminders.

David Pedersen 552 Dec 24, 2022
Provides two APIs for easily cancelling futures, with the option to fallback to a timeout cancellation

tokio-context Provides two different methods for cancelling futures with a provided handle for cancelling all related futures, with a fallback timeout

Peter Farr 18 Dec 27, 2022
global allocator that provides hooks for tracking allocation events

tracking-allocator A GlobalAlloc-compatible allocator implementation that provides the ability to track allocation events. examples As allocators are

Toby Lawrence 39 Dec 22, 2022
`fugit` provides a comprehensive library of `Duration` and `Instant` for the handling of time in embedded systems, doing all it can at compile time.

fugit fugit provides a comprehensive library of Duration and Instant for the handling of time in embedded systems, doing all it can at compile time. T

Emil Fresk 40 Oct 2, 2022
Provides a Suricata Eve output for Kafka with Suricate Eve plugin

Suricata Eve Kafka Output Plugin for Suricata 6.0.x This plugin provides a Suricata Eve output for Kafka. Base on suricata-redis-output: https://githu

Center 7 Dec 15, 2022