8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- #![ allow( missing_doc) ]
12
-
13
- /// A task pool abstraction. Useful for achieving predictable CPU
14
- /// parallelism.
11
+ //! Abstraction of a task pool for basic parallelism.
15
12
16
13
use core:: prelude:: * ;
17
14
@@ -25,6 +22,7 @@ enum Msg<T> {
25
22
Quit
26
23
}
27
24
25
+ /// A task pool used to execute functions in parallel.
28
26
pub struct TaskPool < T > {
29
27
channels : Vec < Sender < Msg < T > > > ,
30
28
next_index : uint ,
@@ -40,11 +38,13 @@ impl<T> Drop for TaskPool<T> {
40
38
}
41
39
42
40
impl < T > TaskPool < T > {
43
- /// Spawns a new task pool with `n_tasks` tasks. If the `sched_mode`
44
- /// is None, the tasks run on this scheduler; otherwise, they run on a
45
- /// new scheduler with the given mode. The provided `init_fn_factory`
46
- /// returns a function which, given the index of the task, should return
47
- /// local data to be kept around in that task.
41
+ /// Spawns a new task pool with `n_tasks` tasks. The provided
42
+ /// `init_fn_factory` returns a function which, given the index of the
43
+ /// task, should return local data to be kept around in that task.
44
+ ///
45
+ /// # Failure
46
+ ///
47
+ /// This function will fail if `n_tasks` is less than 1.
48
48
pub fn new ( n_tasks : uint ,
49
49
init_fn_factory: || -> proc( uint) : Send -> T )
50
50
-> TaskPool < T > {
@@ -87,12 +87,16 @@ impl<T> TaskPool<T> {
87
87
88
88
#[ test]
89
89
fn test_task_pool ( ) {
90
- let f: || -> proc( uint) : Send -> uint = || {
91
- let g: proc ( uint ) : Send -> uint = proc ( i) i;
92
- g
93
- } ;
90
+ let f: || -> proc( uint) : Send -> uint = || { proc ( i) i } ;
94
91
let mut pool = TaskPool :: new ( 4 , f) ;
95
92
for _ in range ( 0 , 8 ) {
96
93
pool. execute ( proc ( i) println ! ( "Hello from thread {}!" , * i) ) ;
97
94
}
98
95
}
96
+
97
+ #[ test]
98
+ #[ should_fail]
99
+ fn test_zero_tasks_failure ( ) {
100
+ let f: || -> proc( uint) : Send -> uint = || { proc ( i) i } ;
101
+ TaskPool :: new ( 0 , f) ;
102
+ }
0 commit comments