Created
February 25, 2016 00:19
-
-
Save reem/c4ba083c624607f279e0 to your computer and use it in GitHub Desktop.
Reproduction for rust-lang/rfcs#1409
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "repro" | |
version = "0.1.0" | |
authors = ["Jonathan Reem <[email protected]>"] | |
[dependencies] | |
scoped-pool = "*" | |
scopeguard = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate scopeguard; // for defer!, https://github.com/bluss/scopeguard | |
extern crate scoped_pool; // https://github.com/reem/rust-scoped-pool | |
use scoped_pool::{Scope, Pool}; | |
#[derive(Default)] | |
pub struct Fs { | |
_blah: Vec<u8> // details | |
} | |
impl Fs { | |
fn init<'fs>(&'fs self, _: &Scope<'fs>) { | |
// Pass off (self, scope) to an internal type which spawns a bunch of worker | |
// threads that operate on self - this function does not block. | |
} | |
// Starts a threadpool, creates Fs, passes it to F, takes care to clean up on panic etc. | |
pub fn run<F, R>(threads: usize, fun: F) -> R | |
where F: for<'fs> FnOnce(&'fs Fs, &Scope<'fs>) -> R { // Problematic clause | |
let pool = Pool::new(threads); | |
defer!(pool.shutdown()); | |
let fs = &Fs::default(); | |
pool.scoped(move |scope| { | |
fs.init(scope); // Start threadpool. | |
defer!(fs.shutdown()); | |
fun(fs, scope) | |
}) | |
} | |
// Stops worker threads started by init | |
fn shutdown(&self) { } | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::Fs; | |
use std::sync::Mutex; | |
#[test] | |
fn it_doesnt_work() { | |
let m = &::std::sync::Mutex::new(()); | |
Fs::run(12, |fs, scope| { | |
scope.execute(move || { | |
let _l = m.lock().unwrap(); | |
}); | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment