1
- // Copyright 2012-2013- 2014 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- // ignore-test arcs no longer unwrap
12
-
13
11
extern crate sync;
14
12
15
13
use std:: from_str:: FromStr ;
16
14
use std:: iter:: count;
17
15
use std:: cmp:: min;
18
16
use std:: os;
19
17
use std:: vec:: from_elem;
20
- use sync:: Arc ;
21
18
use sync:: RWArc ;
22
19
23
20
fn A ( i : uint , j : uint ) -> f64 {
@@ -33,23 +30,32 @@ fn dot(v: &[f64], u: &[f64]) -> f64 {
33
30
}
34
31
35
32
fn mult ( v : RWArc < ~[ f64 ] > , out : RWArc < ~[ f64 ] > , f : fn ( & ~[ f64 ] , uint ) -> f64 ) {
36
- let wait = Arc :: new ( ( ) ) ;
33
+ // We lanch in different tasks the work to be done. To finish
34
+ // this fuction, we need to wait for the completion of every
35
+ // tasks. To do that, we give to each tasks a wait_chan that we
36
+ // drop at the end of the work. At the end of this function, we
37
+ // wait until the channel hang up.
38
+ let ( wait_port, wait_chan) = Chan :: new ( ) ;
39
+
37
40
let len = out. read ( |out| out. len ( ) ) ;
38
41
let chunk = len / 100 + 1 ;
39
42
for chk in count ( 0 , chunk) {
40
43
if chk >= len { break ; }
41
- let w = wait . clone ( ) ;
44
+ let w = wait_chan . clone ( ) ;
42
45
let v = v. clone ( ) ;
43
46
let out = out. clone ( ) ;
44
47
spawn ( proc ( ) {
45
48
for i in range ( chk, min ( len, chk + chunk) ) {
46
49
let val = v. read ( |v| f ( v, i) ) ;
47
50
out. write ( |out| out[ i] = val) ;
48
51
}
49
- let _ = w ;
52
+ drop ( w )
50
53
} ) ;
51
54
}
52
- let _ = wait. unwrap ( ) ;
55
+
56
+ // wait until the channel hang up (every task finished)
57
+ drop ( wait_chan) ;
58
+ for ( ) in wait_port. iter ( ) { }
53
59
}
54
60
55
61
fn mult_Av_impl ( v : & ~[ f64 ] , i : uint ) -> f64 {
@@ -97,7 +103,8 @@ fn main() {
97
103
mult_AtAv ( u. clone ( ) , v. clone ( ) , tmp. clone ( ) ) ;
98
104
mult_AtAv ( v. clone ( ) , u. clone ( ) , tmp. clone ( ) ) ;
99
105
}
100
- let u = u. unwrap ( ) ;
101
- let v = v. unwrap ( ) ;
102
- println ! ( "{:.9f}" , ( dot( u, v) / dot( v, v) ) . sqrt( ) ) ;
106
+
107
+ u. read ( |u| v. read ( |v| {
108
+ println ! ( "{:.9f}" , ( dot( * u, * v) / dot( * v, * v) ) . sqrt( ) ) ;
109
+ } ) )
103
110
}
0 commit comments