https://github.com/rust-lang/rfcs/pull/221 The current terminology of "task failure" often causes problems when writing or speaking about code. You often want to talk about the possibility of an operation that returns a Result "failing", but cannot because of the ambiguity with task failure. Instead, you have to speak of "the failing case" or "when the operation does not succeed" or other circumlocutions. Likewise, we use a "Failure" header in rustdoc to describe when operations may fail the task, but it would often be helpful to separate out a section describing the "Err-producing" case. We have been steadily moving away from task failure and toward Result as an error-handling mechanism, so we should optimize our terminology accordingly: Result-producing functions should be easy to describe. To update your code, rename any call to `fail!` to `panic!` instead. Assuming you have not created your own macro named `panic!`, this will work on UNIX based systems: grep -lZR 'fail!' . | xargs -0 -l sed -i -e 's/fail!/panic!/g' You can of course also do this by hand. [breaking-change]
103 lines
2.8 KiB
Rust
103 lines
2.8 KiB
Rust
// 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.
|
|
|
|
//! Abstraction of a task pool for basic parallelism.
|
|
|
|
use core::prelude::*;
|
|
|
|
use task;
|
|
use task::spawn;
|
|
use vec::Vec;
|
|
use comm::{channel, Sender};
|
|
|
|
enum Msg<T> {
|
|
Execute(proc(&T):Send),
|
|
Quit
|
|
}
|
|
|
|
/// A task pool used to execute functions in parallel.
|
|
pub struct TaskPool<T> {
|
|
channels: Vec<Sender<Msg<T>>>,
|
|
next_index: uint,
|
|
}
|
|
|
|
#[unsafe_destructor]
|
|
impl<T> Drop for TaskPool<T> {
|
|
fn drop(&mut self) {
|
|
for channel in self.channels.iter_mut() {
|
|
channel.send(Quit);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> TaskPool<T> {
|
|
/// Spawns a new task pool with `n_tasks` tasks. 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.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// This function will panic if `n_tasks` is less than 1.
|
|
pub fn new(n_tasks: uint,
|
|
init_fn_factory: || -> proc(uint):Send -> T)
|
|
-> TaskPool<T> {
|
|
assert!(n_tasks >= 1);
|
|
|
|
let channels = Vec::from_fn(n_tasks, |i| {
|
|
let (tx, rx) = channel::<Msg<T>>();
|
|
let init_fn = init_fn_factory();
|
|
|
|
let task_body = proc() {
|
|
let local_data = init_fn(i);
|
|
loop {
|
|
match rx.recv() {
|
|
Execute(f) => f(&local_data),
|
|
Quit => break
|
|
}
|
|
}
|
|
};
|
|
|
|
// Run on this scheduler.
|
|
task::spawn(task_body);
|
|
|
|
tx
|
|
});
|
|
|
|
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`.
|
|
pub fn execute(&mut self, f: proc(&T):Send) {
|
|
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: || -> proc(uint):Send -> uint = || { proc(i) i };
|
|
let mut pool = TaskPool::new(4, f);
|
|
for _ in range(0u, 8) {
|
|
pool.execute(proc(i) println!("Hello from thread {}!", *i));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
#[should_fail]
|
|
fn test_zero_tasks_panic() {
|
|
let f: || -> proc(uint):Send -> uint = || { proc(i) i };
|
|
TaskPool::new(0, f);
|
|
}
|