@@ -65,11 +65,10 @@ mod mode {
65
65
use super :: Ordering ;
66
66
use std:: sync:: atomic:: AtomicU8 ;
67
67
68
- const UNINITIALIZED : u8 = 0 ;
69
68
const DYN_NOT_THREAD_SAFE : u8 = 1 ;
70
69
const DYN_THREAD_SAFE : u8 = 2 ;
71
70
72
- static DYN_THREAD_SAFE_MODE : AtomicU8 = AtomicU8 :: new ( UNINITIALIZED ) ;
71
+ static DYN_THREAD_SAFE_MODE : AtomicU8 = AtomicU8 :: new ( DYN_NOT_THREAD_SAFE ) ;
73
72
74
73
// Whether thread safety is enabled (due to running under multiple threads).
75
74
#[ inline]
@@ -84,15 +83,9 @@ mod mode {
84
83
// Only set by the `-Z threads` compile option
85
84
pub fn set_dyn_thread_safe_mode ( mode : bool ) {
86
85
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
- ) ;
93
86
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 ) ;
96
89
}
97
90
}
98
91
@@ -337,24 +330,6 @@ cfg_if! {
337
330
}
338
331
}
339
332
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
-
358
333
#[ inline]
359
334
pub fn join<A , B , RA : DynSend , RB : DynSend >( oper_a: A , oper_b: B ) -> ( RA , RB )
360
335
where
@@ -385,19 +360,11 @@ cfg_if! {
385
360
/// the current thread. Use that for the longest running block.
386
361
#[ macro_export]
387
362
macro_rules! parallel {
388
- ( impl $fblock: tt [ $( $c: tt, ) * ] [ $block: tt $( , $rest: tt) * ] ) => {
389
363
( impl $fblock: block [ $( $c: expr, ) * ] [ $block: expr $( , $rest: expr) * ] ) => {
390
364
parallel!( impl $fblock [ $block, $( $c, ) * ] [ $( $rest) , * ] )
391
365
} ;
392
- ( impl $fblock: tt [ $( $blocks: tt, ) * ] [ ] ) => {
393
366
( impl $fblock: block [ $( $blocks: expr, ) * ] [ ] ) => {
394
367
:: rustc_data_structures:: sync:: scope( |s| {
395
-
396
-
397
-
398
-
399
-
400
-
401
368
$( let block = rustc_data_structures:: sync:: FromDyn :: from( || $blocks) ;
402
369
s. spawn( move |_| block. into_inner( ) ( ) ) ; ) *
403
370
( || $fblock) ( ) ;
@@ -421,7 +388,6 @@ cfg_if! {
421
388
}
422
389
}
423
390
$(
424
- s. spawn( |_| $blocks) ;
425
391
if let Err ( p) = :: std:: panic:: catch_unwind(
426
392
:: std:: panic:: AssertUnwindSafe ( || $blocks)
427
393
) {
@@ -430,19 +396,11 @@ cfg_if! {
430
396
}
431
397
}
432
398
) *
433
- $fblock;
434
- } )
435
399
if let Some ( panic) = panic {
436
400
:: std:: panic:: resume_unwind( panic) ;
437
401
}
438
402
}
439
403
} ;
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
- } ;
446
404
}
447
405
448
406
use rayon:: iter:: { FromParallelIterator , IntoParallelIterator , ParallelIterator } ;
@@ -453,7 +411,7 @@ cfg_if! {
453
411
) {
454
412
if mode:: is_dyn_thread_safe( ) {
455
413
let for_each = FromDyn :: from( for_each) ;
456
- let panic: Lock <Option <_>> = Lock :: new( None ) ;
414
+ let panic: Mutex <Option <_>> = Mutex :: new( None ) ;
457
415
t. into_par_iter( ) . for_each( |i| if let Err ( p) = catch_unwind( AssertUnwindSafe ( || for_each( i) ) ) {
458
416
let mut l = panic. lock( ) ;
459
417
if l. is_none( ) {
@@ -491,7 +449,7 @@ cfg_if! {
491
449
map: impl Fn ( I ) -> R + DynSync + DynSend
492
450
) -> C {
493
451
if mode:: is_dyn_thread_safe( ) {
494
- let panic: Lock <Option <_>> = Lock :: new( None ) ;
452
+ let panic: Mutex <Option <_>> = Mutex :: new( None ) ;
495
453
let map = FromDyn :: from( map) ;
496
454
// We catch panics here ensuring that all the loop iterations execute.
497
455
let r = t. into_par_iter( ) . filter_map( |i| {
@@ -531,30 +489,6 @@ cfg_if! {
531
489
r
532
490
}
533
491
}
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
558
492
}
559
493
}
560
494
@@ -607,7 +541,7 @@ impl<T> Lock<T> {
607
541
#[ inline]
608
542
pub fn new ( val : T ) -> Self {
609
543
Lock {
610
- single_thread: !active ( ) ,
544
+ single_thread : !is_dyn_thread_safe ( ) ,
611
545
data : UnsafeCell :: new ( val) ,
612
546
borrow : Cell :: new ( false ) ,
613
547
mutex : RawMutex :: INIT ,
@@ -725,10 +659,6 @@ impl<T: Default> Default for Lock<T> {
725
659
}
726
660
}
727
661
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
-
732
662
pub struct LockGuard < ' a , T > {
733
663
lock : & ' a Lock < T > ,
734
664
marker : PhantomData < & ' a mut T > ,
@@ -1037,6 +967,10 @@ pub struct RwLock<T> {
1037
967
data : UnsafeCell < T > ,
1038
968
}
1039
969
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
+
1040
974
impl < T : Debug > Debug for RwLock < T > {
1041
975
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
1042
976
f. debug_struct ( "Lock" ) . field ( "data" , self . read ( ) . deref ( ) ) . finish ( )
@@ -1046,7 +980,11 @@ impl<T: Debug> Debug for RwLock<T> {
1046
980
impl < T : Default > Default for RwLock < T > {
1047
981
fn default ( ) -> Self {
1048
982
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
+ } ,
1050
988
1051
989
data : UnsafeCell :: new ( T :: default ( ) ) ,
1052
990
}
@@ -1057,7 +995,11 @@ impl<T> RwLock<T> {
1057
995
#[ inline( always) ]
1058
996
pub fn new ( inner : T ) -> Self {
1059
997
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
+ } ,
1061
1003
1062
1004
data : UnsafeCell :: new ( inner) ,
1063
1005
}
@@ -1197,10 +1139,6 @@ impl<T> RwLock<T> {
1197
1139
}
1198
1140
}
1199
1141
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
-
1204
1142
// FIXME: Probably a bad idea
1205
1143
impl < T : Clone > Clone for RwLock < T > {
1206
1144
#[ inline]
@@ -1221,7 +1159,7 @@ impl<T> WorkerLocal<T> {
1221
1159
/// value this worker local should take for each thread in the thread pool.
1222
1160
#[ inline]
1223
1161
pub fn new < F : FnMut ( usize ) -> T > ( mut f : F ) -> WorkerLocal < T > {
1224
- if !active ( ) {
1162
+ if !is_dyn_thread_safe ( ) {
1225
1163
WorkerLocal { single_thread : true , inner : Some ( f ( 0 ) ) , mt_inner : None }
1226
1164
} else {
1227
1165
WorkerLocal {
@@ -1246,9 +1184,6 @@ impl<T> Deref for WorkerLocal<T> {
1246
1184
}
1247
1185
}
1248
1186
1249
- // Just for speed test
1250
- unsafe impl <T : Send > std:: marker:: Sync for WorkerLocal <T > { }
1251
-
1252
1187
use std:: thread;
1253
1188
pub use worker_local:: Registry ;
1254
1189
@@ -1261,10 +1196,6 @@ pub struct OneThread<T> {
1261
1196
inner : T ,
1262
1197
}
1263
1198
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
-
1268
1199
impl < T > OneThread < T > {
1269
1200
#[ inline( always) ]
1270
1201
fn check ( & self ) {
@@ -1273,7 +1204,7 @@ impl<T> OneThread<T> {
1273
1204
1274
1205
#[ inline( always) ]
1275
1206
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 }
1277
1208
}
1278
1209
1279
1210
#[ inline( always) ]
0 commit comments