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
1212 test = false
1313
1414 [features]
15−parallel = ["crossbeam-utils", "crossbeam-channel", "num_cpus", "jwalk"]
15+parallel = ["crossbeam-utils", "crossbeam-channel", "num_cpus", "jwalk", "thread-scoped"]
1616 fast-sha1 = ["fastsha1"]
1717 interrupt-handler = ["ctrlc"]
1818 disable-interrupts = []
@@ -22,11 +22,11 @@ io-pipe = ["bytes"] · 3 unmodified lines
2222 name = "parallel"
2323 path = "tests/parallel.rs"
2424 required-features = ["parallel"]
25−#
26−#[[test]]
27−#name = "parallel_shared"
28−#path = "tests/parallel_shared.rs"
29−#required-features = ["parallel"]
25+
26+[[test]]
27+name = "parallel_shared"
28+path = "tests/parallel_shared.rs"
29+required-features = ["parallel"]
3030
3131 [[test]]
3232 name = "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
8383
8484 #[cfg(feature = "parallel")]
8585 impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> {
86− /// Instantiate a new iterator and start working in threads
87− /// TODO: make unsafe to have caller assure invariants will be met for safety
88− pub fn new<Input, ThreadStateFn, ConsumeFn, I, O, S>(
86+ /// Instantiate a new iterator and start working in threads.
87+ /// For a description of parameters, see [`in_parallel()`].
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>(
8994 input: Input,
9095 thread_limit: Option<usize>,
9196 new_thread_state: ThreadStateFn,
@@ -106,7 +111,7 @@ impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> { · 14 unmodified lines
106111 let (send_input, receive_input) = crossbeam_channel::bounded::<I>(num_threads);
107112 let (send_result, receive_result) = std::sync::mpsc::sync_channel::<O>(num_threads);
108113 for thread_id in 0..num_threads {
109− #[allow(unsafe_code)]
114+ #[allow(unsafe_code, unused_unsafe)]
110115 let handle = unsafe {
111116 thread_scoped::scoped({
112117 let send_result = send_result.clone();
@@ -124,7 +129,7 @@ impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> { · 11 unmodified lines
124129 threads.push(handle);
125130 }
126131 threads.push(
127− #[allow(unsafe_code)]
132+ #[allow(unsafe_code, unused_unsafe)]
128133 unsafe {
129134 thread_scoped::scoped(move || {
130135 for item in input {
git-features/src/parallel/mod.rs+2−0
@@ -27,6 +27,8 @@ · 26 unmodified lines
2727 //!
2828 //! It also offers borrowing of stack-local variables which is safe only as long as the `SteppedReduce` instance isn't leaked which
2929 //! 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.
3032 //!
3133 //! In an `async` context this means that progress is only made each time `next()` is called on the iterator, while merely dropping
3234 //! 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
5555 O: Send,
5656 {
5757 /// 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>(
5965 input: Input,
6066 _thread_limit: Option<usize>,
6167 new_thread_state: ThreadStateFn,
git-features/tests/parallel_shared.rs+18−14
@@ -37,13 +37,15 @@ fn in_parallel() { · 36 unmodified lines
3737
3838 #[test]
3939 fn stepped_reduce_next() {
40− let mut iter = parallel::SteppedReduce::new(
41− std::iter::from_fn(|| Some(1)).take(100),
42− None,
43− |_n| (),
44− |input, _state| input,
45− Adder::default(),
46− );
40+ let mut iter = unsafe {
41+ parallel::SteppedReduce::new(
42+ std::iter::from_fn(|| Some(1)).take(100),
43+ None,
44+ |_n| (),
45+ |input, _state| input,
46+ Adder::default(),
47+ )
48+ };
4749
4850 let mut aggregate = 0;
4951 for value in iter.by_ref() {
@@ -54,13 +56,15 @@ fn stepped_reduce_next() { · 4 unmodified lines
5456
5557 #[test]
5658 fn stepped_reduce_finalize() {
57− let iter = parallel::SteppedReduce::new(
58− std::iter::from_fn(|| Some(1)).take(100),
59− None,
60− |_n| (),
61− |input, _state| input,
62− Adder::default(),
63− );
59+ let iter = unsafe {
60+ parallel::SteppedReduce::new(
61+ std::iter::from_fn(|| Some(1)).take(100),
62+ None,
63+ |_n| (),
64+ |input, _state| input,
65+ Adder::default(),
66+ )
67+ };
6468
6569 assert_eq!(iter.finalize().expect("success"), 100);
6670 }

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