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
14 A somewhat reduced test case to expose some Valgrind issues.
16 This originally came from the word-count benchmark.
19 pub fn map(filename: String, mut emit: map_reduce::putter) {
20 emit(filename, "1".to_string());
24 use std::collections::HashMap;
25 use std::sync::mpsc::{channel, Sender};
29 pub type putter<'a> = Box<FnMut(String, String) + 'a>;
31 pub type mapper = extern fn(String, putter);
33 enum ctrl_proto { find_reducer(Vec<u8>, Sender<isize>), mapper_done, }
35 fn start_mappers(ctrl: Sender<ctrl_proto>, inputs: Vec<String>) {
37 let ctrl = ctrl.clone();
39 thread::spawn(move|| map_task(ctrl.clone(), i.clone()) );
43 fn map_task(ctrl: Sender<ctrl_proto>, input: String) {
44 let mut intermediates = HashMap::new();
46 fn emit(im: &mut HashMap<String, isize>,
47 ctrl: Sender<ctrl_proto>, key: String,
49 if im.contains_key(&key) {
52 let (tx, rx) = channel();
53 println!("sending find_reducer");
54 ctrl.send(ctrl_proto::find_reducer(key.as_bytes().to_vec(), tx)).unwrap();
55 println!("receiving");
56 let c = rx.recv().unwrap();
61 let ctrl_clone = ctrl.clone();
62 ::map(input, Box::new(|a,b| emit(&mut intermediates, ctrl.clone(), a, b)));
63 ctrl_clone.send(ctrl_proto::mapper_done).unwrap();
66 pub fn map_reduce(inputs: Vec<String>) {
67 let (tx, rx) = channel();
69 // This thread becomes the master control thread. It spawns others
72 let mut reducers: HashMap<String, isize>;
74 reducers = HashMap::new();
76 start_mappers(tx, inputs.clone());
78 let mut num_mappers = inputs.len() as isize;
80 while num_mappers > 0 {
81 match rx.recv().unwrap() {
82 ctrl_proto::mapper_done => { num_mappers -= 1; }
83 ctrl_proto::find_reducer(k, cc) => {
85 match reducers.get(&str::from_utf8(&k).unwrap().to_string()) {
86 Some(&_c) => { c = _c; }
97 map_reduce::map_reduce(
98 vec!["../src/test/run-pass/hashmap-memory.rs".to_string()]);