Skip to content

Instantly share code, notes, and snippets.

@reem
Created February 25, 2016 00:19
Show Gist options
  • Save reem/c4ba083c624607f279e0 to your computer and use it in GitHub Desktop.
Save reem/c4ba083c624607f279e0 to your computer and use it in GitHub Desktop.
Reproduction for rust-lang/rfcs#1409
[package]
name = "repro"
version = "0.1.0"
authors = ["Jonathan Reem <jonathan.reem@gmail.com>"]
[dependencies]
scoped-pool = "*"
scopeguard = "*"
#[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