Skip to content

Commit 37679ec

Browse files
committed
---
yaml --- r: 141289 b: refs/heads/try2 c: 7ee329e h: refs/heads/master i: 141287: f29d134 v: v3
1 parent 38f5811 commit 37679ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+806
-453
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: c9c4d92889ed1b147368a31127c0aa2e4db76cd5
8+
refs/heads/try2: 7ee329e97a24d259f76c1a6e910f32aa11e07862
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/configure

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,11 @@ then
555555
CFG_CLANG_VERSION=$("$CFG_CLANG" \
556556
--version \
557557
| grep version \
558-
| sed 's/.*\(version .*\)/\1/' \
558+
| sed 's/.*\(version .*\)/\1/; s/.*based on \(LLVM .*\))/\1/' \
559559
| cut -d ' ' -f 2)
560560

561561
case $CFG_CLANG_VERSION in
562-
(3.0svn | 3.0 | 3.1* | 3.2* | 3.3* | 4.0* | 4.1* | 4.2*)
562+
(3.0svn | 3.0 | 3.1* | 3.2* | 3.3*)
563563
step_msg "found ok version of CLANG: $CFG_CLANG_VERSION"
564564
CFG_C_COMPILER="clang"
565565
;;

branches/try2/doc/tutorial-tasks.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ let result = ports.foldl(0, |accum, port| *accum + port.recv() );
284284
# fn some_expensive_computation(_i: uint) -> int { 42 }
285285
~~~
286286

287-
## Futures
287+
## Backgrounding computations: Futures
288288
With `extra::future`, rust has a mechanism for requesting a computation and getting the result
289289
later.
290290

@@ -329,6 +329,77 @@ fn main() {
329329
}
330330
~~~
331331

332+
## Sharing immutable data without copy: ARC
333+
334+
To share immutable data between tasks, a first approach would be to only use pipes as we have seen
335+
previously. A copy of the data to share would then be made for each task. In some cases, this would
336+
add up to a significant amount of wasted memory and would require copying the same data more than
337+
necessary.
338+
339+
To tackle this issue, one can use an Atomically Reference Counted wrapper (`ARC`) as implemented in
340+
the `extra` library of Rust. With an ARC, the data will no longer be copied for each task. The ARC
341+
acts as a reference to the shared data and only this reference is shared and cloned.
342+
343+
Here is a small example showing how to use ARCs. We wish to run concurrently several computations on
344+
a single large vector of floats. Each task needs the full vector to perform its duty.
345+
~~~
346+
use extra::arc::ARC;
347+
348+
fn pnorm(nums: &~[float], p: uint) -> float {
349+
(vec::foldl(0.0, *nums, |a,b| a+(*b).pow(p as float) )).pow(1f / (p as float))
350+
}
351+
352+
fn main() {
353+
let numbers=vec::from_fn(1000000, |_| rand::random::<float>());
354+
println(fmt!("Inf-norm = %?", numbers.max()));
355+
356+
let numbers_arc = ARC(numbers);
357+
358+
for uint::range(1,10) |num| {
359+
let (port, chan) = stream();
360+
chan.send(numbers_arc.clone());
361+
362+
do spawn {
363+
let local_arc : ARC<~[float]> = port.recv();
364+
let task_numbers = local_arc.get();
365+
println(fmt!("%u-norm = %?", num, pnorm(task_numbers, num)));
366+
}
367+
}
368+
}
369+
~~~
370+
371+
The function `pnorm` performs a simple computation on the vector (it computes the sum of its items
372+
at the power given as argument and takes the inverse power of this value). The ARC on the vector is
373+
created by the line
374+
~~~
375+
# use extra::arc::ARC;
376+
# let numbers=vec::from_fn(1000000, |_| rand::random::<float>());
377+
let numbers_arc=ARC(numbers);
378+
~~~
379+
and a clone of it is sent to each task
380+
~~~
381+
# use extra::arc::ARC;
382+
# let numbers=vec::from_fn(1000000, |_| rand::random::<float>());
383+
# let numbers_arc = ARC(numbers);
384+
# let (port, chan) = stream();
385+
chan.send(numbers_arc.clone());
386+
~~~
387+
copying only the wrapper and not its contents.
388+
389+
Each task recovers the underlying data by
390+
~~~
391+
# use extra::arc::ARC;
392+
# let numbers=vec::from_fn(1000000, |_| rand::random::<float>());
393+
# let numbers_arc=ARC(numbers);
394+
# let (port, chan) = stream();
395+
# chan.send(numbers_arc.clone());
396+
# let local_arc : ARC<~[float]> = port.recv();
397+
let task_numbers = local_arc.get();
398+
~~~
399+
and can use it as if it were local.
400+
401+
The `arc` module also implements ARCs around mutable data that are not covered here.
402+
332403
# Handling task failure
333404

334405
Rust has a built-in mechanism for raising exceptions. The `fail!()` macro

branches/try2/src/libextra/bitv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ pub impl Bitv {
462462
*/
463463
fn to_str(&self) -> ~str {
464464
let mut rs = ~"";
465-
for self.each() |i| { if i { rs += ~"1"; } else { rs += ~"0"; } };
465+
for self.each() |i| { if i { rs += "1"; } else { rs += "0"; } };
466466
rs
467467
}
468468

branches/try2/src/libextra/deque.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,31 @@ pub impl<T> Deque<T> {
125125
self.hi = (self.hi + 1u) % self.elts.len();
126126
self.nelts += 1u;
127127
}
128+
129+
/// Reserve capacity for exactly `n` elements in the given deque,
130+
/// doing nothing if `self`'s capacity is already equal to or greater
131+
/// than the requested capacity
132+
///
133+
/// # Arguments
134+
///
135+
/// * n - The number of elements to reserve space for
136+
fn reserve(&mut self, n: uint) {
137+
vec::reserve(&mut self.elts, n);
138+
}
139+
140+
/// Reserve capacity for at least `n` elements in the given deque,
141+
/// over-allocating in case the caller needs to reserve additional
142+
/// space.
143+
///
144+
/// Do nothing if `self`'s capacity is already equal to or greater
145+
/// than the requested capacity.
146+
///
147+
/// # Arguments
148+
///
149+
/// * n - The number of elements to reserve space for
150+
fn reserve_at_least(&mut self, n: uint) {
151+
vec::reserve_at_least(&mut self.elts, n);
152+
}
128153
}
129154

130155
/// Grow is only called on full elts, so nelts is also len(elts), unlike
@@ -149,6 +174,7 @@ mod tests {
149174
use super::*;
150175
use core::cmp::Eq;
151176
use core::kinds::Copy;
177+
use core::vec::capacity;
152178

153179
#[test]
154180
fn test_simple() {
@@ -328,4 +354,29 @@ mod tests {
328354
}
329355

330356
}
357+
358+
#[test]
359+
fn test_reserve() {
360+
let mut d = Deque::new();
361+
d.add_back(0u64);
362+
d.reserve(50);
363+
assert_eq!(capacity(&mut d.elts), 50);
364+
let mut d = Deque::new();
365+
d.add_back(0u32);
366+
d.reserve(50);
367+
assert_eq!(capacity(&mut d.elts), 50);
368+
}
369+
370+
#[test]
371+
fn test_reserve_at_least() {
372+
let mut d = Deque::new();
373+
d.add_back(0u64);
374+
d.reserve_at_least(50);
375+
assert_eq!(capacity(&mut d.elts), 64);
376+
let mut d = Deque::new();
377+
d.add_back(0u32);
378+
d.reserve_at_least(50);
379+
assert_eq!(capacity(&mut d.elts), 64);
380+
}
381+
331382
}

branches/try2/src/libextra/getopts.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,19 @@ pub enum Fail_ {
193193
pub fn fail_str(f: Fail_) -> ~str {
194194
return match f {
195195
ArgumentMissing(ref nm) => {
196-
~"Argument to option '" + *nm + ~"' missing."
196+
~"Argument to option '" + *nm + "' missing."
197197
}
198198
UnrecognizedOption(ref nm) => {
199-
~"Unrecognized option: '" + *nm + ~"'."
199+
~"Unrecognized option: '" + *nm + "'."
200200
}
201201
OptionMissing(ref nm) => {
202-
~"Required option '" + *nm + ~"' missing."
202+
~"Required option '" + *nm + "' missing."
203203
}
204204
OptionDuplicated(ref nm) => {
205-
~"Option '" + *nm + ~"' given more than once."
205+
~"Option '" + *nm + "' given more than once."
206206
}
207207
UnexpectedArgument(ref nm) => {
208-
~"Option " + *nm + ~" does not take an argument."
208+
~"Option " + *nm + " does not take an argument."
209209
}
210210
};
211211
}
@@ -618,7 +618,7 @@ pub mod groups {
618618
row += match hasarg {
619619
No => ~"",
620620
Yes => hint,
621-
Maybe => ~"[" + hint + ~"]",
621+
Maybe => ~"[" + hint + "]",
622622
};
623623

624624
// FIXME: #5516
@@ -650,10 +650,10 @@ pub mod groups {
650650
row
651651
});
652652

653-
return str::to_owned(brief) +
654-
~"\n\nOptions:\n" +
653+
return str::to_owned(brief) +
654+
"\n\nOptions:\n" +
655655
str::connect(rows, "\n") +
656-
~"\n\n";
656+
"\n\n";
657657
}
658658
} // end groups module
659659

branches/try2/src/libextra/json.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ fn escape_str(s: &str) -> ~str {
4747
let mut escaped = ~"\"";
4848
for str::each_char(s) |c| {
4949
match c {
50-
'"' => escaped += ~"\\\"",
51-
'\\' => escaped += ~"\\\\",
52-
'\x08' => escaped += ~"\\b",
53-
'\x0c' => escaped += ~"\\f",
54-
'\n' => escaped += ~"\\n",
55-
'\r' => escaped += ~"\\r",
56-
'\t' => escaped += ~"\\t",
50+
'"' => escaped += "\\\"",
51+
'\\' => escaped += "\\\\",
52+
'\x08' => escaped += "\\b",
53+
'\x0c' => escaped += "\\f",
54+
'\n' => escaped += "\\n",
55+
'\r' => escaped += "\\r",
56+
'\t' => escaped += "\\t",
5757
_ => escaped += str::from_char(c)
5858
}
5959
};
6060

61-
escaped += ~"\"";
61+
escaped += "\"";
6262

6363
escaped
6464
}

branches/try2/src/libextra/md4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub fn md4_str(msg: &[u8]) -> ~str {
115115
let mut i = 0u32;
116116
while i < 4u32 {
117117
let byte = (u >> (i * 8u32)) as u8;
118-
if byte <= 16u8 { result += ~"0"; }
118+
if byte <= 16u8 { result += "0"; }
119119
result += uint::to_str_radix(byte as uint, 16u);
120120
i += 1u32;
121121
}

branches/try2/src/libextra/std.rc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub mod serialize;
123123
// A curious inner-module that's not exported that contains the binding
124124
// 'extra' so that macro-expanded references to std::serialize and such
125125
// can be resolved within libextra.
126-
#[doc(hidden)] // FIXME #3538
126+
#[doc(hidden)]
127127
pub mod std {
128128
pub use serialize;
129129
pub use test;
@@ -134,7 +134,7 @@ pub mod std {
134134
pub use core::cmp;
135135
pub use core::sys;
136136
}
137-
#[doc(hidden)] // FIXME #3538
137+
#[doc(hidden)]
138138
pub mod extra {
139139
pub use serialize;
140140
pub use test;

branches/try2/src/libextra/time.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 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
//
@@ -22,11 +22,11 @@ pub mod rustrt {
2222
pub unsafe fn precise_time_ns(ns: &mut u64);
2323

2424
pub unsafe fn rust_tzset();
25-
// FIXME: The i64 values can be passed by-val when #2064 is fixed.
25+
2626
pub unsafe fn rust_gmtime(sec: i64, nsec: i32, result: &mut Tm);
2727
pub unsafe fn rust_localtime(sec: i64, nsec: i32, result: &mut Tm);
28-
pub unsafe fn rust_timegm(tm: &Tm, sec: &mut i64);
29-
pub unsafe fn rust_mktime(tm: &Tm, sec: &mut i64);
28+
pub unsafe fn rust_timegm(tm: &Tm) -> i64;
29+
pub unsafe fn rust_mktime(tm: &Tm) -> i64;
3030
}
3131
}
3232

@@ -177,12 +177,11 @@ pub impl Tm {
177177
/// Convert time to the seconds from January 1, 1970
178178
fn to_timespec(&self) -> Timespec {
179179
unsafe {
180-
let mut sec = 0i64;
181-
if self.tm_gmtoff == 0_i32 {
182-
rustrt::rust_timegm(self, &mut sec);
183-
} else {
184-
rustrt::rust_mktime(self, &mut sec);
185-
}
180+
let sec = match self.tm_gmtoff {
181+
0_i32 => rustrt::rust_timegm(self),
182+
_ => rustrt::rust_mktime(self)
183+
};
184+
186185
Timespec::new(sec, self.tm_nsec)
187186
}
188187
}

branches/try2/src/librustc/back/arm.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,42 @@ pub fn get_target_strs(target_os: session::os) -> target_strs::t {
2222
data_layout: match target_os {
2323
session::os_macos => {
2424
~"e-p:32:32:32" +
25-
~"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64" +
26-
~"-f32:32:32-f64:64:64" +
27-
~"-v64:64:64-v128:64:128" +
28-
~"-a0:0:64-n32"
25+
"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64" +
26+
"-f32:32:32-f64:64:64" +
27+
"-v64:64:64-v128:64:128" +
28+
"-a0:0:64-n32"
2929
}
3030

3131
session::os_win32 => {
3232
~"e-p:32:32:32" +
33-
~"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64" +
34-
~"-f32:32:32-f64:64:64" +
35-
~"-v64:64:64-v128:64:128" +
36-
~"-a0:0:64-n32"
33+
"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64" +
34+
"-f32:32:32-f64:64:64" +
35+
"-v64:64:64-v128:64:128" +
36+
"-a0:0:64-n32"
3737
}
3838

3939
session::os_linux => {
4040
~"e-p:32:32:32" +
41-
~"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64" +
42-
~"-f32:32:32-f64:64:64" +
43-
~"-v64:64:64-v128:64:128" +
44-
~"-a0:0:64-n32"
41+
"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64" +
42+
"-f32:32:32-f64:64:64" +
43+
"-v64:64:64-v128:64:128" +
44+
"-a0:0:64-n32"
4545
}
4646

4747
session::os_android => {
4848
~"e-p:32:32:32" +
49-
~"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64" +
50-
~"-f32:32:32-f64:64:64" +
51-
~"-v64:64:64-v128:64:128" +
52-
~"-a0:0:64-n32"
49+
"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64" +
50+
"-f32:32:32-f64:64:64" +
51+
"-v64:64:64-v128:64:128" +
52+
"-a0:0:64-n32"
5353
}
5454

5555
session::os_freebsd => {
5656
~"e-p:32:32:32" +
57-
~"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64" +
58-
~"-f32:32:32-f64:64:64" +
59-
~"-v64:64:64-v128:64:128" +
60-
~"-a0:0:64-n32"
57+
"-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64" +
58+
"-f32:32:32-f64:64:64" +
59+
"-v64:64:64-v128:64:128" +
60+
"-a0:0:64-n32"
6161
}
6262
},
6363

0 commit comments

Comments
 (0)