@@ -43,54 +43,6 @@ pub fn parallel_guard<R>(f: impl FnOnce(&ParallelGuard) -> R) -> R {
4343 ret
4444}
4545
46- fn serial_join < A , B , RA , RB > ( oper_a : A , oper_b : B ) -> ( RA , RB )
47- where
48- A : FnOnce ( ) -> RA ,
49- B : FnOnce ( ) -> RB ,
50- {
51- let ( a, b) = parallel_guard ( |guard| {
52- let a = guard. run ( oper_a) ;
53- let b = guard. run ( oper_b) ;
54- ( a, b)
55- } ) ;
56- ( a. unwrap ( ) , b. unwrap ( ) )
57- }
58-
59- /// Runs a list of blocks in parallel. The first block is executed immediately on
60- /// the current thread. Use that for the longest running block.
61- #[ macro_export]
62- macro_rules! parallel {
63- ( impl $fblock: block [ $( $c: expr, ) * ] [ $block: expr $( , $rest: expr) * ] ) => {
64- parallel!( impl $fblock [ $block, $( $c, ) * ] [ $( $rest) ,* ] )
65- } ;
66- ( impl $fblock: block [ $( $blocks: expr, ) * ] [ ] ) => {
67- $crate:: sync:: parallel_guard( |guard| {
68- $crate:: sync:: scope( |s| {
69- $(
70- let block = $crate:: sync:: FromDyn :: from( || $blocks) ;
71- s. spawn( move |_| {
72- guard. run( move || block. into_inner( ) ( ) ) ;
73- } ) ;
74- ) *
75- guard. run( || $fblock) ;
76- } ) ;
77- } ) ;
78- } ;
79- ( $fblock: block, $( $blocks: block) ,* ) => {
80- if $crate:: sync:: is_dyn_thread_safe( ) {
81- // Reverse the order of the later blocks since Rayon executes them in reverse order
82- // when using a single thread. This ensures the execution order matches that
83- // of a single threaded rustc.
84- parallel!( impl $fblock [ ] [ $( $blocks) ,* ] ) ;
85- } else {
86- $crate:: sync:: parallel_guard( |guard| {
87- guard. run( || $fblock) ;
88- $( guard. run( || $blocks) ; ) *
89- } ) ;
90- }
91- } ;
92- }
93-
9446pub fn spawn ( func : impl FnOnce ( ) + DynSend + ' static ) {
9547 if mode:: is_dyn_thread_safe ( ) {
9648 let func = FromDyn :: from ( func) ;
@@ -102,140 +54,6 @@ pub fn spawn(func: impl FnOnce() + DynSend + 'static) {
10254 }
10355}
10456
105- // This function only works when `mode::is_dyn_thread_safe()`.
106- pub fn scope < ' scope , OP , R > ( op : OP ) -> R
107- where
108- OP : FnOnce ( & rustc_thread_pool:: Scope < ' scope > ) -> R + DynSend ,
109- R : DynSend ,
110- {
111- let op = FromDyn :: from ( op) ;
112- rustc_thread_pool:: scope ( |s| FromDyn :: from ( op. into_inner ( ) ( s) ) ) . into_inner ( )
113- }
114-
115- #[ inline]
116- pub fn join < A , B , RA : DynSend , RB : DynSend > ( oper_a : A , oper_b : B ) -> ( RA , RB )
117- where
118- A : FnOnce ( ) -> RA + DynSend ,
119- B : FnOnce ( ) -> RB + DynSend ,
120- {
121- if mode:: is_dyn_thread_safe ( ) {
122- let oper_a = FromDyn :: from ( oper_a) ;
123- let oper_b = FromDyn :: from ( oper_b) ;
124- let ( a, b) = parallel_guard ( |guard| {
125- rustc_thread_pool:: join (
126- move || guard. run ( move || FromDyn :: from ( oper_a. into_inner ( ) ( ) ) ) ,
127- move || guard. run ( move || FromDyn :: from ( oper_b. into_inner ( ) ( ) ) ) ,
128- )
129- } ) ;
130- ( a. unwrap ( ) . into_inner ( ) , b. unwrap ( ) . into_inner ( ) )
131- } else {
132- serial_join ( oper_a, oper_b)
133- }
134- }
135-
136- fn par_slice < I : DynSend > (
137- items : & mut [ I ] ,
138- guard : & ParallelGuard ,
139- for_each : impl Fn ( & mut I ) + DynSync + DynSend ,
140- ) {
141- struct State < ' a , F > {
142- for_each : FromDyn < F > ,
143- guard : & ' a ParallelGuard ,
144- group : usize ,
145- }
146-
147- fn par_rec < I : DynSend , F : Fn ( & mut I ) + DynSync + DynSend > (
148- items : & mut [ I ] ,
149- state : & State < ' _ , F > ,
150- ) {
151- if items. len ( ) <= state. group {
152- for item in items {
153- state. guard . run ( || ( state. for_each ) ( item) ) ;
154- }
155- } else {
156- let ( left, right) = items. split_at_mut ( items. len ( ) / 2 ) ;
157- let mut left = state. for_each . derive ( left) ;
158- let mut right = state. for_each . derive ( right) ;
159- rustc_thread_pool:: join ( move || par_rec ( * left, state) , move || par_rec ( * right, state) ) ;
160- }
161- }
162-
163- let state = State {
164- for_each : FromDyn :: from ( for_each) ,
165- guard,
166- group : std:: cmp:: max ( items. len ( ) / 128 , 1 ) ,
167- } ;
168- par_rec ( items, & state)
169- }
170-
171- pub fn par_for_each_in < I : DynSend , T : IntoIterator < Item = I > > (
172- t : T ,
173- for_each : impl Fn ( & I ) + DynSync + DynSend ,
174- ) {
175- parallel_guard ( |guard| {
176- if mode:: is_dyn_thread_safe ( ) {
177- let mut items: Vec < _ > = t. into_iter ( ) . collect ( ) ;
178- par_slice ( & mut items, guard, |i| for_each ( & * i) )
179- } else {
180- t. into_iter ( ) . for_each ( |i| {
181- guard. run ( || for_each ( & i) ) ;
182- } ) ;
183- }
184- } ) ;
185- }
186-
187- /// This runs `for_each` in parallel for each iterator item. If one or more of the
188- /// `for_each` calls returns `Err`, the function will also return `Err`. The error returned
189- /// will be non-deterministic, but this is expected to be used with `ErrorGuaranteed` which
190- /// are all equivalent.
191- pub fn try_par_for_each_in < T : IntoIterator , E : DynSend > (
192- t : T ,
193- for_each : impl Fn ( & <T as IntoIterator >:: Item ) -> Result < ( ) , E > + DynSync + DynSend ,
194- ) -> Result < ( ) , E >
195- where
196- <T as IntoIterator >:: Item : DynSend ,
197- {
198- parallel_guard ( |guard| {
199- if mode:: is_dyn_thread_safe ( ) {
200- let mut items: Vec < _ > = t. into_iter ( ) . collect ( ) ;
201-
202- let error = Mutex :: new ( None ) ;
203-
204- par_slice ( & mut items, guard, |i| {
205- if let Err ( err) = for_each ( & * i) {
206- * error. lock ( ) = Some ( err) ;
207- }
208- } ) ;
209-
210- if let Some ( err) = error. into_inner ( ) { Err ( err) } else { Ok ( ( ) ) }
211- } else {
212- t. into_iter ( ) . filter_map ( |i| guard. run ( || for_each ( & i) ) ) . fold ( Ok ( ( ) ) , Result :: and)
213- }
214- } )
215- }
216-
217- pub fn par_map < I : DynSend , T : IntoIterator < Item = I > , R : DynSend , C : FromIterator < R > > (
218- t : T ,
219- map : impl Fn ( I ) -> R + DynSync + DynSend ,
220- ) -> C {
221- parallel_guard ( |guard| {
222- if mode:: is_dyn_thread_safe ( ) {
223- let map = FromDyn :: from ( map) ;
224-
225- let mut items: Vec < ( Option < I > , Option < R > ) > =
226- t. into_iter ( ) . map ( |i| ( Some ( i) , None ) ) . collect ( ) ;
227-
228- par_slice ( & mut items, guard, |i| {
229- i. 1 = Some ( map ( i. 0 . take ( ) . unwrap ( ) ) ) ;
230- } ) ;
231-
232- items. into_iter ( ) . filter_map ( |i| i. 1 ) . collect ( )
233- } else {
234- t. into_iter ( ) . filter_map ( |i| guard. run ( || map ( i) ) ) . collect ( )
235- }
236- } )
237- }
238-
23957pub fn broadcast < R : DynSend > ( op : impl Fn ( usize ) -> R + DynSync ) -> Vec < R > {
24058 if mode:: is_dyn_thread_safe ( ) {
24159 let op = FromDyn :: from ( op) ;
0 commit comments