History

Make iterator creation unsafe and document why #(67)

Sebastian Thiel committed 5 years ago

Commit 593d5df1 parent 4a7ef7d5 files · +43 −26Browse files

Unified · Split

5 files
  1. git-features/Cargo.toml +6 −6
  2. git-features/src/parallel/in_parallel.rs +10 −5
  3. git-features/src/parallel/mod.rs +2 −0
  4. git-features/src/parallel/serial.rs +7 −1
  5. git-features/tests/parallel_shared.rs +18 −14
git-features/Cargo.toml+6−6
@@ -12,7 +12,7 @@ doctest = false · 11 unmodified lines
12test = false12test = false
13 13
14[features]14[features]
15parallel = ["crossbeam-utils", "crossbeam-channel", "num_cpus", "jwalk"]15parallel = ["crossbeam-utils", "crossbeam-channel", "num_cpus", "jwalk", "thread-scoped"]
16fast-sha1 = ["fastsha1"]16fast-sha1 = ["fastsha1"]
17interrupt-handler = ["ctrlc"]17interrupt-handler = ["ctrlc"]
18disable-interrupts = []18disable-interrupts = []
@@ -22,11 +22,11 @@ io-pipe = ["bytes"] · 3 unmodified lines
22name = "parallel"22name = "parallel"
23path = "tests/parallel.rs"23path = "tests/parallel.rs"
24required-features = ["parallel"]24required-features = ["parallel"]
25#25
26#[[test]]26[[test]]
27#name = "parallel_shared"27name = "parallel_shared"
28#path = "tests/parallel_shared.rs"28path = "tests/parallel_shared.rs"
29#required-features = ["parallel"]29required-features = ["parallel"]
30 30
31[[test]]31[[test]]
32name = "parallel_shared_in_serial"32name = "parallel_shared_in_serial"
git-features/src/parallel/in_parallel.rs+10−5
@@ -83,9 +83,14 @@ pub struct SteppedReduce<'a, Reducer: crate::parallel::Reducer> { · 82 unmodified lines
83 83
84#[cfg(feature = "parallel")]84#[cfg(feature = "parallel")]
85impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> {85impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> {
86 /// Instantiate a new iterator and start working in threads86 /// Instantiate a new iterator and start working in threads.
87 /// TODO: make unsafe to have caller assure invariants will be met for safety87 /// For a description of parameters, see [`in_parallel()`].
88 pub fn new<Input, ThreadStateFn, ConsumeFn, I, O, S>(88 ///
89 /// # Unsafety
90 ///
91 /// Read all about it in the [module documentation][crate::parallel].
92 #[allow(unsafe_code)]
93 pub unsafe fn new<Input, ThreadStateFn, ConsumeFn, I, O, S>(
89 input: Input,94 input: Input,
90 thread_limit: Option<usize>,95 thread_limit: Option<usize>,
91 new_thread_state: ThreadStateFn,96 new_thread_state: ThreadStateFn,
@@ -106,7 +111,7 @@ impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> { · 14 unmodified lines
106 let (send_input, receive_input) = crossbeam_channel::bounded::<I>(num_threads);111 let (send_input, receive_input) = crossbeam_channel::bounded::<I>(num_threads);
107 let (send_result, receive_result) = std::sync::mpsc::sync_channel::<O>(num_threads);112 let (send_result, receive_result) = std::sync::mpsc::sync_channel::<O>(num_threads);
108 for thread_id in 0..num_threads {113 for thread_id in 0..num_threads {
109 #[allow(unsafe_code)]114 #[allow(unsafe_code, unused_unsafe)]
110 let handle = unsafe {115 let handle = unsafe {
111 thread_scoped::scoped({116 thread_scoped::scoped({
112 let send_result = send_result.clone();117 let send_result = send_result.clone();
@@ -124,7 +129,7 @@ impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> { · 11 unmodified lines
124 threads.push(handle);129 threads.push(handle);
125 }130 }
126 threads.push(131 threads.push(
127 #[allow(unsafe_code)]132 #[allow(unsafe_code, unused_unsafe)]
128 unsafe {133 unsafe {
129 thread_scoped::scoped(move || {134 thread_scoped::scoped(move || {
130 for item in input {135 for item in input {
git-features/src/parallel/mod.rs+2−0
@@ -27,6 +27,8 @@ · 26 unmodified lines
27//!27//!
28//! It also offers borrowing of stack-local variables which is safe only as long as the `SteppedReduce` instance isn't leaked which28//! It also offers borrowing of stack-local variables which is safe only as long as the `SteppedReduce` instance isn't leaked which
29//! will cause threads to access data that by then is likely gone.29//! will cause threads to access data that by then is likely gone.
30//! _Please note_ that doing so is **unsafe** because the caller must assure that the iterator will not be leaked. Doing so while
31//! it is on the stack will make data unavailable which can still be accessed by threads.
30//!32//!
31//! In an `async` context this means that progress is only made each time `next()` is called on the iterator, while merely dropping33//! In an `async` context this means that progress is only made each time `next()` is called on the iterator, while merely dropping
32//! the iterator will wind down the computation without any result.34//! the iterator will wind down the computation without any result.
git-features/src/parallel/serial.rs+7−1
@@ -55,7 +55,13 @@ where · 54 unmodified lines
55 O: Send,55 O: Send,
56{56{
57 /// Instantiate a new iterator.57 /// Instantiate a new iterator.
58 pub fn new<ThreadStateFn>(58 /// For a description of parameters, see [`in_parallel()`].
59 ///
60 /// # Unsafety
61 ///
62 /// Read all about it in the [module documentation][crate::parallel].
63 #[allow(unsafe_code)]
64 pub unsafe fn new<ThreadStateFn>(
59 input: Input,65 input: Input,
60 _thread_limit: Option<usize>,66 _thread_limit: Option<usize>,
61 new_thread_state: ThreadStateFn,67 new_thread_state: ThreadStateFn,
git-features/tests/parallel_shared.rs+18−14
@@ -37,13 +37,15 @@ fn in_parallel() { · 36 unmodified lines
37 37
38#[test]38#[test]
39fn stepped_reduce_next() {39fn stepped_reduce_next() {
40 let mut iter = parallel::SteppedReduce::new(40 let mut iter = unsafe {
41 std::iter::from_fn(|| Some(1)).take(100),41 parallel::SteppedReduce::new(
42 None,42 std::iter::from_fn(|| Some(1)).take(100),
43 |_n| (),43 None,
44 |input, _state| input,44 |_n| (),
45 Adder::default(),45 |input, _state| input,
46 );46 Adder::default(),
47 )
48 };
47 49
48 let mut aggregate = 0;50 let mut aggregate = 0;
49 for value in iter.by_ref() {51 for value in iter.by_ref() {
@@ -54,13 +56,15 @@ fn stepped_reduce_next() { · 4 unmodified lines
54 56
55#[test]57#[test]
56fn stepped_reduce_finalize() {58fn stepped_reduce_finalize() {
57 let iter = parallel::SteppedReduce::new(59 let iter = unsafe {
58 std::iter::from_fn(|| Some(1)).take(100),60 parallel::SteppedReduce::new(
59 None,61 std::iter::from_fn(|| Some(1)).take(100),
60 |_n| (),62 None,
61 |input, _state| input,63 |_n| (),
62 Adder::default(),64 |input, _state| input,
63 );65 Adder::default(),
66 )
67 };
64 68
65 assert_eq!(iter.finalize().expect("success"), 100);69 assert_eq!(iter.finalize().expect("success"), 100);
66}70}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t