From 8aee42a382eff8fc35d1341be5b307da87a25603 Mon Sep 17 00:00:00 2001 From: Jon Morton Date: Sat, 31 Mar 2012 01:57:29 -0500 Subject: [PATCH 1/2] Choose task thread in rust_scheduler by round robin Remove the random context from rust_scheduler and use a simple round robin system to choose which thread a new task gets put on. Also, some incorrect tab indents around scoped blocks were fixed. --- src/rt/rust_scheduler.cpp | 25 +++++++++++++------------ src/rt/rust_scheduler.h | 4 ++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/rt/rust_scheduler.cpp b/src/rt/rust_scheduler.cpp index 8daee5626477a..8b50cfa87d2b2 100644 --- a/src/rt/rust_scheduler.cpp +++ b/src/rt/rust_scheduler.cpp @@ -13,7 +13,6 @@ rust_scheduler::rust_scheduler(rust_kernel *kernel, num_threads(num_threads), id(id) { - isaac_init(kernel, &rctx); create_task_threads(); } @@ -86,9 +85,11 @@ rust_task * rust_scheduler::create_task(rust_task *spawner, const char *name) { size_t thread_no; { - scoped_lock with(lock); - thread_no = isaac_rand(&rctx) % num_threads; - live_tasks++; + scoped_lock with(lock); + live_tasks++; + if (++cur_thread >= num_threads) + cur_thread = 0; + thread_no = cur_thread; } rust_task_thread *thread = threads[thread_no]; return thread->create_task(spawner, name); @@ -98,11 +99,11 @@ void rust_scheduler::release_task() { bool need_exit = false; { - scoped_lock with(lock); - live_tasks--; - if (live_tasks == 0) { - need_exit = true; - } + scoped_lock with(lock); + live_tasks--; + if (live_tasks == 0) { + need_exit = true; + } } if (need_exit) { // There are no more tasks on this scheduler. Time to leave @@ -129,10 +130,10 @@ void rust_scheduler::release_task_thread() { uintptr_t new_live_threads; { - scoped_lock with(lock); - new_live_threads = --live_threads; + scoped_lock with(lock); + new_live_threads = --live_threads; } if (new_live_threads == 0) { - kernel->release_scheduler_id(id); + kernel->release_scheduler_id(id); } } diff --git a/src/rt/rust_scheduler.h b/src/rt/rust_scheduler.h index ad188b79126e1..dae867536fb3b 100644 --- a/src/rt/rust_scheduler.h +++ b/src/rt/rust_scheduler.h @@ -10,16 +10,16 @@ class rust_scheduler : public kernel_owned { rust_srv *srv; rust_env *env; private: - // Protects the random number context and live_threads + // Protects live_threads and cur_thread increments lock_and_signal lock; // When this hits zero we'll tell the kernel to release us uintptr_t live_threads; // When this hits zero we'll tell the threads to exit uintptr_t live_tasks; - randctx rctx; array_list threads; const size_t num_threads; + size_t cur_thread; rust_sched_id id; From 9851a906a59faee4ca13a8cf76677287793809a2 Mon Sep 17 00:00:00 2001 From: Jon Morton Date: Sat, 31 Mar 2012 13:14:54 -0500 Subject: [PATCH 2/2] initialize cur_thread, first task on thread 0 --- src/rt/rust_scheduler.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rt/rust_scheduler.cpp b/src/rt/rust_scheduler.cpp index 8b50cfa87d2b2..482739ded99be 100644 --- a/src/rt/rust_scheduler.cpp +++ b/src/rt/rust_scheduler.cpp @@ -11,6 +11,7 @@ rust_scheduler::rust_scheduler(rust_kernel *kernel, live_threads(num_threads), live_tasks(0), num_threads(num_threads), + cur_thread(0), id(id) { create_task_threads(); @@ -87,9 +88,9 @@ rust_scheduler::create_task(rust_task *spawner, const char *name) { { scoped_lock with(lock); live_tasks++; - if (++cur_thread >= num_threads) + thread_no = cur_thread++; + if (cur_thread >= num_threads) cur_thread = 0; - thread_no = cur_thread; } rust_task_thread *thread = threads[thread_no]; return thread->create_task(spawner, name);