Debian Codegraph @

src/parallel/in_parallel.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#, AtomicIsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicIsize#, AtomicUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicUsize#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#};
2
3 use craterust-analyzer cargo gix-features 0.48.0 crate/::parallelrust-analyzer cargo gix-features 0.48.0 parallel/::{num_threadsrust-analyzer cargo gix-features 0.48.0 parallel/num_threads()., Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#};
4
5 /// A scope to start threads within.
6 pub type Scoperust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/Scope#<'scope, 'env> = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Scoperust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/Scope#<'scope, 'env>;
7
8 /// Runs `left` and `right` in parallel, returning their output when both are done.
9 pub fn joinrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/join().<O1: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#, O2: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#>(left: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> O1 + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#, right: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> O2 + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#) -> (O1, O2) {
10 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::scoperust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/scope().(|s| {
11 let left = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]new().()
12 .namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]name().("gitoxide.join.left".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
13 .spawn_scopedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[Builder]spawn_scoped().(s, left)
14 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("valid name");
15 let right = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]new().()
16 .namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]name().("gitoxide.join.right".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
17 .spawn_scopedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[Builder]spawn_scoped().(s, right)
18 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("valid name");
19 (left.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[`ScopedJoinHandle<'scope, T>`]join().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), right.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[`ScopedJoinHandle<'scope, T>`]join().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().())
20 })
21 }
22
23 /// Runs `f` with a scope to be used for spawning threads that will not outlive the function call.
24 /// That way it's possible to handle threads without needing the 'static lifetime for data they interact with.
25 ///
26 /// Note that the threads should not rely on actual parallelism as threading might be turned off entirely, hence should not
27 /// connect each other with channels as deadlock would occur in single-threaded mode.
28 pub fn threadsrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/threads().<'env, F, R>(f: F) -> R
29 where
30 F: for<'scope> FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&'scope stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Scoperust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/Scope#<'scope, 'env>) -> R,
31 {
32 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::scoperust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/scope().(f)
33 }
34
35 /// Create a builder for threads which allows them to be spawned into a scope and configured prior to spawning.
36 pub fn build_threadrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/build_thread().() -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder# {
37 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]new().()
38 }
39
40 /// Read items from `input` and `consume` them in multiple threads,
41 /// whose output is collected by a `reducer`. Its task is to
42 /// aggregate these outputs into the final result returned by this function, with the benefit of not having to be thread-safe.
43 ///
44 /// * if `thread_limit` is `Some`, then the given number of threads will be used. If `None`, all logical cores will be used.
45 /// * `new_thread_state(thread_number) -> State` produces thread-local state once per thread to be passed to `consume`
46 /// * `consume(Item, &mut State) -> Output` produces an output given an input obtained by `input` along with mutable state initially
47 /// created by `new_thread_state(…)`.
48 /// * For `reducer`, see the [`Reduce`] trait
49 pub fn in_parallelrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/in_parallel().<I, S, O, R>(
50 input: impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = I> + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
51 thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
52 new_thread_state: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(usize) -> S + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
53 consume: impl FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(I, &mut S) -> O + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
54 mut reducer: R,
55 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<<R as Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#, <R as Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#>
56 where
57 R: Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#<Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input# = O>,
58 I: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
59 O: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
60 {
61 let num_threads = num_threadsrust-analyzer cargo gix-features 0.48.0 parallel/num_threads().(thread_limit);
62 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::scoperust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/scope().(move |s| {
63 let receive_result = {
64 let (send_input, receive_input) = crossbeam_channelrust-analyzer cargo crossbeam-channel 0.5.15 crate/::boundedrust-analyzer cargo crossbeam-channel 0.5.15 channel/bounded().::<I>(num_threads);
65 let (send_result, receive_result) = crossbeam_channelrust-analyzer cargo crossbeam-channel 0.5.15 crate/::boundedrust-analyzer cargo crossbeam-channel 0.5.15 channel/bounded().::<O>(num_threads);
66 for thread_id in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#num_threads {
67 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]new().()
68 .namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]name().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("gitoxide.in_parallel.produce.{thread_id}"))
69 .spawn_scopedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[Builder]spawn_scoped().(s, {
70 let send_result = send_result.clonerust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Sender<T>`][Clone]clone().();
71 let receive_input = receive_input.clonerust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Receiver<T>`][Clone]clone().();
72 let new_thread_state = new_thread_state.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().();
73 let mut consume = consume.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().();
74 move || {
75 let mut state = new_thread_state(thread_id);
76 for item in receive_input {
77 if send_result.sendrust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Sender<T>`]send().(consume(item, &mut state)).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
78 break;
79 }
80 }
81 }
82 })
83 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("valid name");
84 }
85 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]new().()
86 .namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]name().("gitoxide.in_parallel.feed".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
87 .spawn_scopedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[Builder]spawn_scoped().(s, move || {
88 for item in input {
89 if send_input.sendrust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Sender<T>`]send().(item).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
90 break;
91 }
92 }
93 })
94 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("valid name");
95 receive_result
96 };
97
98 for item in receive_result {
99 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(reducer.feedrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#feed().(item)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
100 }
101 reducer.finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#finalize().()
102 })
103 }
104
105 /// Read items from `input` and `consume` them in multiple threads,
106 /// whose output is collected by a `reducer`. Its task is to
107 /// aggregate these outputs into the final result returned by this function with the benefit of not having to be thread-safe.
108 /// Call `finalize` to finish the computation, once per thread, if there was no error sending results earlier.
109 ///
110 /// * if `thread_limit` is `Some`, then the given number of threads will be used. If `None`, all logical cores will be used.
111 /// * `new_thread_state(thread_number) -> State` produces thread-local state once per thread to be passed to `consume`
112 /// * `consume(Item, &mut State) -> Output` produces an output given an input obtained by `input` along with mutable state initially
113 /// created by `new_thread_state(…)`.
114 /// * `finalize(State) -> Output` is called to potentially process remaining work that was placed in `State`.
115 /// * For `reducer`, see the [`Reduce`] trait
116 pub fn in_parallel_with_finalizerust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/in_parallel_with_finalize().<I, S, O, R>(
117 input: impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = I> + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
118 thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
119 new_thread_state: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(usize) -> S + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
120 consume: impl FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(I, &mut S) -> O + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
121 finalize: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(S) -> O + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
122 mut reducer: R,
123 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<<R as Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#, <R as Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#>
124 where
125 R: Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#<Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input# = O>,
126 I: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
127 O: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
128 {
129 let num_threads = num_threadsrust-analyzer cargo gix-features 0.48.0 parallel/num_threads().(thread_limit);
130 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::scoperust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/scope().(move |s| {
131 let receive_result = {
132 let (send_input, receive_input) = crossbeam_channelrust-analyzer cargo crossbeam-channel 0.5.15 crate/::boundedrust-analyzer cargo crossbeam-channel 0.5.15 channel/bounded().::<I>(num_threads);
133 let (send_result, receive_result) = crossbeam_channelrust-analyzer cargo crossbeam-channel 0.5.15 crate/::boundedrust-analyzer cargo crossbeam-channel 0.5.15 channel/bounded().::<O>(num_threads);
134 for thread_id in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#num_threads {
135 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]new().()
136 .namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]name().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("gitoxide.in_parallel.produce.{thread_id}"))
137 .spawn_scopedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[Builder]spawn_scoped().(s, {
138 let send_result = send_result.clonerust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Sender<T>`][Clone]clone().();
139 let receive_input = receive_input.clonerust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Receiver<T>`][Clone]clone().();
140 let new_thread_state = new_thread_state.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().();
141 let mut consume = consume.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().();
142 let finalize = finalize.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().();
143 move || {
144 let mut state = new_thread_state(thread_id);
145 let mut can_send = true;
146 for item in receive_input {
147 if send_result.sendrust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Sender<T>`]send().(consume(item, &mut state)).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
148 can_send = false;
149 break;
150 }
151 }
152 if can_send {
153 send_result.sendrust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Sender<T>`]send().(finalize(state)).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().();
154 }
155 }
156 })
157 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("valid name");
158 }
159 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]new().()
160 .namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]name().("gitoxide.in_parallel.feed".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
161 .spawn_scopedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[Builder]spawn_scoped().(s, move || {
162 for item in input {
163 if send_input.sendrust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Sender<T>`]send().(item).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() {
164 break;
165 }
166 }
167 })
168 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("valid name");
169 receive_result
170 };
171
172 for item in receive_result {
173 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(reducer.feedrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#feed().(item)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
174 }
175 reducer.finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#finalize().()
176 })
177 }
178
179 /// An experiment to have fine-grained per-item parallelization with built-in aggregation via thread state.
180 /// This is only good for operations where near-random access isn't detrimental, so it's not usually great
181 /// for file-io as it won't make use of sorted inputs well.
182 /// Note that `periodic` is not guaranteed to be called in case other threads come up first and finish too fast.
183 /// `consume(&mut item, &mut stat, &Scope, &threads_available, &should_interrupt)` is called for performing the actual computation.
184 /// Note that `threads_available` should be decremented to start a thread that can steal your own work (as stored in `item`),
185 /// which allows callees to implement their own work-stealing in case the work is distributed unevenly.
186 /// Work stealing should only start after having processed at least one item to give all threads naturally operating on the slice
187 /// some time to start. Starting threads while slice-workers are still starting up would lead to over-allocation of threads,
188 /// which is why the number of threads left may turn negative. Once threads are started and stopped, be sure to adjust
189 /// the thread-count accordingly.
190 // TODO: better docs
191 pub fn in_parallel_with_slicerust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/in_parallel_with_slice().<I, S, R, E>(
192 input: &mut [I],
193 thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
194 new_thread_state: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(usize) -> S + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
195 consume: impl FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&mut I, &mut S, &AtomicIsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicIsize#, &AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), E> + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
196 mut periodic: impl FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#> + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
197 state_to_rval: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(S) -> R + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
198 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<R>, E>
199 where
200 I: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
201 E: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
202 R: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#,
203 {
204 let num_threads = num_threadsrust-analyzer cargo gix-features 0.48.0 parallel/num_threads().(thread_limit);
205 let mut results = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(num_threads);
206 let stop_everything = &AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool][Default]default().();
207 let index = &AtomicUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicUsize#::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize][Default]default().();
208 let threads_left = &AtomicIsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicIsize#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicIsize]new().(num_threads as isize);
209
210 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::scoperust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/scope().({
211 move |s| {
212 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]new().()
213 .namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]name().("gitoxide.in_parallel_with_slice.watch-interrupts".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
214 .spawn_scopedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[Builder]spawn_scoped().(s, {
215 move || loop {
216 if stop_everything.loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#) {
217 break;
218 }
219
220 match periodic() {
221 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(duration) => stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(duration),
222 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
223 stop_everything.storerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]store().(true, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#);
224 break;
225 }
226 }
227 }
228 })
229 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("valid name");
230
231 let input_len = input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().();
232 struct Inputrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/Input#<I>(*mut I)
233 where
234 I: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#;
235
236 // SAFETY: I is Send, and we only use the pointer for creating new
237 // pointers (within the input slice) from the threads.
238 #[allow(unsafe_code)]
239 unsafe impl<I> Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# for Inputrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/Input#<I> where I: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# {}
240
241 let threads: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#num_threads)
242 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|thread_id| {
243 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::Builderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/Builder#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]new().()
244 .namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/builder/impl#[Builder]name().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("gitoxide.in_parallel_with_slice.produce.{thread_id}"))
245 .spawn_scopedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[Builder]spawn_scoped().(s, {
246 let new_thread_state = new_thread_state.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().();
247 let state_to_rval = state_to_rval.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().();
248 let mut consume = consume.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().();
249 let input = Inputrust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/Input#(input.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().());
250 move || {
251 let _ = &input;
252 threads_left.fetch_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicIsize]fetch_sub().(1, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#);
253 let mut state = new_thread_state(thread_id);
254 let res = (|| {
255 while let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(input_index) =
256 index.fetch_updaterust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize]fetch_update().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#, |x| {
257 (x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().input_len).then_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then_some().(x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1)
258 })
259 {
260 if stop_everything.loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#) {
261 break;
262 }
263 // SAFETY: our atomic counter for `input_index` is only ever incremented, yielding
264 // each item exactly once.
265 let item = {
266 #[allow(unsafe_code)]
267 unsafe {
268 &mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().input.0rust-analyzer cargo gix-features 0.48.0 parallel/in_parallel/Input#0..addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]add().(input_index)
269 }
270 };
271 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = consume(item, &mut state, threads_left, stop_everything) {
272 stop_everything.storerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]store().(true, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#);
273 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err);
274 }
275 }
276 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(state_to_rval(state))
277 })();
278 threads_left.fetch_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicIsize]fetch_add().(1, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#);
279 res
280 }
281 })
282 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("valid name")
283 })
284 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
285 for thread in threads {
286 match thread.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/scoped/impl#[`ScopedJoinHandle<'scope, T>`]join().() {
287 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(res) => {
288 results.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(res?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
289 }
290 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
291 // a panic happened, stop the world gracefully (even though we panic later)
292 stop_everything.storerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]store().(true, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#);
293 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::resume_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/resume_unwind().(err);
294 }
295 }
296 }
297
298 stop_everything.storerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]store().(true, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#);
299 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(results)
300 }
301 })
302 }