Skip to content

Commit 62b51d9

Browse files
committed
rt: Implement ThreadPerCore scheduling mode
Fixes #3465.
1 parent 308ca06 commit 62b51d9

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/libcore/task.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1403,11 +1403,9 @@ fn spawn_raw(+opts: TaskOpts, +f: fn~()) {
14031403

14041404
let num_threads = match opts.mode {
14051405
SingleThreaded => 1u,
1406-
ThreadPerCore => {
1407-
fail ~"thread_per_core scheduling mode unimplemented"
1408-
}
1406+
ThreadPerCore => rustrt::rust_num_threads(),
14091407
ThreadPerTask => {
1410-
fail ~"thread_per_task scheduling mode unimplemented"
1408+
fail ~"ThreadPerTask scheduling mode unimplemented"
14111409
}
14121410
ManualThreads(threads) => {
14131411
if threads == 0u {
@@ -1657,6 +1655,8 @@ extern mod rustrt {
16571655

16581656
fn rust_get_sched_id() -> sched_id;
16591657
fn rust_new_sched(num_threads: libc::uintptr_t) -> sched_id;
1658+
fn sched_threads() -> libc::size_t;
1659+
fn rust_num_threads() -> libc::uintptr_t;
16601660

16611661
fn get_task_id() -> task_id;
16621662
#[rust_stack]
@@ -2422,3 +2422,13 @@ fn test_tls_cleanup_on_failure() unsafe {
24222422
local_data_set(int_key, @31337);
24232423
fail;
24242424
}
2425+
2426+
#[test]
2427+
fn test_sched_thread_per_core() {
2428+
let cores = rustrt::rust_num_threads();
2429+
let mut reported_threads = 0u;
2430+
do spawn_sched(ThreadPerCore) {
2431+
reported_threads = rustrt::sched_threads();
2432+
}
2433+
assert(cores == reported_threads);
2434+
}

src/rt/rust_builtin.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,12 @@ rust_get_sched_id() {
572572
return task->sched->get_id();
573573
}
574574

575+
extern "C" CDECL uintptr_t
576+
rust_num_threads() {
577+
rust_task *task = rust_get_current_task();
578+
return task->kernel->env->num_sched_threads;
579+
}
580+
575581
extern "C" CDECL rust_sched_id
576582
rust_new_sched(uintptr_t threads) {
577583
rust_task *task = rust_get_current_task();
@@ -620,7 +626,7 @@ start_task(rust_task *target, fn_env_pair *f) {
620626
target->start(f->f, f->env, NULL);
621627
}
622628

623-
extern "C" CDECL int
629+
extern "C" CDECL size_t
624630
sched_threads() {
625631
rust_task *task = rust_get_current_task();
626632
return task->sched->number_of_threads();

src/rt/rustrt.def.in

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ rand_seed
3232
rust_get_sched_id
3333
rust_new_sched
3434
rust_new_task_in_sched
35+
rust_num_threads
3536
rust_path_is_dir
3637
rust_path_exists
3738
rust_getcwd

0 commit comments

Comments
 (0)