Unified · Split
| @@ -12,7 +12,7 @@ doctest = false · 11 unmodified lines | ||
| 12 | 12 | test = false |
| 13 | 13 | |
| 14 | 14 | [features] |
| 15 | −parallel = ["crossbeam-utils", "crossbeam-channel", "num_cpus", "jwalk"] | |
| 15 | +parallel = ["crossbeam-utils", "crossbeam-channel", "num_cpus", "jwalk", "thread-scoped"] | |
| 16 | 16 | fast-sha1 = ["fastsha1"] |
| 17 | 17 | interrupt-handler = ["ctrlc"] |
| 18 | 18 | disable-interrupts = [] |
| @@ -22,11 +22,11 @@ io-pipe = ["bytes"] · 3 unmodified lines | ||
| 22 | 22 | name = "parallel" |
| 23 | 23 | path = "tests/parallel.rs" |
| 24 | 24 | 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"] | |
| 30 | 30 | |
| 31 | 31 | [[test]] |
| 32 | 32 | name = "parallel_shared_in_serial" |
| @@ -83,9 +83,14 @@ pub struct SteppedReduce<'a, Reducer: crate::parallel::Reducer> { · 82 unmodified lines | ||
| 83 | 83 | |
| 84 | 84 | #[cfg(feature = "parallel")] |
| 85 | 85 | 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>( | |
| 89 | 94 | input: Input, |
| 90 | 95 | thread_limit: Option<usize>, |
| 91 | 96 | new_thread_state: ThreadStateFn, |
| @@ -106,7 +111,7 @@ impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> { · 14 unmodified lines | ||
| 106 | 111 | let (send_input, receive_input) = crossbeam_channel::bounded::<I>(num_threads); |
| 107 | 112 | let (send_result, receive_result) = std::sync::mpsc::sync_channel::<O>(num_threads); |
| 108 | 113 | for thread_id in 0..num_threads { |
| 109 | − #[allow(unsafe_code)] | |
| 114 | + #[allow(unsafe_code, unused_unsafe)] | |
| 110 | 115 | let handle = unsafe { |
| 111 | 116 | thread_scoped::scoped({ |
| 112 | 117 | let send_result = send_result.clone(); |
| @@ -124,7 +129,7 @@ impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> { · 11 unmodified lines | ||
| 124 | 129 | threads.push(handle); |
| 125 | 130 | } |
| 126 | 131 | threads.push( |
| 127 | − #[allow(unsafe_code)] | |
| 132 | + #[allow(unsafe_code, unused_unsafe)] | |
| 128 | 133 | unsafe { |
| 129 | 134 | thread_scoped::scoped(move || { |
| 130 | 135 | for item in input { |
| @@ -27,6 +27,8 @@ · 26 unmodified lines | ||
| 27 | 27 | //! |
| 28 | 28 | //! It also offers borrowing of stack-local variables which is safe only as long as the `SteppedReduce` instance isn't leaked which |
| 29 | 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 | 33 | //! In an `async` context this means that progress is only made each time `next()` is called on the iterator, while merely dropping |
| 32 | 34 | //! the iterator will wind down the computation without any result. |
| @@ -55,7 +55,13 @@ where · 54 unmodified lines | ||
| 55 | 55 | O: Send, |
| 56 | 56 | { |
| 57 | 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 | 65 | input: Input, |
| 60 | 66 | _thread_limit: Option<usize>, |
| 61 | 67 | new_thread_state: ThreadStateFn, |
| @@ -37,13 +37,15 @@ fn in_parallel() { · 36 unmodified lines | ||
| 37 | 37 | |
| 38 | 38 | #[test] |
| 39 | 39 | 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 | + }; | |
| 47 | 49 | |
| 48 | 50 | let mut aggregate = 0; |
| 49 | 51 | for value in iter.by_ref() { |
| @@ -54,13 +56,15 @@ fn stepped_reduce_next() { · 4 unmodified lines | ||
| 54 | 56 | |
| 55 | 57 | #[test] |
| 56 | 58 | 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 | + }; | |
| 64 | 68 | |
| 65 | 69 | assert_eq!(iter.finalize().expect("success"), 100); |
| 66 | 70 | } |