1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-emscripten No support for threads
13 #![allow(unknown_features)]
17 A somewhat reduced test case to expose some Valgrind issues.
19 This originally came from the word-count benchmark.
22 pub fn map(filename: String, mut emit: map_reduce::putter) {
23 emit(filename, "1".to_string());
27 use std::collections::HashMap;
28 use std::sync::mpsc::{channel, Sender};
32 pub type putter<'a> = Box<FnMut(String, String) + 'a>;
34 pub type mapper = extern fn(String, putter);
36 enum ctrl_proto { find_reducer(Vec<u8>, Sender<isize>), mapper_done, }
38 fn start_mappers(ctrl: Sender<ctrl_proto>, inputs: Vec<String>) {
40 let ctrl = ctrl.clone();
42 thread::spawn(move|| map_task(ctrl.clone(), i.clone()) );
46 fn map_task(ctrl: Sender<ctrl_proto>, input: String) {
47 let mut intermediates = HashMap::new();
49 fn emit(im: &mut HashMap<String, isize>,
50 ctrl: Sender<ctrl_proto>, key: String,
52 if im.contains_key(&key) {
55 let (tx, rx) = channel();
56 println!("sending find_reducer");
57 ctrl.send(ctrl_proto::find_reducer(key.as_bytes().to_vec(), tx)).unwrap();
58 println!("receiving");
59 let c = rx.recv().unwrap();
64 let ctrl_clone = ctrl.clone();
65 ::map(input, Box::new(|a,b| emit(&mut intermediates, ctrl.clone(), a, b)));
66 ctrl_clone.send(ctrl_proto::mapper_done).unwrap();
69 pub fn map_reduce(inputs: Vec<String>) {
70 let (tx, rx) = channel();
72 // This thread becomes the master control thread. It spawns others
75 let mut reducers: HashMap<String, isize>;
77 reducers = HashMap::new();
79 start_mappers(tx, inputs.clone());
81 let mut num_mappers = inputs.len() as isize;
83 while num_mappers > 0 {
84 match rx.recv().unwrap() {
85 ctrl_proto::mapper_done => { num_mappers -= 1; }
86 ctrl_proto::find_reducer(k, cc) => {
88 match reducers.get(&str::from_utf8(&k).unwrap().to_string()) {
89 Some(&_c) => { c = _c; }
100 map_reduce::map_reduce(
101 vec!["../src/test/run-pass/hashmap-memory.rs".to_string()]);