File tree Expand file tree Collapse file tree 2 files changed +26
-9
lines changed Expand file tree Collapse file tree 2 files changed +26
-9
lines changed Original file line number Diff line number Diff line change 33use std:: { env, num:: NonZeroUsize , thread} ;
44
55pub 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 ( ) )
1129 } 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 )
1331 }
1432}
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ mod tests;
8282
8383use core:: any:: Any ;
8484use event:: { CompletedTest , TestEvent } ;
85- use helpers:: concurrency:: get_concurrency;
85+ use helpers:: concurrency:: { get_concurrency, supports_threads } ;
8686use helpers:: exit_code:: get_exit_code;
8787use helpers:: shuffle:: { get_shuffle_seed, shuffle_tests} ;
8888use options:: RunStrategy ;
@@ -592,8 +592,7 @@ pub fn run_test(
592592 // If the platform is single-threaded we're just going to run
593593 // the test synchronously, regardless of the concurrency
594594 // level.
595- let supports_threads = !cfg ! ( target_os = "emscripten" ) && !cfg ! ( target_family = "wasm" ) ;
596- if supports_threads {
595+ if supports_threads ( ) {
597596 let cfg = thread:: Builder :: new ( ) . name ( name. as_slice ( ) . to_owned ( ) ) ;
598597 let mut runtest = Arc :: new ( Mutex :: new ( Some ( runtest) ) ) ;
599598 let runtest2 = runtest. clone ( ) ;
You can’t perform that action at this time.
0 commit comments