Skip to content

Commit 8afec77

Browse files
committed
std::rt: Configure test threads with RUST_TEST_THREADS. Default is ncores x2
1 parent f9a5005 commit 8afec77

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/libstd/rt/test.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,32 @@ pub fn run_in_newsched_task(f: ~fn()) {
5959
/// in one of the schedulers. The schedulers will stay alive
6060
/// until the function `f` returns.
6161
pub fn run_in_mt_newsched_task(f: ~fn()) {
62+
use libc;
63+
use os;
64+
use from_str::FromStr;
6265
use rt::uv::uvio::UvEventLoop;
6366
use rt::sched::Shutdown;
6467

6568
let f_cell = Cell(f);
6669

6770
do run_in_bare_thread {
68-
static N: uint = 4;
71+
let nthreads = match os::getenv("RUST_TEST_THREADS") {
72+
Some(nstr) => FromStr::from_str(nstr).get(),
73+
None => unsafe {
74+
// Using more threads than cores in test code
75+
// to force the OS to preempt them frequently.
76+
// Assuming that this help stress test concurrent types.
77+
rust_get_num_cpus() * 2
78+
}
79+
};
6980

7081
let sleepers = SleeperList::new();
7182
let work_queue = WorkQueue::new();
7283

7384
let mut handles = ~[];
7485
let mut scheds = ~[];
7586

76-
for uint::range(0, N) |_| {
87+
for uint::range(0, nthreads) |_| {
7788
let loop_ = ~UvEventLoop::new();
7889
let mut sched = ~Scheduler::new(loop_, work_queue.clone(), sleepers.clone());
7990
let handle = sched.make_handle();
@@ -111,6 +122,10 @@ pub fn run_in_mt_newsched_task(f: ~fn()) {
111122
// Wait for schedulers
112123
let _threads = threads;
113124
}
125+
126+
extern {
127+
fn rust_get_num_cpus() -> libc::uintptr_t;
128+
}
114129
}
115130

116131
/// Test tasks will abort on failure instead of unwinding

0 commit comments

Comments
 (0)