Unified · Split
| @@ -12,7 +12,7 @@ doctest = false · 11 unmodified lines | |||
| 12 | test = false | 12 | test = false |
| 13 | 13 | ||
| 14 | [features] | 14 | [features] |
| 15 | parallel = ["crossbeam-utils", "crossbeam-channel", "num_cpus", "jwalk"] | 15 | parallel = ["crossbeam-utils", "crossbeam-channel", "num_cpus", "jwalk", "thread-scoped"] |
| 16 | fast-sha1 = ["fastsha1"] | 16 | fast-sha1 = ["fastsha1"] |
| 17 | interrupt-handler = ["ctrlc"] | 17 | interrupt-handler = ["ctrlc"] |
| 18 | disable-interrupts = [] | 18 | disable-interrupts = [] |
| @@ -22,11 +22,11 @@ io-pipe = ["bytes"] · 3 unmodified lines | |||
| 22 | name = "parallel" | 22 | name = "parallel" |
| 23 | path = "tests/parallel.rs" | 23 | path = "tests/parallel.rs" |
| 24 | required-features = ["parallel"] | 24 | required-features = ["parallel"] |
| 25 | # | 25 | |
| 26 | #[[test]] | 26 | [[test]] |
| 27 | #name = "parallel_shared" | 27 | name = "parallel_shared" |
| 28 | #path = "tests/parallel_shared.rs" | 28 | path = "tests/parallel_shared.rs" |
| 29 | #required-features = ["parallel"] | 29 | required-features = ["parallel"] |
| 30 | 30 | ||
| 31 | [[test]] | 31 | [[test]] |
| 32 | name = "parallel_shared_in_serial" | 32 | name = "parallel_shared_in_serial" |
| @@ -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")] |
| 85 | impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> { | 85 | impl<'a, Reducer: crate::parallel::Reducer> SteppedReduce<'a, Reducer> { |
| 86 | /// Instantiate a new iterator and start working in threads | 86 | /// Instantiate a new iterator and start working in threads. |
| 87 | /// TODO: make unsafe to have caller assure invariants will be met for safety | 87 | /// 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 { |
| @@ -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 which | 28 | //! 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 dropping | 33 | //! 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. |
| @@ -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, |
| @@ -37,13 +37,15 @@ fn in_parallel() { · 36 unmodified lines | |||
| 37 | 37 | ||
| 38 | #[test] | 38 | #[test] |
| 39 | fn stepped_reduce_next() { | 39 | fn 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] |
| 56 | fn stepped_reduce_finalize() { | 58 | fn 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 | } |