Skip to content

Commit 4ba369c

Browse files
author
Jethro Beekman
committed
When testing std, don't create two variants of threading internals
1 parent 0f949c2 commit 4ba369c

File tree

19 files changed

+518
-486
lines changed

19 files changed

+518
-486
lines changed

src/libstd/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ extern crate backtrace_sys;
338338
// would generate duplicate lang item errors), and any globals it defines are
339339
// _not_ the globals used by "real" std. So this import, defined only during
340340
// testing gives test-std access to real-std lang items and globals. See #2912
341-
#[cfg(test)] extern crate std as realstd;
341+
#[cfg(test)]
342+
#[macro_use]
343+
extern crate std as realstd;
342344

343345
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
344346
#[macro_use]
@@ -442,7 +444,13 @@ pub mod f32;
442444
pub mod f64;
443445

444446
#[macro_use]
447+
#[cfg(not(test))]
445448
pub mod thread;
449+
#[cfg(test)]
450+
pub use realstd::thread;
451+
#[cfg(test)]
452+
#[path = "thread/tests.rs"]
453+
mod thread_tests;
446454
pub mod ascii;
447455
pub mod collections;
448456
pub mod env;

src/libstd/panicking.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
300300
}
301301

302302
/// Determines whether the current thread is unwinding because of panic.
303+
#[cfg(not(test))]
303304
pub fn panicking() -> bool {
304305
update_panic_count(0) != 0
305306
}

src/libstd/sys/cloudabi/thread.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(test, allow(dead_code))]
2+
13
use boxed::FnBox;
24
use cmp;
35
use ffi::CStr;

src/libstd/sys/redox/ext/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ pub mod fs;
2525
pub mod io;
2626
pub mod net;
2727
pub mod process;
28+
#[cfg(not(test))]
2829
pub mod thread;
30+
#[cfg(test)]
31+
pub use realstd::os::redox::thread;
2932

3033
/// A prelude for conveniently writing platform-specific code.
3134
///

src/libstd/sys/redox/thread.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(test, allow(dead_code))]
2+
13
use boxed::FnBox;
24
use ffi::CStr;
35
use io;

src/libstd/sys/sgx/abi/tls.rs

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,14 @@ const USIZE_BITS: usize = 64;
1010
const TLS_KEYS: usize = 128; // Same as POSIX minimum
1111
const TLS_KEYS_BITSET_SIZE: usize = (TLS_KEYS + (USIZE_BITS - 1)) / USIZE_BITS;
1212

13+
#[cfg(not(test))]
1314
static TLS_KEY_IN_USE: SyncBitset = SYNC_BITSET_INIT;
1415
macro_rules! dup {
1516
((* $($exp:tt)*) $($val:tt)*) => (dup!( ($($exp)*) $($val)* $($val)* ));
1617
(() $($val:tt)*) => ([$($val),*])
1718
}
18-
static TLS_DESTRUCTOR: [AtomicUsize; TLS_KEYS] = [
19-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
20-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
21-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
22-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
23-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
24-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
25-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
26-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
27-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
28-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
29-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
30-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
31-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
32-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
33-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
34-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
35-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
36-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
37-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
38-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
39-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
40-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
41-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
42-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
43-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
44-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
45-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
46-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
47-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
48-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
49-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
50-
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
51-
];
19+
#[cfg(not(test))]
20+
static TLS_DESTRUCTOR: [AtomicUsize; TLS_KEYS] = dup!((* * * * * * *) (AtomicUsize::new(0)));
5221

5322
extern "C" {
5423
fn get_tls_ptr() -> *const u8;
@@ -86,6 +55,7 @@ pub struct ActiveTls<'a> {
8655
tls: &'a Tls
8756
}
8857

58+
#[cfg(not(test))]
8959
impl<'a> Drop for ActiveTls<'a> {
9060
fn drop(&mut self) {
9161
let value_with_destructor = |key: usize| {
@@ -127,7 +97,10 @@ impl Tls {
12797
unsafe fn current<'a>() -> &'a Tls {
12898
&*(get_tls_ptr() as *const Tls)
12999
}
100+
}
130101

102+
#[cfg(not(test))]
103+
impl Tls {
131104
pub fn create(dtor: Option<unsafe extern fn(*mut u8)>) -> Key {
132105
let index = TLS_KEY_IN_USE.set().expect("TLS limit exceeded");
133106
TLS_DESTRUCTOR[index].store(dtor.map_or(0, |f| f as usize), Ordering::Relaxed);
@@ -151,6 +124,25 @@ impl Tls {
151124
}
152125
}
153126

127+
#[cfg(test)]
128+
impl Tls {
129+
pub fn create(_dtor: Option<unsafe extern fn(*mut u8)>) -> Key {
130+
rtabort!("Calling cfg-test version of thread-local storage internals")
131+
}
132+
133+
pub fn set(_key: Key, _value: *mut u8) {
134+
rtabort!("Calling cfg-test version of thread-local storage internals")
135+
}
136+
137+
pub fn get(_key: Key) -> *mut u8 {
138+
rtabort!("Calling cfg-test version of thread-local storage internals")
139+
}
140+
141+
pub fn destroy(_key: Key) {
142+
rtabort!("Calling cfg-test version of thread-local storage internals")
143+
}
144+
}
145+
154146
mod sync_bitset {
155147
use sync::atomic::{AtomicUsize, Ordering};
156148
use iter::{Enumerate, Peekable};

src/libstd/sys/sgx/thread.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(test, allow(dead_code))]
2+
13
use boxed::FnBox;
24
use ffi::CStr;
35
use io;
@@ -10,8 +12,7 @@ pub struct Thread(task_queue::JoinHandle);
1012
pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
1113

1214
mod task_queue {
13-
use sync::{Mutex, MutexGuard, Once};
14-
use sync::mpsc;
15+
use sync::{MutexGuard, mpsc};
1516
use boxed::FnBox;
1617

1718
pub type JoinHandle = mpsc::Receiver<()>;
@@ -33,15 +34,23 @@ mod task_queue {
3334
}
3435
}
3536

36-
static TASK_QUEUE_INIT: Once = Once::new();
37-
static mut TASK_QUEUE: Option<Mutex<Vec<Task>>> = None;
38-
37+
#[cfg(not(test))]
3938
pub(super) fn lock() -> MutexGuard<'static, Vec<Task>> {
39+
use sync::{Mutex, Once};
40+
41+
static TASK_QUEUE_INIT: Once = Once::new();
42+
static mut TASK_QUEUE: Option<Mutex<Vec<Task>>> = None;
43+
4044
unsafe {
4145
TASK_QUEUE_INIT.call_once(|| TASK_QUEUE = Some(Default::default()) );
4246
TASK_QUEUE.as_ref().unwrap().lock().unwrap()
4347
}
4448
}
49+
50+
#[cfg(test)]
51+
pub(super) fn lock() -> MutexGuard<'static, Vec<Task>> {
52+
rtabort!("Calling cfg-test version of threading internals")
53+
}
4554
}
4655

4756
impl Thread {

src/libstd/sys/unix/ext/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ pub mod ffi;
3232
pub mod fs;
3333
pub mod process;
3434
pub mod raw;
35+
#[cfg(not(test))]
3536
pub mod thread;
37+
#[cfg(test)]
38+
pub use realstd::os::unix::thread;
3639
pub mod net;
3740

3841
/// A prelude for conveniently writing platform-specific code.

src/libstd/sys/unix/fast_thread_local.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg_attr(test, allow(dead_code))]
12
#![cfg(target_thread_local)]
23
#![unstable(feature = "thread_local_internals", issue = "0")]
34

src/libstd/sys/unix/thread.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(test, allow(dead_code))]
2+
13
use boxed::FnBox;
24
use cmp;
35
use ffi::CStr;

src/libstd/sys/wasm/thread.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(test, allow(dead_code))]
2+
13
use boxed::FnBox;
24
use ffi::CStr;
35
use io;

src/libstd/sys/windows/ext/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ pub mod fs;
1515
pub mod io;
1616
pub mod raw;
1717
pub mod process;
18+
#[cfg(not(test))]
1819
pub mod thread;
20+
#[cfg(test)]
21+
pub use realstd::os::windows::thread;
1922

2023
/// A prelude for conveniently writing platform-specific code.
2124
///

src/libstd/sys/windows/thread.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(test, allow(dead_code))]
2+
13
use boxed::FnBox;
24
use io;
35
use ffi::CStr;

src/libstd/sys_common/thread.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
#![allow(dead_code)]
2+
13
use boxed::FnBox;
24
use env;
35
use sync::atomic::{self, Ordering};
46
use sys::stack_overflow;
57
use sys::thread as imp;
68

7-
#[allow(dead_code)]
89
pub unsafe fn start_thread(main: *mut u8) {
910
// Next, set up our stack overflow handler which may get triggered if we run
1011
// out of stack.

src/libstd/sys_common/thread_info.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct ThreadInfo {
1212
thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(None) }
1313

1414
impl ThreadInfo {
15+
#[cfg(not(test))]
1516
fn with<R, F>(f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R {
1617
THREAD_INFO.try_with(move |c| {
1718
if c.borrow().is_none() {
@@ -23,6 +24,11 @@ impl ThreadInfo {
2324
f(c.borrow_mut().as_mut().unwrap())
2425
}).ok()
2526
}
27+
28+
#[cfg(test)]
29+
fn with<R, F>(_f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R {
30+
None
31+
}
2632
}
2733

2834
pub fn current_thread() -> Option<Thread> {

0 commit comments

Comments
 (0)