rt: Extract rust_sched_launcher from rust_task_thread

rust_sched_launcher is actually responsible for setting up the thread and
starting the loop. There will be other implementations that do not actually
set up a new thread, in order to support scheduling tasks on the main OS
thread.
This commit is contained in:
Brian Anderson 2012-03-29 15:21:32 -07:00
parent 620b4d4946
commit 6bf8d19712
7 changed files with 71 additions and 37 deletions

View File

@ -51,6 +51,7 @@ RUNTIME_CS_$(1) := \
rt/rust_run_program.cpp \
rt/rust_env.cpp \
rt/rust_task_thread.cpp \
rt/rust_sched_launcher.cpp \
rt/rust_scheduler.cpp \
rt/rust_task.cpp \
rt/rust_stack.cpp \

View File

@ -0,0 +1,16 @@
#include "rust_sched_launcher.h"
#include "rust_scheduler.h"
const size_t SCHED_STACK_SIZE = 1024*100;
rust_sched_launcher::rust_sched_launcher(rust_scheduler *sched,
rust_srv *srv, int id)
: rust_thread(SCHED_STACK_SIZE),
kernel(sched->kernel),
thread(sched, srv, id) {
}
void
rust_sched_launcher::run() {
thread.start_main_loop();
}

View File

@ -0,0 +1,29 @@
#ifndef RUST_SCHED_LAUNCHER_H
#define RUST_SCHED_LAUNCHER_H
#include "rust_internal.h"
#include "sync/rust_thread.h"
#ifndef _WIN32
#include <pthread.h>
#else
#include <windows.h>
#endif
class rust_sched_launcher
: public kernel_owned<rust_sched_launcher>,
public rust_thread {
public:
rust_kernel *kernel;
private:
rust_task_thread thread;
public:
rust_sched_launcher(rust_scheduler *sched, rust_srv *srv, int id);
virtual void run();
rust_task_thread *get_loop() { return &thread; }
};
#endif // RUST_SCHED_LAUNCHER_H

View File

@ -1,5 +1,6 @@
#include "rust_scheduler.h"
#include "rust_util.h"
#include "rust_sched_launcher.h"
rust_scheduler::rust_scheduler(rust_kernel *kernel,
rust_srv *srv,
@ -21,21 +22,20 @@ rust_scheduler::~rust_scheduler() {
destroy_task_threads();
}
rust_task_thread *
rust_sched_launcher *
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);
rust_sched_launcher *thread =
new (kernel, "rust_sched_launcher") rust_sched_launcher(this, srv, id);
KLOG(kernel, kern, "created task thread: " PTR ", id: %d",
thread, id);
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;
rust_scheduler::destroy_task_thread(rust_sched_launcher *thread) {
KLOG(kernel, kern, "deleting task thread: " PTR, thread);
rust_srv *srv = thread->get_loop()->srv;
delete thread;
delete srv;
}
@ -60,7 +60,7 @@ void
rust_scheduler::start_task_threads()
{
for(size_t i = 0; i < num_threads; ++i) {
rust_task_thread *thread = threads[i];
rust_sched_launcher *thread = threads[i];
thread->start();
}
}
@ -69,7 +69,7 @@ void
rust_scheduler::join_task_threads()
{
for(size_t i = 0; i < num_threads; ++i) {
rust_task_thread *thread = threads[i];
rust_sched_launcher *thread = threads[i];
thread->join();
}
}
@ -77,8 +77,8 @@ rust_scheduler::join_task_threads()
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();
rust_sched_launcher *thread = threads[i];
thread->get_loop()->kill_all_tasks();
}
}
@ -92,8 +92,8 @@ rust_scheduler::create_task(rust_task *spawner, const char *name) {
if (cur_thread >= num_threads)
cur_thread = 0;
}
rust_task_thread *thread = threads[thread_no];
return thread->create_task(spawner, name);
rust_sched_launcher *thread = threads[thread_no];
return thread->get_loop()->create_task(spawner, name);
}
void
@ -118,7 +118,7 @@ rust_scheduler::exit() {
// 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) {
threads[i]->exit();
threads[i]->get_loop()->exit();
}
}

View File

@ -3,6 +3,8 @@
#include "rust_internal.h"
class rust_sched_launcher;
class rust_scheduler : public kernel_owned<rust_scheduler> {
// FIXME: Make these private
public:
@ -17,7 +19,7 @@ private:
// When this hits zero we'll tell the threads to exit
uintptr_t live_tasks;
array_list<rust_task_thread *> threads;
array_list<rust_sched_launcher *> threads;
const size_t num_threads;
size_t cur_thread;
@ -26,8 +28,8 @@ private:
void create_task_threads();
void destroy_task_threads();
rust_task_thread *create_task_thread(int id);
void destroy_task_thread(rust_task_thread *thread);
rust_sched_launcher *create_task_thread(int id);
void destroy_task_thread(rust_sched_launcher *thread);
void exit();

View File

@ -13,7 +13,6 @@ pthread_key_t rust_task_thread::task_key;
DWORD rust_task_thread::task_key;
#endif
const size_t SCHED_STACK_SIZE = 1024*100;
const size_t C_STACK_SIZE = 1024*1024;
bool rust_task_thread::tls_initialized = false;
@ -21,7 +20,6 @@ bool rust_task_thread::tls_initialized = false;
rust_task_thread::rust_task_thread(rust_scheduler *sched,
rust_srv *srv,
int id) :
rust_thread(SCHED_STACK_SIZE),
_log(srv, this),
id(id),
should_exit(false),
@ -256,6 +254,8 @@ rust_task_thread::start_main_loop() {
destroy_stack(kernel->region(), cached_c_stack);
cached_c_stack = NULL;
}
sched->release_task_thread();
}
rust_task *
@ -327,11 +327,6 @@ rust_task_thread::transition(rust_task *task,
lock.signal();
}
void rust_task_thread::run() {
this->start_main_loop();
sched->release_task_thread();
}
#ifndef _WIN32
void
rust_task_thread::init_tls() {

View File

@ -2,16 +2,9 @@
#define RUST_TASK_THREAD_H
#include "rust_internal.h"
#include "sync/rust_thread.h"
#include "rust_stack.h"
#include "context.h"
#ifndef _WIN32
#include <pthread.h>
#else
#include <windows.h>
#endif
enum rust_task_state {
task_state_newborn,
task_state_running,
@ -21,8 +14,7 @@ enum rust_task_state {
typedef indexed_list<rust_task> rust_task_list;
struct rust_task_thread : public kernel_owned<rust_task_thread>,
rust_thread
struct rust_task_thread
{
private:
@ -75,6 +67,7 @@ public:
randctx rctx;
// FIXME: Neither of these are used
int32_t list_index;
const char *const name;
@ -103,8 +96,6 @@ public:
rust_task_state src, rust_task_state dst,
rust_cond *cond, const char* cond_name);
virtual void run();
void init_tls();
void place_task_in_tls(rust_task *task);