2012-02-03 17:12:18 -06:00
|
|
|
#include "rust_scheduler.h"
|
|
|
|
#include "rust_util.h"
|
|
|
|
|
|
|
|
rust_scheduler::rust_scheduler(rust_kernel *kernel,
|
|
|
|
rust_srv *srv,
|
2012-02-06 20:00:49 -06:00
|
|
|
size_t num_threads,
|
|
|
|
rust_sched_id id) :
|
2012-02-03 17:12:18 -06:00
|
|
|
kernel(kernel),
|
|
|
|
srv(srv),
|
|
|
|
env(srv->env),
|
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),
|
|
|
|
id(id)
|
2012-02-03 17:12:18 -06:00
|
|
|
{
|
|
|
|
create_task_threads();
|
|
|
|
}
|
|
|
|
|
|
|
|
rust_scheduler::~rust_scheduler() {
|
|
|
|
destroy_task_threads();
|
|
|
|
}
|
|
|
|
|
|
|
|
rust_task_thread *
|
|
|
|
rust_scheduler::create_task_thread(int id) {
|
|
|
|
rust_srv *srv = this->srv->clone();
|
|
|
|
rust_task_thread *thread =
|
|
|
|
new (kernel, "rust_task_thread") rust_task_thread(this, srv, id);
|
|
|
|
KLOG(kernel, kern, "created task thread: " PTR ", id: %d, index: %d",
|
|
|
|
thread, id, thread->list_index);
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_scheduler::destroy_task_thread(rust_task_thread *thread) {
|
|
|
|
KLOG(kernel, kern, "deleting task thread: " PTR ", name: %s, index: %d",
|
|
|
|
thread, thread->name, thread->list_index);
|
|
|
|
rust_srv *srv = thread->srv;
|
|
|
|
delete thread;
|
|
|
|
delete srv;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
rust_task_thread *thread = threads[i];
|
|
|
|
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) {
|
|
|
|
rust_task_thread *thread = threads[i];
|
|
|
|
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) {
|
|
|
|
rust_task_thread *thread = threads[i];
|
|
|
|
thread->kill_all_tasks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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++;
|
|
|
|
if (++cur_thread >= num_threads)
|
|
|
|
cur_thread = 0;
|
|
|
|
thread_no = cur_thread;
|
2012-02-03 17:12:18 -06:00
|
|
|
}
|
|
|
|
rust_task_thread *thread = threads[thread_no];
|
2012-03-21 16:47:48 -05:00
|
|
|
return thread->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) {
|
|
|
|
// There are no more tasks on this scheduler. Time to leave
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-03 17:12:18 -06:00
|
|
|
threads[i]->exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|