aboutsummaryrefslogtreecommitdiffstats
path: root/components/util/taskpool.rs
blob: 8521ef9dc93c2d7e556999960c51d924938f44fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! A load-balancing task pool.
//!
//! This differs in implementation from std::sync::TaskPool in that each job is
//! up for grabs by any of the child tasks in the pool.
//!

//
// This is based on the cargo task pool.
// https://github.com/rust-lang/cargo/blob/master/src/cargo/util/pool.rs
//
// The only difference is that a normal channel is used instead of a sync_channel.
//

use task::spawn_named;
use std::sync::{Arc, Mutex};

pub struct TaskPool {
    tx: Sender<proc():Send>,
}

impl TaskPool {
    pub fn new(tasks: uint) -> TaskPool {
        assert!(tasks > 0);
        let (tx, rx) = channel();

        let state = Arc::new(Mutex::new(rx));

        for i in range(0, tasks) {
            let state = state.clone();
            spawn_named(
                format!("TaskPoolWorker {}/{}", i+1, tasks),
                proc() worker(&*state));
        }

        return TaskPool { tx: tx };

        fn worker(rx: &Mutex<Receiver<proc():Send>>) {
            loop {
                let job = rx.lock().recv_opt();
                match job {
                    Ok(job) => job(),
                    Err(..) => break,
                }
            }
        }
    }

    pub fn execute(&self, job: proc():Send) {
        self.tx.send(job);
    }
}