@@ -61,32 +61,37 @@ mod mode {
61
61
use std:: sync:: atomic:: AtomicU8 ;
62
62
63
63
const UNINITIALIZED : u8 = 0 ;
64
- const INACTIVE : u8 = 1 ;
65
- const ACTIVE : u8 = 2 ;
64
+ const DYN_NOT_SYNC : u8 = 1 ;
65
+ const DYN_SYNC : u8 = 2 ;
66
66
67
- static MODE : AtomicU8 = AtomicU8 :: new ( UNINITIALIZED ) ;
67
+ static DYN_SYNC_MODE : AtomicU8 = AtomicU8 :: new ( UNINITIALIZED ) ;
68
68
69
+ // Weather control thread safety dynamically
69
70
#[ inline]
70
- pub fn active ( ) -> bool {
71
- match MODE . load ( Ordering :: Relaxed ) {
72
- INACTIVE => false ,
73
- ACTIVE => true ,
71
+ pub fn is_dyn_thread_safe ( ) -> bool {
72
+ match DYN_SYNC_MODE . load ( Ordering :: Relaxed ) {
73
+ DYN_NOT_SYNC => false ,
74
+ DYN_SYNC => true ,
74
75
_ => panic ! ( "uninitialized parallel mode!" ) ,
75
76
}
76
77
}
77
78
78
79
// Only set by the `-Z threads` compile option
79
- pub fn set ( parallel : bool ) {
80
- let set: u8 = if parallel { ACTIVE } else { INACTIVE } ;
81
- let previous =
82
- MODE . compare_exchange ( UNINITIALIZED , set, Ordering :: Relaxed , Ordering :: Relaxed ) ;
80
+ pub fn set_dyn_thread_safe_mode ( parallel : bool ) {
81
+ let set: u8 = if parallel { DYN_SYNC } else { DYN_NOT_SYNC } ;
82
+ let previous = DYN_SYNC_MODE . compare_exchange (
83
+ UNINITIALIZED ,
84
+ set,
85
+ Ordering :: Relaxed ,
86
+ Ordering :: Relaxed ,
87
+ ) ;
83
88
84
89
// Check that the mode was either uninitialized or was already set to the requested mode.
85
90
assert ! ( previous. is_ok( ) || previous == Err ( set) ) ;
86
91
}
87
92
}
88
93
89
- pub use mode:: { active , set } ;
94
+ pub use mode:: { is_dyn_thread_safe , set_dyn_thread_safe_mode } ;
90
95
cfg_if ! {
91
96
if #[ cfg( not( parallel_compiler) ) ] {
92
97
pub unsafe auto trait Send { }
@@ -358,7 +363,7 @@ cfg_if! {
358
363
A : FnOnce ( ) -> RA + DynSend ,
359
364
B : FnOnce ( ) -> RB + DynSend ,
360
365
{
361
- if mode:: active ( ) {
366
+ if mode:: is_dyn_thread_safe ( ) {
362
367
let oper_a = FromDyn :: from( oper_a) ;
363
368
let oper_b = FromDyn :: from( oper_b) ;
364
369
let ( a, b) = rayon:: join( move || FromDyn :: from( oper_a. into_inner( ) ( ) ) , move || FromDyn :: from( oper_b. into_inner( ) ( ) ) ) ;
@@ -368,7 +373,7 @@ cfg_if! {
368
373
}
369
374
}
370
375
371
- // This function only works when `mode::active ()`.
376
+ // This function only works when `mode::is_dyn_thread_safe ()`.
372
377
pub fn scope<' scope, OP , R >( op: OP ) -> R
373
378
where
374
379
OP : FnOnce ( & rayon:: Scope <' scope>) -> R + DynSend ,
@@ -393,7 +398,7 @@ cfg_if! {
393
398
} ) ;
394
399
} ;
395
400
( $fblock: block, $( $blocks: block) , * ) => {
396
- if rustc_data_structures:: sync:: active ( ) {
401
+ if rustc_data_structures:: sync:: is_dyn_thread_safe ( ) {
397
402
// Reverse the order of the later blocks since Rayon executes them in reverse order
398
403
// when using a single thread. This ensures the execution order matches that
399
404
// of a single threaded rustc
@@ -431,7 +436,7 @@ cfg_if! {
431
436
t: T ,
432
437
for_each: impl Fn ( I ) + DynSync + DynSend
433
438
) {
434
- if mode:: active ( ) {
439
+ if mode:: is_dyn_thread_safe ( ) {
435
440
let for_each = FromDyn :: from( for_each) ;
436
441
let panic: Lock <Option <_>> = Lock :: new( None ) ;
437
442
t. into_par_iter( ) . for_each( |i| if let Err ( p) = catch_unwind( AssertUnwindSafe ( || for_each( i) ) ) {
@@ -470,7 +475,7 @@ cfg_if! {
470
475
t: T ,
471
476
map: impl Fn ( I ) -> R + DynSync + DynSend
472
477
) -> C {
473
- if mode:: active ( ) {
478
+ if mode:: is_dyn_thread_safe ( ) {
474
479
let panic: Lock <Option <_>> = Lock :: new( None ) ;
475
480
let map = FromDyn :: from( map) ;
476
481
// We catch panics here ensuring that all the loop iterations execute.
0 commit comments