Giter Site home page Giter Site logo

try-mutex's Introduction

A simple non-blocking mutex (i.e. only try_lock is supported), using atomics.

Simpler than the one found in stdlib. Does not support poisoning.

This used to be faster than the mutex in the standard library, but benchmarking indicates that optimizations in the standard library means there is no longer a significant difference (on my machine). Be sure to run them on your own machine to compare.

Nevertheless, this library may still be useful for embedded or similar cases.

try-mutex's People

Contributors

mpdn avatar kestrer avatar finomnis avatar hezuikn avatar sergiobenitez avatar

Stargazers

Nick Sanders avatar  avatar José Luis Cruz avatar BEN BEHAI  avatar Ross MacArthur avatar  avatar CossonLeo avatar Mèir avatar  avatar  avatar

Watchers

 avatar  avatar James Cloos avatar José Luis Cruz avatar  avatar

try-mutex's Issues

TryMutex<T> should have Send bound on T

Hi there, we (Rust group @sslab-gatech) are scanning crates on crates.io for potential soundness bugs. We noticed that TryMutex implements Sync for all types T:

unsafe impl<T> Sync for TryMutex<T> {}

This should probably be bounded by T: Send just like the standard library's Mutex, otherwise it allows smuggling non-Send types like Rc across thread-boundaries like so:

#![forbid(unsafe_code)]

use try_mutex::TryMutex;

use std::rc::Rc;
use crossbeam_utils::thread;

fn main() {
    let rc = Rc::new(());
    let rc_clone = rc.clone();

    let try_mutex = TryMutex::new(rc_clone);
    thread::scope(|s| {    
        s.spawn(|_| {
            let smuggled_rc = try_mutex.try_lock().unwrap();
            println!("RC in thread: {:p}", *smuggled_rc);
        });
    });
    println!("RC in main:   {:p}", rc);
}

This outputs:

RC in thread: 0x5642ef5ffa50
RC in main:   0x5642ef5ffa50

and can lead to data races from safe Rust code.

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.