rust/src/libstd/task_pool.rs

102 lines
3.0 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/// A task pool abstraction. Useful for achieving predictable CPU
/// parallelism.
2013-02-02 05:10:12 -06:00
use core::comm::{Chan, Port};
use core::prelude::*;
use core::task::{SchedMode, SingleThreaded};
use core::task;
use core::vec;
enum Msg<T> {
Execute(~fn(&T)),
Quit
}
pub struct TaskPool<T> {
channels: ~[Chan<Msg<T>>],
mut next_index: uint,
}
impl<T> Drop for TaskPool<T> {
fn finalize(&self) {
for self.channels.each |channel| {
channel.send(Quit);
}
}
}
pub impl<T> TaskPool<T> {
/// Spawns a new task pool with `n_tasks` tasks. If the `sched_mode`
/// is None, the tasks run on this scheduler; otherwise, they run on a
/// new scheduler with the given mode. The provided `init_fn_factory`
/// returns a function which, given the index of the task, should return
/// local data to be kept around in that task.
static fn new(n_tasks: uint,
opt_sched_mode: Option<SchedMode>,
init_fn_factory: ~fn() -> ~fn(uint) -> T) -> TaskPool<T> {
assert n_tasks >= 1;
let channels = do vec::from_fn(n_tasks) |i| {
2013-02-02 05:10:12 -06:00
let (port, chan) = comm::stream::<Msg<T>>();
let init_fn = init_fn_factory();
2013-02-15 01:30:30 -06:00
let task_body: ~fn() = || {
let local_data = init_fn(i);
loop {
match port.recv() {
2013-02-15 01:30:30 -06:00
Execute(f) => f(&local_data),
Quit => break
}
}
};
// Start the task.
match opt_sched_mode {
None => {
// Run on this scheduler.
2013-02-15 01:30:30 -06:00
task::spawn(task_body);
}
Some(sched_mode) => {
2013-02-15 01:30:30 -06:00
task::task().sched_mode(sched_mode).spawn(task_body);
}
}
2013-02-15 01:30:30 -06:00
chan
};
2013-02-15 01:30:30 -06:00
return TaskPool { channels: channels, next_index: 0 };
}
/// Executes the function `f` on a task in the pool. The function
/// receives a reference to the local data returned by the `init_fn`.
fn execute(&self, f: ~fn(&T)) {
2013-02-15 01:30:30 -06:00
self.channels[self.next_index].send(Execute(f));
self.next_index += 1;
if self.next_index == self.channels.len() { self.next_index = 0; }
}
}
#[test]
fn test_task_pool() {
let f: ~fn() -> ~fn(uint) -> uint = || {
let g: ~fn(uint) -> uint = |i| i;
2013-02-15 01:30:30 -06:00
g
};
2013-02-15 01:30:30 -06:00
let pool = TaskPool::new(4, Some(SingleThreaded), f);
for 8.times {
pool.execute(|i| io::println(fmt!("Hello from thread %u!", *i)));
}
}