Skip to content

When testing std, don't create two variants of threading internals #58399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ extern crate backtrace_sys;
// would generate duplicate lang item errors), and any globals it defines are
// _not_ the globals used by "real" std. So this import, defined only during
// testing gives test-std access to real-std lang items and globals. See #2912
#[cfg(test)] extern crate std as realstd;
#[cfg(test)]
#[macro_use]
extern crate std as realstd;

#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
#[macro_use]
Expand Down Expand Up @@ -442,7 +444,13 @@ pub mod f32;
pub mod f64;

#[macro_use]
#[cfg(not(test))]
pub mod thread;
#[cfg(test)]
pub use realstd::thread;
#[cfg(test)]
#[path = "thread/tests.rs"]
mod thread_tests;
pub mod ascii;
pub mod collections;
pub mod env;
Expand Down
1 change: 1 addition & 0 deletions src/libstd/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
}

/// Determines whether the current thread is unwinding because of panic.
#[cfg(not(test))]
pub fn panicking() -> bool {
update_panic_count(0) != 0
}
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/cloudabi/thread.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(test, allow(dead_code))]

use boxed::FnBox;
use cmp;
use ffi::CStr;
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/sys/redox/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub mod fs;
pub mod io;
pub mod net;
pub mod process;
#[cfg(not(test))]
pub mod thread;
#[cfg(test)]
pub use realstd::os::redox::thread;

/// A prelude for conveniently writing platform-specific code.
///
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/redox/thread.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(test, allow(dead_code))]

use boxed::FnBox;
use ffi::CStr;
use io;
Expand Down
60 changes: 26 additions & 34 deletions src/libstd/sys/sgx/abi/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,14 @@ const USIZE_BITS: usize = 64;
const TLS_KEYS: usize = 128; // Same as POSIX minimum
const TLS_KEYS_BITSET_SIZE: usize = (TLS_KEYS + (USIZE_BITS - 1)) / USIZE_BITS;

#[cfg(not(test))]
static TLS_KEY_IN_USE: SyncBitset = SYNC_BITSET_INIT;
macro_rules! dup {
((* $($exp:tt)*) $($val:tt)*) => (dup!( ($($exp)*) $($val)* $($val)* ));
(() $($val:tt)*) => ([$($val),*])
}
static TLS_DESTRUCTOR: [AtomicUsize; TLS_KEYS] = [
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
];
#[cfg(not(test))]
static TLS_DESTRUCTOR: [AtomicUsize; TLS_KEYS] = dup!((* * * * * * *) (AtomicUsize::new(0)));

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

#[cfg(not(test))]
impl<'a> Drop for ActiveTls<'a> {
fn drop(&mut self) {
let value_with_destructor = |key: usize| {
Expand Down Expand Up @@ -127,7 +97,10 @@ impl Tls {
unsafe fn current<'a>() -> &'a Tls {
&*(get_tls_ptr() as *const Tls)
}
}

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

#[cfg(test)]
impl Tls {
pub fn create(_dtor: Option<unsafe extern fn(*mut u8)>) -> Key {
rtabort!("Calling cfg-test version of thread-local storage internals")
}

pub fn set(_key: Key, _value: *mut u8) {
rtabort!("Calling cfg-test version of thread-local storage internals")
}

pub fn get(_key: Key) -> *mut u8 {
rtabort!("Calling cfg-test version of thread-local storage internals")
}

pub fn destroy(_key: Key) {
rtabort!("Calling cfg-test version of thread-local storage internals")
}
}

mod sync_bitset {
use sync::atomic::{AtomicUsize, Ordering};
use iter::{Enumerate, Peekable};
Expand Down
19 changes: 14 additions & 5 deletions src/libstd/sys/sgx/thread.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(test, allow(dead_code))]

use boxed::FnBox;
use ffi::CStr;
use io;
Expand All @@ -10,8 +12,7 @@ pub struct Thread(task_queue::JoinHandle);
pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;

mod task_queue {
use sync::{Mutex, MutexGuard, Once};
use sync::mpsc;
use sync::{MutexGuard, mpsc};
use boxed::FnBox;

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

static TASK_QUEUE_INIT: Once = Once::new();
static mut TASK_QUEUE: Option<Mutex<Vec<Task>>> = None;

#[cfg(not(test))]
pub(super) fn lock() -> MutexGuard<'static, Vec<Task>> {
use sync::{Mutex, Once};

static TASK_QUEUE_INIT: Once = Once::new();
static mut TASK_QUEUE: Option<Mutex<Vec<Task>>> = None;

unsafe {
TASK_QUEUE_INIT.call_once(|| TASK_QUEUE = Some(Default::default()) );
TASK_QUEUE.as_ref().unwrap().lock().unwrap()
}
}

#[cfg(test)]
pub(super) fn lock() -> MutexGuard<'static, Vec<Task>> {
rtabort!("Calling cfg-test version of threading internals")
}
}

impl Thread {
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/sys/unix/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ pub mod ffi;
pub mod fs;
pub mod process;
pub mod raw;
#[cfg(not(test))]
pub mod thread;
#[cfg(test)]
pub use realstd::os::unix::thread;
pub mod net;

/// A prelude for conveniently writing platform-specific code.
Expand Down
1 change: 1 addition & 0 deletions src/libstd/sys/unix/fast_thread_local.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(test, allow(dead_code))]
#![cfg(target_thread_local)]
#![unstable(feature = "thread_local_internals", issue = "0")]

Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/unix/thread.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(test, allow(dead_code))]

use boxed::FnBox;
use cmp;
use ffi::CStr;
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/wasm/thread.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(test, allow(dead_code))]

use boxed::FnBox;
use ffi::CStr;
use io;
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/sys/windows/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub mod fs;
pub mod io;
pub mod raw;
pub mod process;
#[cfg(not(test))]
pub mod thread;
#[cfg(test)]
pub use realstd::os::windows::thread;

/// A prelude for conveniently writing platform-specific code.
///
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/windows/thread.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(test, allow(dead_code))]

use boxed::FnBox;
use io;
use ffi::CStr;
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/sys_common/thread.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![allow(dead_code)]

use boxed::FnBox;
use env;
use sync::atomic::{self, Ordering};
use sys::stack_overflow;
use sys::thread as imp;

#[allow(dead_code)]
pub unsafe fn start_thread(main: *mut u8) {
// Next, set up our stack overflow handler which may get triggered if we run
// out of stack.
Expand Down
6 changes: 6 additions & 0 deletions src/libstd/sys_common/thread_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct ThreadInfo {
thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(None) }

impl ThreadInfo {
#[cfg(not(test))]
fn with<R, F>(f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R {
THREAD_INFO.try_with(move |c| {
if c.borrow().is_none() {
Expand All @@ -23,6 +24,11 @@ impl ThreadInfo {
f(c.borrow_mut().as_mut().unwrap())
}).ok()
}

#[cfg(test)]
fn with<R, F>(_f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R {
None
}
}

pub fn current_thread() -> Option<Thread> {
Expand Down
Loading