@@ -65,11 +65,10 @@ mod mode {
6565 use super :: Ordering ;
6666 use std:: sync:: atomic:: AtomicU8 ;
6767
68- const UNINITIALIZED : u8 = 0 ;
6968 const DYN_NOT_THREAD_SAFE : u8 = 1 ;
7069 const DYN_THREAD_SAFE : u8 = 2 ;
7170
72- static DYN_THREAD_SAFE_MODE : AtomicU8 = AtomicU8 :: new ( UNINITIALIZED ) ;
71+ static DYN_THREAD_SAFE_MODE : AtomicU8 = AtomicU8 :: new ( DYN_NOT_THREAD_SAFE ) ;
7372
7473 // Whether thread safety is enabled (due to running under multiple threads).
7574 #[ inline]
@@ -84,15 +83,9 @@ mod mode {
8483 // Only set by the `-Z threads` compile option
8584 pub fn set_dyn_thread_safe_mode ( mode : bool ) {
8685 let set: u8 = if mode { DYN_THREAD_SAFE } else { DYN_NOT_THREAD_SAFE } ;
87- let previous = DYN_THREAD_SAFE_MODE . compare_exchange (
88- UNINITIALIZED ,
89- set,
90- Ordering :: Relaxed ,
91- Ordering :: Relaxed ,
92- ) ;
9386
94- // Check that the mode was either uninitialized or was already set to the requested mode.
95- assert ! ( previous . is_ok ( ) || previous == Err ( set) ) ;
87+ // just for speed test
88+ DYN_THREAD_SAFE_MODE . store ( set, Ordering :: Relaxed ) ;
9689 }
9790}
9891
@@ -337,24 +330,6 @@ cfg_if! {
337330 }
338331 }
339332
340- #[ inline]
341- pub fn join<A , B , RA : DynSend , RB : DynSend >( oper_a: A , oper_b: B ) -> ( RA , RB )
342- where
343- A : FnOnce ( ) -> RA + DynSend ,
344- B : FnOnce ( ) -> RB + DynSend ,
345- {
346- if mode:: active( ) {
347- let oper_a = FromDyn :: from( oper_a) ;
348- let oper_b = FromDyn :: from( oper_b) ;
349- let ( a, b) = rayon:: join( move || FromDyn :: from( oper_a. into_inner( ) ( ) ) , move || FromDyn :: from( oper_b. into_inner( ) ( ) ) ) ;
350- ( a. into_inner( ) , b. into_inner( ) )
351- } else {
352- ( oper_a( ) , oper_b( ) )
353- }
354- }
355-
356- use std:: thread;
357-
358333 #[ inline]
359334 pub fn join<A , B , RA : DynSend , RB : DynSend >( oper_a: A , oper_b: B ) -> ( RA , RB )
360335 where
@@ -385,19 +360,11 @@ cfg_if! {
385360 /// the current thread. Use that for the longest running block.
386361 #[ macro_export]
387362 macro_rules! parallel {
388- ( impl $fblock: tt [ $( $c: tt, ) * ] [ $block: tt $( , $rest: tt) * ] ) => {
389363 ( impl $fblock: block [ $( $c: expr, ) * ] [ $block: expr $( , $rest: expr) * ] ) => {
390364 parallel!( impl $fblock [ $block, $( $c, ) * ] [ $( $rest) , * ] )
391365 } ;
392- ( impl $fblock: tt [ $( $blocks: tt, ) * ] [ ] ) => {
393366 ( impl $fblock: block [ $( $blocks: expr, ) * ] [ ] ) => {
394367 :: rustc_data_structures:: sync:: scope( |s| {
395-
396-
397-
398-
399-
400-
401368 $( let block = rustc_data_structures:: sync:: FromDyn :: from( || $blocks) ;
402369 s. spawn( move |_| block. into_inner( ) ( ) ) ; ) *
403370 ( || $fblock) ( ) ;
@@ -421,7 +388,6 @@ cfg_if! {
421388 }
422389 }
423390 $(
424- s. spawn( |_| $blocks) ;
425391 if let Err ( p) = :: std:: panic:: catch_unwind(
426392 :: std:: panic:: AssertUnwindSafe ( || $blocks)
427393 ) {
@@ -430,19 +396,11 @@ cfg_if! {
430396 }
431397 }
432398 ) *
433- $fblock;
434- } )
435399 if let Some ( panic) = panic {
436400 :: std:: panic:: resume_unwind( panic) ;
437401 }
438402 }
439403 } ;
440- ( $fblock: tt, $( $blocks: tt) , * ) => {
441- // Reverse the order of the later blocks since Rayon executes them in reverse order
442- // when using a single thread. This ensures the execution order matches that
443- // of a single threaded rustc
444- parallel!( impl $fblock [ ] [ $( $blocks) , * ] ) ;
445- } ;
446404 }
447405
448406 use rayon:: iter:: { FromParallelIterator , IntoParallelIterator , ParallelIterator } ;
@@ -453,7 +411,7 @@ cfg_if! {
453411 ) {
454412 if mode:: is_dyn_thread_safe( ) {
455413 let for_each = FromDyn :: from( for_each) ;
456- let panic: Lock <Option <_>> = Lock :: new( None ) ;
414+ let panic: Mutex <Option <_>> = Mutex :: new( None ) ;
457415 t. into_par_iter( ) . for_each( |i| if let Err ( p) = catch_unwind( AssertUnwindSafe ( || for_each( i) ) ) {
458416 let mut l = panic. lock( ) ;
459417 if l. is_none( ) {
@@ -491,7 +449,7 @@ cfg_if! {
491449 map: impl Fn ( I ) -> R + DynSync + DynSend
492450 ) -> C {
493451 if mode:: is_dyn_thread_safe( ) {
494- let panic: Lock <Option <_>> = Lock :: new( None ) ;
452+ let panic: Mutex <Option <_>> = Mutex :: new( None ) ;
495453 let map = FromDyn :: from( map) ;
496454 // We catch panics here ensuring that all the loop iterations execute.
497455 let r = t. into_par_iter( ) . filter_map( |i| {
@@ -531,30 +489,6 @@ cfg_if! {
531489 r
532490 }
533491 }
534-
535- pub unsafe trait DynSend { }
536- pub unsafe trait DynSync { }
537-
538- unsafe impl <T > DynSend for T where T : Send { }
539- unsafe impl <T > DynSync for T where T : Sync { }
540-
541- #[ derive( Copy , Clone ) ]
542- pub struct FromDyn <T >( T ) ;
543-
544- impl <T > FromDyn <T > {
545- #[ inline( always) ]
546- pub fn from( val: T ) -> Self {
547- // Check that `sync::active()` is true on creation so we can
548- // implement `Send` and `Sync` for this structure when `T`
549- // implements `DynSend` and `DynSync` respectively.
550- #[ cfg( parallel_compiler) ]
551- assert!( mode:: active( ) ) ;
552- FromDyn ( val)
553- }
554-
555- #[ inline( always) ]
556- pub fn into_inner( self ) -> T {
557- self . 0
558492 }
559493}
560494
@@ -607,7 +541,7 @@ impl<T> Lock<T> {
607541 #[ inline]
608542 pub fn new ( val : T ) -> Self {
609543 Lock {
610- single_thread: !active ( ) ,
544+ single_thread : !is_dyn_thread_safe ( ) ,
611545 data : UnsafeCell :: new ( val) ,
612546 borrow : Cell :: new ( false ) ,
613547 mutex : RawMutex :: INIT ,
@@ -725,10 +659,6 @@ impl<T: Default> Default for Lock<T> {
725659 }
726660}
727661
728- // Just for speed test
729- unsafe impl <T : Send > std:: marker:: Send for Lock <T > { }
730- unsafe impl <T : Send > std:: marker:: Sync for Lock <T > { }
731-
732662pub struct LockGuard < ' a , T > {
733663 lock : & ' a Lock < T > ,
734664 marker : PhantomData < & ' a mut T > ,
@@ -1037,6 +967,10 @@ pub struct RwLock<T> {
1037967 data : UnsafeCell < T > ,
1038968}
1039969
970+ // just for speed test
971+ unsafe impl < T > std:: marker:: Send for RwLock < T > { }
972+ unsafe impl < T > std:: marker:: Sync for RwLock < T > { }
973+
1040974impl < T : Debug > Debug for RwLock < T > {
1041975 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
1042976 f. debug_struct ( "Lock" ) . field ( "data" , self . read ( ) . deref ( ) ) . finish ( )
@@ -1046,7 +980,11 @@ impl<T: Debug> Debug for RwLock<T> {
1046980impl < T : Default > Default for RwLock < T > {
1047981 fn default ( ) -> Self {
1048982 RwLock {
1049- raw: RwLockRaw { single_thread: !active( ) , borrow: Cell :: new( 0 ) , raw: RawRwLock :: INIT } ,
983+ raw : RwLockRaw {
984+ single_thread : !is_dyn_thread_safe ( ) ,
985+ borrow : Cell :: new ( 0 ) ,
986+ raw : RawRwLock :: INIT ,
987+ } ,
1050988
1051989 data : UnsafeCell :: new ( T :: default ( ) ) ,
1052990 }
@@ -1057,7 +995,11 @@ impl<T> RwLock<T> {
1057995 #[ inline( always) ]
1058996 pub fn new ( inner : T ) -> Self {
1059997 RwLock {
1060- raw: RwLockRaw { single_thread: !active( ) , borrow: Cell :: new( 0 ) , raw: RawRwLock :: INIT } ,
998+ raw : RwLockRaw {
999+ single_thread : !is_dyn_thread_safe ( ) ,
1000+ borrow : Cell :: new ( 0 ) ,
1001+ raw : RawRwLock :: INIT ,
1002+ } ,
10611003
10621004 data : UnsafeCell :: new ( inner) ,
10631005 }
@@ -1197,10 +1139,6 @@ impl<T> RwLock<T> {
11971139 }
11981140}
11991141
1200- // just for speed test
1201- unsafe impl <T : Send > std:: marker:: Send for RwLock <T > { }
1202- unsafe impl <T : Send + Sync > std:: marker:: Sync for RwLock <T > { }
1203-
12041142// FIXME: Probably a bad idea
12051143impl < T : Clone > Clone for RwLock < T > {
12061144 #[ inline]
@@ -1221,7 +1159,7 @@ impl<T> WorkerLocal<T> {
12211159 /// value this worker local should take for each thread in the thread pool.
12221160 #[ inline]
12231161 pub fn new < F : FnMut ( usize ) -> T > ( mut f : F ) -> WorkerLocal < T > {
1224- if !active ( ) {
1162+ if !is_dyn_thread_safe ( ) {
12251163 WorkerLocal { single_thread : true , inner : Some ( f ( 0 ) ) , mt_inner : None }
12261164 } else {
12271165 WorkerLocal {
@@ -1246,9 +1184,6 @@ impl<T> Deref for WorkerLocal<T> {
12461184 }
12471185}
12481186
1249- // Just for speed test
1250- unsafe impl <T : Send > std:: marker:: Sync for WorkerLocal <T > { }
1251-
12521187use std:: thread;
12531188pub use worker_local:: Registry ;
12541189
@@ -1261,10 +1196,6 @@ pub struct OneThread<T> {
12611196 inner : T ,
12621197}
12631198
1264- // just for speed test now
1265- unsafe impl <T > std:: marker:: Sync for OneThread <T > { }
1266- unsafe impl <T > std:: marker:: Send for OneThread <T > { }
1267-
12681199impl < T > OneThread < T > {
12691200 #[ inline( always) ]
12701201 fn check ( & self ) {
@@ -1273,7 +1204,7 @@ impl<T> OneThread<T> {
12731204
12741205 #[ inline( always) ]
12751206 pub fn new ( inner : T ) -> Self {
1276- OneThread { single_thread: !active ( ) , thread: thread:: current( ) . id( ) , inner }
1207+ OneThread { single_thread : !is_dyn_thread_safe ( ) , thread : thread:: current ( ) . id ( ) , inner }
12771208 }
12781209
12791210 #[ inline( always) ]
0 commit comments