2012-02-03 17:12:18 -06:00
|
|
|
#include "rust_scheduler.h"
|
|
|
|
#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
|
|
|
rust_srv *srv,
|
|
|
|
size_t num_threads,
|
2012-04-01 18:38:42 -05:00
|
|
|
rust_sched_id id,
|
|
|
|
bool allow_exit) :
|
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-03-31 13:14:54 -05:00
|
|
|
cur_thread(0),
|
2012-04-01 18:38:42 -05:00
|
|
|
may_exit(allow_exit),
|
|
|
|
num_threads(num_threads),
|
2012-02-06 20:00:49 -06:00
|
|
|
id(id)
|
2012-02-03 17:12:18 -06:00
|
|
|
{
|
2012-04-01 19:22:24 -05:00
|
|
|
rust_thread_sched_launcher_factory launchfac;
|
|
|
|
create_task_threads(&launchfac);
|
2012-02-03 17:12:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
rust_scheduler::~rust_scheduler() {
|
|
|
|
destroy_task_threads();
|
|
|
|
}
|
|
|
|
|
2012-03-29 17:21:32 -05:00
|
|
|
rust_sched_launcher *
|
2012-04-01 19:22:24 -05:00
|
|
|
rust_scheduler::create_task_thread(rust_sched_launcher_factory *launchfac,
|
|
|
|
int id) {
|
|
|
|
rust_sched_launcher *thread = launchfac->create(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);
|
|
|
|
rust_srv *srv = thread->get_loop()->srv;
|
2012-02-03 17:12:18 -06:00
|
|
|
delete thread;
|
|
|
|
delete srv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-04-01 19:22:24 -05:00
|
|
|
rust_scheduler::create_task_threads(rust_sched_launcher_factory *launchfac) {
|
2012-02-03 17:12:18 -06:00
|
|
|
KLOG(kernel, kern, "Using %d scheduler threads.", num_threads);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
2012-04-01 19:22:24 -05:00
|
|
|
threads.push(create_task_thread(launchfac, i));
|
2012-02-03 17:12:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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--;
|
2012-04-01 18:38:42 -05:00
|
|
|
if (live_tasks == 0 && may_exit) {
|
2012-03-31 01:57:29 -05:00
|
|
|
need_exit = true;
|
|
|
|
}
|
2012-02-07 01:38:22 -06:00
|
|
|
}
|
|
|
|
if (need_exit) {
|
2012-04-01 01:12:06 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2012-04-01 18:38:42 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
rust_scheduler::allow_exit() {
|
|
|
|
bool need_exit = false;
|
|
|
|
{
|
|
|
|
scoped_lock with(lock);
|
|
|
|
may_exit = true;
|
|
|
|
need_exit = live_tasks == 0;
|
|
|
|
}
|
|
|
|
if (need_exit) {
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|