#async #primitive #forms #operations #block #dropped

deprecated nursery

An implemenation of Nathaniel J. Smiths Concurrency primitive for Rust.

1 unstable release

0.0.1 Apr 20, 2019

#23 in #dropped

24 downloads per month

Apache-2.0

16KB
269 lines

Nursery - A Rust concurrency primitive.

An implementation of Nathaniel J. Smiths Structured Concurrency model: Notes on Structured Concurrency.

Description

The building block of concurrency is called a Nursery. They can adopt or schedule concurrent operations. Before a Nursery is dropped it will block on all of it's pending concurrent operations. A Nursery is itself a concurrent operation so it can be adopted by another Nursery to form a hierarchy.

Example

extern crate nursery;
use nursery::thread::{Handle, Pending};
use nursery::{Nursery, Waitable};
use std::sync::Arc;
use std::sync::Mutex;

pub struct Counter {
    count: i32,
}

impl Counter {
    pub fn incr(&mut self) {
        self.count += 1;
    }
}

let counter = Arc::new(Mutex::new(Counter { count: 0 }));
{
    let h_counter = counter.clone();
    let h1 = Pending::new(move || {
        let mut c = h_counter.lock().unwrap();
        c.incr();
    });
    let h_counter = counter.clone();
    let h2 = Pending::new(move || {
        let mut c = h_counter.lock().unwrap();
        c.incr();
    });
    let mut child = Nursery::new();
    child.schedule(Box::new(h1));
    child.schedule(Box::new(h2));
    let mut parent = Nursery::new();
    parent.adopt(child.into());
    // Before parent is dropped all of the above concurrent operations
    // will complete.
}
assert_eq!(counter.lock().unwrap().count, 2);

No runtime deps