2012-04-02 22:18:01 -05:00
|
|
|
|
2012-02-03 17:12:18 -06:00
|
|
|
#include "rust_scheduler.h"
|
2012-04-02 22:18:01 -05:00
|
|
|
#include "rust_task.h"
|
|
|
|
#include "rust_globals.h"
|
2012-02-03 17:12:18 -06:00
|
|
|
#include "rust_util.h"
|
2012-03-29 17:21:32 -05:00
|
|
|
#include "rust_sched_launcher.h"
|
2012-02-03 17:12:18 -06:00
|
|
|
|
|
|
|
rust_scheduler::rust_scheduler(rust_kernel *kernel,
|
2012-04-01 01:12:06 -05:00
|
|
|
size_t num_threads,
|
|
|
|
rust_sched_id id) :
|
2012-02-03 17:12:18 -06:00
|
|
|
kernel(kernel),
|
2012-02-04 16:54:10 -06:00
|
|
|
live_threads(num_threads),
|
2012-02-07 01:38:22 -06:00
|
|
|
live_tasks(0),
|
2012-02-06 20:00:49 -06:00
|
|
|
num_threads(num_threads),
|
2012-03-31 13:14:54 -05:00
|
|
|
cur_thread(0),
|
2012-02-06 20:00:49 -06:00
|
|
|
id(id)
|
2012-02-03 17:12:18 -06:00
|
|
|
{
|
|
|
|
create_task_threads();
|
|
|
|
}
|
|
|
|
|
|
|
|
rust_scheduler::~rust_scheduler() {
|
|
|
|
destroy_task_threads();
|
|
|
|
}
|
|
|
|
|
2012-03-29 17:21:32 -05:00
|
|
|
rust_sched_launcher *
|
2012-02-03 17:12:18 -06:00
|
|
|
rust_scheduler::create_task_thread(int id) {
|
2012-03-29 17:21:32 -05:00
|
|
|
rust_sched_launcher *thread =
|
2012-04-01 02:13:59 -05:00
|
|
|
new (kernel, "rust_thread_sched_launcher")
|
2012-04-01 22:18:40 -05:00
|
|
|
rust_thread_sched_launcher(this, id);
|
2012-03-29 17:21:32 -05:00
|
|
|
KLOG(kernel, kern, "created task thread: " PTR ", id: %d",
|
|
|
|
thread, id);
|
2012-02-03 17:12:18 -06:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-29 17:21:32 -05:00
|
|
|
rust_scheduler::destroy_task_thread(rust_sched_launcher *thread) {
|
|
|
|
KLOG(kernel, kern, "deleting task thread: " PTR, thread);
|
2012-02-03 17:12:18 -06:00
|
|
|
delete thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_scheduler::create_task_threads() {
|
|
|
|
KLOG(kernel, kern, "Using %d scheduler threads.", num_threads);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
|
|
|
threads.push(create_task_thread(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_scheduler::destroy_task_threads() {
|
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
|
|
|
destroy_task_thread(threads[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_scheduler::start_task_threads()
|
|
|
|
{
|
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
2012-03-29 17:21:32 -05:00
|
|
|
rust_sched_launcher *thread = threads[i];
|
2012-02-03 17:12:18 -06:00
|
|
|
thread->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-27 15:36:54 -06:00
|
|
|
void
|
|
|
|
rust_scheduler::join_task_threads()
|
|
|
|
{
|
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
2012-03-29 17:21:32 -05:00
|
|
|
rust_sched_launcher *thread = threads[i];
|
2012-02-27 15:36:54 -06:00
|
|
|
thread->join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 17:12:18 -06:00
|
|
|
void
|
|
|
|
rust_scheduler::kill_all_tasks() {
|
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
2012-03-29 17:21:32 -05:00
|
|
|
rust_sched_launcher *thread = threads[i];
|
|
|
|
thread->get_loop()->kill_all_tasks();
|
2012-02-03 17:12:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-14 22:22:34 -05:00
|
|
|
rust_task *
|
2012-03-21 16:47:48 -05:00
|
|
|
rust_scheduler::create_task(rust_task *spawner, const char *name) {
|
2012-02-03 17:12:18 -06:00
|
|
|
size_t thread_no;
|
|
|
|
{
|
2012-03-31 01:57:29 -05:00
|
|
|
scoped_lock with(lock);
|
|
|
|
live_tasks++;
|
2012-03-31 13:14:54 -05:00
|
|
|
thread_no = cur_thread++;
|
|
|
|
if (cur_thread >= num_threads)
|
2012-03-31 01:57:29 -05:00
|
|
|
cur_thread = 0;
|
2012-02-03 17:12:18 -06:00
|
|
|
}
|
2012-03-29 17:21:32 -05:00
|
|
|
rust_sched_launcher *thread = threads[thread_no];
|
|
|
|
return thread->get_loop()->create_task(spawner, name);
|
2012-02-03 17:12:18 -06:00
|
|
|
}
|
|
|
|
|
2012-02-07 01:38:22 -06:00
|
|
|
void
|
|
|
|
rust_scheduler::release_task() {
|
|
|
|
bool need_exit = false;
|
|
|
|
{
|
2012-03-31 01:57:29 -05:00
|
|
|
scoped_lock with(lock);
|
|
|
|
live_tasks--;
|
|
|
|
if (live_tasks == 0) {
|
|
|
|
need_exit = true;
|
|
|
|
}
|
2012-02-07 01:38:22 -06:00
|
|
|
}
|
|
|
|
if (need_exit) {
|
2012-04-01 01:12:06 -05:00
|
|
|
// There are no more tasks on this scheduler. Time to leave
|
|
|
|
exit();
|
2012-02-07 01:38:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 17:12:18 -06:00
|
|
|
void
|
|
|
|
rust_scheduler::exit() {
|
2012-02-07 01:38:22 -06:00
|
|
|
// Take a copy of num_threads. After the last thread exits this
|
|
|
|
// scheduler will get destroyed, and our fields will cease to exist.
|
|
|
|
size_t current_num_threads = num_threads;
|
|
|
|
for(size_t i = 0; i < current_num_threads; ++i) {
|
2012-03-29 17:21:32 -05:00
|
|
|
threads[i]->get_loop()->exit();
|
2012-02-03 17:12:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
rust_scheduler::number_of_threads() {
|
|
|
|
return num_threads;
|
|
|
|
}
|
2012-02-04 16:54:10 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
rust_scheduler::release_task_thread() {
|
|
|
|
uintptr_t new_live_threads;
|
|
|
|
{
|
2012-03-31 01:57:29 -05:00
|
|
|
scoped_lock with(lock);
|
|
|
|
new_live_threads = --live_threads;
|
2012-02-04 16:54:10 -06:00
|
|
|
}
|
|
|
|
if (new_live_threads == 0) {
|
2012-03-31 01:57:29 -05:00
|
|
|
kernel->release_scheduler_id(id);
|
2012-02-04 16:54:10 -06:00
|
|
|
}
|
|
|
|
}
|