2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2012-11-15 14:32:00 -06:00
|
|
|
/// A task pool abstraction. Useful for achieving predictable CPU
|
2012-10-31 20:29:44 -05:00
|
|
|
/// parallelism.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-03-26 15:38:07 -05:00
|
|
|
use core::comm::Chan;
|
2013-05-24 21:35:29 -05:00
|
|
|
use core::comm;
|
2013-03-26 15:38:07 -05:00
|
|
|
use core::task::SchedMode;
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::task;
|
|
|
|
use core::vec;
|
2012-10-31 20:29:44 -05:00
|
|
|
|
2013-03-26 15:38:07 -05:00
|
|
|
#[cfg(test)] use core::task::SingleThreaded;
|
|
|
|
|
2012-10-31 20:29:44 -05:00
|
|
|
enum Msg<T> {
|
|
|
|
Execute(~fn(&T)),
|
|
|
|
Quit
|
|
|
|
}
|
|
|
|
|
2012-11-15 14:32:00 -06:00
|
|
|
pub struct TaskPool<T> {
|
2012-10-31 20:29:44 -05:00
|
|
|
channels: ~[Chan<Msg<T>>],
|
2013-03-24 11:41:19 -05:00
|
|
|
next_index: uint,
|
2013-02-27 18:13:53 -06:00
|
|
|
}
|
|
|
|
|
2013-03-20 20:18:57 -05:00
|
|
|
#[unsafe_destructor]
|
2013-02-27 18:13:53 -06:00
|
|
|
impl<T> Drop for TaskPool<T> {
|
2013-06-20 20:06:13 -05:00
|
|
|
fn drop(&self) {
|
2013-06-21 07:29:53 -05:00
|
|
|
for self.channels.iter().advance |channel| {
|
2012-10-31 20:29:44 -05:00
|
|
|
channel.send(Quit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl<T> TaskPool<T> {
|
2012-11-15 14:32:00 -06:00
|
|
|
/// Spawns a new task pool with `n_tasks` tasks. If the `sched_mode`
|
2012-10-31 20:29:44 -05:00
|
|
|
/// 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.
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn new(n_tasks: uint,
|
|
|
|
opt_sched_mode: Option<SchedMode>,
|
|
|
|
init_fn_factory: ~fn() -> ~fn(uint) -> T)
|
|
|
|
-> TaskPool<T> {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(n_tasks >= 1);
|
2012-10-31 20:29:44 -05:00
|
|
|
|
|
|
|
let channels = do vec::from_fn(n_tasks) |i| {
|
2013-02-02 05:10:12 -06:00
|
|
|
let (port, chan) = comm::stream::<Msg<T>>();
|
2012-10-31 20:29:44 -05:00
|
|
|
let init_fn = init_fn_factory();
|
|
|
|
|
2013-02-15 01:30:30 -06:00
|
|
|
let task_body: ~fn() = || {
|
2012-10-31 20:29:44 -05:00
|
|
|
let local_data = init_fn(i);
|
|
|
|
loop {
|
|
|
|
match port.recv() {
|
2013-02-15 01:30:30 -06:00
|
|
|
Execute(f) => f(&local_data),
|
2012-10-31 20:29:44 -05:00
|
|
|
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);
|
2012-10-31 20:29:44 -05:00
|
|
|
}
|
|
|
|
Some(sched_mode) => {
|
2013-05-03 15:21:33 -05:00
|
|
|
let mut task = task::task();
|
|
|
|
task.sched_mode(sched_mode);
|
|
|
|
task.spawn(task_body);
|
2012-10-31 20:29:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 01:30:30 -06:00
|
|
|
chan
|
2012-10-31 20:29:44 -05:00
|
|
|
};
|
|
|
|
|
2013-02-15 01:30:30 -06:00
|
|
|
return TaskPool { channels: channels, next_index: 0 };
|
2012-10-31 20:29:44 -05:00
|
|
|
}
|
|
|
|
|
2012-11-15 14:32:00 -06:00
|
|
|
/// Executes the function `f` on a task in the pool. The function
|
2012-10-31 20:29:44 -05:00
|
|
|
/// receives a reference to the local data returned by the `init_fn`.
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn execute(&mut self, f: ~fn(&T)) {
|
2013-02-15 01:30:30 -06:00
|
|
|
self.channels[self.next_index].send(Execute(f));
|
2012-10-31 20:29:44 -05:00
|
|
|
self.next_index += 1;
|
|
|
|
if self.next_index == self.channels.len() { self.next_index = 0; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-11-15 14:32:00 -06:00
|
|
|
fn test_task_pool() {
|
2012-10-31 20:29:44 -05:00
|
|
|
let f: ~fn() -> ~fn(uint) -> uint = || {
|
|
|
|
let g: ~fn(uint) -> uint = |i| i;
|
2013-02-15 01:30:30 -06:00
|
|
|
g
|
2012-10-31 20:29:44 -05:00
|
|
|
};
|
2013-03-24 11:41:19 -05:00
|
|
|
let mut pool = TaskPool::new(4, Some(SingleThreaded), f);
|
2012-10-31 20:29:44 -05:00
|
|
|
for 8.times {
|
2013-05-24 21:35:29 -05:00
|
|
|
pool.execute(|i| println(fmt!("Hello from thread %u!", *i)));
|
2012-10-31 20:29:44 -05:00
|
|
|
}
|
|
|
|
}
|