Skip to content

Commit

Permalink
std::sync::TaskPool: Improve module documentation
Browse files Browse the repository at this point in the history
The struct and module doc comments are reformulated. The `execute`
method's documentation are put up to date, and failure information
is added. A test is also added to address the possible failure.
  • Loading branch information
alxgnon committed Jun 20, 2014
1 parent 282705c commit af520e1
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/libstd/sync/task_pool.rs
Expand Up @@ -8,10 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(missing_doc)]

/// A task pool abstraction. Useful for achieving predictable CPU
/// parallelism.
//! Abstraction of a task pool for basic parallelism.

use core::prelude::*;

Expand All @@ -25,6 +22,7 @@ enum Msg<T> {
Quit
}

/// A task pool used to execute functions in parallel.
pub struct TaskPool<T> {
channels: Vec<Sender<Msg<T>>>,
next_index: uint,
Expand All @@ -40,11 +38,13 @@ impl<T> Drop for TaskPool<T> {
}

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.
/// 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.
///
/// # Failure
///
/// This function will fail if `n_tasks` is less than 1.
pub fn new(n_tasks: uint,
init_fn_factory: || -> proc(uint):Send -> T)
-> TaskPool<T> {
Expand Down Expand Up @@ -87,12 +87,16 @@ impl<T> TaskPool<T> {

#[test]
fn test_task_pool() {
let f: || -> proc(uint):Send -> uint = || {
let g: proc(uint):Send -> uint = proc(i) i;
g
};
let f: || -> proc(uint):Send -> uint = || { proc(i) i };
let mut pool = TaskPool::new(4, f);
for _ in range(0, 8) {
pool.execute(proc(i) println!("Hello from thread {}!", *i));
}
}

#[test]
#[should_fail]
fn test_zero_tasks_failure() {
let f: || -> proc(uint):Send -> uint = || { proc(i) i };
TaskPool::new(0, f);
}

9 comments on commit af520e1

@bors
Copy link
Contributor

@bors bors commented on af520e1 Jun 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on af520e1 Jun 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alxgnon/rust/taskpooldocfix = af520e1 into auto

@bors
Copy link
Contributor

@bors bors commented on af520e1 Jun 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alxgnon/rust/taskpooldocfix = af520e1 merged ok, testing candidate = 9a944c5e

@bors
Copy link
Contributor

@bors bors commented on af520e1 Jun 20, 2014

@bors
Copy link
Contributor

@bors bors commented on af520e1 Jun 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on af520e1 Jun 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alxgnon/rust/taskpooldocfix = af520e1 into auto

@bors
Copy link
Contributor

@bors bors commented on af520e1 Jun 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alxgnon/rust/taskpooldocfix = af520e1 merged ok, testing candidate = 75eb50d

@bors
Copy link
Contributor

@bors bors commented on af520e1 Jun 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 75eb50d

Please sign in to comment.