Skip to content

dekerakt/futures-mutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

futures-mutex

A mutual exclusion future-powered synchronisation primitive useful for protecting shared data.

A mutex is designed to use along with futures crate. API is similar to BiLock<T> from futures crate. Though, it can be cloned as long as you need.

Installation

This crate can be included in your Cargo enabled project like this:

[dependencies]
futures-mutex = { git = "https://github.com/dekerakt/futures-mutex" }

Then include futures-mutex in your project like this:

extern crate futures_mutex;

Examples

let lock1 = Mutex::new(0);

// Mutex can be easily cloned as long as you need
let lock2 = lock1.clone();

assert!(lock1.poll_lock().is_ready());
assert!(lock2.poll_lock().is_ready());

let mut guard = match lock1.poll_lock() {
    Async::Ready(v) => v,
    Async::NotReady => unreachable!()
};

*guard += 1;

assert!(lock1.poll_lock().is_not_ready());
assert!(lock2.poll_lock().is_not_ready());

drop(guard);

assert!(lock1.poll_lock().is_ready());
assert!(lock2.poll_lock().is_ready());

Note that poll_lock method of the mutex must be called inside the context of the task, otherwise it will panic.

You can also call lock method of the mutex, which returns a future which resolves to the owned-guard.

let data = Mutex::new(2);
data.clone().lock().map(|mut guard| *guard += 2).wait().unwrap();
data.clone().lock().map(|guard| assert_eq!(*guard, 4)).wait().unwrap();

You can see full source code of examples in examples/ directory.

About

Mutex<T> type similar to BiLock<T> from futures crate.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages