gix_features/parallel/
serial.rs1use crate::parallel::Reduce;
2
3#[cfg(not(feature = "parallel"))]
4mod not_parallel {
5 use std::sync::atomic::{AtomicBool, AtomicIsize};
6
7 pub fn join<O1, O2>(left: impl FnOnce() -> O1, right: impl FnOnce() -> O2) -> (O1, O2) {
9 (left(), right())
10 }
11
12 pub struct Scope<'scope, 'env: 'scope> {
14 _scope: std::marker::PhantomData<&'scope mut &'scope ()>,
15 _env: std::marker::PhantomData<&'env mut &'env ()>,
16 }
17
18 pub struct ThreadBuilder;
19
20 pub fn build_thread() -> ThreadBuilder {
22 ThreadBuilder
23 }
24
25 #[allow(unsafe_code)]
26 unsafe impl Sync for Scope<'_, '_> {}
27
28 impl ThreadBuilder {
29 pub fn name(self, _new: String) -> Self {
30 self
31 }
32 pub fn spawn_scoped<'scope, 'env, F, T>(
33 &self,
34 scope: &'scope Scope<'scope, 'env>,
35 f: F,
36 ) -> std::io::Result<ScopedJoinHandle<'scope, T>>
37 where
38 F: FnOnce() -> T + 'scope,
39 T: 'scope,
40 {
41 Ok(scope.spawn(f))
42 }
43 }
44
45 impl<'scope> Scope<'scope, '_> {
46 pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
48 where
49 F: FnOnce() -> T + 'scope,
50 T: 'scope,
51 {
52 ScopedJoinHandle {
53 result: f(),
54 _marker: Default::default(),
55 }
56 }
57 }
58
59 pub fn threads<'env, F, R>(f: F) -> R
62 where
63 F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> R,
64 {
65 f(&Scope {
66 _scope: Default::default(),
67 _env: Default::default(),
68 })
69 }
70
71 pub struct ScopedJoinHandle<'scope, T> {
76 result: T,
78 _marker: std::marker::PhantomData<&'scope mut &'scope ()>,
79 }
80
81 impl<T> ScopedJoinHandle<'_, T> {
82 pub fn join(self) -> std::thread::Result<T> {
83 Ok(self.result)
84 }
85 pub fn is_finished(&self) -> bool {
86 true
87 }
88 }
89
90 pub fn in_parallel_with_slice<I, S, R, E>(
95 input: &mut [I],
96 _thread_limit: Option<usize>,
97 new_thread_state: impl FnOnce(usize) -> S + Clone,
98 mut consume: impl FnMut(&mut I, &mut S, &AtomicIsize, &AtomicBool) -> Result<(), E> + Clone,
99 mut periodic: impl FnMut() -> Option<std::time::Duration>,
100 state_to_rval: impl FnOnce(S) -> R + Clone,
101 ) -> Result<Vec<R>, E> {
102 let mut state = new_thread_state(0);
103 let should_interrupt = &AtomicBool::default();
104 let threads_left = &AtomicIsize::default();
105 for item in input {
106 consume(item, &mut state, threads_left, should_interrupt)?;
107 if periodic().is_none() {
108 break;
109 }
110 }
111 Ok(vec![state_to_rval(state)])
112 }
113}
114
115#[cfg(not(feature = "parallel"))]
116pub use not_parallel::{build_thread, in_parallel_with_slice, join, threads, Scope};
117
118pub fn in_parallel<I, S, O, R>(
129 input: impl Iterator<Item = I>,
130 _thread_limit: Option<usize>,
131 new_thread_state: impl FnOnce(usize) -> S,
132 mut consume: impl FnMut(I, &mut S) -> O,
133 mut reducer: R,
134) -> Result<<R as Reduce>::Output, <R as Reduce>::Error>
135where
136 R: Reduce<Input = O>,
137{
138 let mut state = new_thread_state(0);
139 for item in input {
140 drop(reducer.feed(consume(item, &mut state))?);
141 }
142 reducer.finalize()
143}
144
145#[cfg(not(feature = "parallel"))]
157pub fn in_parallel_with_finalize<I, S, O, R>(
158 input: impl Iterator<Item = I>,
159 _thread_limit: Option<usize>,
160 new_thread_state: impl FnOnce(usize) -> S,
161 mut consume: impl FnMut(I, &mut S) -> O,
162 finalize: impl FnOnce(S) -> O + Send + Clone,
163 mut reducer: R,
164) -> Result<<R as Reduce>::Output, <R as Reduce>::Error>
165where
166 R: Reduce<Input = O>,
167{
168 let mut state = new_thread_state(0);
169 for item in input {
170 drop(reducer.feed(consume(item, &mut state))?);
171 }
172 reducer.feed(finalize(state))?;
173 reducer.finalize()
174}