Skip to content

Commit 52072de

Browse files
committed
Add a Racy type to bench tests
1 parent 29b3698 commit 52072de

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/test/bench/shootout-reverse-complement.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extern crate libc;
4646

4747
use std::io::stdio::{stdin_raw, stdout_raw};
4848
use std::num::{div_rem};
49-
use std::ptr::{copy_memory};
49+
use std::ptr::{copy_memory, Unique};
5050
use std::io::{IoResult, EndOfFile};
5151

5252
struct Tables {
@@ -219,10 +219,15 @@ fn reverse_complement(seq: &mut [u8], tables: &Tables) {
219219
}
220220
}
221221

222+
223+
struct Racy<T>(T);
224+
225+
unsafe impl<T: 'static> Send for Racy<T> {}
226+
222227
/// Executes a closure in parallel over the given iterator over mutable slice.
223228
/// The closure `f` is run in parallel with an element of `iter`.
224229
fn parallel<'a, I, T, F>(mut iter: I, f: F)
225-
where T: Send + Sync,
230+
where T: 'a+Send + Sync,
226231
I: Iterator<&'a mut [T]>,
227232
F: Fn(&'a mut [T]) + Sync {
228233
use std::mem;
@@ -234,11 +239,11 @@ fn parallel<'a, I, T, F>(mut iter: I, f: F)
234239

235240
// Need to convert `f` and `chunk` to something that can cross the task
236241
// boundary.
237-
let f = &f as *const F as *const uint;
238-
let raw = chunk.repr();
242+
let f = Racy(&f as *const F as *const uint);
243+
let raw = Racy(chunk.repr());
239244
spawn(move|| {
240-
let f = f as *const F;
241-
unsafe { (*f)(mem::transmute(raw)) }
245+
let f = f.0 as *const F;
246+
unsafe { (*f)(mem::transmute(raw.0)) }
242247
drop(tx)
243248
});
244249
}

src/test/bench/shootout-spectralnorm.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ fn dot(v: &[f64], u: &[f64]) -> f64 {
108108
v.iter().zip(u.iter()).map(|(a, b)| *a * *b).sum()
109109
}
110110

111+
112+
struct Racy<T>(T);
113+
114+
unsafe impl<T: 'static> Send for Racy<T> {}
115+
111116
// Executes a closure in parallel over the given mutable slice. The closure `f`
112117
// is run in parallel and yielded the starting index within `v` as well as a
113118
// sub-slice of `v`.
@@ -122,11 +127,11 @@ fn parallel<'a, T, F>(v: &'a mut [T], f: F)
122127

123128
// Need to convert `f` and `chunk` to something that can cross the task
124129
// boundary.
125-
let f = &f as *const _ as *const uint;
126-
let raw = chunk.repr();
130+
let f = Racy(&f as *const _ as *const uint);
131+
let raw = Racy(chunk.repr());
127132
spawn(move|| {
128-
let f = f as *const F;
129-
unsafe { (*f)(i * size, mem::transmute(raw)) }
133+
let f = f.0 as *const F;
134+
unsafe { (*f)(i * size, mem::transmute(raw.0)) }
130135
drop(tx)
131136
});
132137
}

0 commit comments

Comments
 (0)