@@ -259,33 +259,6 @@ cfg_if! {
259
259
260
260
use std:: cell:: Cell ;
261
261
262
- #[ derive( Debug ) ]
263
- pub struct WorkerLocal <T >( OneThread <T >) ;
264
-
265
- impl <T > WorkerLocal <T > {
266
- /// Creates a new worker local where the `initial` closure computes the
267
- /// value this worker local should take for each thread in the thread pool.
268
- #[ inline]
269
- pub fn new<F : FnMut ( usize ) -> T >( mut f: F ) -> WorkerLocal <T > {
270
- WorkerLocal ( OneThread :: new( f( 0 ) ) )
271
- }
272
-
273
- /// Returns the worker-local value for each thread
274
- #[ inline]
275
- pub fn into_inner( self ) -> Vec <T > {
276
- vec![ OneThread :: into_inner( self . 0 ) ]
277
- }
278
- }
279
-
280
- impl <T > Deref for WorkerLocal <T > {
281
- type Target = T ;
282
-
283
- #[ inline( always) ]
284
- fn deref( & self ) -> & T {
285
- & self . 0
286
- }
287
- }
288
-
289
262
pub type MTLockRef <' a, T > = & ' a mut MTLock <T >;
290
263
291
264
#[ derive( Debug , Default ) ]
@@ -445,8 +418,6 @@ cfg_if! {
445
418
} ;
446
419
}
447
420
448
- pub use rayon_core:: WorkerLocal ;
449
-
450
421
use rayon:: iter:: { FromParallelIterator , IntoParallelIterator , ParallelIterator } ;
451
422
452
423
pub fn par_for_each_in<I , T : IntoIterator <Item = I > + IntoParallelIterator <Item = I >>(
@@ -851,6 +822,48 @@ impl<T: Clone> Clone for RwLock<T> {
851
822
}
852
823
}
853
824
825
+ #[ derive( Debug ) ]
826
+ pub struct WorkerLocal < T > {
827
+ single_thread : bool ,
828
+ inner : T ,
829
+ mt_inner : Option < rayon_core:: WorkerLocal < T > > ,
830
+ }
831
+
832
+ impl < T > WorkerLocal < T > {
833
+ /// Creates a new worker local where the `initial` closure computes the
834
+ /// value this worker local should take for each thread in the thread pool.
835
+ #[ inline]
836
+ pub fn new < F : FnMut ( usize ) -> T > ( mut f : F ) -> WorkerLocal < T > {
837
+ if !active ( ) {
838
+ WorkerLocal { single_thread : true , inner : f ( 0 ) , mt_inner : None }
839
+ } else {
840
+ WorkerLocal {
841
+ single_thread : false ,
842
+ inner : unsafe { MaybeUninit :: uninit ( ) . assume_init ( ) } ,
843
+ mt_inner : Some ( rayon_core:: WorkerLocal :: new ( f) ) ,
844
+ }
845
+ }
846
+ }
847
+
848
+ /// Returns the worker-local value for each thread
849
+ #[ inline]
850
+ pub fn into_inner ( self ) -> Vec < T > {
851
+ if self . single_thread { vec ! [ self . inner] } else { self . mt_inner . unwrap ( ) . into_inner ( ) }
852
+ }
853
+ }
854
+
855
+ impl < T > Deref for WorkerLocal < T > {
856
+ type Target = T ;
857
+
858
+ #[ inline( always) ]
859
+ fn deref ( & self ) -> & T {
860
+ if self . single_thread { & self . inner } else { self . mt_inner . as_ref ( ) . unwrap ( ) . deref ( ) }
861
+ }
862
+ }
863
+
864
+ // Just for speed test
865
+ unsafe impl < T : Send > std:: marker:: Sync for WorkerLocal < T > { }
866
+
854
867
/// A type which only allows its inner value to be used in one thread.
855
868
/// It will panic if it is used on multiple threads.
856
869
#[ derive( Debug ) ]
0 commit comments