[][src]Module tokio_sync::lock

An asynchronous Mutex-like type.

This module provides Lock, a type that acts similarly to an asynchronous Mutex, with one major difference: the LockGuard returned by poll_lock is not tied to the lifetime of the Mutex. This enables you to acquire a lock, and then pass that guard into a future, and then release it at some later point in time.

This allows you to do something along the lines of:

use tokio::sync::lock::{Lock, LockGuard};
struct MyType<S> {
    lock: Lock<S>,
}

impl<S> Future for MyType<S>
  where S: Stream<Item = u32> + Send + 'static
{
    type Item = ();
    type Error = ();

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.lock.poll_lock() {
            Async::Ready(mut guard) => {
                tokio::spawn(future::poll_fn(move || {
                    let item = try_ready!(guard.poll().map_err(|_| ()));
                    println!("item = {:?}", item);
                    Ok(().into())
                }));
                Ok(().into())
            },
            Async::NotReady => Ok(Async::NotReady)
        }
    }
}

Structs

Lock

An asynchronous mutual exclusion primitive useful for protecting shared data

LockGuard

A handle to a held Lock.