1- // Copyright 2012-2013- 2014 The Rust Project Developers. See the COPYRIGHT
1+ // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22// file at the top-level directory of this distribution and at
33// http://rust-lang.org/COPYRIGHT.
44//
88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- // ignore-test arcs no longer unwrap
12-
1311extern crate sync;
1412
1513use std:: from_str:: FromStr ;
1614use std:: iter:: count;
1715use std:: cmp:: min;
1816use std:: os;
1917use std:: vec:: from_elem;
20- use sync:: Arc ;
2118use sync:: RWArc ;
2219
2320fn A ( i : uint , j : uint ) -> f64 {
@@ -33,23 +30,32 @@ fn dot(v: &[f64], u: &[f64]) -> f64 {
3330}
3431
3532fn 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+
3740 let len = out. read ( |out| out. len ( ) ) ;
3841 let chunk = len / 100 + 1 ;
3942 for chk in count ( 0 , chunk) {
4043 if chk >= len { break ; }
41- let w = wait . clone ( ) ;
44+ let w = wait_chan . clone ( ) ;
4245 let v = v. clone ( ) ;
4346 let out = out. clone ( ) ;
4447 spawn ( proc ( ) {
4548 for i in range ( chk, min ( len, chk + chunk) ) {
4649 let val = v. read ( |v| f ( v, i) ) ;
4750 out. write ( |out| out[ i] = val) ;
4851 }
49- let _ = w ;
52+ drop ( w )
5053 } ) ;
5154 }
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 ( ) { }
5359}
5460
5561fn mult_Av_impl ( v : & ~[ f64 ] , i : uint ) -> f64 {
@@ -97,7 +103,8 @@ fn main() {
97103 mult_AtAv ( u. clone ( ) , v. clone ( ) , tmp. clone ( ) ) ;
98104 mult_AtAv ( v. clone ( ) , u. clone ( ) , tmp. clone ( ) ) ;
99105 }
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+ } ) )
103110}
0 commit comments