File tree 2 files changed +26
-9
lines changed
2 files changed +26
-9
lines changed Original file line number Diff line number Diff line change 3
3
use std:: { env, num:: NonZeroUsize , thread} ;
4
4
5
5
pub fn get_concurrency ( ) -> usize {
6
- if let Ok ( value) = env:: var ( "RUST_TEST_THREADS" ) {
7
- match value. parse :: < NonZeroUsize > ( ) . ok ( ) {
8
- Some ( n) => n. get ( ) ,
9
- _ => panic ! ( "RUST_TEST_THREADS is `{value}`, should be a positive integer." ) ,
10
- }
6
+ rust_test_threads_from_env ( )
7
+ . unwrap_or_else ( || thread:: available_parallelism ( ) . map ( |n| n. get ( ) ) . unwrap_or ( 1 ) )
8
+ }
9
+
10
+ pub fn supports_threads ( ) -> bool {
11
+ if cfg ! ( target_os = "emscripten" ) || cfg ! ( target_family = "wasm" ) {
12
+ return false ;
13
+ }
14
+
15
+ // Accommodate libraries that may rely on shared thread-local storage (e.g.
16
+ // integrating with old C libraries).
17
+ if let Some ( 1 ) = rust_test_threads_from_env ( ) {
18
+ return false ;
19
+ }
20
+
21
+ true
22
+ }
23
+
24
+ fn rust_test_threads_from_env ( ) -> Option < usize > {
25
+ let value = env:: var ( "RUST_TEST_THREADS" ) . ok ( ) ?;
26
+
27
+ if let Ok ( value) = value. parse :: < NonZeroUsize > ( ) {
28
+ Some ( value. get ( ) )
11
29
} else {
12
- thread :: available_parallelism ( ) . map ( |n| n . get ( ) ) . unwrap_or ( 1 )
30
+ panic ! ( "RUST_TEST_THREADS is `{value}`, should be a positive integer." , value = value )
13
31
}
14
32
}
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ mod tests;
82
82
83
83
use core:: any:: Any ;
84
84
use event:: { CompletedTest , TestEvent } ;
85
- use helpers:: concurrency:: get_concurrency;
85
+ use helpers:: concurrency:: { get_concurrency, supports_threads } ;
86
86
use helpers:: exit_code:: get_exit_code;
87
87
use helpers:: shuffle:: { get_shuffle_seed, shuffle_tests} ;
88
88
use options:: RunStrategy ;
@@ -592,8 +592,7 @@ pub fn run_test(
592
592
// If the platform is single-threaded we're just going to run
593
593
// the test synchronously, regardless of the concurrency
594
594
// level.
595
- let supports_threads = !cfg ! ( target_os = "emscripten" ) && !cfg ! ( target_family = "wasm" ) ;
596
- if supports_threads {
595
+ if supports_threads ( ) {
597
596
let cfg = thread:: Builder :: new ( ) . name ( name. as_slice ( ) . to_owned ( ) ) ;
598
597
let mut runtest = Arc :: new ( Mutex :: new ( Some ( runtest) ) ) ;
599
598
let runtest2 = runtest. clone ( ) ;
You can’t perform that action at this time.
0 commit comments